Python--46 Simple Python Exercises-Very simple exercises 46 Simple Python Exercises-Very simple exer

46 Simple Python Exercises-Very simple exercises

 


四、Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.html

#編寫一個函數,該函數接受一個字符(即長度爲1的字符串),若是是元音,則返回true,不然返回false。python

def if_vowel(a):
a=a.lower()
if a in('a','e','i','o','u'):
return True
else:
return False
print(if_vowel('A'))

五、Write a function translate() that will translate a text into "r�varspr�ket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".express

 編寫一個函數translate(),將文本轉換爲「r varspr ket」(瑞典語表示「強盜的語言」)。也就是說,將每一個輔音加倍,並在中間加上「o」。例如,translate(「this is fun」)應該返回字符串「totohohisos isos fofunon」。app

def if_vowel(a):
if a in('a','e','i','o','u'):
return True
else:
return False
def translate(string):
char_list=[]
for char in string:
if if_vowel(char) or char==" ":
char_list.append(char)
else:
char_list.append(char+'o'+char)
return "".join(char_list)
print(translate("this is fun"))

六、Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) should return 24.
定義一個函數sum()和一個函數multiple(),分別對數字列表中的全部數字求和和和相乘。例如,SUM([1,2,3,4])應返回10,乘法([1,2,3,4])應返回24。
def sum(sum_list):
sum1 = 0
for b in sum_list:
sum1 += b
return sum1
print(sum([1,2,3,4]))

def mul(mul_list):
mul1=1
for a in mul_list:
mul1 = mul1*a
return mul1
print(mul([1,2,3,4,5]))

七、Define a function reverse() that computes the reversal of a string. For example, reverse("I am testing") should return the string "gnitset ma I".
定義計算字符串反轉的函數reverse()。例如,Reverse(「I am testing」)應該返回字符串「gnitset ma i」。
def reverse(string):
revStr = ''
for i in range(len(string)):
revStr += string[len(string) - i - 1]

return revStra

print(reverse('test'))

#方法二
def reverse(string):
revStr=''
for i in range(len(string)-1, -1, -1):
revStr += string[i]

return revStr

print(reverse('test'))

八、Define a function is_palindrome() that recognizes palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar") should return True.
定義一個函數是識別迴文的_palindrome()(即向後寫的單詞)。例如,「迴文」(「radar」)應該返回true。
def is_palindrome(string):
if string == ''.join(list(reversed(string))):#reversed輸出的是一個迭代器
return True
else:
return False
print(is_palindrome('8radar8'))

#迭代器:http://www.runoob.com/python3/python3-iterator-generator.html
#Python join() 方法用於將序列中的元素以指定的字符鏈接生成一個新的字符串。
#.join: http://www.runoob.com/python3/python3-string-join.html


九、Write a function is_member() that takes a value (i.e. a number, string, etc)
x and a list of values a, and returns True if x is a member of a, False otherwise. (Note that this is exactly
what the in operator does, but for the sake of the exercise you should pretend Python did not have this operator.)

寫一個函數是獲取一個值(即數字、字符串等)的_member()。函數

x和值a的列表,若是x是a的成員,則返回true,不然返回false。(注意,這正是oop

in操做符所作的,可是爲了練習,您應該僞裝python沒有這個操做符。)post

 
def is_member(x,a_list):
for b in a_list:
if x==b:
return True
return False

print(is_member(3,[2,3,4]))

十、Define a function overlapping() that takes two lists and returns True if they have at least one member in common, False otherwise. You may use your is_member() function, or the in operator, but for the sake of the exercise, you should (also) write it using two nested for-loops.this

定義一個函數overlapping(),該函數接受兩個列表,若是它們至少有一個公共成員,則返回true,不然返回false。您可使用is_member()函數或in運算符,但爲了練習,您還應該(也)使用兩個嵌套的for循環來編寫它。lua

def overlapping(list1,list2):
for a in list1:
for b in list2:
if a==b:
return True
return False
print(overlapping([1,2,3],[11,5,6,7]))


11.Define a function generate_n_chars() that takes an integer n and a character c and returns a string, n characters long, consisting only of c:s.
For example, generate_n_chars(5,"x") should return the string "xxxxx". (Python is unusual in that you can actually write an expression 5 * "x"
that will evaluate to "xxxxx". For the sake of the exercise you should ignore that the problem can be solved in this manner.)

def generate_n_chars(n,c):
list1=[]
for i in range(n):
list1.append(c)#注意append的用法,以前寫成了list1=list1.append() 這樣就錯了
return ''.join(list1)
print(generate_n_chars(10,'s2'))


十二、Define a procedure histogram() that takes a list of integers and prints a histogram to the screen.
For example, histogram([4, 9, 7]) should print the following:

****url

*********

*******

def histogram(list1):    list2=[]    for a in list1:        list2.append(a*'*')    return '\n'.join(list2)print(histogram([3,4,5]))
相關文章
相關標籤/搜索