【无标题】

1.采用os模块及os.path模块,完成输出一个指定路劲下所有的文件
当碰见文件时打印文件名称,当碰见目录时,则进入目录
使用递归完成

import os

def traverse_files(path):
    for item in os.listdir(path):
        item_path = os.path.join(path, item)
        if os.path.isfile(item_path):
            print(item)
        elif os.path.isdir(item_path):
            traverse_files(item_path)

# 指定路径
path = "/your/path"
traverse_files(path)


2.用户输入一个字符串,统计字符串中各字符出现的次数,并将结果写入本地文件

def count_characters_and_write_to_file(s):
    char_count = {}
    for char in s:
        if char in char_count:
            char_count[char] += 1
        else:
            char_count[char] = 1
    with open("result.txt", "w") as f:
        for char, count in char_count.items():
            f.write(f"{char}: {count}\n")

# 用户输入字符串
string = input("请输入字符串: ")
count_characters_and_write_to_file(string)


3.给定一个包含n+1个整数的数组nums,其数字在1到n之间(包含1和n),可知至少存在一个重复的整数 假设只有一个重复的整数,请找出这个重复的数,如[1,2,3,3,4],输出3

def find_duplicate(nums):
    for i in range(len(nums)):
        while nums[i]!= i + 1:
            if nums[nums[i] - 1] == nums[i]:
                return nums[i]
            nums[nums[i]], nums[i] = nums[i], nums[nums[i]]
    return None

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


4.编写控制台登录系统,假设已加密的账号密码存储在本地文件中,文件名为userinfo.text,用户输入账户名及密码,并验证登录,提示是否登录成功

import hashlib

# 假设已加密的账号密码,实际中应从文件读取
encrypted_account = "encrypted_account"
encrypted_password = "encrypted_password"

def login():
    account = input("请输入账号: ")
    password = input("请输入密码: ")

    # 简单模拟加密
    hashed_account = hashlib.sha256(account.encode()).hexdigest()
    hashed_password = hashlib.sha256(password.encode()).hexdigest()

    if hashed_account == encrypted_account and hashed_password == encrypted_password:
        print("登录成功")
    else:
        print("登录失败")

login()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值