46 Simple Python Exercises 6-15题

46 Simple Python Exercises 

会贴出原题和答案,答案不是最优的,也反映了我的学习过程,如果有时间会更新优化的代码。

  1. 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.
#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.
def sum(list):
    sum = 0
    for  number in list:
        sum=sum+number
    return sum
a=sum([1,2,3,4])
print(a)
def multiply(list):
    mul = 1
    for number in list:
        mul = mul * number
    return mul
b = multiply([1, 2, 3, 4])
print(b)

 

  1. Define a function reverse() that computes the reversal of a string. For example, reverse("I am testing") should return the string "gnitset ma I".
#  7. Define a function reverse() that computes the reversal of a string.
# For example, reverse("I am testing") should return the string "gnitset ma I".
def reverse(string):
    album=[]
    for str in string:
        album.insert(0,str)
    return (''.join(album))
a=reverse("I am testing")
print(a)

 

  1. 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.
#  8. 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.
def reverse(string):
    album=[]
    for str in string:
        album.insert(0,str)
    return (''.join(album))
def is_palindrome(word):
    if  word==reverse(word):
        return  True
    else:
        return False
print(is_palindrome("radiar"))
print(is_palindrome("radar"))

 

  1. 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.)
#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.)
def is_member(x,a):
    if x in a:
        return  True
    else:
        return False
print(is_member('a',['c','b']))
print(is_member('a',['c','a']))

 

  1. 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.
#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.
def is_member(x,a):
    if x in a:
        return  True
    else:
        return False
def overlapping(list1,list2):
    for li in list1:
        if is_member(li,list2):
            return True
    else:
        return False
print(overlapping(['e','a','b'],['c','d','e','f']))
print(overlapping(['a','b','c'],['f','d','e']))
  1. 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.)

我做的不对,需要重做

  1. 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:
****
*********
*******
#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:
#****
#*********
#*******
def histogram(list):
    for a in list:
        print(a*'*')
histogram([4,9,7])

 

  1. The function max() from exercise 1) and the function max_of_three() from exercise 2) will only work for two and three numbers, respectively. But suppose we have a much larger number of numbers, or suppose we cannot tell in advance how many they are? Write a function max_in_list() that takes a list of numbers and returns the largest one.
#The function max() from exercise 1) and the function max_of_three() from exercise 2)
# will only work for two and three numbers, respectively.
# But suppose we have a much larger number of numbers,
# or suppose we cannot tell in advance how many they are?
# Write a function max_in_list() that takes a list of numbers and returns the largest one.
def max(number1, number2):
    if number1>=number2:
        return number1
    else:
        return  number2
def max_in_list(list):
    a = max(list[0], list[1])
    i=2
    while i<len(list):
        a= max(a,list[i])
        i=i+1
    return a

print(max(1,2))
b=[5,5,3,19,0,2]
c=[6,6,6]
d=[1,3,3,5,55]
e=[5,7,3,99,4,8]
print(max_in_list(b))
print(max_in_list(c))
print(max_in_list(d))
print(max_in_list(e))

 

  1. Write a program that maps a list of words into a list of integers representing the lengths of the correponding words.
#Write a program that maps a list of words into a list of integers
# representing the lengths of the correponding words.
def words_into_integers(list):
    a=[]
    for words in list:
        a.append(len(words))
    return a
b=['abc','uuue','correponding']
print(words_into_integers(b))

 

  1. Write a function find_longest_word() that takes a list of words and returns the length of the longest one.
#Write a function find_longest_word() that takes a list of words
# and returns the length of the longest one.
def max(a,b):
    if len(a)>=len(b):
        return a
    else:
        return b
def find_longest_word(list):
    max_word=max(list[0],list[1])
    i=2
    while i<len(list):
        max_word=max(max_word,list[i])
        i=i+1
    return len(max_word)
a=['abc','correponding','word','user','finding']
print(find_longest_word(a))

 

转载于:https://www.cnblogs.com/wangchao0203/p/6561932.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值