2025年Python編程項目實戰(zhàn)沖刺試卷_第1頁
2025年Python編程項目實戰(zhàn)沖刺試卷_第2頁
2025年Python編程項目實戰(zhàn)沖刺試卷_第3頁
2025年Python編程項目實戰(zhàn)沖刺試卷_第4頁
2025年Python編程項目實戰(zhàn)沖刺試卷_第5頁
已閱讀5頁,還剩6頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

2025年Python編程項目實戰(zhàn)沖刺試卷考試時間:______分鐘總分:______分姓名:______第一題請編寫一個Python函數(shù)`process_student_data`,該函數(shù)接收一個包含學生信息的列表,其中每個學生信息是一個字典,包含`'name'`(姓名,字符串)、`'scores'`(成績,字典,鍵為科目名稱,值為分數(shù))和`'id'`(學號,字符串)三個鍵。函數(shù)需要完成以下處理:1.計算每個學生的總成績和平均成績,并將這兩個值添加到學生的字典中。2.過濾出平均成績大于等于60分的學生。3.返回過濾后的學生列表,并按平均成績從高到低排序。假設輸入數(shù)據(jù)如下:```pythonstudents=[{'id':'S001','name':'Alice','scores':{'Math':85,'English':92,'Science':78}},{'id':'S002','name':'Bob','scores':{'Math':59,'English':67,'Science':70}},{'id':'S003','name':'Charlie','scores':{'Math':90,'English':88,'Science':95}},{'id':'S004','name':'David','scores':{'Math':45,'English':52,'Science':60}},{'id':'S005','name':'Eve','scores':{'Math':72,'English':68,'Science':75}}]```請實現(xiàn)該函數(shù)。第二題請編寫Python代碼,實現(xiàn)一個簡單的命令行圖書管理系統(tǒng)。該系統(tǒng)需要支持以下功能:1.`add_book(title,author,year)`:添加一本新書,書信息包括標題、作者和出版年份。書信息應存儲在一個列表中,每個元素是一個包含上述三個鍵的字典。確保不添加重復的書(根據(jù)標題和作者判斷)。2.`list_books()`:列出所有圖書信息。3.`search_books(query)`:根據(jù)給定的查詢字符串`query`,搜索標題或作者中包含該字符串的圖書,并顯示結果。4.`remove_book(title,author)`:根據(jù)標題和作者刪除一本圖書。請在代碼中實現(xiàn)上述功能,并添加一個簡單的文本菜單,允許用戶輸入命令(如`add`,`list`,`search`,`remove`,`exit`)來執(zhí)行相應操作。為簡化,假設用戶輸入的命令和參數(shù)都是合法的。第三題請編寫一個Python腳本,實現(xiàn)以下功能:1.從當前目錄下名為`data.csv`的文件中讀取學生成績數(shù)據(jù)(假設文件格式為逗號分隔,包含姓名、數(shù)學、英語、科學四列)。2.計算每個學生的總分和平均分,并將這兩列添加到數(shù)據(jù)中。3.找出數(shù)學成績最高的學生和英語成績最低的學生,打印出他們的姓名和對應科目成績。4.將處理后的數(shù)據(jù)保存到一個新的文件`processed_data.csv`中。假設`data.csv`文件內容如下(無標題行):```Alice,85,92,78Bob,59,67,70Charlie,90,88,95David,45,52,60Eve,72,68,75```第四題請編寫一個Python函數(shù)`find_prime_pairs(n)`,該函數(shù)接收一個正整數(shù)`n`作為參數(shù)。函數(shù)需要找出所有小于或等于`n`的連續(xù)質數(shù)對(即兩個相差為2的質數(shù),例如(3,5),(5,7),(11,13))。函數(shù)應返回一個列表,其中包含所有找到的質數(shù)對,每個質數(shù)對本身也是一個元組。例如,如果`n=20`,函數(shù)應返回`[(3,5),(5,7),(11,13)]`。請實現(xiàn)該函數(shù),注意考慮效率。第五題請編寫Python代碼,實現(xiàn)一個生成器函數(shù)`countdown(start,end)`)。該函數(shù)接收兩個正整數(shù)參數(shù)`start`和`end`,其中`start`大于或等于`end`。該生成器從`start`開始,遞減計數(shù),每次yield當前的計數(shù)值,直到計數(shù)到`end`(包含`end`)停止。例如,`foriincountdown(5,1):print(i)`應該輸出`54321`。請實現(xiàn)該生成器函數(shù)。試卷答案第一題解析思路:1.遍歷輸入的學生列表。2.對于每個學生字典,計算其`scores`字典中所有分數(shù)的總和,并計算平均分。3.將計算出的總成績和平均成績添加到該學生字典中。4.使用列表推導式或過濾器函數(shù),根據(jù)平均成績是否大于等于60,篩選出符合條件的學生。5.使用`sorted()`函數(shù)對過濾后的學生列表進行排序,排序關鍵字為學生字典中的`'average_score'`(假設已添加)。6.返回排序后的列表。第一題答案:```pythondefprocess_student_data(students):forstudentinstudents:scores=student['scores']total=sum(scores.values())average=total/len(scores)student['total_score']=totalstudent['average_score']=averagefiltered_sorted_students=sorted([sforsinstudentsifs['average_score']>=60],key=lambdax:x['average_score'],reverse=True)returnfiltered_sorted_students```第二題解析思路:1.定義一個全局列表`books`用于存儲圖書信息。2.實現(xiàn)`add_book`函數(shù):檢查是否已存在相同標題和作者的圖書,如果不存在,則創(chuàng)建新字典添加到`books`列表。3.實現(xiàn)`list_books`函數(shù):直接返回`books`列表。4.實現(xiàn)`search_books`函數(shù):遍歷`books`列表,檢查每本圖書的標題或作者是否包含查詢字符串`query`,如果包含則添加到結果列表中,最后返回結果列表。5.實現(xiàn)`remove_book`函數(shù):遍歷`books`列表,找到匹配標題和作者的圖書,使用`remove()`方法從列表中移除。6.編寫主程序,使用無限循環(huán)和`input()`獲取用戶命令,根據(jù)命令調用相應的函數(shù)。使用`break`語句在輸入`exit`時退出循環(huán)。第二題答案:```pythonbooks=[]defadd_book(title,author,year):globalbooksforbookinbooks:ifbook['title']==titleandbook['author']==author:return#Bookalreadyexists,donothingbooks.append({'title':title,'author':author,'year':year})deflist_books():globalbooksforbookinbooks:print(f"Title:{book['title']},Author:{book['author']},Year:{book['year']}")defsearch_books(query):globalbooksresults=[]forbookinbooks:ifquery.lower()inbook['title'].lower()orquery.lower()inbook['author'].lower():results.append(book)returnresultsdefremove_book(title,author):globalbooksbooks=[bookforbookinbooksifnot(book['title']==titleandbook['author']==author)]whileTrue:command=input("Entercommand(addlistsearchremoveexit):")ifcommand=='add':title=input("Entertitle:")author=input("Enterauthor:")year=int(input("Enteryear:"))add_book(title,author,year)elifcommand=='list':list_books()elifcommand=='search':query=input("Entersearchquery:")results=search_books(query)forbookinresults:print(f"Found:Title:{book['title']},Author:{book['author']},Year:{book['year']}")elifcommand=='remove':title=input("Entertitletoremove:")author=input("Enterauthortoremove:")remove_book(title,author)elifcommand=='exit':breakelse:print("Invalidcommand.")```第三題解析思路:1.使用`csv.reader`讀取`data.csv`文件,跳過標題行(如果存在)。2.遍歷每一行數(shù)據(jù),將姓名、數(shù)學、英語分數(shù)轉換為適當類型(分數(shù)為整數(shù)),并存儲在列表或字典中。3.遍歷處理后的數(shù)據(jù),對于每個學生,計算總分和平均分。4.在遍歷過程中,同時記錄數(shù)學成績最高和英語成績最低的學生信息。5.打印出數(shù)學成績最高和英語成績最低的學生姓名及對應科目成績。6.使用`csv.writer`將處理后的數(shù)據(jù)(包含總分和平均分)寫入新的文件`processed_data.csv`。第三題答案:```pythonimportcsvdefprocess_csv():students=[]withopen('data.csv','r',newline='')asfile:reader=csv.reader(file)next(reader)#Skipheaderrowifpresentforrowinreader:name,math_str,english_str,science_str=rowmath=int(math_str)english=int(english_str)science=int(science_str)total=math+english+scienceaverage=total/3students.append({'name':name,'math':math,'english':english,'science':science,'total':total,'average':average})#Findstudentswithmaxmathandminenglishmax_math_student=max(students,key=lambdax:x['math'],default=None)min_english_student=min(students,key=lambdax:x['english'],default=None)ifmax_math_student:print(f"Mathhighest:{max_math_student['name']}-Math:{max_math_student['math']},English:{max_math_student['english']},Science:{max_math_student['science']}")ifmin_english_student:print(f"Englishlowest:{min_english_student['name']}-Math:{min_english_student['math']},English:{min_english_student['english']},Science:{min_english_student['science']}")#Writeprocesseddatatonewfilewithopen('processed_data.csv','w',newline='')asoutfile:writer=csv.writer(outfile)#Writeheaderwriter.writerow(['Name','Math','English','Science','Total','Average'])#Writedatarowsforstudentinstudents:writer.writerow([student['name'],student['math'],student['english'],student['science'],student['total'],student['average']])process_csv()```第四題解析思路:1.編寫一個輔助函數(shù)`is_prime(num)`,用于判斷一個數(shù)是否為質數(shù)。質數(shù)定義為大于1的自然數(shù),且除了1和其本身外沒有其他約數(shù)。判斷方法是從2到`sqrt(num)`(包含)檢查是否有約數(shù)。2.實現(xiàn)`find_prime_pairs(n)`函數(shù):*初始化一個空列表`result`用于存儲找到的質數(shù)對。*使用一個循環(huán)從3開始(因為小于3的連續(xù)質數(shù)對不存在),步長為2,遍歷到`n`。*在每次循環(huán)中,使用`is_prime()`檢查當前數(shù)`i`和`i+2`是否都是質數(shù)。*如果兩者都是質數(shù),則將`(i,i+2)`元組添加到`result`列表中。*循環(huán)結束后,返回`result`列表。第四題答案:```pythonimportmathdefis_prime(num

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論