From eed153a110048e5f8d7ac1b0476414b178953958 Mon Sep 17 00:00:00 2001 From: zhucgcc <709917984@qq.com> Date: Thu, 26 Feb 2026 16:38:55 +0800 Subject: [PATCH] Add test.py for palindrome calculation This script checks if a number is palindrome and finds palindromes in a given range. Co-Authored-By: Claude Opus 4.6 --- test.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..653eb83 --- /dev/null +++ b/test.py @@ -0,0 +1,69 @@ +def is_palindrome(num): + """ + 判断一个整数是否是回文数 + + 参数: + num (int): 要检查的整数 + + 返回: + bool: 如果是回文数返回True,否则返回False + """ + if num < 0: + return False + str_num = str(num) + return str_num == str_num[::-1] + + +def find_palindromes_in_range(start, end): + """ + 查找指定范围内的所有回文数 + + 参数: + start (int): 起始值(包含) + end (int): 结束值(包含) + + 返回: + list: 范围内的回文数列表 + """ + palindromes = [] + for num in range(start, end + 1): + if is_palindrome(num): + palindromes.append(num) + return palindromes + + +def main(): + """主函数,演示回文数计算""" + print("回文数计算示例") + print("=" * 30) + + # 测试一些数字 + test_numbers = [121, 123, 12321, -121, 0, 1, 10, 11, 1221] + + for num in test_numbers: + result = is_palindrome(num) + print(f"{num} 是回文数吗?{result}") + + print("\n查找 1 到 200 之间的回文数:") + palindromes = find_palindromes_in_range(1, 200) + print(f"共找到 {len(palindromes)} 个回文数:") + print(palindromes) + + # 用户输入示例 + print("\n" + "=" * 30) + try: + user_input = input("请输入一个整数检查是否是回文数 (按Enter跳过): ") + if user_input.strip(): + num = int(user_input) + if is_palindrome(num): + print(f"{num} 是回文数!") + else: + print(f"{num} 不是回文数。") + except ValueError: + print("输入无效,请输入一个整数。") + except EOFError: + pass # 脚本运行而非交互时可能发生 + + +if __name__ == "__main__": + main() \ No newline at end of file