Python每日一练(牛客网新题库)——第10天:从入门到实践四十招

文章目录

  • 1. 发送offer
  • 2. 派对名单
  • 3. 投递简历
  • 4. 排序与反转
  • 5. 数到20
  • 6. 如何让刷题更加高效呢?

前言

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

在这里插入图片描述

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

在这里插入图片描述

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

1. 发送offer

描述:某公司在面试结束后,创建了一个依次包含字符串 ‘Allen’ 和 ‘Tom’ 的列表offer_list,作为通过面试的名单。
请你依次对列表中的名字发送类似 ‘Allen, you have passed our interview and will soon become a member of our company.’ 的句子。
但由于Tom有了其他的选择,没有确认这个offer,HR选择了正好能够确认这个offer的Andy,所以请把列表offer_list中 ‘Tom’ 的名字换成 ‘Andy’ ,
再依次发送类似 ‘Andy, welcome to join us!’ 的句子。

输入描述:无

输出描述:按题目描述进行输出即可。
Allen, you have passed our interview and will soon become a member of our company.
Tom, you have passed our interview and will soon become a member of our company.
Allen, welcome to join us!
Andy, welcome to join us!

实现代码:

offer_list = ['Allen', 'Tom']

# out
for i in range(len(offer_list)):
    print('{}, you have passed our interview and will soon become a member of our company.'.format(offer_list[i]))

for str_i in offer_list:
    if str_i == 'Tom':
        print('Andy, welcome to join us!')
    else:
        print('{}, welcome to join us!'.format(str_i))

运行结果:

Allen, you have passed our interview and will soon become a member of our company.
Tom, you have passed our interview and will soon become a member of our company.
Allen, welcome to join us!
Andy, welcome to join us!

2. 派对名单

描述:为庆祝驼瑞驰在牛爱网找到合适的对象,所以驼瑞驰创建了一个依次包含字符串 ‘Niuniu’ 和 ‘Niu Ke Le’ 的列表guest_list,作为庆祝派对的邀请名单。
请你依次对列表中的名字发送类似’Niuniu, do you want to come to my celebration party?'的句子。
驼瑞驰的好朋友牛牛、GURR哥和LOLO姐也正好有空,所以请使用insert()方法把字符串’GURR’插入到列表guest_list的开头,
再使用insert()方法把字符串’Niumei’插入到字符串’Niu Ke Le’的前面,再使用append()方法把字符串’LOLO’插入到列表guest_list的最后,
再依次发送类似’Niuniu, thank you for coming to my celebration party!'的句子。

输入描述:无

输出描述:按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。
Niuniu, do you want to come to my celebration party?
Niu Ke Le, do you want to come to my celebration party?

GURR, thank you for coming to my celebration party!
Niuniu, thank you for coming to my celebration party!
Niumei, thank you for coming to my celebration party!
Niu Ke Le, thank you for coming to my celebration party!
LOLO, thank you for coming to my celebration party!

实现代码:

guest_list = ['Niuniu', 'Niu Ke Le']
for i in guest_list:
    print('%s, do you want to come to my celebration party?' % i)
print()
guest_list.insert(0, 'GURR')
guest_list.insert(2, 'Niumei')
guest_list.append('LOLO')
for j in guest_list:
    print('%s, thank you for coming to my celebration party!' % j)

运行结果:

Niuniu, do you want to come to my celebration party?
Niu Ke Le, do you want to come to my celebration party?

GURR, thank you for coming to my celebration party!
Niuniu, thank you for coming to my celebration party!
Niumei, thank you for coming to my celebration party!
Niu Ke Le, thank you for coming to my celebration party!
LOLO, thank you for coming to my celebration party!

3. 投递简历

描述:毕业季到了,牛牛为了找工作准备了自己简历,以及投递公司的列表company_list,其中包括了字符串 ‘Alibaba’, ‘Baidu’, ‘Tencent’, ‘MeiTuan’, ‘JD’ 作为他投递简历的目标公司。
请向列表中每个公司发送一条消息类似 ‘Hello Alibaba, here is my resume!’。
然而,遗憾的是Alibaba、Tencent、MeiTuan、JD都没有通过牛牛的简历审核,只有Baidu回复了他,邀请他去参加面试。因此你需要:
使用 del() 函数删除列表company_list中的字符串 ‘Alibaba’.
使用 pop() 函数依次删除列表company_list中的字符串’JD’,‘MeiTuan’.
使用 remove() 函数删除列表company_list中的字符串’Tencent’.
最后向列表中的剩余的公司发送类似 ‘Baidu, thank you for passing my resume. I will attend the interview on time!’ 的消息。

