python第四次作业

  1. 位运算:计算 56 及 - 18 的所有位运算符结果,并使在注释中体现计算过程
python 复制代码
# 56的原码: 00111000  -18的源码:10010010 反码:11101101 补码:11101110
# & 与 
# 00111000 & 11101110 = 00101000
# 00111000 | 11101110 = 11111110
# 00111000 ^ 11101110 = 11010110
  1. 完成文件读取功能,任意读取某个文件内容时,请编写装饰器,实现写出文件时增加当前系统时间,并打印至控制台最后一行
python 复制代码
import time
class wirte_file2():
    def __init__(self,time):
        self.time = time
    def __call__(self,func):
        def wrapper(*args, **kwargs):
            with open("../test_log.txt", "a+") as f:
                f.write(f"time:{self.time}\n")
            func(*args, **kwargs)
        return wrapper

@wirte_file2(time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) )
def test3(str):
    with open("../test_log.txt", "a+") as f:
        f.write(str)

test3("a\n")
  1. 给定一个包含 n+1 个整数的数组 nums,其数字在 1 到 n 之间 (包含 1 和 n),可知至少存在一个重复的整数 假设只有一个重复的整数,请找出这个重复的数
python 复制代码
def find_duplicate(nums):
    nums.sort()
    for i in range(1,len(nums)):
        if nums[i] == nums[i-1]:
            return nums[i]
    return -1

nums1 = [1, 3, 4, 2,2]
print(find_duplicate(nums1))

运行结果:

  1. 完成登录系统,登录时数据使用序列化和反序列化.
python 复制代码
import pickle
import os

USER_FILE = "users.pkl"
# 初始化用户文件
if not os.path.exists(USER_FILE):
    with open(USER_FILE, 'wb') as f:
        pickle.dump({}, f)

def register(username, password):
    with open(USER_FILE, 'rb') as f:
        users = pickle.load(f)
    if username in users:
        return "用户名已存在"
    users[username] = password
    with open(USER_FILE, 'wb') as f:
        pickle.dump(users, f)
    return "注册成功"

def login(username, password):
    with open(USER_FILE, 'rb') as f:
        users = pickle.load(f)
    if username not in users:
        return "用户名不存在"
    return "登录成功" if users[username] == password else "密码错误"

register("user1", "123456")
login("user1", "123456") 

运行结果:

相关推荐
CoderIsArt7 小时前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
AI情绪识别开源10 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
lupai10 小时前
手机在网状态接口实测效果与质量评估
大数据·python·智能手机·api接口
「QT(C++)开发工程师」11 小时前
C++ auto 用法详解
开发语言·c++
OPEN-F11 小时前
C++STL教程:容器适配器与实用工具
开发语言·c++
yaoxin52112311 小时前
507. Java 反射 - 在 BeanFactory 中实现依赖注入
java·开发语言
OPEN-F11 小时前
C++模板教程:变参模板、折叠表达式与SFINAE
java·开发语言·c++
有点。11 小时前
C++二叉搜索树进阶
开发语言·c++
HugoStudio_SWAN11 小时前
【擦除重绘】C++ 控制台动画:弹跳 Logo DVD 屏保效果
开发语言·c++·学习·程序人生