Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write a simple python program that prints letters in ascending order?

For example I would like to have:

a

.
.
.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

z

aa

ab

.
.
.

az

bz

.
.
.

zz

aaa

and so on.

Currently I’m here but I am lost. So feel free to propose a completely different solution.

count = 0
string = ''
for i in range(100):
    count += 1
    if i % 26 == 0:
        count = 0
        string += 'a'
    ch = 'a'
    x = chr(ord(ch) + count)
    string = string[:-1] + x
    print(i + 1, string)

and my output is something like this:

1 a

2 b

.
.
.

26 z

27 za

28 zb

.
.
.

52 zz

53 zza

54 zzb

.
.
.

>Solution :

Maybe try something like the following:

range(97,123) simply creates a range of numbers from 97 to 122, which converted to ASCII equates to a…z (done using chr())

So all our FUnction does, is it recieves a base string (starts with empty), prints out the base + range of charachters and calls its self with base + every charachter as the new base and depth decremented by 1

def printcharachters(base, depth):
    if depth > 0:
        for a in range(97, 123):
            print(base + chr(a))

        for a in range(97, 123):
            printcharachters(base + chr(a), depth - 1)



printcharachters("", 2)

Replace the depth with your desired depth (for 2 the last string would be zz for 4 it would be zzzz).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading