import turtle as t import time def gap(): t.penup() t.fd(5) def drawline(draw): gap() t.pendown() if draw else t.penup() t.fd(40) gap() t.right(90) def drawDigit(n): drawline(True) if n in [2,3,4,5,6,8,9] else draw(False) drawline(True) if n in [1,3,4,6,7,8,9,0] else draw(False) drawline(True) if n in [2,3,5,6,8,9,0] else draw(False) drawline(True) if n in [2,5,6,8,9,0] else draw(False) t.left(90) drawline(True) if n in [4,5,6,8,9,0] else draw(False) drawline(True) if n in [2,3,5,6,7,8,9,0] else draw(False) drawline(True) if n in [1,2,3,4,6,7,8,9,0] else draw(False) t.left(180) t.penup() t.fd(20) def drawDate(date): t.color("red") for i in range date: if i == '-' : t.write("year",font("Arial", 18, "normal")) t.pencolor("yellow") t.fd(40) elif i == '+' : t.write("month",font("Arial", 18, "normal")) t.pencolor("bule") t.fd(40) else if i == '=' : t.write("day",font("Arial", 18, "normal")) t.pencolor("green") t.fd(40) else: drawDigit(i) def main(): t.setup(700,350) t.penup() t.fd(-330) t.pensize(5) drawDate(time.strftime("%Y-%m+%d+",time.gtime())) h.hideturtle() t.done() main()Koch snow small package
import turtle def koch(size, n): if n == 0 : turtle.fd(size) else: for angle in [0,60,-120,60] turtle.left(angle) koch(size/3,n-1) def main(level): turtle.setup(600,600) turtle.penup() turtle.goto(-200, 100) turtle.pendown() turtle.pensize(2) ... try: level = eval(input("Please enter the order of koch curve: ")) main(level) except: print("Input error")Arbitrary accumulation
def cmul(a,*b): for i in b: a*=i return a print(eval("cmul({})".format(input())))Fibonacci series
def fbi(n): if n == 1 or n == 2 : return 1 else: return fbi(n-1)+fbi(n-2) n = eval(input()) print(fbi(n))Hanoi Tower practice
steps = 0 def hanoi(src, des, mid, n): global steps if n == 1: steps+=1 print("[STEP{:>4}] {}->{}".format(steps, src, des)) else: hanoi(src,mid,des,n-1) steps+=1 print("[STEP{:>4}] {}->{}".format(steps, src, des)) hanoi(mid,des,src,n-1) N = eval(input()) hanoi("A", "C", "B", N)Basic statistics calculation
#Please add one or more lines of code to #CalStatisticsV1.py def getNum(): #Get user input of variable length getNums = input() s= getNums.split(",") for i in range(len(s)): s[i] = eval(s[i]) return s def mean(numbers): #Calculate average #numbers is a list res = 0 for i in range(len(numbers)): res += numbers[i] return res / len(numbers) def dev(numbers, mean): #Calculate standard deviation sdev = 0.0 for num in numbers: sdev = sdev + (num - mean)**2 return pow(sdev / (len(numbers)-1), 0.5) def median(numbers): #Calculate median #Sort sorted(numbers) size = len(numbers) if size % 2 == 0: return (numbers[size//2 -1] + numbers[size // 2 ])/2 else : return numbers[size//2] n = getNum() #Principal function m = mean(n) print("average value:{:.2f},standard deviation:{:.2f},median:{}".format(m,dev(n,m),median(n)))Text frequency statistics: Quotation version Hamlet
def getText(): txt = open("hamlet.txt", "r").read() #Remove case effect txt = txt.lower() #Replace special characters with spaces for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_'{|}~' : txt = txt.replace(ch," ") return txt hamletTxt = getText() #Split into words and return a list words = hamletTxt.split() #Use dictionary to save word - mapping of occurrence times counts = {} #Traversal list, statistic words and corresponding times for word in words: counts[word] = counts.get(word,0)+1 #Convert dictionary to list for sorting items = list(counts.items()) #sort items.sort(key=lambda x:x[1], reverse = True) #Take the top ten for i in range(10): print(items[i][0])Statistics of uniqueness of names
s = input() try: d = eval(s) res ={} for k in d: res[d[k]] = k print(res) except: print("Input error")The most words in the silent lamb
import jieba f = open("Silent lamb.txt") ls = jieba.lcut(f.read()) #ls = f.read().split() d = {} for w in ls: if len(w) > 2: d[w] = d.get(w, 0) + 1 maxc = 0 maxw = "" #Traverse the dictionary to find the maximum word frequency for k in d: if d[k] > maxc: maxc = d[k] maxw = k elif d[k] == maxc and k > maxw: maxw = k print(maxw) f.close()File lines
f = open("latex.log","r") count = 0 for line in f: line = line.strip("\n") if line == "": continue count+=1 print("common{}That's ok".format(count))File character distribution
f = open("latex.log","r") #Scan only a -z, so initialize the dictionary #Define a dictionary to count the mapping between words and occurrences d = {} #initialization for i in range(26): d[chr(ord("a")+i)] = 0 #Count the number of characters count = 0 #Nested traversal for line in f: for c in line: d[chr(ord(c))] = d.get(chr(ord(c)),0)+1 count+=1 print("common{}character".format(count),end="") #At last, I only traverse 26 letters for i in range(26): word,count =(chr(ord("a")+i)) ,d.get(chr(ord("a")+i)) print(",{}:{}".format(word,count),end="")File unique lines
f = open("latex.log") #First, read all the lines, and return a list. Each line represents an element lines = f.readlines() #One time for advanced travel s = set(lines) #Delete non duplicate rows for i in s: lines.remove(i) #All that's left is a repeating line #The repeated lines are de duplicated, t = set(lines) #Final result = non repeating rows - repeating rows print("common{}Unique line".format(len(s) - len(t)))CSV format column transformation:
f = open("data.csv") ls = [] for line in f: line = line.replace("\n","") ls.append(line.split(",")) f.close() #Reverse each line for row in ls: print(",".join(row[::-1]))CSV format data cleaning
f = open("data.csv") #Read each row, then use, to separate and return a list for line in f: #Remove the line break of the current line first line = line.strip("\n") #Replace, with, ls = line.replace(", ",",") #output print(ls) f.close()