Python3程序设计实验二
文章摘自:Python3程序设计实验|PengWill
排序
程序设计说明
不使用python中的提供的sort/sorted,自己实现3种排序方法,随机产生100000个[1,1000]之间的整数,各进行10次,对比三种方法的执行速度。
程序设计思路
按照排序算法首先定义好三个函数,之后用random模块随机产生一个list,通过shuffle在其内部进行打乱,同时调用函数执行排序。最后用time模块记录时间输出即可。
程序代码
import random
import time
def bubble_sort(lst):
for i in range(0,len(lst)):
for j in range(i):
if lst[j] > lst[j+1]:
lst[j],lst[j+1] = lst[j+1],lst[j]
return lst
def qsort(lst):
if len(lst) <= 1:
return lst
else:
temp1 = [i for i in lst[1:] if i < lst[0]]
temp2 = [i for i in lst[1:] if i >= lst[0]]
return qsort(temp1) + lst[:1] + qsort(temp2)
def select_sort(lst):
for i in range(0,len(lst)):
min = i
for j in range(i+1,len(lst)):
if lst[min] > lst[j]:
min = j
lst[min],lst[i] = lst[i], lst[min]
return lst
lst = list()
for i in range(1,10001):
lst.append(random.randrange(1,1001))
########################################
# Bubble_sort
print("Bubble_Sorting, Please wait……")
t1 = time.clock()
for i in range(1,2):
random.shuffle(lst)
bubble_sort(lst)
t2 = time.clock()
print('Bubble_sort ' + str(t2 - t1))
########################################
# Select_sort
print("Select_Sorting, Please wait……")
t1 = time.clock()
for i in range(1,2):
random.shuffle(lst)
select_sort(lst)
t2 = time.clock()
print('Select_sort ' + str(t2 - t1))
########################################
# Quick_sort
print("Quick_Sorting, Please wait……")
t1 = time.clock()
for i in range(1,11):
random.shuffle(lst)
qsort(lst)
t2 = time.clock()
print('Quik_sort ' + str(t2 - t1))
我爱记单词
程序设计说明
实现一个简单的记生词软件,基本功能包括:添加新的生词及中文含义,浏览已经记录的单词,随机选择部分单词进行复习。可考虑其他拓展的功能。
程序设计思路
程序分为两个界面。第一个界面添加生词,查看已经记录的单词,同时可以对记录的单词就行修改和删除,还可以设置打算复习的单词的数目。第二个界面为复习单词的界面,给出当前选择单词是否选对和正确率等等情况。
程序代码
Main.py
#-*- coding:utf-8 -*-
import os, sys
import json
import random
import words
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
from tkinter.messagebox import *
class Application_ui(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('Lovely Words-Main')
self.master.geometry('575x204')
self.createWidgets()
def createWidgets(self):
self.top = self.winfo_toplevel()
self.style = Style()
self.style.configure('Delete.TButton',font=('宋体',9))
self.Delete = Button(self.top, text='删除单词', command=self.Delete_Cmd, style='Delete.TButton')
self.Delete.place(relx=0.042, rely=0.51, relwidth=0.252, relheight=0.123)
self.style.configure('Start.TButton',font=('宋体',9))
self.Start = Button(self.top, text='开始训练', command=self.Start_Cmd, style='Start.TButton')
self.Start.place(relx=0.306, rely=0.824, relwidth=0.266, relheight=0.123)
self.WordNumVar = StringVar(value='请输入复习单词个数')
self.WordNum = Entry(self.top, text='请输入复习单词个数', textvariable=self.WordNumVar, font=('宋体',9))
self.WordNum.place(relx=0.042, rely=0.824, relwidth=0.238, relheight=0.123)
self.List1Var = StringVar(value='List1')
self.List1Font = Font(font=('宋体',9))
self.List1 = Listbox(self.top, listvariable=self.List1Var, font=self.List1Font)
self.List1.place(relx=0.612, rely=0.078, relwidth=0.35, relheight=0.843)
self.List1.bind('<ButtonRelease-1>',self.ViewWord)
self.op4Var = StringVar(value='示例:西瓜')
self.op4 = Entry(self.top, text='示例:西瓜', textvariable=self.op4Var, font=('宋体',9))
self.op4.place(relx=0.32, rely=0.51, relwidth=0.252, relheight=0.123)
self.op3Var = StringVar(value='示例:桃子')
self.op3 = Entry(self.top, text='示例:桃子', textvariable=self.op3Var, font=('宋体',9))
self.op3.place(relx=0.32, rely=0.392, relwidth=0.252, relheight=0.123)
self.op2Var = StringVar(value='示例:苹果')
self.op2 = Entry(self.top, text='示例:苹果', textvariable=self.op2Var, font=('宋体',9))
self.op2.place(relx=0.32, rely=0.275, relwidth=0.252, relheight=0.123)
self.op1Var = StringVar(value='示例:香蕉')
self.op1 = Entry(self.top, text='示例:香蕉', textvariable=self.op1Var, font=('宋体',9))
self.op1.place(relx=0.32, rely=0.157, relwidth=0.252, relheight=0.123)
self.wordVar = StringVar(value='示例:banana')
self.word = Entry(self.top, text='示例:banana', textvariable=self.wordVar, font=('宋体',9))
self.word.place(relx=0.042, rely=0.157, relwidth=0.252, relheight=0.115)
self.style.configure('Add.TButton',font=('宋体',9))
self.Add = Button(self.top, text='添加单词', command=self.Add_Cmd, style='Add.TButton')
self.Add.place(relx=0.042, rely=0.275, relwidth=0.252, relheight=0.123)
self.style.configure('Num.TLabel',anchor='center', foreground='#000000', font=('宋体',9))
self.Num = Label(self.top, text='当前词库共有%d个单词', style='Num.TLabel')
self.Num.place(relx=0.042, rely=0.706, relwidth=0.53, relheight=0.083)
self.style.configure('Tips.TLabel',anchor='w', font=('宋体',9))
self.Tips = Label(self.top, text='请将答案输入右侧第一个输入框中', style='Tips.TLabel')
self.Tips.place(relx=0.042, rely=0.070, relwidth=0.53, relheight=0.083)
self.style.configure('Modify.TButton',font=('宋体',9))
self.Modify = Button(self.top, text='修改单词', command=self.Modify_Cmd, style='Modify.TButton')
self.Modify.place(relx=0.042, rely=0.392, relwidth=0.252, relheight=0.123)
class Application(Application_ui):
def __init__(self, master=None):
Application_ui.__init__(self, master)
self.NumUpdate()
self.id = 1
self.curid = -1
self.List1.delete(0,END)
for item in word:
self.List1.insert(self.id,item['WORD'])
self.id+=1
def NumUpdate(self):
self.Num['text'] = '当前词库共有'+str(len(word))+'个单词'
def Add_Cmd(self, event=None):
self.key = self.op1.get()
self.optemp = [self.op1.get(),self.op2.get(),self.op3.get(),self.op4.get()]
random.shuffle(self.optemp)
self.pos = 0
for i in range(len(self.optemp)):
if self.optemp[i] == self.key:
self.pos = i+1
break
tmp = {'WORD':self.word.get(),'OP1':self.optemp[0],'OP2':self.optemp[1],'OP3':self.optemp[2],'OP4':self.optemp[3],'KEY':self.pos}
word.append(tmp)
self.List1.insert(END,tmp['WORD'])
self.NumUpdate()
def Start_Cmd(self, event=None):
try:
t = int(self.WordNum.get())
if t > len(word):
messagebox.showwarning(title = '警告',message = '题目过多!')
return None
except:
messagebox.showwarning(title = '警告',message = '复习单词数目有误!')
else:
randtar = set()
for i in range(0,t):
test = random.randint(0,len(word)-1)
if test not in randtar:
randtar.add(test)
else:
while test in randtar:
test = random.randint(0,len(word)-1)
randtar.add(test)
randword = list()
for i in randtar:
randword.append(word[i])
random.shuffle(randword)
print(randword)
with open('temp.json','w') as f:
json.dump(randword,f)
words.init(t)
def Delete_Cmd(self, event=None):
tar = list(self.List1.curselection())
print(tar)
for i in tar:
del word[i]
self.List1.delete(i)
def Modify_Cmd(self, event=None):
self.key = self.op1.get()
self.optemp = [self.op1.get(),self.op2.get(),self.op3.get(),self.op4.get()]
random.shuffle(self.optemp)
self.pos = 0
for i in range(len(self.optemp)):
if self.optemp[i] == self.key:
self.pos = i+1
break
tmp = {'WORD':self.word.get(),'OP1':self.optemp[0],'OP2':self.optemp[1],'OP3':self.optemp[2],'OP4':self.optemp[3],'KEY':self.pos}
word[self.curid] = tmp
def ViewWord(self, event = None):
tar = self.List1.curselection()
if len(tar) == 1:
self.curid = tar[0]
self.word.delete(0,END)
self.word.insert(0,word[self.curid]['WORD'])
self.op1.delete(0,END)
self.op1.insert(0,word[self.curid]['OP1'])
self.op2.delete(0,END)
self.op2.insert(0,word[self.curid]['OP2'])
self.op3.delete(0,END)
self.op3.insert(0,word[self.curid]['OP3'])
self.op4.delete(0,END)
self.op4.insert(0,word[self.curid]['OP4'])
else:
pass
def start():
global word
with open('word.json', 'r') as f:
word = json.load(f)
top = Tk()
Application(top).mainloop()
with open('word.json','w') as f:
json.dump(word,f)
try: top.destroy()
except: pass
if __name__ == "__main__":
start()
else:
pass
words.py
#-*- coding:utf-8 -*-
import os, sys
import json
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
from tkinter.messagebox import *
class Application_ui(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('Lovely Words-Review')
self.master.geometry('451x188')
self.createWidgets()
def createWidgets(self):
self.top = self.winfo_toplevel()
self.style = Style()
self.style.configure('Option4.TRadiobutton',font=('宋体',12))
self.Option4 = Radiobutton(self.top, text='Option4', value=4,command=self.Option4_Cmd, style='Option4.TRadiobutton')
self.Option4.place(relx=0.603, rely=0.681, relwidth=0.392, relheight=0.176)
self.style.configure('Option3.TRadiobutton',font=('宋体',12))
self.Option3 = Radiobutton(self.top, text='Option3', value=3,command=self.Option3_Cmd, style='Option3.TRadiobutton')
self.Option3.place(relx=0.603, rely=0.553, relwidth=0.375, relheight=0.133)
self.style.configure('Option2.TRadiobutton',font=('宋体',12))
self.Option2 = Radiobutton(self.top, text='Option2', value=2,command=self.Option2_Cmd, style='Option2.TRadiobutton')
self.Option2.place(relx=0.603, rely=0.383, relwidth=0.357, relheight=0.176)
self.style.configure('Confirm.TButton',font=('宋体',16,'bold'))
self.Confirm = Button(self.top, text='确认', command=self.Confirm_Cmd, style='Confirm.TButton')
self.Confirm.place(relx=0.053, rely=0.511, relwidth=0.463, relheight=0.261)
self.style.configure('Option1.TRadiobutton',font=('宋体',12))
self.Option1 = Radiobutton(self.top, text='Option1', value=1,command=self.Option1_Cmd, style='Option1.TRadiobutton')
self.Option1.place(relx=0.603, rely=0.255, relwidth=0.339, relheight=0.133)
self.style.configure('Statues.TLabel',anchor='center', font=('宋体',12))
self.Statues = Label(self.top, text='Statues', style='Statues.TLabel')
self.Statues.place(relx=0.124, rely=0.128, relwidth=0.736, relheight=0.106)
self.style.configure('Words.TLabel',anchor='center', font=('宋体',15,'bold'))
self.Words = Label(self.top, text='Words', style='Words.TLabel')
self.Words.place(relx=0.018, rely=0.255, relwidth=0.55, relheight=0.261)
self.style.configure('Mes.TLabel',anchor='center', font=('宋体',9))
self.Mes = Label(self.top, text='Statues', style='Mes.TLabel')
self.Mes.place(relx=0.035, rely=0.851, relwidth=0.914, relheight=0.106)
class Application(Application_ui):
var = -1
global nowid
global totnum
def Update(self):
self.Words['text'] = word[nowid]['WORD']
self.Option1['text'] = word[nowid]['OP1']
self.Option2['text'] = word[nowid]['OP2']
self.Option3['text'] = word[nowid]['OP3']
self.Option4['text'] = word[nowid]['OP4']
def Update_Mes(self):
self.acc = self.rnum * 1.0 / (nowid+1)
self.Mes['text'] = '共有'+str(totnum)+'题' +'-当前第'+str(nowid+1)+'题'+'-正确率'+str(self.acc*100)+'%'
def __init__(self, master=None):
Application_ui.__init__(self, master)
self.rnum = 0
self.acc = 0
self.Statues['text'] = '开始训练!'
self.Update()
self.Update_Mes()
def Option1_Cmd(self, event=None):
self.var = 1
def Option2_Cmd(self, event=None):
self.var = 2
def Option3_Cmd(self, event=None):
self.var = 3
def Option4_Cmd(self, event=None):
self.var = 4
def Confirm_Cmd(self, event=None):
global nowid
global totnum
self.key = word[nowid]['KEY']
if self.var != -1:
print(self.var,self.key)
if self.var == self.key:
print('in')
self.Statues['text'] = '正确! '+ word[nowid]['WORD'] +': ' + word[nowid]['OP'+str(self.key)]
self.rnum += 1
else:
self.Statues['text'] = '错误! '+ word[nowid]['WORD'] +': ' + word[nowid]['OP'+str(self.key)]
self.Update_Mes()
nowid += 1
if nowid >= totnum:
self.Statues['text'] = self.Statues['text']+'--训练结束--'
else:
self.Update()
else:
self.Statues['text'] = '请选择一个选项!'
def init(temp):
global word
with open('temp.json', 'r') as f:
word = json.load(f)
global nowid
global totnum
nowid = 0
totnum = temp
top = Tk()
top_ui = Application(top)
top_ui.mainloop()
try: top.destroy()
except: pass
if __name__ == "__main__":
pass
else:
pass