PAT 1035 Password python解法

本文介绍了一个编程挑战,旨在解决密码混淆问题,通过将容易混淆的字符如'1', '0', 'l', 'O'替换为'@', '%', 'L', 'o',以增强密码的可读性和区分度。文章提供了详细的解题思路和Python实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1035 Password (20 分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.

Input Specification:
Each input file contains one test case. Each case contains a positive integer N (≤1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.

Output Specification:
For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified where N is the total number of accounts. However, if N is one, you must print There is 1 account and no account is modified instead.

Sample Input 1:
3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa
Sample Output 1:
2
Team000002 RLsp%dfa
Team000001 R@spodfa
Sample Input 2:
1
team110 abcdefg332
Sample Output 2:
There is 1 account and no account is modified
Sample Input 3:
2
team110 abcdefg222
team220 abcdefg333
Sample Output 3:
There are 2 accounts and no account is modified

题意:简单来说,就是替换字符,将[‘0’, ‘1’, ‘l’, ‘O’]分别替换成 [’%’, ‘@’,‘L’, ‘o’],需要注意的点是:1.只输出发生替换行为的字符串。2.如果没有发生任何替换行为,需要输出一句提示语,这句提示语要根据输出的字符串个数进行单复数的变动。

解题思路:
1.使用两个列表来存放需要替换的字符old = [‘0’, ‘1’, ‘l’, ‘O’],new = [’%’, ‘@’,‘L’, ‘o’]
2.对每个输入的字符串首先进行分割,注意到其实空格之前的字符串(也就是ID)是不会发生变化的,我们只需要处理后面的字符串(也就是密码)。
3.扫描后面的密码字符串,定义一个空字符串s,如果该字符在列表old中,将其在列表new中对应的字符添加到字符串s中,否则将原字符添加到s中。
4.定义一个空列表out,如果发生了替换行为(用一个flag来记录),则将替换之后的字符串s和原来的ID一起拼成一个新的字符串添加到out中。
5.如果out的长度不为0,则输出out的长度,并输出out里面的字符串。如果out的长度为0,则判断输入的n是否为1,为1输出(‘There is 1 account and no account is modified’),否则输出(‘There are %d accounts and no account is modified’%n)

old = ['0', '1', 'l', 'O']
new = ['%', '@','L', 'o']
n = int(input())
out = []
for i in range(n):
    l = input().split()
    s = ''
    flag = False
    for j in l[1]:
        if j in old:
            flag = True
            s += new[old.index(j)]
        else:
            s += j
    if flag:
        out.append(l[0]+ ' ' +s)
if len(out):
    print(len(out))
    print('\n'.join(out))
else:
    if n == 1:
        print('There is 1 account and no account is modified')
    else:
        print('There are %d accounts and no account is modified'%n)

<think>好的,用户想查找关于PAT题目1035Python相关内容。首先,我需要确认用户提到的PAT题目的具体内容。根据引用[3],PAT 1035是关于插入排序与归并排序的题目。用户需要根据原始序列和中间序列判断使用的是哪种排序算法,并输出下一步的结果。 接下来,我需要分析用户的查询需求。用户可能希望得到解决这个问题的Python代码示例,以及相关的思路解释。此外,用户可能还需要了解如何区分插入排序和归并排序的中间步骤,这是该题的关键点。 根据引用中的信息,引用[3]提到了输入格式:第一行是N,接着是原始序列和中间序列。题目要求判断是插入还是归并排序,并输出下一步结果。因此,我需要确保提供的代码能正确处理这些输入,并实现相应的逻辑。 首先,区分插入排序和归并排序的方法。插入排序的特点是前k个元素有序,后面与原序列相同。而归并排序是分组排序,每次归并的子数组长度为2的幂次。因此,代码需要遍历中间序列,找到第一个不满足递增的位置,然后检查后面的元素是否与原序列一致。如果符合,则是插入排序,否则是归并排序。 然后,对于插入排序,下一步需要将下一个元素插入到正确位置。对于归并排序,需要确定当前的归并子数组长度,并执行下一轮归并。 需要处理可能的边界情况,例如当N=1时的处理,或者归并排序中子数组长度超过N的情况。代码需要确保正确模拟排序过程,并找到正确的中间步骤。 此外,用户可能希望代码有注释解释关键步骤,以便理解。同时,可以参考引用[1]和引用[2]中的代码结构,保持一致的风格,例如使用输入处理和循环结构。 最后,生成相关问题,帮助用户进一步巩固相关知识,例如插入排序与归并排序的区别、如何实现归并排序的迭代步骤等。 总结来说,需要提供一个结构清晰、注释详细的Python代码,解释解题思路,并生成相关问题,确保用户能够理解并应用所学内容。</think>### PAT 1035 插入与归并排序的Python实现 #### 题目分析 题目要求根据原始序列和某排序算法的中间序列,判断是插入排序还是归并排序,并输出下一步排序结果[^3]。 --- #### 解题思路 1. **区分插入排序与归并排序**: - **插入排序**:前$k$个元素有序,后续元素与原序列一致。 - **归并排序**:通过分组排序(子数组长度按$2^k$增长)逐步合并。 2. **算法步骤**: - 遍历中间序列,找到第一个无序的位置。 - 检查后续元素是否与原序列一致,确定是否为插入排序。 - 若为归并排序,计算当前归并的子数组长度并执行下一步归并。 --- #### Python代码实现 ```python def is_insertion_sort(original, temp): k = 0 # 找到第一个无序的位置 while k < len(temp)-1 and temp[k] <= temp[k+1]: k += 1 # 检查后续元素是否与原序列一致 if temp[k+1:] == original[k+1:]: return True, k+1 # 返回插入排序的下一个处理位置 return False, 0 def next_merge_step(arr, step): n = len(arr) res = [] i = 0 # 按当前步长分组并排序 while i < n: # 每组长度为 2*step,但需处理剩余元素 end = min(i + 2*step, n) mid = min(i + step, end) res.extend(sorted(arr[i:end])) i = end return res def main(): n = int(input()) original = list(map(int, input().split())) temp = list(map(int, input().split())) # 判断是否为插入排序 is_insert, pos = is_insertion_sort(original.copy(), temp.copy()) if is_insert: # 执行下一步插入排序 next_arr = temp[:pos+1] next_arr.sort() next_arr += temp[pos+1:] print("Insertion Sort") print(" ".join(map(str, next_arr))) else: # 寻找归并排序的当前步长 step = 1 while True: current = original.copy() step *= 2 # 模拟归并过程 i = 0 while i < n: end = min(i + step, n) current[i:end] = sorted(current[i:end]) i = end if current == temp: next_step = next_merge_step(current, step) print("Merge Sort") print(" ".join(map(str, next_step))) break step = step // 2 # 回退到实际步长 if __name__ == "__main__": main() ``` --- #### 代码说明 1. **插入排序判断**:通过比较中间序列后半部分是否与原序列一致,快速确定排序类型[^3]。 2. **归并排序模拟**:通过逐步增加归并子数组长度($2^k$)并验证中间结果,确定当前排序阶段。 3. **边界处理**:对剩余元素的分组排序使用`sorted()`简化实现,确保逻辑清晰。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

D_ry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值