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 = '' 'Shuanger, Hong Qigong, Zhao Min, Zhao Min, Xiaoyao, Zi aobai, king of Golden Wheel of Yin Tianzheng, Qiao Feng, Yang Guo Jing, Hong Qigong Yang xiaoaobai, Yin Tianzheng, Duanyu, Yang xiaomurongfu, Zi murongfu, Guo Fu, Qiao Feng, Linghu Chong, Guo Fu Yang Guo, the daughter of Xiao Long, king of the golden wheel, Murong Fu, Mei Chaofeng, Li Mochou, Hong Qigong, Zhang Wuji, Mei Chaofeng, Yang Xiao Aobai yuebuqun Huang Yaoshi Huang Rong Duanyu, king of the golden wheel, Kublai Khan, Zhang Sanfeng, Qiao Feng A Zi, Qiao Feng, Jin Lun, FA Wang, Yuan Guannan, Zhang Wuji, Guo Xiang, Huang Rong, Li Mochou, Zhao Min, Zhao Min, Guo Fu, Zhang Sanfeng Qiao Feng, Zhao Min, Mei Chaofeng, Shuanger, aobai, Chen Jialuo, Yuan Guan, Nanguo Fu, Guo Fu, Yang Xiao, Zhao Min, king of the Golden Wheel Kublai Khan, Murong Fu, Zhang Sanfeng, Yang Xiao, Linghu Chong, Huang Yaoshi, Yuan Guannan, Yang Xiao, Yan Honglie, Yin Tianzheng Li Mochou, a Zi, Xiao Yao Zi, Qiao Feng, Xiao Yao Zi, Yan Honglie, Guo Fu, Yang Xiao, Zhang Wuji, Yang Guo, Murong Fu Xiao Yaozi, Xu zhushuanger, Qiao Feng, Guo Fu, Huang Rong, Li Mochou, Chen Jialuo, Yang Guo, Kublai Khan, aobai, Wang Yuyan Hong Qigong, Wei Xiaobao, a Zhu, Mei Chaofeng, Duanyu, Yue Lingshan, Wanyan, Honglie, Qiao Feng, Duanyu, Yang Guo, Yang Guo, and Murong Fu Huang Rong, Yang Guo, a Zi, Yang Xiao, Zhang Sanfeng, Zhang Sanfeng, Zhao Min, Zhang Sanfeng, Yang Xiao, Huang Rong, king of the Golden Wheel law, Guo Xiang Zhang Sanfeng, Linghu Chong, Guo Fu, Wei Xiaobao, Huang Yaoshi, a Zi, Wei Xiaobao, king of golden wheel, Yang Xiao, Linghu Chong, a Zi Hong Qigong, Yuan Guannan, Guo Jing, aobai, Xie Xun, a Zi, Guo Xiang, Mei Chaofeng, Zhang Wuji, Duan Yu, Kublai lie Yan Honglie, Xiao Yaozi, Xie Xun, Yan Honglie, Yin Tianzheng, king of the golden wheel, Zhang Sanfeng, Shuanger, Guo Xiang, Zhu Guo Xiang's twin son Li Mochou, Guo Xiang, Kublai Khan, king of the golden wheel, Zhang Wuji, worships Kublai Khan, Guo Xiang, Linghu Chong Xie Xun, Mei Chaofeng, Yin Tianzheng, Duanyu, Yuan Guannan, Zhang Sanfeng, Wang Yuyan, a Zi, Xie Xun, Yang Guo Jing, Huang Rong Shuanger exterminates abbess Duan Yu, Zhang Wuji, Chen Jialuo, Huang Rong, aobai, Huang Yaoshi, xiaoyaozi, Kublai, Zhao Min Xiaoyaozi, Wanyan Honglie, jinlunfa king, Shuanger, aobai, Hongqigong, Guofu, Guoxiang " #Divide the string into spaces, return a list, use set to de duplicate, and return the size print(len(set(s.split()))) Dictionary reverse output
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()