取一个char并从char打印到'a'并反转它应该是递归的 [英] take a char and print out from char to 'a' and reverse it should be recursive

查看:76
本文介绍了取一个char并从char打印到'a'并反转它应该是递归的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码应将char作为参数,并按字母顺序将其显示为'a'并反转为char。

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 wrote 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

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

这篇关于取一个char并从char打印到'a'并反转它应该是递归的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