开发者

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 wro开发者_运维知识库te so far but it is not the correct output

def characters(char):
    numb=ord(char)
    while numb>ord('a'):
>>      print chr(numb),
        numb=numb-1

    return


>>> characters('h')
g f e d c b a


def characters(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

def characters(c):
  for n in xrange(ord(c), ord('a'), -1):
    print chr(n),
  for n in xrange(ord('a'), ord(c)+1):
   print chr(n),
  print


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