输入描述:无

输出描述:按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。
Hello Alibaba, here is my resume!
Hello Baidu, here is my resume!
Hello Tencent, here is my resume!
Hello MeiTuan, here is my resume!
Hello JD, here is my resume!

Baidu, thank you for passing my resume. I will attend the interview on time!

实现代码:

company_list = ['Alibaba', 'Baidu', 'Tencent', 'MeiTuan', 'JD']
for i in company_list:
    print('Hello %s, here is my resume!' % i)
del company_list[0]
company_list.pop()
company_list.pop()
company_list.remove('Tencent')
print()
for j in company_list:
    print('%s, thank you for passing my resume. I will attend the interview on time!' % j)

运行结果:

Hello Alibaba, here is my resume!
Hello Baidu, here is my resume!
Hello Tencent, here is my resume!
Hello MeiTuan, here is my resume!
Hello JD, here is my resume!

Baidu, thank you for passing my resume. I will attend the interview on time!

4. 排序与反转

描述:创建一个依次包含字符串’P’、‘y’、‘t’、‘h’、‘o’和’n’的列表my_list后,
先使用print()语句一行打印字符串’Here is the original list:’,再直接使用print()语句把刚刚创建的列表my_list整个打印出来,

输出一个换行,再使用print()语句一行打印字符串’The result of a temporary reverse order:’,
再使用print()语句把使用sorted()函数对列表my_list进行临时降序排序的结果整个打印出来;

输出一个换行,再使用print()语句一行打印字符串’Here is the original list again:’,
再使用print()语句把原来的列表my_list整个打印出来,确认没有改变原来的列表my_list;

对列表my_list调用sort()方法,使列表my_list中的字符串以降序排序,
输出一个换行,再使用print()语句一行打印字符串’The list was changed to:’,
再使用print()语句把修改后的列表my_list整个打印出来,确认列表my_list的字符串以降序排序;

对列表my_list调用reverse()方法,使列表my_list中的字符串的位置前后翻转,
输出一个换行,再使用print()语句一行打印字符串’The list was changed to:’,
再使用print()语句把修改后的列表my_list整个打印出来,确认列表my_list的字符串的位置前后翻转了。

输入描述:无

输出描述:按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。
Here is the original list:
[‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]

The result of a temporary reverse order:
[‘y’, ‘t’, ‘o’, ‘n’, ‘h’, ‘P’]

Here is the original list again:
[‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]

The list was changed to:
[‘y’, ‘t’, ‘o’, ‘n’, ‘h’, ‘P’]

The list was changed to:
[‘P’, ‘h’, ‘n’, ‘o’, ‘t’, ‘y’]

代码实现:

def func():
    my_list = ['P', 'y', 't', 'h', 'o', 'n']
    print('Here is the original list:')
    print(my_list)

    print('')  # 输出换行
    print('The result of a temporary reverse order:')
    print(sorted(my_list, reverse=True))  # 输出的同时进行正序排序,同时在反转一下,也就是反顺序排序(原列表并没有改变)

    print('')
    print('Here is the original list again:')
    print(my_list)

    print('')
    my_list.sort(reverse=True)  # 降序排序,同时反转,也就是升序排序(这个是整个列表都改变了)
    print('The list was changed to:')
    print(my_list)

    print('')
    my_list.reverse()
    print('The list was changed to:')
    print(my_list)

func()

运行结果:

Here is the original list:
['P', 'y', 't', 'h', 'o', 'n']

The result of a temporary reverse order:
['y', 't', 'o', 'n', 'h', 'P']

Here is the original list again:
['P', 'y', 't', 'h', 'o', 'n']

The list was changed to:
['y', 't', 'o', 'n', 'h', 'P']

The list was changed to:
['P', 'h', 'n', 'o', 't', 'y']

5. 数到20

描述:使用一个 for 循环 或 while 循环 打印[1, 20]中的所有整数(一行一个数字)。

输入描述:无

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

实现代码:

for i in range(1, 21):
    print(i)

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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

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

在这里插入图片描述

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

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