# 第五章课后练习

1
def choose_sort(list):
list_len = len(list)
for i in range(0, list_len):
for j in range(i + 1, list_len):
if list[i] > list[j]:
list[i], list[j] = list[j], list[i]
list1 = [3, 6, 1, 8, 5, -20, 100, 50, 200]
choose_sort(list1)
print(list1)

2
def bubble_sort(list):
list_len = len(list)
for i in range(0, list_len):
is_sorted = True
for j in range(0, list_len - i - 1):
if list[j] > list[j + 1]:
list[j], list[j + 1] = list[j + 1], list[j]
is_sorted = False
if is_sorted: return
list1 = [3, 6, 1, 8, 5, -20, 100, 50, 200, -32, 123]
bubble_sort(list1)
print(list1)


3
def is_leap(year):
year = int(year)
if (year % 4 == 0) and (year % 100 != 0):
return True
elif year % 400 == 0:
return True
else:
return False
while(True):
year = input("请输入一个年份:")
if year == 'exit':
import sys
sys.exit(0)
print("%s是闰年吗? %s" % (year, is_leap(year)))

4
def count_str_char(string):
char_num, digit_num, space_num, other_num = 0, 0, 0, 0
for c in string:
if c.isdigit(): digit_num += 1
elif c.isalpha(): char_num += 1
elif c.isspace(): space_num += 1
else: other_num += 1
return char_num, digit_num, space_num, other_num
while(True):
string = input("请输入一个字符串:")
if string == 'exit':
import sys
sys.exit(0)
char_num, digit_num, space_num, other_num = count_str_char(string)
print('字母个数', char_num)
print('数字个数', digit_num)
print('空白个数', space_num)
print('其他字母个数', other_num)

5
def fn(n):
if n < 2:
exit()
sum = 1
for i in range(2, n+1):
sum += i ** 3
return sum
n = int(input("请输入数字:"))
result = fn(n)
print("1～%d的立方和是%d" % (n, result))

6
def fn(n):
result_list = [1]
for i in range(n):
result_list.append(result_list[-1] * len(result_list))
return result_list[-1]

n = int(input("请输入数字:"))
result = fn(n)
print("%d的阶乘是%d" % (n, result))

7
def remove_duplicate(source_list):
new_list = list({}.fromkeys(source_list).keys())
return new_list
# 另一种方法
#    new_list = []
#    [new_list.append(i) for i in source_list if not i in new_list]
#    return new_list

length = int(input("请输入列表的长度:"))
my_list = []
for i in range(length):
my_list.append(input('请输入字符串:'))
print(remove_duplicate(my_list))

8
import random

def fn(n):
i, tmp_list = 0, []
while True:
num = random.randint(0, 100)
# 如果随机数不包含在列表中，则保存
if num not in tmp_list:
tmp_list.append(num)
i += 1
if i == n:
break
# 将列表转成元组返回
return tuple(tmp_list)
n = int(input("请输入整数n:"))
print(fn(n))

9
import random

def fn(n):
i, tmp_list = 0, []
while True:
num = random.randint(65, 65 + 25)
# 如果随机数不包含在列表中，则保存
if chr(num) not in tmp_list:
tmp_list.append(chr(num))
i += 1
if i == n:
break
# 将列表转成元组返回
return tuple(tmp_list)
n = int(input("请输入整数n:"))
print(fn(n))

10
import random

def fn(n):
# 输出矩阵
for i in range(n):
for j in range(n):
print(' %2d ' % (i * n + j + 1), end="")
print()
print('-' * (4 * n))
# 输出转置矩阵
for i in range(n):
for j in range(n):
print(' %2d ' % (j * n + i + 1), end="")
print()

n = int(input("请输入整数n:"))
fn(n)

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

Ronny-starting

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

06-04

11-09
07-21
11-12
01-14
02-24
10-17
06-14
05-10
04-20
03-22