Python每日五道练手试题以及日常解析(八)

题目1:
创建一个工具类ArrayUtils,实现以下功能:

  • find_max(array):返回数组中的最大值
  • find_min(array):返回数组中的最小值
  • calculate_sum(array):返回数组中所有元素的和
  • calculate_average(array):返回数组中所有元素的平均值
  • is_sorted(array):判断数组是否已排序(升序或降序)

答案和解析:

 

python

class ArrayUtils: def find_max(self, array): return max(array) def find_min(self, array): return min(array) def calculate_sum(self, array): return sum(array) def calculate_average(self, array): return sum(array) / len(array) def is_sorted(self, array): return array == sorted(array) or array == sorted(array, reverse=True) # 测试 array_utils = ArrayUtils() print(array_utils.find_max([1, 5, 3, 9, 2])) # 输出:9 print(array_utils.find_min([1, 5, 3, 9, 2])) # 输出:1 print(array_utils.calculate_sum([1, 5, 3, 9, 2])) # 输出:20 print(array_utils.calculate_average([1, 5, 3, 9, 2])) # 输出:4.0 print(array_utils.is_sorted([1, 2, 3, 4, 5])) # 输出:True

题目2:
创建一个工具类StringListUtils,实现以下功能:

  • concatenate_strings(strings):将字符串列表中的所有字符串连接起来
  • find_longest_string(strings):返回字符串列表中最长的字符串
  • count_occurrences(strings, substring):返回字符串列表中包含子字符串的字符串个数
  • remove_duplicates(strings):返回去除字符串列表中重复字符串后的结果
  • sort_strings(strings):返回按字母顺序排序的字符串列表

答案和解析:

 

python

class StringListUtils: def concatenate_strings(self, strings): return ''.join(strings) def find_longest_string(self, strings): return max(strings, key=len) def count_occurrences(self, strings, substring): count = 0 for string in strings: if substring in string: count += 1 return count def remove_duplicates(self, strings): return list(set(strings)) def sort_strings(self, strings): return sorted(strings) # 测试 string_list_utils = StringListUtils() print(string_list_utils.concatenate_strings(["hello", "world"])) # 输出:helloworld print(string_list_utils.find_longest_string(["hello", "world", "python"])) # 输出:python print(string_list_utils.count_occurrences(["hello", "world", "hello"], "hello")) # 输出:2 print(string_list_utils.remove_duplicates(["hello", "world", "hello"])) # 输出:['world', 'hello'] print(string_list_utils.sort_strings(["hello", "world", "python"])) # 输出:['hello', 'python', 'world']

题目3:
创建一个工具类FileIOUtils,实现以下功能:

  • read_file(filename):返回文件中的内容
  • write_file(filename, content):将content写入文件中
  • count_lines(filename):返回文件中的行数
  • count_words(filename):返回文件中的单词数
  • count_characters(filename):返回文件中的字符数

答案和解析:

 

python

class FileIOUtils: def read_file(self, filename): with open(filename, 'r') as file: return file.read() def write_file(self, filename, content): with open(filename, 'w') as file: file.write(content) def count_lines(self, filename): with open(filename, 'r') as file: return len(file.readlines()) def count_words(self, filename): with open(filename, 'r') as file: content = file.read() words = content.split() return len(words) def count_characters(self, filename): with open(filename, 'r') as file: content = file.read() return len(content) # 测试 file_io_utils = FileIOUtils() file_io_utils.write_file("example.txt", "Hello, world!") print(file_io_utils.read_file("example.txt")) # 输出:Hello, world! print(file_io_utils.count_lines("example.txt")) # 输出:1 print(file_io_utils.count_words("example.txt")) # 输出:2 print(file_io_utils.count_characters("example.txt")) # 输出:13

题目4:
创建一个工具类MathGeometryUtils,实现以下功能:

  • calculate_circle_area(radius):返回圆的面积
  • calculate_circle_circumference(radius):返回圆的周长
  • calculate_rectangle_area(length, width):返回矩形的面积
  • calculate_rectangle_perimeter(length, width):返回矩形的周长
  • calculate_triangle_area(base, height):返回三角形的面积

答案和解析:

 

python

import math class MathGeometryUtils: def calculate_circle_area(self, radius): return math.pi * radius ** 2 def calculate_circle_circumference(self, radius): return 2 * math.pi * radius def calculate_rectangle_area(self, length, width): return length * width def calculate_rectangle_perimeter(self, length, width): return 2 * (length + width) def calculate_triangle_area(self, base, height): return 0.5 * base * height # 测试 math_geometry_utils = MathGeometryUtils() print(math_geometry_utils.calculate_circle_area(2)) # 输出:12.566370614359172 print(math_geometry_utils.calculate_circle_circumference(2)) # 输出:12.566370614359172 print(math_geometry_utils.calculate_rectangle_area(3, 4)) # 输出:12 print(math_geometry_utils.calculate_rectangle_perimeter(3, 4)) # 输出:14 print(math_geometry_utils.calculate_triangle_area(5, 6)) # 输出:15.0

题目5:
创建一个工具类DateUtils,实现以下功能:

  • get_current_date():返回当前日期的字符串表示(格式为YYYY-MM-DD)
  • get_days_diff(start_date, end_date):返回两个日期之间的天数差
  • is_leap_year(year):判断输入的年份是否为闰年
  • get_weekday(date):返回输入日期的星期几(0表示星期一,1表示星期二,以此类推,6表示星期日)

答案和解析:

 

python

import datetime class DateUtils: def get_current_date(self): return datetime.date.today().strftime("%Y-%m-%d") def get_days_diff(self, start_date, end_date): start = datetime.datetime.strptime(start_date, "%Y-%m-%d").date() end = datetime.datetime.strptime(end_date, "%Y-%m-%d").date() return (end - start).days def is_leap_year(self, year): if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: return True else: return False else: return True else: return False def get_weekday(self, date): weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] date_obj = datetime.datetime.strptime(date, "%Y-%m-%d").date() return weekdays[date_obj.weekday()] # 测试 date_utils = DateUtils() print(date_utils.get_current_date()) # 输出:当前日期的字符串表示 print(date_utils.get_days_diff("2022-01-01", "2022-01-10")) # 输出:9 print(date_utils.is_leap_year(2024)) # 输出:True print(date_utils.get_weekday("2022-01-01")) # 输出:Saturday

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值