设为首页 收藏本站
查看: 818|回复: 0

[经验分享] 几道Python的习题

[复制链接]

尚未签到

发表于 2015-4-27 11:32:22 | 显示全部楼层 |阅读模式
搞着玩。

1。allVowels (inString)
Returns True if all characters in inString are vowels, False otherwise. Note that an empty
string is trivially all vowels.
要求是当输入的inString里的字母都是元音的话, 执行程序后显示TRUE ,不都是的话显示FALSE。
列子:
allVowels (inString)
>>> allVowels("a")
True
>>> allVowels("a eoa")
False
>>> allVowels("iaiaa")
True



1 #!/usr/bin/python
2 # Filename: function1.py
3
4 def allVowels( inString ) :
5     vowels = "aeiouAEIOU";
6     ret = True;
7     for c in inString:
8         if c not in vowels :
9             ret = False;
10             break;
11     return ret;
12
13
14 print allVowels("a")
15 print allVowels("a eoa")
16 print allVowels("iaiaa")



2。average(integerList)
Returns the mean value of the integers stored in the list integerList.
要求显示输入的一组数字的平均数
列子:
average(integerList)
>>> average([5,7,8])
6.666666666666667
>>> average([10])
10.0
>>> average([1,2,3])
2.0


1 #!/usr/bin/python
2 # Filename: 2.py
3
4 def average( integerList ) :
5         sum = 0
6         for i in integerList :
7             sum += i
8         num = len( integerList )
9         if num > 0 :
10             return sum / num
11         else :
12             return None
13
14
15 print average([5,7,8])
16 print average([10])
17 print average([1,2,3])
18 print average( [] )
19



3。toBinary (integerValue)
Returns string consisting of the binary representation of integerValue.
要求把输入的10进制数字转为2进制
列子:
toBinary (integerValue)
>>> toBinary(17)
'10001'
>>> toBinary(6)
'110'
>>> toBinary(0)
'0'
>>> toBinary(1012)
'1111110100'


1 #!/usr/bin/python
2 # Filename: 3.py
3
4 def toBinary (integerValue) :   
5     str = ""
6     tryit = True
7     while tryit :
8         i = integerValue % 2;
9         if (i == 0) :
10             str = "0" + str;
11         else:
12             str = "1" + str;
13         integerValue = integerValue // 2
14         tryit = integerValue > 0;
15     return str;
16
17 print toBinary(17)
18 print toBinary(6)
19 print toBinary(0)
20 print toBinary(1012)


4。complexPalindrome(inString)
Returns True if inString is a complex palindrome, False if it it not. A complex palindrome is
a string that is the same forwards and backwards, ignoring spaces and capitalization.
要求如果输入的字母是对称的话 (大小写和空格忽略)显示TRUE, 不是的话显示FALSE。
列子:
complexPalindrome(inString)
>>> complexPalindrome("Aha")
True
>>> complexPalindrome("AHA")
True
>>> complexPalindrome("Race car")
True
>>> complexPalindrome("a")
True


1 #!/usr/bin/python
2 # Filename: 4.py
3
4 def complexPalindrome(inString) :   
5     i=0
6     j=-1
7     L = len(inString)
8     while i - j < L :
9         if (inString == " ") :
10             i += 1
11             continue
12         if (inString[j] == " ") :
13             j -= 1
14             continue
15         if inString.lower() != inString[j].lower() :
16             return False
17         i += 1
18         j -= 1
19         
20     return True
21     
22 print complexPalindrome("Aha")
23 print complexPalindrome("AHA")
24 print complexPalindrome("Race car")
25 print complexPalindrome("a")
26


5。playGame()
Repeatedly issues intelligent guesses to the guessing game you built in tutorial. See below for
more detailed information.
In tutorial, you saw a game that two players play together - one player chooses the number and
the other player guesses it, and the computer tells the 2nd player if the guess was too high or too
low. Now, you will write a computer program that actually plays the game &#8211; it will make smart
guesses until it finds the right answer.
The Rules
1. The user will pick a number between 1 & 100 (but not tell the computer what it is)
2. The computer will make a guess
3. The user will tell the computer whether its guess was too high, too low, or right on
4. Steps 2 & 3 repeat until the computer guesses right, when it will output &#8220;I got it!&#8221;
要求是在0-100之间让电脑猜数字, 电脑猜了之后问你他猜的是大了还是小了, 你要输入大了小了还是对了, 一直到猜对为止。
列子:
playGame()
>>> game()
I guess 50
Am I too high (1), too low(2), or right on? (3)
1
I guess 25
Am I too high (1), too low(2), or right on? (3)
1
I guess 12
Am I too high (1), too low(2), or right on? (3)
2
I guess 18
Am I too high (1), too low(2), or right on? (3)
1
I guess 15
Am I too high (1), too low(2), or right on? (3)
2
I guess 16
Am I too high (1), too low(2), or right on? (3)
2
I guess 17
Am I too high (1), too low(2), or right on? (3)
3
I got it!



1 #!/usr/bin/python
2 # Filename: 5.py
3
4
5 def playGame() :
6     _min = 0
7     _max = 100   
8     while True :
9         num = (_min + _max) // 2
10         print "I guess " + str(num)
11         print "Am I too high(1), too low(2), or right on? (3)"
12         act = int(input())
13         if act == 1 :
14             _max = num
15         elif act == 2 :
16             _min = num
17         else :
18             print "I got it!"
19             break   
20
21     
22     
23 playGame()
24

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-61189-1-1.html 上篇帖子: python程序 计算时间差加减 下篇帖子: Unofficial Windows Binaries for Python Extension Packages
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表