程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python每日一練(牛客網新題庫)——第10天:從入門到實踐四十招

編輯:Python

文章目錄

  • 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從入門到實踐四十招,廢話少說速度上號!!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved