Home > python > A to Z, then Z to A

A to Z, then Z to A

Challenge

Write a Python script which prints the alphabet from “a” to “z”. Reverse its source code as explained in the previous post, i.e. reverse the order of lines and reverse the order of characters in each line. This reversed script should now print the alphabet from “z” to “a”!

Demonstration:

$ python fromAtoZ.py                                        
a b c d e f g h i j k l m n o p q r s t u v w x y z
$ ./reverse-file.py fromAtoZ.py >za.py
$ python za.py 
z y x w v u t s r q p o n m l k j i h g f e d c b a
$ ./reverse-file.py za.py >az.py
$ python az.py 
a b c d e f g h i j k l m n o p q r s t u v w x y z
$ diff fromAtoZ.py az.py 
$

You can find the script reverse-file.py in my previous post. As you can see, applying reverse-file.py twice, you get back the original file.

Note that this challenge is not specific to Python. It can be solved in C++ too, for instance.

The solution is below, but first try to solve it yourself.

Solution

for c in range( ord('a'), ord('z')+1 ):         #
    print chr(c),                               #
#                               ,)c(rhc tnirp    
#   :]1-::[) 1+)'z'(dro ,)'a'(dro (egnar ni c rof
Categories: python Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: