46 Simple Python Exercises 1-5题

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

This is version 0.45 of a collection of simple Python exercises constructed (but in many cases only found and collected) by Torbjörn Lager (torbjorn.lager@ling.gu.se). Most of them involve characters, words and phrases, rather than numbers, and are therefore suitable for students interested in language rather than math.

  1. Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Python. (It is true that Python has the max() function built in, but writing it yourself is nevertheless a good exercise.)
#Define a function max() that takes two numbers as
# arguments and returns the largest of them.
# Use the if-then-else construct available in Python.
# (It is true that Python has the max() function built in,
# but writing it yourself is nevertheless a good exercise
def max(number1, number2):
    if number1>number2:
        return number1
    else:
        return number2
print(max(1,2))
print(max(3,2))
print(max(2,2))

 

  1. Define a function max_of_three() that takes three numbers as arguments and returns the largest of them
#Define a function max_of_three() that takes three numbers
# as arguments and returns the largest of them
def max(number1, number2):
    if number1>number2:
        return number1
    else:
        return number2
def max_of_three(number1,number2,number3):
    if max(number1,number2)>number3:
        return max(number1,number2)
    else:
        return number3
print(max_of_three(1,2,3))
print(max_of_three(2,2,3))
print(max_of_three(1,3,3))
print(max_of_three(3,2,3))
print(max_of_three(2,3,2))
print(max_of_three(1,3,2))
print(max_of_three(1,3,3))
print(max_of_three(3,2,1))
print(max_of_three(3,3,1))
print(max_of_three(3,2,2))
print(max_of_three(2,2,2))

 

  1. Define a function that computes the length of a given list or string. (It is true that Python has the len() function built in, but writing it yourself is nevertheless a good exercise.)
#Define a function that computes the length of a given list or string.
# (It is true that Python has the len() function built in,
# but writing it yourself is nevertheless a good exercise.)
def len_a(words):
    i=0
    for word in words:
        i=i+1
    return i
print(len_a([1,2,3,'num','a']))
print(len_a('asdfghjkl'))
print(len_a('hjfjsd,fdf'))

 

  1. Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.
#Write a function that takes a character (i.e. a string of length 1)
# and returns True if it is a vowel, False otherwise.
def vowel_t(string):
    vowel = ['a', 'e', 'i', 'o', 'u']
    if string in vowel:
        return True
    else:
        return False
print(vowel_t('a'))
print(vowel_t('b'))

 

  1. 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".
#  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".
def robber_a(robber):
    vowel = ['a', 'e', 'i', 'o', 'u']
    robb=[]
    for rob in robber:
            if rob in vowel:
                robb.append(rob)
            elif rob==" ":
                robb.append(rob)
            else:
                robb.append((rob+'o'+rob))
    return(''.join(robb))
a=robber_a("this is fun")
print(a)

 

 




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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值