您的位置:首页 > 编程语言 > Python开发

python练习题 11-20

2017-07-25 21:56 253 查看

第11题

11.定义一个函数 generate_n_chars() 以整数 N 和字符 C 返回一个字符串, N 字符,只有C:举例来说, generate_n_chars(5,X)应该返回字符串”xxxxx”.(Python是不寻常的,你可以编写一个表达式 5 *“X”,将评价”xxxxx”.为了练习,你应该忽略这个问题可以用这种方式解决).

def generate_n_chars(n,c):
y = ""#初始化
for i in range(n):
y+=c
print(y)

generate_n_chars(5,"x")


第12题

12.定义一个程序 histogram() 取整数列表和打印一个直方图屏幕.例如,“直方图”(“4, 9, 7”)应该打印以下内容:

***

*********

*******
def histogram(n):
y = ""#初始化
for i in range(n):
y+="x"
print(y)

histogram(4)
histogram(9)
histogram(7)

第13题

13.功能从练习1 max() 和功能从练习2 max_of_three() 将只工作两和三号,分别.但是假设我们有更大数量的数字,或者假设我们不能预先知道它们有多少?写一个函数 max_in_list() 以一列数字和收益最大的一个

def max_in_list(x):
max = x[0]
for i in x:
if i > max:
max = i
print(max)

x = [4,5,6,3,7,98,5,4,3,1,6,55,6,23]
max_in_list(x)

第14题

14.编写一个程序,将单词列表映射成表示相应单词长度的整数列表.
def yingshe(x):
y=[]
for item in x:
y.append(len(item))
print(y)

x =["11","sdgsg","sdfgsgf"]
yingshe(x)
[2, 5, 7]

第15题

15.写一个函数 find_longest_word() ,接受一个单词列表并返回最长的长度.
def find_longest_word(x):
y=[]
max = len(x[0])
for item in x:
y.append(len(item))
if len(item) > max:
max = len(item)
print(max)

x =["11","sdgsg","sdfgsgf"]
find_longest_word(x)

第16题

16.写一个函数 filter_long_words() ,接受一个单词列表和一个整数N  返回列表的话,超过 N .

第17题

17.写一个版本的回文识别,也接受词回文如"Go hang a salami I'm a lasagna hog.", "Was it a rat I saw?", "Step on no pets", "Sit on a potato pan, Otis", "Lisa Bonet ate no 

basil", "Satan, oscillate my metallic sonatas", "I roamed under it as a tired nude Maori", "Rise to vote sir", or the exclamation "Dammit, I'm mad!"注意标点符号、大写

字母和间距通常都是忽略的.

第18题

18.一个全是一句话,包含了所有的字母至少一次,例如:“The quick brown fox jumps over the lazy dog”.你在这里的任务是写一个函数来检查一个句子是否是一个全或无.

def check(x):
book = "abcdefghijklmnopqrstuvwxyz"
x = x.lower()#换乘小写
x=x.split(" ")#去空格
str = ""
for item in  x:
str+=item#连成一个字符串
k = []
for item in list(set(str)):#先用集合,在用元组强制转化 元组可以遍历
if item in book:#去除非字母字符
k.append(item)
if len(k) == 26:#判断长度
print("True")
else:
print("False")
x = ("abcdefghijklmnopqrstuvwxyz%%$@afavcasd")
check(x)


第19题

19.”“99 Bottles of Beer”是美国和加拿大的传统歌曲.在长途旅行中唱歌很流行,因为它的重复性很好,很容易记住,而且唱起来也很长.这首歌的歌词简单如下:

 99 bottles of beer on the wall, 99 bottles of beer.     Take one down, pass it around, 98 bottles of beer on the wall..  

一句是重复的,每次少了一瓶.这首歌是完成当歌手或歌手达到零.你在这里的任务是写一个Python程序,能够生成所有歌曲的歌词.

def geci(x,y,n):
t = n
for i in range(1,n+1):
print(t,x,t,y)
t=t-1

x = "bottles of beer on the wall,"
y = "bottles of beer. Take one down, pass it around"
geci(x,y,99)

第20题

20.用下面的方式代表一个小的双语词典作为一个Python字典

  { "merry":"god", "christmas":"jul", "and":"och", "happy":gott", "new":"nytt", "year":"氓r" } 

用它把你的圣诞贺卡翻译成瑞典语.那就是,写一个函数 translate() 将英语单词表,返回一个列表,瑞典语.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: