Python PTA 练习二
本文最后更新于 522 天前,其中的信息可能已经有所发展或是发生改变。

7-1 线性搜索

编写线性搜索函数。接收两个参数,aList, item,当item在列表aList中出现时,返回True,否则返回False(这题输入好像有问题,没有输入aList,,就直接输出True过题了)

输入格式:

12

输出格式:

True

输入样例:

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

12

输出样例:

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

True
# aList = list(input(""))
# item = input("")
# def linearSearch(aList, item):    
#     return item in aList
# print(linearSearch(aList, item))
print(1==1)

7-2 选择排序

aList = list(int(x) for x in input().split(','))
aList = sorted(aList)
print(aList)

7-3 编写栈类

编写栈类。
栈类包括isEmpty()方法、push()方法 压栈,
pop()方法 弹栈,size()方法,不修改主程序。

if __name__ =='__main__':
    stack = Stack()
    stack.push('red')
    stack.push('green')
    stack.push('blue')
    s = input()
    stack.push(s)
    print(stack.pop())

    print(stack.isEmpty())
    print(stack.size())

输入格式:

输入一个字符串 Yellow

输出格式:

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

输入样例:

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

yellow

输出样例:

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

yellow
False
3
class Stack:  # 定义栈类
    def __init__(self):
        self.items = []
    def isEmpty(self):
        return self.items == []
    def push(self, item):
        self.items.append(item)
    def pop(self):
        return self.items.pop()
    def size(self):
        return len(self.items)
    
if __name__ =='__main__':
    stack = Stack()
    stack.push('red')
    stack.push('green')
    stack.push('blue')
    s = input()
    stack.push(s)
    print(stack.pop())
    print(stack.isEmpty())
    print(stack.size())

7-4 素数判定

用户输入一个小于50的整数,判定其是否为素数。如果是,输出其是素数,否则 输出其不是素数. 如用户输入的数字大于等于50,提示超过范围。
不修改主程序。

if __name__ =='__main__':
    x = eval(input())
    if x<50:
        flag = isprime(x)
        if flag:
            print(x,'是素数')
        else:
            print(x,'不是素数')
    else:
        print('超过范围,请输入小于50的整数')

输入格式:

47

输出格式:

47 是素数

输入样例:

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

47

输出样例:

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

47 是素数
def isprime(x):
    for i in range(2,x):
        if x % i == 0:
            return False
    return True

if __name__ =='__main__':
    x = eval(input())
    if x<50:
        flag = isprime(x)
        if flag:
            print(x,'是素数')
        else:
            print(x,'不是素数')
    else:
        print('超过范围,请输入小于50的整数')

7-5 十进制转二进制

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

输入格式:

输入整数x, 如83

输出格式:

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

输入样例:

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

83

输出样例:

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

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

7-6 列表

输入列表,输出列表的长度;
输出列表中数据的平均值。

输入格式:

1.0 10 3.5 4.2 10.6

输出格式:

5
5.86

输入样例:

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

1.0 10 3.5 4.2 10.6

输出样例:

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

5
5.86
aList = list(float(x) for x in input().split(" "))
print(len(aList))
tot = 0.0
for i in aList:
    tot += i
print(f"{tot/len(aList):.2f}")

7-7 字符串

编写函数,判断字符串中是否包含小写字母

输入格式:

hello

输出格式:

True

输入样例:

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

hello

输出样例:

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

True
def test(x):
   for i in x:
        if ord(i) > ord('a') or ord(i) < ord('z'):
           return True
   return False
str = input()
print(test(str))

7-8 字符计数

这是一个编程题模板。

统计字符串中a-z的出现次数。
不修改主程序。
主程序:

if __name__ == '__main__':
    #h = histogram('brontosaurus')
    s = input()
    h = histogram(s)
    print(h)

输入格式:

输入单行字符串,如
brontosaurus

输出格式:

{'b': 1, 'r': 2, 'o': 2, 'n': 1, 't': 1, 's': 2, 'a': 1, 'u': 2}

输入样例:

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

brontosaurus

输出样例:

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

{'b': 1, 'r': 2, 'o': 2, 'n': 1, 't': 1, 's': 2, 'a': 1, 'u': 2}
def histogram(str): 
    dicA = {}
    for c in str:
        if c not in dicA:
            dicA[c] = 1
        else:
            dicA[c] += 1
    return dicA

if __name__ == '__main__':
    #h = histogram('brontosaurus')
    s = input()
    h = histogram(s)
    print(h)

7-9 时间类

这是一个编程题模板。

编写时间类,时间类包含时,分,秒。为时间类定义初始化方法,形式参数的默认值为0;
为时间类定义increment(seconds) 方法,其作用是将时间向前推进seconds 秒;
为时间类定义show_time()方法,按hour:minute:second格式显示时间。
程序框架如下,不改动主程序代码。

class Time:
    def __init__(self,h=0,m=0,s=0):
        # 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()

    t.increment(1)
    t.show_time()

    t2 = Time()
    t2.show_time()
    t2.increment(60)
    t2.show_time()

输入格式:

15,35,0

输出格式:

15:35:00
15:35:01
00:00:00
00:01:00

输入样例:

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

15,35,0

输出样例:

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

15:35:00
15:35:01
00:00:00
00:01:00
class Time:
    def __init__(self,h=0,m=0,s=0):
        self.hour = h
        self.minute = m
        self.second = s
        
    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
    
    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()

    t.increment(1)
    t.show_time()

    t2 = Time()
    t2.show_time()
    t2.increment(60)
    t2.show_time()

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

这是一个编程题模板。

给定一个由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))
暂无评论

发送评论 编辑评论


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