python综合练习题(二)

1.有40个人围成一圈,顺序排号。从第一个人开始报数(只报123这三个数),凡报到数字为3的人退出圈子,问最后留下的是原来第几号的那位。

# 创建一个包含40人的列表
people = list(range(1,41))

# 初始化计数器和索引
count = 0
index = 0

while len(people) > 1:
    # 每次报数,计数器加1
    count += 1

    # 如果报数到3,则移除当前的人
    if count == 3:
        people.pop(index) # 移除当前报数的人
        count = 0 # 重置计数器为0
    else:
        index += 1 # 如果计数器不等于3,继续下一个人

    if index >= len(people):# 如果索引超过了列表长度,重置索引为0,以实现循环报数
        index = 0

# 最后剩下的一个人即为答案,因为只剩下一个人了。
res = people[0]
print(res)

# 运行结果:28

 2.编写一个程序,提取以逗号分割的字符串中的所有数字,并将数字重新组合成一个数组;要求输入'34岁,67年,55岁,33岁,12日,98年'这一个字符串,提取出['34', '67', '55', '33', '12', '98'],注意数字用字符串来表示,结果保存到L2中;

import re 

input_string = '34岁,67年,55岁,33岁,12日,98年'

#使用正则表达式来提取数字
pattern = r'\d+' # 设置匹配模式:匹配一个或多个数字,\d表示匹配数字 +表示多个
#findall()方法用于在整个字符串中搜索所有符合正则表达式的字符串,并以列表的形式返回。
numbers = re.findall(pattern,input_string) 


# 将提取到的数字组合成一个数组
L2 = numbers

print(L2)

# 运行结果:['34', '67', '55', '33', '12', '98']

3.编写一个程序,根据给定的公式计算并打印值:Q=[(2∗C∗D)/H]。其中,假定C=50。H=30。D是一个变量,它的值应该以逗号分隔的序列输入到程序中。
要求输入:'100,150,180'这一字符串,依次按逗号分隔出每个数字,并带入公式中计算,计算结果保存到L3数组中【注意:计算出的数字用字符串表示】;

C = 50
H = 30

L3 = []

input_string = '100,150,180'

numbers = input_string.split(',')
for number in numbers:
    num = int(number)
    Q = int(((2*C*num)/H)**0.5)
    L3.append(str(Q))

print(L3)

# 运行结果:['18', '22', '24']

 4.编写一个程序,X,Y作为输入数值,根据两个数值会生成一个二维数组,**数组的第i行和第j列的元素值等于i×j**。注意:i和j都是从0开始计;  
要求输入'3, 5'这一字符串,则程序输出为:[[0,0,0,0,0],[0,1,2,3,4],[0,2,4,6,8]];将二维数组转化为一维数组,保存到L4数组中;

input_str = '3,5'

# 使用map()函数将分割出的数字进行映射成整数
X,Y = map(int,input_str.split(','))
    
# 创建一个二维数组并计算每个位置的数值
array_2d = [[i*j for j in range(Y)]for i in range(X)]

# 输出该二维数组
for row in array_2d:
    print(row)

# 将二维数组转化为一维数组
L4 = [element for row in array_2d for element in row]

# 输出一维数组
print(L4)

'''
运行结果:
[0, 0, 0, 0, 0]
[0, 1, 2, 3, 4]
[0, 2, 4, 6, 8]
[0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 0, 2, 4, 6, 8]
'''

5. 编写一个程序,以逗号分隔的单词序列作为输入,按照字母顺序对每个单词进行排序,并通过逗号分隔的序列来打印单词。要求输入'without,hello,bag,world'这一字符串,则输出为:['bag', 'hello', 'without', 'world'];将结果保存到数组L5中;

input_str = 'without,hello,bag,world'

# 使用split()方法将字符串分隔出来,返回的是一个列表
word_list = input_str.split(',')

# 使用sorted()函数进行排序,并将结果保存到L5中
L5 = sorted(word_list)

print(L5)

# 运行结果:['bag', 'hello', 'without', 'world']

6. 编写一个程序,接收一行序列作为输入,并在将句子中的所有字符大写后打印出来。  
要求输入'Practice makes perfect',并将每个单词大写后的结果保存到数组L6中;

L6 = []

input_str = 'Practice makes perfect'

word_list = input_str.split(' ')

#print(word_list)
for word in word_list:
    upper_word = word.upper()
    L6.append(upper_word)

print(L6)

# 运行结果:['PRACTICE', 'MAKES', 'PERFECT']

'''
知识点:
str = "hello"
print(str.upper())          把所有字符中的小写字母转换成大写字母
print(str.lower())          把所有字符中的大写字母转换成小写字母
print(str.capitalize())     把第一个字母转化为大写字母,其余小写
print(str.title())          把每个单词的第一个字母转化为大写,其余小写 
'''

7. 编写一个程序,以一系列空格分隔的单词作为输入,并在删除所有重复的单词后,按字母顺序排序后打印这些单词。  要求输入'hello world and practice makes perfect and hello world again'这一字符串,将最终结果保存到L7数组中;

input_str = 'hello world and practice makes perfect and hello world again'

word_list = input_str.split(' ')

nrep_word_list = []

for word in word_list:

    if word not in nrep_word_list:
        nrep_word_list.append(word)

L7 = sorted(nrep_word_list)
print(L7)

# 运行结果:['again', 'and', 'hello', 'makes', 'perfect', 'practice', 'world']

8.编写一个程序,接收一系列以逗号分隔的4位二进制数作为输入,然后检查它们是否可被5整除, 可被5整除的数字将以逗号分隔的顺序打印。  要求输入'0100,0011,1010,1000'这一字符串,将输出结果保存到L8数组中; 

L8 = []

input_str = '0100,0011,1010,1000'

binary_numbers = input_str.split(',')

for binary in binary_numbers:
    binary_int = int(binary,2)
    #print(binary_int)
    if binary_int % 5 == 0:
        L8.append(binary)

print(L8)

# 运行结果:['1010']

9. 编写一个程序,找到1000到3000之间并且所有位数均为偶数的所有数字,比如2000,2002等,将最终结果保存到L9中;

L9 = []

for i in range(1000,3000):
    gw = i%1000%100%10//1 # 个位
    sw = i%1000%100//10   # 十位
    bw = i%1000//100      # 百位
    qw = i//1000          # 千位

    if gw%2 == 0 and sw%2 == 0 and bw%2 == 0 and qw%2 == 0:
        L9.append(i)

print(L9)

'''
运行结果:
['2000', '2002', '2004', '2006', '2008', '2020', '2022', '2024', '2026', '2028', '2040', '2042', '2044', '2046', '2048', '2060', '2062', '2064', '2066', '2068', '2080', '2082', '2084', '2086', '2088', '2200', '2202', '2204', '2206', '2208', '2220', '2222', '2224', '2226', '2228', '2240', '2242', '2244', '2246', '2248', '2260', '2262', '2264', '2266', '2268', '2280', '2282', '2284', '2286', '2288', '2400', '2402', '2404', '2406', '2408', '2420', '2422', '2424', '2426', '2428', '2440', '2442', '2444', '2446', '2448', '2460', '2462', '2464', '2466', '2468', '2480', '2482', '2484', '2486', '2488', '2600', '2602', '2604', '2606', '2608', '2620', '2622', '2624', '2626', '2628', '2640', '2642', '2644', '2646', '2648', '2660', '2662', '2664', '2666', '2668', '2680', '2682', '2684', '2686', '2688', '2800', '2802', '2804', '2806', '2808', '2820', '2822', '2824', '2826', '2828', '2840', '2842', '2844', '2846', '2848', '2860', '2862', '2864', '2866', '2868', '2880', '2882', '2884', '2886', '2888']
'''

10. 编写一个输入英文句并计算该句子中共有多少字母和数字的程序。  

要求输入'Hello world! 123',将该字符串中字母和数字的个数保存到L10中;【注意:这题答案是用数字来表示】

L10 = []

# 初始化字母和数字的个数为0
letter = 0 
number = 0

input_str = 'Hello world! 123'

for string in input_str:
    if string.isalpha():
        letter += 1
    elif string.isdigit():
        number += 1
    else:
        continue
        
