Python百道分类入门练习 新手易入佳境

7天前发布 gsjqwyl
7 0 0

Python百道分类练习 新手起步篇 – 整合版

一、数学问题

题目1:组合数字

题目1:有四个数字1、2、3、4,要找出能组成多少个互不相同且无重复数字的三位数,并列出这些数。

# 常规方法
num_list = []
for a in range(1, 5):
    for b in range(1, 5):
        for c in range(1, 5):
            if a != b and a != c and b != c:
                num_list.append(a * 100 + b * 10 + c)
print(num_list)

# 简洁写法
digits = [1, 2, 3, 4]
result = [i * 100 + j * 10 + k for i in digits for j in digits for k in digits if j != i and k != j and k != i]

# 使用itertools模块
import itertools
perms = itertools.permutations(digits, 3)
print(list(perms))
题目2:利润计算

题目2:根据企业利润计算奖金。利润在不同区间有不同提成比例,输入当月利润,求奖金总数。

def calculate_bonus(profit):
    bonus = 0
    if profit <= 100000:
        bonus = profit * 0.1
    elif 100000 < profit <= 200000:
        bonus = calculate_bonus(100000) + (profit - 100000) * 0.075
    elif 200000 < profit <= 400000:
        bonus = calculate_bonus(200000) + (profit - 200000) * 0.05
    elif 400000 < profit <= 600000:
        bonus = calculate_bonus(400000) + (profit - 400000) * 0.03
    elif 600000 < profit <= 1000000:
        bonus = calculate_bonus(600000) + (profit - 600000) * 0.015
    else:
        bonus = calculate_bonus(1000000) + (profit - 1000000) * 0.01
    return bonus

calculate_bonus(1000000)
题目3:完全平方数

题目3:找一个整数,使其加上100后是完全平方数,再加168也是完全平方数。

for c in range(1, 168):
    for b in range(1, c):
        if c ** 2 - b ** 2 == 168:
            number = b ** 2 - 100
            print(number)
题目4:日期天数计算

题目4:输入年、月、日,计算该日是当年第几天。

def get_day_of_year(year, month, day):
    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    total = 1
    if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
        days_in_month[1] += 1
    for i in range(month - 1):
        total += days_in_month[i]
    total += day
    return total

get_day_of_year(2021, 3, 1)

二、字符串操作

题目13:水仙花数

题目13:找出所有三位数的水仙花数,即各位数字立方和等于自身的数。

narcissistic_nums = []
for number in range(100, 1000):
    hundreds = number // 100
    tens = (number // 10) % 10
    units = number % 10
    if number == hundreds ** 3 + tens ** 3 + units ** 3:
        narcissistic_nums.append(number)
print(narcissistic_nums)
题目17:字符统计

题目17:输入一行字符,统计其中英文字母、空格、数字和其他字符的数量。

import string

def count_chars(text):
    letters = 0
    spaces = 0
    digits = 0
    others = 0
    for char in text:
        if char.isalpha():
            letters += 1
        elif char.isspace():
            spaces += 1
        elif char.isdigit():
            digits += 1
        else:
            others += 1
    print(f'字母: {letters}, 空格: {spaces}, 数字: {digits}, 其他: {others}')

count_chars("Kobe 8&24")

三、列表与字典操作

题目5:三数排序

题目5:输入三个整数,将其从小到大排序。

def sort_three(a, b, c):
    nums = [a, b, c]
    nums.sort()
    return nums

sort_three(1, 8, 2)
题目38:矩阵对角线元素和

题目38:求3×3矩阵主对角线元素之和。

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum_diagonal = 0
for i in range(3):
    sum_diagonal += matrix[i][i]
print(sum_diagonal)

四、文件操作

题目97:写入文件

题目97:从键盘输入字符,直到输入#为止,将内容写入磁盘文件。

filename = input('输入文件名: ')
with open(filename, 'w+') as file:
    content = ''
    while '#' not in content:
        file.write(content)
        content = input('输入内容: ')

五、日期时间处理

题目10:格式化时间

题目10:暂停一秒后输出当前格式化时间。

import time

time.sleep(1)
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(current_time)

六、图形绘制

题目56:圆形绘制

题目56:使用turtle库绘制圆形。

import turtle

turtle.title("绘制圆形")
turtle.setup(800, 600)
pen = turtle.Turtle()
pen.color("blue")
pen.width(5)
pen.circle(100)
turtle.done()

七、算法与数据结构

题目6:斐波那契数列

题目6:生成斐波那契数列前若干项。

def fibonacci(n):
    a, b = 0, 1
    for _ in range(n):
        print(a)
        a, b = b, a + b

fibonacci(10)
题目12:素数筛选

题目12:找出101到200之间的所有素数。

prime_numbers = []
for num in range(101, 200):
    is_prime = True
    for divisor in range(2, num):
        if num % divisor == 0:
            is_prime = False
            break
    if is_prime:
        prime_numbers.append(num)
print(prime_numbers)

八、位运算

题目51:按位与操作

题目51:学习按位与运算。

a = 0b1010  # 10
b = 0b1100  # 12
result = a & b
print(bin(result))  # 输出0b1000,即8
题目82:八进制转十进制

题目82:将八进制数转换为十进制。

octal = input('输入八进制数: ')
decimal = 0
for digit in octal:
    decimal = decimal * 8 + int(digit)
print(decimal)

九、其他编程问题

题目27:递归逆序输出

题目27:递归逆序输出输入的5个字符。

def reverse_print(s):
    if len(s) > 0:
        print(s[-1])
        reverse_print(s[:-1])

reverse_print("abcde")
题目34:三次输出函数

题目34:定义函数输出三次指定字符串。

def print_three_times():
    for _ in range(3):
        print("RUNOOB")

print_three_times()
© 版权声明

相关文章

暂无评论

暂无评论...