1、輸入一行字元,分別統計出其中英文字母、空格、數字和其他字元的個數。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/6/5 17:53
# @Author : chenjiahe
# @File : count.py
#用isdigit函式判斷是否數字
#用isalpha判斷是否字母
#用isalnum判斷是否數字和字母的組合
while 1:
strings = input("Please input a strings(quit will exit): ")
alpha, dig, space, other = 0, 0, 0, 0
if strings.strip() == "quit":
exit(1)
for i in strings:
if i.isalpha():
alpha += 1
elif i.isdigit():
dig += 1
elif i.isspace():
space += 1
else:
other += 1
print("alpha:{0}".format(alpha))
print("dig:{0}".format(dig))
print("space:{0}".format(space))
print("other:{0}".format(other))
執行結果
2、99乘法表
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/6/5 18:33
# @Author : chenjiahe
# @File : 9*9.py
for a in range(1,10):
for b in range(1,a+1):
print("{0} x {1} = {2} ".format(a,b,a*b),end=' ')
print()
結果:
3、九宮格(數獨)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/6/6 16:11
# @Author : chenjiahe
# @File : 九宮格.py
'''
九宮格
-------------
| A | B | C |
| D | E | F |
| G | H | I |
-------------
所有的橫豎斜線加起來都等於15
'''
number = [1,2,3,4,5,6,7,8,9]
count = 1
for A in number:
a = number.copy()
a.remove(A)
for B in a:
b = a.copy()
b.remove(B)
for C in b:
c = b.copy()
c.remove(C)
for D in c:
d = c.copy()
d.remove(D)
for E in d:
e = d.copy()
e.remove(E)
for F in e:
f = e.copy()
f.remove(F)
for G in f:
g = f.copy()
g.remove(G)
for H in g:
h = g.copy()
h.remove(H)
for I in h:
if((A+B+C == D+E+F == G+H+I == A+D+G == B+E+H == C+F+I ==A+E+I == C+E+G == 15)):
print('''
方法{9}
-------------------
| {0} | {1} | {2} |
| {3} | {4} | {5} |
| {6} | {7} | {8} |
-------------------
'''.format(A,B,C,D,E,F,G,H,I,count))
count += 1
結果:
4、ABCD乘九=DCBA,A=? B=? C=? D=?
#ABCD乘九=DCBA,A=? B=? C=? D=? 答案: a=1,b=0,c=8,d=9 1089*9=9801
for a in range(1,10):
for b in range(0,10):
for c in range(0,10):
for d in range(1,10):
if ((a*1000 + b*100 + c*10 +d)*9 == (d*1000 +c*100 +b*10 + a)):
print("a = {0}".format(a))
print("b = {0}".format(b))
print("c = {0}".format(c))
print("d = {0}".format(d))
print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(a,b,c,d))
結果:
5、階乘
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/6/6 18:28 # @Author : chenjiahe # @File : 階乘.py ''' 計算n個階乘之和 0! + 1! + 2! …… + n! n! = n*(n-1)*(n-2)*(n-1)……*1 ''' #計算單個階乘 def jc(n): result = 1 if n == 0: return result else: for i in range(1,n+1): result *= i return result #計算階乘之和 n = input("計算n的階乘之和:") count = 0 for i in range(0,int(n)+1): count += jc(i) print("{0}的階乘之和是{1}".format(n,count))
結果:
6、面試題
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/6/7 14:07 # @Author : chenjiahe # @File : 面試題.py ''' 用python完成轉換序列的字元,有一串字串,包含只包含四個字母,GCTA,現在需要進行以下轉換: *'G' ->'C' *'C' ->'G' *'T' ->'A' *'A' ->'U' 如果序列中有不包含這四個字母的字元,就返回空。舉例: 1.'ACGTGGTCTTAA' -> 'UGCACCAGAAUU' 2.'ACGTXXXCTTAA' -> '' ''' while 1: string1 = input("Please input a string only with 'G' 'C' 'T' 'A': ") string2 = '' length1 = len(string1) for i in range(0,length1): if ((string1[i] == 'G')): string2 = string2 + 'C' elif ((string1[i] == 'C')): string2 = string2 + 'G' elif ((string1[i] == 'T')): string2 = string2 + 'A' elif ((string1[i] == 'A')): string2 = string2 + 'U' else: print(' ') exit(1) print("{0}".format(string2))
結果: