Python 3: Trying to iterate lines of alphabet based on function of i
-
07-07-2021 - |
Question
I'm trying to write a program that does the following:
In this program, we print out i**q % p
letters from the alpabet on line i
for example, if q=2
and p=12
then we want to print:
1**2 % 12 = 1
2**2 % 12 = 4
3**2 % 12 = 9
4**2 % 12 = 4
5**2 % 12 = 1
...so the first five lines of the output will be:
a
bcde
fghijklmn
opqr
s
In my case, the function to determine character count is (i**2)%9
, but it's the same idea.
The specific task is as follows:
"Print out 1834
lines using the letters a-z in sequence such that line number i will use (i**2 ) % 9
letters. The first letter used to print line 1834
is:_________
"
I have started coding, and I have two main questions.
Firstly, how do I make the alphabet start over, once I reach "z" (or more likely, "the 25th place")?
Also, What should I do for the program? So far I have:
letters="abcdefghijklmnopqrstuvwxyz"
for i in range[1,1834]:
x=((i**2)%9)
print(letters[])
...but I don't know how to proceed. I was going to try a slice method, but I don't know. Any input would be appreciated.
Solution
Try the following solution. You are moving a frame over the letters, so every time a new loop iteration starts, a variable start_position
should remember at which letter the previous iteration stopped. The size of the frame is determined by your x
calculation.
If you want to go back to the beginning of the alphabet, it should be modulo the length of the alphabet. end_position = end_position % 26
letters="abcdefghijklmnopqrstuvwxyz"
alphabet_length = len(letters)
start_position = 0
for i in range(1,1834):
x=((i**2)%9)
end_position = start_position + x
end_position = end_position % alphabet_length
if end_position < start_position:
print letters[start_position:alphabet_length-1] + letters[0:end_position]
else:
print letters[start_position:end_position]
start_position = end_position
OTHER TIPS
Here's something you can work with using itertools
:
from string import ascii_lowercase
from itertools import cycle, islice
letters = cycle(ascii_lowercase)
for num in (1, 4, 9, 4, 1, 13, 12, 9, 5):
print (''.join(islice(letters, num)))
#a
#bcde
#fghijklmn
#opqr
#s
#tuvwxyzabcdef
#ghijklmnopqr
#stuvwxyza
#bcdef
It seems like this should be homework, so I'll answer it as such:
- To slice from index
a
to indexb
, you can usemy_list[a:b]
- To restart counting from 0 when you reach 26, you need the remainder of the iterator
i
divided by the number of letters 26.a%b
is the remainder ofa
divided byb
. range(n)
is a function which returns numbers from0
ton-1
, so in total you would get n elements. And it is a function so you should use parentheses, i.e.range(1834)
notrange[1834]
(that is slicing).- Also note that
range(1,1834)
will have1834-1=1833
elements in total, which is not what you need... - There's a package called
string
which has lists of alphabet characters (lowercase, uppercase, digits, ...). See for example string.lowercase.
The two questions are linked: you can use cycle from itertools to get an iterator that will endlessly loop over the alphabet (or any other string, for that matter), and to get n letters for a given row you can call next() on the iterator n times.
Your question looks like homework, so rather than short-circuit your education I'll give an example for the similar question of printing n letters on line n:
import itertools
str = "abcdefghijklmnopqrstuvwxyzicopiedthisfromstackoverflow"
it = itertools.cycle( str )
for i in range( 1 , 10 ):
line=""
for j in range( i ):
line+=it.next()
print i,line
edit as appropriate :-)