【Python百日刷题计划】Day7~牛客题库高效练习

本次练习的题目大部分来自牛客网的Python基础题库,想要学习Python和巩固基础的可以现在打开牛客来一起学习吧。

📔编程练习直达链接👉由此进入免费的刷题练习网站

📚前言

编程刷题是必不可少的,在刷题过程中你会认识到自己学习的不足之处,发现日常编程某一知识点的不足。这样及时发现和弥补短板,但是网络题库这么多到底哪些好呢?我觉得牛客就挺不错的,不仅可以针对训练,还有提供牛客手机app可以让我们充分利用碎片时间学习(如果有在线编程的需要,还是得到电脑牛客网网站上进行)。这样一个网站足够满足我们的刷题需求了。话不多说下面我分享一些我在牛客网刷的题,一起来看看吧!

【Python百日刷题计划】Day7~牛客题库高效练习

 

文章目录

📚前言

📚结语


📰题目一:毕业生就业调查

📓题目要求

【Python百日刷题计划】Day7~牛客题库高效练习

📓我的分析

本题难度不大,我的思路是将survey_list中每一个元素通过字典的get()方法,判断其是否在字典result_dict中。get方法具体使用如下:

描述

Python 字典 get() 函数返回指定键的值。

语法

dict.get(key[, value])

参数

  • key — 字典中要查找的键。
  • value — 可选,如果指定键的值不存在时,返回该默认值。

返回值

返回指定键的值,如果键不在字典中返回默认值 None 或者设置的默认值

📓我的答案

survey_list = ['Niumei','Niu Ke Le','GURR','LOLO']                                     
result_dict = {'Niumei': 'Nowcoder','GURR': 'HUAWEI'}                                  
for people in survey_list:                                                             
    if result_dict.get(people) == None:        #判断列表中的人是否在字典中                                        
        print('Hi, %s! Could you take part in our graduation survey?' % people)        
    else:                                                                              
        print('Hi, %s! Thank you for participating in our graduation survey!' % people)

输出结果

【Python百日刷题计划】Day7~牛客题库高效练习

📰题目二:梦想的大学

📓题目要求

【Python百日刷题计划】Day7~牛客题库高效练习

示例

输入:
Tom
Fudan University
Yes
Ben
Peking University
Yes
Andy
Tsinghua University
No

输出:
If you have the chance, which university do you want to go to most?
What is your name?
Which university do you want to go to most?
Is there anyone who hasn't been investigated yet?
If you have the chance, which university do you want to go to most?
What is your name?
Which university do you want to go to most?
Is there anyone who hasn't been investigated yet?
If you have the chance, which university do you want to go to most?
What is your name?
Which university do you want to go to most?
Is there anyone who hasn't been investigated yet?
I'am Andy. I'd like to go to Tsinghua University if I have the chance!
I'am Ben. I'd like to go to Peking University if I have the chance!
I'am Tom. I'd like to go to Fudan University if I have the chance!

📓我的分析

本题主要考察的是列表,字典,循环和内置函数sorted()的使用方法,跟着题目要求来应该可以很快得出答案,其中各种方法的具体使用方法如下

👉内置函数sorted()的使用方法

👉列表,字典的使用方法

📓我的答案

survey_dict = {}
while 1:
    print('If you have the chance, which university do you want to go to most?')
    print('What is your name?')
    name = input()
    print('Which university do you want to go to most?')
    university = input()
    survey_dict[name] = university
    print("Is there anyone who hasn't been investigated yet?")
    res = input()
    if res == 'No':
        break
for name in sorted(survey_dict.keys()):
    print("I'am %s. I'd like to go to %s if I have the chance!" % (name,survey_dict[name]))

其中我的代码最后一段:

 print("I'am %s. I'd like to go to %s if I have the chance!" % (name,survey_dict[name]))

换成以下代码也是很好的:

 print(f"I'am {name}. I'd like to go to {survey_dict[name]} if I have the chance!")

📰题目三: 拯救被污染的字符串

📓题目要求

【Python百日刷题计划】Day7~牛客题库高效练习

📓我的分析

本题题目知识点考察比较单一,主要是让我们复习一下字符串中join和split方法的使用,如果能够第一时间想到使用这些方法那么这题是很快可以写出,下面就来复习join和split方法的使用:

描述

Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

语法

str.join(sequence)

参数

  • sequence — 要连接的元素序列。

返回值

返回通过指定字符连接序列中元素后生成的新字符串。

👉 字符串中split方法的使用

📓我的答案

my_str = 'I am$ N$iuniu$, an$d I $am stu$dying in $Now$coder!'
my_list = my_str.split('$')
print(my_list)
new_str = ''.join(my_list)
print(new_str)

📰题目四: 游乐园的门票

📓题目要求

【Python百日刷题计划】Day7~牛客题库高效练习

 示例:

输入:
0.5
1.2
quit

输出:
Please tell me your height!
Enter 'quit' to end the program.
Your admission cost is 0 yuan.
Please tell me your height!
Enter 'quit' to end the program.
Your admission cost is 80 yuan.
Please tell me your height!
Enter 'quit' to end the program.

📓我的分析

本题主要考察了循环语句的使用方法和数据类型的强制转换方法,考点主要在”不同身高的人需购买不同的门票”,我们这里使用” if – elif – else”语句会相对简便

📓我的答案

while 1:
    print("Please tell me your height!\nEnter 'quit' to end the program.")
    read = input()    #用read变量接收我们读入的身高
    if read == 'quit':
        break
    if float(read) < 1.0:    
        print('Your admission cost is 0 yuan.')
    elif float(read) < 1.2:
        print('Your admission cost is 80 yuan.')
    else:
        print('Your admission cost is 150 yuan.')

📰题目五:栈和队列的实现

📓题目要求

【Python百日刷题计划】Day7~牛客题库高效练习

示例

输入:
5
4
3
2
1

输出:
[5, 4]
[5, 4]
[5]
[4]
[5, 3, 2, 1]
[4, 3, 2, 1]
1 2 3 5

📓我的分析

本题主要考察的是我们对栈和队列放值和取值的顺序的考察

👉栈(先进来的后出去):是一种线性数据结构,用先进后出或者是后进先出的方式存储数据,栈中数据的插入删除操作都是在栈顶端进行

👉队列(先进来的先出去):队列是一种只允许在一端进行插入操作,而在另一端进行删除操作的线性表

📓我的答案

  • lst1用来实现栈的列表,lst2用来实现队列的列表
  • lst_append()函数是用来向这两个列表中添加元素
  • lst_print()函数是用来打印这两个列表
lst1 = []
lst2 = []
def lst_append(n):
    for i in range(n):
        num = int(input())
        lst1.append(num)
        lst2.append(num)
def lst_print():
    print(lst1)
    print(lst2)

lst_append(2)
lst_print()
 
lst1.pop()
lst2.pop(0)
lst_print()
 
lst_append(3)
lst_print()

while len(lst1):
    print(lst1[-1], end=' ')
    lst1.pop()

📚结语

📙:别忘了刷题哟😊😊😊👉由此进入免费好用的刷题网站

📙:感谢各位能够看到这里💖💖💖:在鲁迅一篇未发表的文章中说过:“代码看懂了不是懂✨一定要自己实际操作哇✨这样才能更好的理解和吸收。”
最后来一句:一个人可以在任何他怀有无限热忱的事情上成功,让我们一起进步吧✨✨

【Python百日刷题计划】Day7~牛客题库高效练习

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年3月10日
下一篇 2023年3月10日

相关推荐