-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
201511580440 李坦 #661
Comments
2(20分)写函数,该函数接收一个参数:列表nums。nums可表示为: [x1,x2,x3...xn]。该函数将nums中的元素重新排列到一个新列表中,结果为:[每个小于等于x1的元素, x1, 每个大于x1的元素],并返回新列表(新列表列表元素个数与原列表是相同的)。 def f(nums):
result = []
for i in range(len(nums)):
result.append(nums[i])
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if (result[i] > result[j]):
t = result[i]
result[i] = result[j]
result[j] = t
return result
# Linear Congruential Generators
test = []
seed = 1234567891234
for i in range(20):
seed = ((seed * 1103515245) % (2 ** 31) + 12345) % 20
test.append(seed)
for i in test:
print(i, end=' ')
print()
for i in f(test):
print(i, end = ' ') |
3(20分)建立列表[x1,x2,x3...xn],利用该列表,得到并打印新列表[x1, x1+x2, x1+x2+x3, ..., x1+x2+x3...+xn],注意,每个元素是该元素及之前所有元素累加和的值。以[2,4,6,8,...100]为例。 # list(map(print, reducelist(lambda x, y: x+y, range(2, 101, 2))))
list1 = []
for i in range(2, 101, 2):
list1.append(i)
list2 = []
acc = 0
for i in list1:
acc = acc + i
list2.append(acc)
for i in list2:
print(i) |
4199801.txt文本文件中存储了《人民日报》1998年1月的全文内容。读取该文件,完成以下任务: with open('199801.txt', 'r') as f:
word_count = 0
idiom_count = 0
s = dict()
s2 = dict()
line = None
while line != '':
line = f.readline()
for word in line.split(' '):
word_count += 1
s[word] = None
if word.endswith('/i'):
idiom_count += 1
if (word in s2):
s2[word] += 1
else:
s2[word] = 1
list1 = []
for i in s2:
list1.append([i, s2[i]])
length = len(list1)
for i in range(length):
for j in range(i+1, length):
if (list1[i][1] > list1[j][1]):
t = list1[i]
list1[i] = list1[j]
list1[j] = t
with open('res.txt', 'w') as f2:
for i in reversed(list1):
f2.write(i[0])
f2.write(' ')
f2.write(str(i[1]))
f2.write('\n')
for i in list1:
if (i[1] >9):
print('* ', end='')
print(i[0], end=' ')
print(i[1])
print('以上是频次达到10次的词语频次表的倒序')
print("词个数:", end=' ')
print(word_count)
print("不同的词个数:", end=' ')
print(len(s))
print("成语个数:", end=' ')
print(len(s))
print("不同的成语个数:", end=' ')
print(len(s2))
以上是频次达到10次的词语频次表的倒序 词个数: 2263993 |
5(5分)写递归函数,该函数可返回一个给定列表中的最大值(其中元素均为数值型)。主程序以列表[2,344,3,23,34234,22,11,4,8,2]为例调用,并打印最大值。 def f(nums):
if len(nums) == 1:
return nums[0]
else:
rest_max = f(nums[1:])
return nums[0] if nums[0] > rest_max else rest_max
print(f([2,344,3,23,34234,22,11,4,8,2])) |
6 (3分)给定一个字符串,该字符串是一个数学表达式,其中有若干个小括号'(' 及 ')'。写代码,判断该表达式中的小括号的匹配是否正确(不考虑数字等其他字符是否导致表达式错误),对小括号正确匹配指:左小括号必须与右小括号成对闭合,且不能剩余未闭合的左或者右小括号。如 def is_valid_expression(expr):
balance = 0
for c in expr:
if c == '(':
balance += 1
elif c == ')':
balance -= 1
if balance < 0:
return False
return True if balance == 0 else False |
1
(20分)写函数,该函数接收一个参数n。参数n为[1,9]之间的正整数。函数可根据n打印倒三角形到屏幕。形式如图所示,注意除了最后一行以外,每一行的两个数字之间有一个空格。主程序以整数7调用这个函数并打印倒三角形。(如给定n=9,则函数打印三角形如下:)
The text was updated successfully, but these errors were encountered: