-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter10.txt
More file actions
127 lines (118 loc) · 4.21 KB
/
Chapter10.txt
File metadata and controls
127 lines (118 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#列表的方法:
append方法:在列表尾部添加新的元素:t.append('d')
extend方法:接收一个列表作为参数,并将其所有的元素附加到列表中:t1.extend(t2)
sort方法:将列表中的元素从低到高重新排序:t.sort()
对列表元素累加,sum函数,sum(t)
#编写一个nested_sum,接收一个由内嵌的整数列表组成的列表作为形参,并将内嵌列表中的值全部加起来
def nested_sum(t):
summ=0
i=0
for i in range(len(t)):
summ=summ+sum(t[i])
print(summ)
#接收一个字符串列表,并返回一个新列表,其元素是大写的字符串
def capitalize_all(t):
res=[]
for s in t:
res.append(s.capitalize())
return res
#使用capitalize_all来编写一个函数capitalize_nested,接收一个由内嵌字符串列表组成的列表作为形参,并返回一个新列表,其元素为内嵌的大写字符串的列表
def capitalize_nested(t):
resum=[]
i=0
for i in range(len(t)):
resum.append(capitalize_all(t[i]))
return resum
#接收一个字符串列表,并返回那些只包含大写字母的字符串
def only_upper(t):
res=[]
for s in t:
if s.isupper():
res.append(s)
return res
#编写一个函数,接收一个数字列表,并返回其累积和,即一个新的列表,其第i位元素是原始列表的前i+1个元素的和,例如,[1,2,3]的累积和是[1,3,6]
def addex(t):
i=0
res=[]
for i in range(len(t)):
res.append(sum(t[0:i+1]))
return res
#编写一个函数middle,接收一个列表作为形参,并返回一个新列表,包含除了第一个和最后一个元素之外的所有元素。
def middle(t):
t.pop(0)
t.pop(len(t)-1)
return t
#编写一个函数is_sorted,接收一个列表作为形参,并当列表是按照升序排好序的时候返回True,否则返回False。你可以假定(作为前置条件)列表的元素是可以使用关系操作符<,>等比较的
def is_sorted(t):
i=0
while i <len(t)-1:
if t[i]>t[i+1]:
return False
i=i+1
return True
#回文,编写一个函数is_anagram,接收两个字符串,当它们互为回文时返回True
def is_anagram(s,t):
if s==t[::-1]:
return True
else:
return False
#编写一个函数has_duplicates接收一个列表,当其中任何一个元素出现多于一次时,返回true。它不应当修改原始列表
def has_duplicates(t):
d = {}
for x in t:
if x in d:
return True
d[x] = True
return False
#如果班级中有23个学生,那么其中有两人生日相同的几率有多大?可以通过随机生成23个生日的样本并检查是否有相同的匹配来估计这个几率。提示:可以使用random模块中的randint函数来生成随机生日
import random
def srxtgl():
class1= []
i=0
while i<23:
class1.append(random.randint(1,365))
i=i+1
print(class1)
d={}
sum=0
for x in class1:
if x in d:
sum=sum+1
d[x]=True
print(sum)
p=sum/23
return p
#参考答案
import random
def has_duplicates(t):
"""Returns True if any element appears more than once in (t),
False otherwise."""
s = t[:]
s.sort()
for i in range(len(s)-1):
if s[i] == s[i+1]:
return True
return False
def random_bdays(n):
"""Returns a list of integers between 1 and 365, with length (n)."""
t = []
for i in range(n):
bday = random.randint(1, 365)
t.append(bday)
return t
def count_matches(students, samples):
"""Generates (samples) samples of (students) students, and counts
how many of them have at least one pair of students with the same bday."""
count = 0
for i in range(samples):
t = random_bdays(students)
if has_duplicates(t):
count += 1
return count
"""run the birthday simulation 1000 times and print the number of matches"""
num_students = 23
num_simulations = 1000
count = count_matches(num_students, num_simulations)
print('After %d simulations' % num_simulations)
print('with %d students' % num_students)
print('there were %d simulations with at least one match' % count)