Take A Char And Print Out From Char To 'a' And Reverse It Should Be Recursive
this code should take a char as an argument and print out that char in alphabetically order to 'a' and reverse to char. >>> characters('d') d c b a b c d this is what Ii
Solution 1:
defcharacters(c):
print' '.join(map(chr, range(ord(c), ord('a'), -1) + range(ord('a'), ord(c)+1)))
>>> characters('d')
d c b a b c d
or
defcharacters(c):
for n in xrange(ord(c), ord('a'), -1):
printchr(n),
for n in xrange(ord('a'), ord(c)+1):
printchr(n),
print
Solution 2:
Well, you're halfway there as it stands. Now you just have to figure out how to take numb back to your letter.
In order to make it go backwards in the alphabet, you're using numb=numb-1
. So in order to make it go forward in the alphabet, what would be the opposite of that? Then you could put that in another loop afterwards.
Post a Comment for "Take A Char And Print Out From Char To 'a' And Reverse It Should Be Recursive"