PTA三日十题
本文最后更新于 525 天前,其中的信息可能已经有所发展或是发生改变。

7-1 实现队列

实现队列类。队列类包含初始化方法、isEmtpy()方法、enqueue() 入队方法,dequeue() 出队方法、size() 方法。
不修改主程序。

if __name__ == '__main__':
    queue = Queue()
    queue.enqueue('red')
    queue.enqueue('green')
    queue.enqueue('blue')
    s = input()
    queue.enqueue(s)
    print(queue.size())
    print(queue.dequeue())
    print(queue.dequeue())
    print(queue.isEmpty())

输入格式:

输入一个字符串 yellow

输出格式:

第一行输出对应queue.size()的返回结果;
第二行输出对应queue.dequeue()的返回结果;
第三行输出对应queue.dequeue()的返回结果;
第四行输出对应queue.isEmpty()的返回结果。

输入样例:

在这里给出一组输入。例如:

yellow

输出样例:

在这里给出相应的输出。例如:

4
red
green
False
class Queue:
    def __init__(self):
        self.items = []
    def isEmpty(self):
        return self.items == []
    def enqueue(self, item):
        self.items.insert(0, item)
    def dequeue(self):
        return self.items.pop()
    def size(self):
        return len(self.items)
if __name__ == '__main__':
    queue = Queue()
    queue.enqueue('red')
    queue.enqueue('green')
    queue.enqueue('blue') 
    s = input()
    queue.enqueue(s) 
    print(queue.size())
    print(queue.dequeue())
    print(queue.dequeue())
    print(queue.isEmpty())

7-2 冒泡排序

编写冒泡排序函数。
不修改主程序。

if __name__ == '__main__':
    aList = [25,21,22,24,23,27]
    x = eval(input())
    aList.append(x)
    bList = bubbleSort(aList)
    print(bList)

输入格式:

输入列表中最后一个元素的值。

输出格式:

输出排序后的列表。

输入样例:

在这里给出一组输入。例如:

26

输出样例:

在这里给出相应的输出。例如:

[21, 22, 23, 24, 25, 26, 27]
def bubbleSort(aList):  
    for i in range(len(aList) - 1):  
        for j in range(len(aList) - 1 - i):       
            if aList[j] > aList[j + 1]:          
                aList[j], aList[j + 1] = aList[j + 1], aList[j]  
    return aList
if __name__ == '__main__':  
    aList = [25,21,22,24,23,27]  
    x = eval(input())   
    aList.append(x)  
    bList = bubbleSort(aList)    
    print(bList)

7-3 十进制转二进制

编写程序,将十进制数转换为对应的二进制数。

输入格式:

输入整数x, 如83

输出格式:

输出对应的二进制形式。1010011

输入样例:

在这里给出一组输入。例如:

83

输出样例:

在这里给出相应的输出。例如:

1010011
print(bin(int(input()))[2:])

7-4 加密算法

输入字符串,将字符串中的英文字母其后第三个位置上的字母。如字母A后的第三个位置为字母D;字母Z后的第三个位置是字母C.

输入格式:

BE OR NOT BE

输出格式:

EH RU QRW EH

输入样例:

在这里给出一组输入。例如:

BE OR NOT BE

输出样例:

在这里给出相应的输出。例如:

EH RU QRW EH
str = input()
for i in str:
    if ord("A") <= ord(i) <= ord("Z"):
        print(chr((ord(i) - ord("A") + 3)%26 + ord("A")), end = "")
    else:
        print(i, end = "")

7-5 字典反转

这是一个编程题模板。将字典反转。不修改主程序。程序框架如下:

def histogram(s):     d = dict()     for c in s:         if c not in d:             d[c]=1         else:             d[c]+=1     return d def invert(d):     # to do if __name__ == '__main__':     #h = histogram('brontosaurus')     s = input()     h = histogram(s)     print(h)     inverse = invert(h)     print(inverse) 输入格式:

输入一个字符串 parrot

输出格式:

第一行 字母及其出现的次数构成的字典;第二行 出现次数和字母构成的字典。{'p': 1, 'a': 1, 'r': 2, 'o': 1, 't': 1}{1: ['p', 'a', 'o', 't'], 2: ['r']}

输入样例:

在这里给出一组输入。例如:

parrot输出样例:

在这里给出相应的输出。例如:

{'p': 1, 'a': 1, 'r': 2, 'o': 1, 't': 1} {1: ['p', 'a', 'o', 't'], 2: ['r']}

def histogram(s): 
    d = dict()
    for c in s:
        if c not in d:
            d[c]=1
        else:
            d[c]+=1
    return d
def invert(d):  
    afterInvert = {}
    for key,value in d.items():
        if value not in afterInvert:
            afterInvert[value] = [key]
        else:
            afterInvert[value].append(key)
    return afterInvert
if __name__ == '__main__': 
    #h = histogram('brontosaurus')  
    s = input()  
    h = histogram(s)  
    print(h)    
    inverse = invert(h)    
    print(inverse)

7-6 运算符重载

这是一个编程题模板。

假设t1 是时间类的对象,对加号运算符进行重载,实现如下功能:
t2 = t1 + 60
t2 为时间类的对象。
不改动程序的结构,不改动主程序。

class Time:
    def __init__(self,h=0,m=0,s=0):
        self.hour=h
        self.minute =m
        self.second =s
    
    def __add__(self,seconds):
        # to do

    def increment(self,seconds):
        # to do
    
    def show_time(self):
        print("%02d:%02d:%02d" %(self.hour,self.minute,self.second))

if __name__== '__main__':
    hour,minute,second = eval(input())
    t = Time(hour,minute,second)
    t.show_time()
    t2 = t + 60
    t2.show_time()

输入格式:

15,35,0

输出格式:

15:35:00
15:36:00

输入样例:

在这里给出一组输入。例如:

15,35,0

输出样例:

在这里给出相应的输出。例如:

15:35:00
15:36:00
class Time:   
    def __init__(self,h=0,m=0,s=0):    
        self.hour=h      
        self.minute =m     
        self.second =s     
    def __add__(self,seconds):     
        return self.increment(seconds)   
    def increment(self,seconds):   
        self.second += seconds     
        if self.second >= 60:        
            self.second -= 60         
            self.minute += 1      
        if self.minute >= 60:           
            self.minute -= 60          
            self.hour += 1  
        return self
    def show_time(self):    
        print("%02d:%02d:%02d" %(self.hour,self.minute,self.second))
if __name__== '__main__':  
    hour,minute,second = eval(input())  
    t = Time(hour,minute,second) 
    t.show_time()   
    t2 = t + 60  
    t2.show_time()

7-7 找出列表中缺失的数

这是一个编程题模板。

给定一个由n-1个整数组成的未排序的数组序列,其元素都是1到n中的不同的整数。编写程序,寻找数组序列中缺失整数。

不修改程序结构,不改动主程序。

def get_num(aList):
    # to do


if __name__ == '__main__':
    n=eval(input())
    aList = [1,4,3,2,7,5]
    print(get_num(aList))

输入格式:

输入n

输出格式:

6

输入样例:

在这里给出一组输入。例如:

7

输出样例:

在这里给出相应的输出。例如:

6
def get_num(aList,n):
    tot = []
    for i in range(n+1):   
        tot.insert(0,0) 
    for i in range(len(aList)):
        tot[aList[i]] = tot[aList[i]] + 1  
    for i in range(len(tot)):   
        if tot[i] == 0 and i != 0:      
            return i  
    return -1 
if __name__ == '__main__':
    n=eval(input())
    ls=[1,4,3,2,7,5]
    print(get_num(ls,n))

7-8 列表基础

编写程序,逐个实现如下功能:定义空列表list1;向列表list1 新增n个元素,设n为5,1,2,3,4,5;修改列表list1中第二个(下标为2)元素,修改为7;判断列表中是否包含数字0;输出列表list1。例如:本题目要求读入n。

输入格式:

输入n。例如:5。

输出格式:

请在这里描述输出格式。例如:对每一组输入,在一行中输出A+B的值。

输入样例:

在这里给出一组输入。例如:

5输出样例:

在这里给出相应的输出。例如:

False [1, 2, 7, 4, 5]

list1 = []
n = int(input())
for i in range(1,n+1):
    list1.append(i)
list1[2] = 7
print(0 in list1)
print(list1)

7-9 字典基础

编写程序,逐句实现如下功能:
定义空字典 dict1;
输入学号,例如 '2020001',向字典dict1 中增加键值对 'studentid','2020001';
向字典dict1 中增加键值对
'name','zhang'
输出字典的长度;
判断'clazz'是否是字典dict1的键;
。例如:本题目要求读入学号,如'2020001'。

输入格式:

输入学号。例如:2020001。

输出格式:

输出字典的长度。输出是或否。

输入样例:

在这里给出一组输入。例如:

2020001

输出样例:

在这里给出相应的输出。例如:

2
False
dict1 = {}
dict1['studentid'] = input()
dict1['name'] = 'zhang'
print(len(dict1))
print('clazz' in dict1)

7-10 复制文件

复制文件。(这题好像有问题,下面的代码不是对的)

输入格式:

source.py

输出格式:

x=int(input())
if x%2==0:
print(x,’odd’)
else:
print(x,’oven’)

输入样例:

在这里给出一组输入。例如:

source.py

输出样例:

在这里给出相应的输出。例如:

x=int(input())
if x%2==0:
    print(x,’odd’)
else:
    print(x,’oven’)
source = input()
try:
    file_source = open(source,"r")#这句报FileNotFoundError
# except PermissionError:
#     print("PermissionError")
except FileNotFoundError:
    print("FileNotFoundError")
# except:
#     print("unknowError")
try:
    file_source = open("dest.py","w")#这句报
# except FileNotFoundError:
#     print("File not found")
# except PermissionError:
#     print("PermissionError")
except IOError:   
    print("IOError")
# except:   
#     print("unknowError")
# print(source)
# dest_file = "dest.py"

# file_source = open('../source.py',"r")
# file_dest = open(dest_file,"w")

# for line in file_source:
    # print(line)
    # file_dest.write(line)
    
# file_source.close()
# file_dest.close()
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