Skip to content

python刷题系统每周代码 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions python/week 1/#TempConvert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#TempConvert.py
TempStr=input("What is the temperature?")
#赋值TempStr,括号里面的是提示
if TempStr[-1] in ['F','f']:
#假如字符串最后一个字符是F或者f
C=(eval(TempStr[0:-1])-32)/1.8-1
#第一个字符到最后一个字符之前的所有字符,也就是温度值,eval函数是脱掉字符串结构,运行公式
print("The converted temperature is {:.0f}C".format(C))
#输出结果,保留最后两位小数,是C的格式化
elif TempStr[-1] in ['C','c']:
#else if最后一个字符是C或者c
F=1.8*eval(TempStr[0:-1])+32-1
#这个括号加括号的表达,其实从里到外的顺序来看就不会混乱
print("The converted temperature is {:.0f}F".format(F))
#输出华氏温度
else:
#以上两个if都不符合
print("输入格式错误")
#输出提示文字
10 changes: 10 additions & 0 deletions python/week 1/sun花.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import turtle
turtle.pensize(5)
turtle.color("red","yellow")
turtle.begin_fill()
while True:
turtle.forward(300)
turtle.left(175)
if abs(turtle.pos())<1:
break
turtle.end_fill()
13 changes: 13 additions & 0 deletions python/week 1/一元二次函数.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from math import *
a,b,c = map(float,input().split())
'''b=float(input("b"))
c=float(input("c"))'''
if b**2-4*a*c < 0:
print("No")
else:
x1=(-b+sqrt(b**2-4*a*c))/2*a
x2=(-b-sqrt(b**2-4*a*c))/2*a
if x1>x2:
print("{:.2f} {:.2f}".format(x1,x2))
else:
print("{:.2f} {:.2f}".format(x2,x1))
9 changes: 9 additions & 0 deletions python/week 1/五角星.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import turtle
turtle.fillcolor("red")
turtle.begin_fill()
while True:
turtle.forward(300)
turtle.right(144)
if abs(turtle.pos())<1:
break
turtle.end_fill()
17 changes: 17 additions & 0 deletions python/week 1/打印大小写和数字.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
str=input()
int_count=0
A_count=0
a_count=0
for i in str:
if i.isupper():
A_count+=1
elif i.islower():
a_count+=1
elif i.isdigit():
int_count+=1
print(A_count)
print(a_count)
print(int_count)



17 changes: 17 additions & 0 deletions python/week 1/算π.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from random import random, seed
from math import sqrt


seed(123)
DARTS = eval(input())
hits =0.0


for i in range(1,DARTS+1):
x,y=random(),random()
dist = sqrt(x**2+y**2)
if dist<=1.0:
hits=hits+1
pi = 4*(hits/DARTS)
print('{:.6f}'.format(pi))

17 changes: 17 additions & 0 deletions python/week 1/鸡兔同笼.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
x , y= map(int,input().split()) #x animal y leg
a=True
rabbit=0
while a==True:
rabbit+=1
duck = x - rabbit
Leg = 2 * duck
rabLeg = 4 * rabbit

if(Leg + rabLeg>y or duck <0):
print("Data Error!")
a=False
break

if (Leg + rabLeg == y ):
print("{0} {1}".format(str(duck), str(rabbit)))
break
13 changes: 13 additions & 0 deletions python/week 2/一元二次函数.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from math import *
a,b,c = map(float,input().split())
'''b=float(input("b"))
c=float(input("c"))'''
if b**2-4*a*c < 0:
print("No")
else:
x1=(-b+sqrt(b**2-4*a*c))/2*a
x2=(-b-sqrt(b**2-4*a*c))/2*a
if x1>x2:
print("{:.2f} {:.2f}".format(x1,x2))
else:
print("{:.2f} {:.2f}".format(x2,x1))
17 changes: 17 additions & 0 deletions python/week 2/打印大小写和数字.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
str=input()
int_count=0
A_count=0
a_count=0
for i in str:
if i.isupper():
A_count+=1
elif i.islower():
a_count+=1
elif i.isdigit():
int_count+=1
print(A_count)
print(a_count)
print(int_count)



17 changes: 17 additions & 0 deletions python/week 2/算π.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from random import random, seed
from math import sqrt


seed(123)
DARTS = eval(input())
hits =0.0


for i in range(1,DARTS+1):
x,y=random(),random()
dist = sqrt(x**2+y**2)
if dist<=1.0:
hits=hits+1
pi = 4*(hits/DARTS)
print('{:.6f}'.format(pi))

17 changes: 17 additions & 0 deletions python/week 2/鸡兔同笼.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
x , y= map(int,input().split()) #x animal y leg
a=True
rabbit=0
while a==True:
rabbit+=1
duck = x - rabbit
Leg = 2 * duck
rabLeg = 4 * rabbit

if(Leg + rabLeg>y or duck <0):
print("Data Error!")
a=False
break

if (Leg + rabLeg == y ):
print("{0} {1}".format(str(duck), str(rabbit)))
break
Binary file added python/week 2/鸡兔同笼图.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions python/week 3/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
using namespace std;

