Python每日一练(牛客新题库)——第17天:综合练习

文章目录

  • 1. 披萨店的订单
  • 2. 售罄的培根披萨
  • 3. 梦想的大学
  • 4. 拯救被污染的字符串
  • 5. 如何让刷题更加高效呢?

前言

最近很多学了基础的小伙伴问我该怎么提升编程水平?学了基础该上哪刷题?明明学了很多,做项目却不知道怎么上手,其实这就是练得太少,只注重了学,却忽视了刷题,只有不断练习才能提高和巩固编程思维和能力!

在这里插入图片描述

刚好看到牛客网最近出了Python的新题库于是体验了一番感觉还不错

在这里插入图片描述

链接地址:牛客网 | Python从入门到实践四十招,废话少说速度上号,或者跟着下文一起刷题!!!

1. 披萨店的订单

描述
创建一个依次包含字符串’chichen’、’ bacon’和’durian’的列表 pizza_orders,再创建一个名为 finished_pizza 的空列表,
使用 while 循环判断列表 pizza_orders 里面是否还有元素,如果有则使用pop()方法删掉将列表 pizza_orders 的最后一个元素,并把刚刚删掉的元素存到一个名为pizza的变量,假设pizza的值为’bacon’,请使用print()语句一行打印类似字符串’I made your bacon pizza!'的语句,并使用append()语句将pizza添加到列表 finished_pizza 的末尾,然后本次循环结束,再次进入 while 循环中的条件测试。
在 while 循环结束后,再使用print()语句把列表 finished_pizza 整个打印出来。

输入描述:无

输出描述:按题目描述进行输出即可。
I made your durian pizza!
I made your bacon pizza!
I made your chichen pizza!
[‘durian’, ‘bacon’, ‘chichen’]

实现代码:

pizza_orders = ['chichen','bacon','durian']
finished_pizza = []
while True:
    if len(pizza_orders) == 0:
        break
    else:    
        pizza = pizza_orders.pop(-1)
        print('I made your {} pizza!'.format(pizza))
        finished_pizza.append(pizza)    
print(finished_pizza)

运行结果:

I made your durian pizza!
I made your bacon pizza!
I made your chichen pizza!
['durian', 'bacon', 'chichen']

2. 售罄的培根披萨

描述
创建一个依次包含字符串’bacon’、‘durian’、‘bacon’、‘bacon’、‘chicken’和’durian’的列表pizza_inventory,
使用 while循环 判断字符串’bacon’是否存在于列表pizza_inventory中,
如果存在,则使用remove()方法删掉列表pizza_inventory中的一个字符串’bacon’,并使用print()语句一行输出字符串’A bacon pizza was deleted from list.’,
然后本次循环结束,再次进入 while 循环中的条件测试。
在 while 循环结束后,如果if语句判断字符串’bacon’确实不在列表pizza_inventory中,请使用print()语句一行输出字符串’There is really no bacon in pizza_inventory!’。

输入描述:保证每一行的输入只有数字或字符串’quit’,且保证数字合法,范围在[1, 20]。

输出描述:按题目描述进行输出即可。

实现代码:

pizz_inventory = ['bacon', 'durian', 'bacon', 'bacon', 'chicken', 'durian']
while 'bacon' in pizz_inventory:
    pizz_inventory.remove('bacon')
    print('A pastrami order was deleted from list.')
if 'bacon' not in pizz_inventory:
    print('There is really no pastrami in sandwich_orders!')

运行结果:

A pastrami order was deleted from list.
A pastrami order was deleted from list.
A pastrami order was deleted from list.
There is really no pastrami in sandwich_orders!

3. 梦想的大学

描述
创建一个名为survey_dict的空字典,
请编写一个死循环,
每次循环开始先使用print()语句一行输出字符串’If you have the chance, which university do you want to go to most?’,
再使用print()语句一行输出字符串’What is your name?’,再将读取到的字符串存储在变量name中,
再使用print()语句一行输出字符串’Which university do you want to go to most?’,再将读取到的字符串存储在变量university中,
再把键-值对name: university存储在字典survey_dict中,
再使用print()语句一行输出字符串"Is there anyone who hasn’t been investigated yet?",
如果输入的字符串为’No’,则使用 break 语句退出循环,否则本次循环结束,再次进入 while 循环中的条件测试。
在 while 循环结束后,使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典survey_dict的所有键的列表,
对于每一个遍历到的被调查者的名字,使用print()语句一行输出类似字符串"I’am Tom. I’d like to go to Fudan University if I have the chance!"的语句。

输入描述:无

输出描述:按题目描述进行输出即可。

实现代码:

survey_dict = {}
while True:
    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?")
    a = input()
    if a == "No":
        break
for a, b in sorted(survey_dict.items()):
    print(f"I'am {a}. I'd like to go to {b} if I have the chance!")

运行结果:

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!

4. 拯救被污染的字符串

描述
牛牛有一份字符串my_str = ‘I am$ N i u n i u iuniu iuniu, an$d I a m s t u am stu amstudying in N o w Now Nowcoder!’ 被污染了,其中出现了很多奇怪的 符 号 。 现 请 你 使 用 s p l i t 函 数 将 这 份 字 符 串 从 符 号 符号。 现请你使用split函数将这份字符串从符号 使split处分割成众多字符列表,记录在my_list中,并使用print函数直接打印my_list中的结果。
然后再使用join函数将my_list中的分段字符串重新连接成一个新的字符串,并使用print函数输出。

输入描述:保证每一行的输入只有浮点数或字符串’quit’,且保证数字合法,范围在[0, 3]。

输出描述:按题目描述进行输出即可。
[‘I am’, ’ N’, ‘iuniu’, ‘, an’, 'd I ', ‘am stu’, 'dying in ', ‘Now’, ‘coder!’]
I am Niuniu, and I am studying in Nowcoder!

代码实现:

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

运行结果:

['I am', ' N', 'iuniu', ', an', 'd I ', 'am stu', 'dying in ', 'Now', 'coder!']
I am Niuniu, and I am studying in Nowcoder!

5. 如何让刷题更加高效呢?

嫌博主更新慢的小伙伴牛客网上号自行刷题

在这里插入图片描述

链接地址:牛客网 | Python从入门到实践四十招,废话少说速度上号!!!

你可能感兴趣的:(Python每日一练,python,c++,开发语言)