再这个题中,我试图用xrange代替range来解决问题,但是结果输出了‘xrange(2, 26, 4)’这样奇怪的答案。然后才理解了书上的“xrange不产生列表”这句话的含义。
”’python
if name == “main“:
numbers = raw_input(“Please input from, to, incremen”)
lst = numbers.strip().split()
f, t, i = (int(num) for num in lst)
print range(f, t, i)
”’
此处应该给while…else…一个掌声。
#!/usr/bin/env python
# coding: utf-8
def IsPrime(a):
b = a/2
while b > 1:
if a % b == 0:
return False
b -= 1
else:
return True
if __name__ == "__main__":
num = int(raw_input("Please input a number: ").strip())
if (IsPrime(num)):
print "The number is a prime."
else:
print "The number is not a prime."
#!/usr/bin/env python
# coding: utf-8
def ShowFactor(a):
count = a/2 + 1
lst = [i for i in xrange(1, count) if a % i == 0]
return lst
if __name__ == "__main__":
num = int(raw_input("input a number: "))
lst = ShowFactor(num)
print lst
#!/usr/bin/env python
# coding: utf-8
def ShowFactors(a, lst = []):
print "the lst is : ", lst
count = a / 2
for i in range(2, count+1):
if a % i == 0:
a = a/i
lst.append(i)
return ShowFactors(a, lst)
else:
lst.append(a)
return lst
if __name__ == "__main__":
num = int(raw_input("Please input a num: ").strip())
lst = ShowFactors(num)
print lst
这道题倒是简单,直接用8_5的答案改动一些就好了
#!/usr/bin/env python
# coding: utf-8
def ShowFactor(a):
count = a/2 + 1
lst = [i for i in xrange(1, count) if a % i == 0]
return lst
def IsPerfect(num):
lst = ShowFactor(num)
if sum(lst) == num:
return 1
else:
return 0
if __name__ == "__main__":
num = int(raw_input("input a number: "))
if(IsPerfect(num)):
print "It's a perfect num"
else:
print "not a perfect"
#!/usr/bin/env python
# coding: utf-8
def Factorial(n):
factor = 1
for i in xrange(1, n+1):
factor *= i
return factor
if __name__ == "__main__":
num = int(raw_input("Input a number: "))
print "N! is: ", Factorial(num)
这道题要为了提高计算的效率,我用了一个小技巧,每次返回的时候不但返回n-1的fibonacci数,还将n-2一并返回了,因此可以极大程度上避免重复计算。(详见数据结构与算法中有这部分的介绍)
#!/usr/bin/env python
# coding: utf-8
def Fibonacci(n):
if n == 1:
#之所以返回两个数是避免重复计算n-2的大小
return (0, 1)
Fibn2, Fibn1 = Fibonacci(n-1)
return Fibn1, Fibn2+Fibn1
def Fib(n):
#fib2得到的是n-1的Fibonacci数,所以无用
fib2, fib1 = Fibonacci(n)
return fib1
if __name__ == "__main__":
num = int(raw_input("Input a number: "))
print Fib(num)
总觉得这道题写的不够pythonic,不知道有没有什么其他更好的写法呢?
#/usr/bin/env python
# coding: utf-8
def CountWord(sentence):
iVowel = 0
iConsonant = 0
strVower = 'aeiou'
for i in sentence:
if i.isalpha():
if i.lower() in strVower:
iVowel += 1
else:
iConsonant += 1
iWord = len(sentence.split())
return (iVowel, iConsonant, iWord)
if __name__ == "__main__":
sentence = raw_input("Input a sentence: ")
iNum = CountWord(sentence)
print "The number of vowerl, consonant, word is: %s, %s, %s" %iNum
#!/usr/bin/env python
# coding: utf-8
def ListPrint():
print "_" * 20
iStart = int(raw_input("Enter begin value: "))
iEnd = int(raw_input("Enter end value: "))
bIsStr = False
if 32 < iStart < 128 or 34 < iEnd < 129 or (iStart<33 and iEnd>128):
bIsStr = True
print "DEC\t\tBIN\t\tOCT\t\tHEX\t\tASCII"
else:
bIsStr = False
print "DEC\t\tBIN\t\tOCT\t\tHEX"
print "_" * 30
for i in xrange(iStart, iEnd):
if 32128:
print "%s\t\t%s\t%s\t\t%s\t\t%s" %(i, bin(i), oct(i), hex(i), chr(i))
else:
print "%s\t\t%s\t%s\t\t%s" %(i, bin(i), oct(i), hex(i))
if __name__ == "__main__":
ListPrint()