int main()
{
int x = 1, y = 2;
int res = x * y;
cout << res << endl;
return 0;
}
15 changes: 15 additions & 0 deletions python/week 3/凯撒密码.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
a=input()
if a[1]==' ':
x=int(a[0:1])
y=a[2:]
elif a[2]==' ':
x=int(a[0:2])
y=a[3:]

for p in y:
if ord('a') <= ord(p) <= ord('z'):
print(chr( ord('a') + (ord(p) - ord('a') + x )%26), end='')
elif ord('A') <= ord(p) <= ord('Z'):
print(chr( ord('A') + (ord(p) - ord('A') + x )%26), end='')
else:
print(p,end='')
56 changes: 56 additions & 0 deletions python/week 3/报告.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
题目①反复猜数

一、源程序调试过程
(该项是成绩评定的主要评分依据之一,越详细越好。调试过程中发生的语法错误与逻辑错误,以及如何改正等,都要写出来)
1、确定思路为无限循环,则有C==True ,满足条件直接Break
2、循环内写三个判断语句,根据题设要求需要满足顺序
……

二、实验实习结果分析
(要求测试用例尽量全面,覆盖程序运行时的每个分支)
1、if(n>m):
print('larger than expected')
elif(n<100):
print('less than expected')
elif(n==100):
print('you win')
c==False
break

三、心得体会
(通过该实验,你学到了什么?)
预先构思好行程图再书写可以更好完成


题目② 凯撒密码

一、源程序调试过程
(该项是成绩评定的主要评分依据之一,越详细越好。调试过程中发生的语法错误与逻辑错误,以及如何改正等,都要写出来)
1、一行赋值,一个赋值数字一个赋值字符串的语法在python中不存在,所以需要用到切片,因为中间空格连接,且要么是个一位数字要么是个两位数字(字母只有26个),所以只需要判断第二个或者第三个是否是
空格,如果是,则切片,将空格前面的转化为Int属性
2、切好片之后用循环遍历空格后面的字符串,一个一个检测,通过ord 与 chr关键字进行变化得出凯撒密码
……

二、实验实习结果分析
(要求测试用例尽量全面,覆盖程序运行时的每个分支)
1、切片部分代码
if a[1]==' ':
x=int(a[0:1])
y=a[2:]
elif a[2]==' ':
x=int(a[0:2])
y=a[3:]

2、循环部分代码
for p in y:
if ord('a') <= ord(p) <= ord('z'):
print(chr( ord('a') + (ord(p) - ord('a') + x )%26), end='')
elif ord('A') <= ord(p) <= ord('Z'):
print(chr( ord('A') + (ord(p) - ord('A') + x )%26), end='')
else:
print(p,end='')

三、心得体会
1丶赋值Input都是字符串
2丶map是元组
3丶切片中最后一个数字是不算在内的,如果要包含最后一个则:后面什么都不打
12 changes: 12 additions & 0 deletions python/week 3/猜数.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
m = 100
c = True
while c == True:
n = eval(input())
if(n > m):
print('larger than expected')
elif(n < 100):
print('less than expected')
elif(n == 100):
print('you win')
c == False
break
24 changes: 24 additions & 0 deletions python/week 3/统计正负数个数.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

count=-1
zhengshu=0
num=0
fushu=0
while True:
m=eval(input())
count+=1
#print(count,'次')
if m>0:
zhengshu+=1
num+=m
elif m<0:
fushu+=1
num+=m
else:
m==0
break
#print('总数是',num)
ave = num/count
#print(type(ave))
print(ave)
print(zhengshu)
print(fushu)
31 changes: 31 additions & 0 deletions python/week 3/账户密码(字典).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
dic={"aaa":"123456","bbb":"888888","ccc":"333333"}

log_error_count = 0

username=input()
if username not in dic.keys():
print('Wrong User')
exit()
while True:
if username in dic.keys():
password=input()
if password == dic[username]:
print('Success')
sys.exit()
else:
log_error_count+=1
#print(log_error_count)
if log_error_count<=2:
print('Fail,'+str(3-log_error_count)+' Times Left')

else:
print('Login Denied')
sys.exit()


# print(dic)
# a=dic.keys()
# print(a)
# b=dic.values()
# print(b)
19 changes: 19 additions & 0 deletions python/week 4/列表删除.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import random

n = int(input())
random.seed(n)
m = random.randint(1, n)
# print(m)

ls = list(range(1, n+1))
ls2 = ls.copy()
print(ls2)

k = n//m
# print(k)
a = list(range(1, k+1))
# print(a[0],a[1])
for i in range(k):
index = (a[i]*m)-(i+1)
ls.pop(index)
print(ls)
7 changes: 7 additions & 0 deletions python/week 4/列表去重.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Name_list = input().split(',')
Name_set = set(Name_list)
Name_list2 = list(Name_set)

# 重新排序
Name_list2.sort(key=Name_list.index)
print(Name_list2)
17 changes: 17 additions & 0 deletions python/week 4/反复猜数2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
m = 100
c = True
count = 0
while c == True:
count = count+1
try:
n = eval(input())
if(n > m):
print('larger than expected')
elif(n < 100):
print('less than expected')
elif(n == 100):
print('you have tried {:.0f} times, you win'.format(count))
c == False
break
except NameError:
print("input error")
Loading