Skip to content

Commit 0ad52e5

Browse files
committed
Basic exercises
1 parent 5bbabd7 commit 0ad52e5

22 files changed

+487
-0
lines changed

basic_exercise/Christmas_tree.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python3
2+
3+
# input a number then print a '*' Christmas tree
4+
# like this:
5+
# *
6+
# ***
7+
# *****
8+
# *******
9+
# *********
10+
# ***********
11+
# *
12+
# *
13+
# *
14+
# *
15+
# *
16+
# *
17+
18+
n = int(input('输入一个整数:'))
19+
20+
for i in range(0,n+1):
21+
print(' '*(n-i)+ (2*i-1)*'*')
22+
else:
23+
for x in range(n):
24+
print('*'.center(2*n-1))
25+
26+
# then change the code to print a number Christmas tree
27+
# like this:
28+
# (the placeholder of the number above 10 is two byte, and the number for 0 to 9 is one.
29+
# so, print this kind of tree, you need input the number less than 10)
30+
# 1
31+
# 222
32+
# 33333
33+
# 4444444
34+
# #
35+
# #
36+
# #
37+
# #
38+
39+
for i in range(0,n+1):
40+
print(' '*(n-i)+ (2*i-1)*str(i))
41+
else:
42+
for x in range(n):
43+
print('#'.center(2*n-1))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/python3
2+
# 练习:
3+
# 1.算出100到999以内的水仙花数(Narcissistic Number)
4+
# (水仙花数是值百位的3次方加上十位的3次方加上各位的3次方等于原数的数字)
5+
# 例如: 153 等于 1**3 + 5**3 + 3**3
6+
7+
# 方法1
8+
for x in range(1,10):
9+
for y in range(10):
10+
for z in range(10):
11+
n = x*100 + y *10 + z
12+
if x **3 + y **3 + z**3 == n:
13+
print('100到999以内的水仙花有:', n)
14+
# 方法2
15+
for x in range(100,1000):
16+
a = x//100
17+
b = x%100//10
18+
c = x % 10
19+
if a**3 + b**3 + c**3 == x:
20+
print('100到999以内的水仙花有:', x)
21+
# 方法3
22+
for x in range(100, 1000):
23+
a = int(str(x)[0])
24+
b = int(str(x)[1])
25+
c = int(str(x)[2])
26+
if a**3 + b**3 + c**3 == x:
27+
print('100到999以内的水仙花有:', x)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python3
2+
import time
3+
4+
# input your birthday,the function will tell you what day it is, and how many days you have spend in the earth
5+
def birthday():
6+
year = int(input('请输入你的出生年:'))
7+
month = int(input('请输入你的出生月:'))
8+
day = int(input('请输入你的出生日:'))
9+
mt = time.mktime((year, month, day,0,0,0,0,0,0))
10+
lt = time.localtime(mt)
11+
l = ['星期一',
12+
'星期二',
13+
'星期三',
14+
'星期四',
15+
'星期五',
16+
'星期六',
17+
'星期日']
18+
print(l[lt[6]])
19+
days = (time.time()-mt)//(3600*24)
20+
print('今天是你在地球上第 %d 天' % days)
21+
22+
birthday()

basic_exercise/clock.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/python3
2+
import time
3+
4+
# this is a alarm clock
5+
6+
def clock():
7+
hour = int(input('设定闹钟,小时:'))
8+
minute = int(input('设定闹钟,分钟:'))
9+
while True:
10+
t = time.localtime()
11+
pt = '\r%02d:%02d:%02d' % (t[3],t[4],t[5])
12+
print(pt,end='')
13+
if t[3] == hour and t[4] == minute:
14+
print()
15+
print('时间到')
16+
return
17+
time.sleep(1)
18+
clock()

basic_exercise/electric_clock.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/python3
2+
3+
import time
4+
5+
#This is a function to show you what is the time now
6+
7+
def show_time():
8+
while True:
9+
lt = time.localtime()
10+
s = '\r%02d:%02d:%02d' % lt[3:6]
11+
print(s)
12+
time.sleep(1)
13+
show_time()

basic_exercise/fabonacci.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!\usr\bin\python3
2+
# author:kepner
3+
# date:2018.04.12
4+
# 计算出20个斐波那契数(fabonacci)的两种方法
5+
6+
# method 1
7+
# 定义一个空的列表用于存放计算出的数
8+
l = [1, 1] # 创建一个列表,包含前两个没有规律的数
9+
for x in range(18): # 循环18次,依次将从第三个数起的所有数字添加进列表
10+
l.append(l[-1]+l[-2]) # 每个数等于前两个数的和
11+
print(l)
12+
13+
# method 2:
14+
l2 = []
15+
i = 1 # 作为第一个数
16+
j = 1 # 作为第二个数
17+
l.append(i)
18+
l.append(j)
19+
for x in range(18): # 循环18次,依次将从第三个数起的所有数字添加进列表
20+
l2.append(i+j) # 每个数等于前两个数的和
21+
i, j = j, i+j
22+
print(l2)

basic_exercise/interactive.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#! /usr/bin/python3
2+
3+
# author:kepner
4+
# date:2018.04.18
5+
6+
# a simple python3 interactive simulation
7+
8+
g = {}
9+
l = {}
10+
11+
while True:
12+
s = input('>>>')
13+
if s == 'exit()':
14+
break
15+
exec(s, g, l)
16+
17+
# if you need, this statement can print the variable you have created.
18+
print('您刚刚创建的变量有:', l)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/python3
2+
3+
# input a number then print a '*' isosceles trangle
4+
# *
5+
# ***
6+
# *****
7+
# *******
8+
# *********
9+
n = int(input('输入一个整数:'))
10+
i = 1
11+
while i <= n:
12+
print(('*'*(2*i-1)).center(2*n-1))
13+
i += 1
14+
print('----------------------')
15+
16+
for x in range(1, n+1):
17+
print((n-x)*' '+ (2*x-1)*'*')

basic_exercise/monkey_peach.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/python3
2+
# 有一只小猴子,摘了很多桃子。第一天吃了全部桃子的一半,感觉不饱有吃了一个;第二天吃了剩下桃子的一半,感觉不饱有吃了一个;...以此类推,到第十天,发现只剩一个了
3+
# 请问第一天摘了多少个桃子
4+
peach = 1
5+
for x in range(1,10):
6+
peach = (peach + 1)*2
7+
print('猴子总共摘了:%d 个桃子' % peach)

basic_exercise/mutiply_9.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 输出 9*9 乘法口诀表。
2+
3+
for x in range(1,10):
4+
for y in range(1,x+1):
5+
print(str(y)+'*'+str(x),end=' | ')
6+
print()
7+
print(x*'------')

0 commit comments

Comments
 (0)