L10.append(letter)
L10.append(number)

print(L10)

# 运行结果:[10,3]

11. 编写一个输入英文句子并计算该句子中大写字母和小写字母数量的程序。  
要求输入'Hello world!',将大写字母和小写字母的个数存到数组L11中;

L11 = []

input_str = 'Hello world!'

upper_letter = 0
lower_letter = 0

for letter in input_str:
    if letter.isupper():
        upper_letter += 1
    elif letter.islower():
        lower_letter += 1
    else:
        continue

L11.append(upper_letter)
L11.append(lower_letter)

print(L11)

# 运行结果:[1,9]

12.

网站要求用户输入用户名和密码进行注册。编写程序以检查用户输入的密码有效性。  
以下是检查密码的标准:  
1  [a-z]之间至少有1个字母  
2 [0-9]之间至少有1个数字  
3 [A-Z]之间至少有一个字母  
4 [$#@]中至少有1个字符  
5 最短交易密码长度:6  
6 交易密码的最大长度:12  
您的程序接收一系列逗号分隔的密码,并将根据上述标准进行检查,将打印符合条件的密码,每个密码用逗号分隔。  

要求输入'ABd1234@1,a F1#,2w3E*,2We3345',将输出结果保存到L12中;

import re

def is_valid_password(password):
    
    if len(password)<6 or len(password)>12:
        return False
    if not re.search('[a-z]',password):
        return False
    if not re.search('[0-9]',password):
        return False
    if not re.search('[A-Z]',password):
        return False
    if not re.search('[$#@]',password):
        return False

    return True

input_password = 'ABd1234@1,a F1#,2w3E*,2We3345'

passwords = input_password.split(',')

L12 = []

for password in passwords:
    if is_valid_password(password):
        L12.append(password)

print(L12)

# 运行结果:['ABd1234@1']

13.数组1:[49, 38, 65, 97, 76, 13, 27, 49, 55, 4] ,排序方法:冒泡排序  

将排序后的结果保存到L13中; 

​array = [49, 38, 65, 97, 76, 13, 27, 49, 55, 4]

def bubblesort(array):
    for i in range(1,len(array)):
        for j in range(0,len(array)-i):
            if array[j] > array[j+1]:
                array[j],array[j+1] = array[j+1],array[j]
    return array

L13 = bubblesort(array)

print(L13)

# 运行结果:[4, 13, 27, 38, 49, 49, 55, 65, 76, 97]

14. 机器人从原点(0,0)开始在平面中移动,机器人可以通过给定的步骤向上,向下,向左和向右移动。  
机器人运动的痕迹如下所示:  
UP 5;DOWN 3;LEFT 3;RIGHT 2;方向之后的数字是步骤。  

请编写一个程序,计算一系列运动和原点之后距当前位置的距离。如果距离是浮点数,则只打印最接近的整数。  
要求输入'UP 5;DOWN 3;LEFT 3;RIGHT 2'这一字符串,将最终结果保存到L14中;

X,Y = 0,0

movement = 'UP 5;DOWN 3;LEFT 3;RIGHT 2'

actions = movement.split(';')

for action in actions:

    direction,steps = action.strip().split()
    steps = int(steps)

    if direction == 'UP':
        Y += steps
    elif direction == 'DOWN':
        Y -= steps
    elif direction == 'LEFT':
        X -= steps
    elif direction == 'RIGHT':
        X += steps

# 用欧几里得距离公式计算两点之间的距离
distance = round(((X-0)**2+(Y-0)**2)**0.5) 

L14 = [distance]
print(L14)

# 运行结果:[2]

15.假定我们数农场里的鸡和兔子中有35个头和94条腿。问我们有多少只鸡和多少只兔子?将最终结果保存到数组L15中; 

heads = 35
legs = 94

def chicken_rabbit(heads,legs):
    for chicken in range(1,heads):

        rabbit = heads - chicken

        if (2*chicken) + (4*rabbit) == legs:

            L15 = [chicken,rabbit]

            print(L15)

chicken_rabbit(heads,legs)

# 运行结果:[23,12]
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值