Python Encoder练习

admin 2022年1月6日01:52:52安全博客评论15 views8375字阅读27分55秒阅读模式

给定一个列表,使用列表推导式找出列表中长度大于 5 的名字,并打印该列表。

1
2
3
4
names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']]
ret = [name for lst in names for name in lst if len(name)>5]
print(ret)

本题的关键在于,如何把names切片。names是一个二元列表。

本想着用split去做切片,但是不太合适,因为split是用来切割字符串的。

其实只用一个for循环就可以把元素全部提取出来。

name for lst in names for name in lst if len(name)>5 的作用等同于:

1
2
3
4
for lst in names:
#print(lst)
for name in lst:
print(name)

这里只用一个for循环,把names的二元列表变为两个一元列表lst,然后再通过name获取每个列表中的元素,并输出

image-20200330104931887

最后ret = [name for lst in names for name in lst if len(name)>5]

用一个列表推导式,把for循环中提取出来的元素赋值给ret列表

按规则输出一句话

1
2
3
4
5
6
#--coding:utf-8
v0 =5.0
g = 9.81
t = input() #输入时间
y = v0*t-0.5*g*t**2 #牛顿第二定律
print "At t =%.1fs, a ball with\ninitial velocity v0 = %.3E m/s\nis located at the height %.2f m." % (t,v0,y)

求解一元二次方程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from numpy.lib.scimath import sqrt
a = int(input())
b = int(input())
c = int(input())
if a == 0:
if b == 0 and c ==0:
print("There are infinitely many solutions!")
else:
if b==0:
print ("There is no solution")
else:
print("There is a unique solution x=%g" %(-1*c/b))
else:
disc = b*b -4*a*c
if disc > 0:
x1 = (-b + sqrt(disc)) / 2 / a
x2 = (-b - sqrt(disc)) / 2 / a
if disc > 0:
print("There are two distinct real roots x1=%g and x2=%g" % (x1, x2))
else:
print("There are two conjugate roots x1=%g and x2=%g" % (x1, x2))
else:
if disc == 0:
print("There are two equal real roots x1=x2=%g" % (-1 * b / 2 / a))
else:
x1 = (-b + sqrt(disc)) / 2 / a
x2 = (-b - sqrt(disc)) / 2 / a
print("There are two conjugate roots x1=%s and x2=%s" % (x1, x2))

lambda表达式应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# coding=utf-8

# 请在此添加代码,使用lambda来创建匿名函数,能够判断输入的两个数值的大小
########## Begin ##########

MAXIMUM = lambda a,b:a if a>b else b
MINIMUM = lambda a,b:a if a<b else b


########## End ##########

# 输入两个正整数
a = int(input())
b = int(input())

# 输出较大的值和较小的值
print('较大的值是:%d' % MAXIMUM(a,b))
print('较小的值是:%d' % MINIMUM(a,b))

注意:三元运算、条件表达式

正整数分解质因数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# coding=utf-8

# 输入一个正整数
x = int(input())

# 请在此添加代码,将输入的一个正整数分解质因数
########## Begin ##########

n = x
result = []
while n>1:
for i in range(2,n+1):
if n%i ==0:
n = int(n/i)
result.append(i)
break
########## End ##########
# 输出结果,利用map()函数将结果按照规定字符串格式输出
print(x,'=','*'.join(map(str,result)))

image-20200413171845681

本题要点:

  1. 使用for循环,如何输出i之后再次用n/i作为新的n去从小到大输出i
  2. 最后结果怎么去把每次输出的i放入

奇偶数分别相加

1
2
3
4
5
6
7
8
9
m = int(input()) #接受输入的数字并将其转换为int型
n = m%2 #判断其是奇数还是偶数
sum = 0 #预设sum为0
for i in alist: #遍历alist
if i%2 == n: #选择与输入数奇偶性相同的数
sum += i #累加到sum里
alist.append(sum) #将sum添加到alist的末尾
print(alist) #打印alist

按揭贷款–定义抽象类

part 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def findPayment(loan, r, m):
#********** Begin *********#
# 请在下面编写代码
return loan*((r*(1+r)**m)/((1+r)**m-1))

# 请不要修改下面的代码
#********** End *********#
class Mortgage(object):
def __init__(self, loan, annRate, months):
#********** Begin *********#
# 请在下面编写代码
self.loan =loan
self.rate = annRate/1200.0
self.months = months
self.paid = [0.0]
self.owed = [loan]
self.payment = findPayment(loan,self.rate,months)
# 请不要修改下面的代码
#********** End *********#
self.legend = None

def makePayment(self):
#********** Begin *********#
# 请在下面编写代码
self.paid.append(self.payment)
reduction = self.payment - self.owed[-1]*self.rate
self.owed.append(self.owed[-1] - reduction)
# 请不要修改下面的代码
#********** End *********#

def getTotalPaid(self):
#********** Begin *********#
# 请在下面编写代码
return sum(self.paid)
# 请不要修改下面的代码
#********** End *********#

def __str__(self):
return 'The Mortgage is {self.legend}, Loan is {self.loan}, Months is {self.months}, Rate is {self.rate:.2f}, Monthly payment is {self.payment:.2f}'.format(self=self)

if __name__=="__main__":
print(Mortgage(100000, 6.5, 36))
print(Mortgage(100000, 6.5, 120))


part 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def findPayment(loan, r, m):
return loan * ((r * (1 + r) ** m) / ((1 + r) ** m - 1))

class Mortgage(object):
def __init__(self, loan, annRate, months):
self.loan = loan
self.rate = annRate / 1200.0
self.months = months
self.paid = [0.0]
self.owed = [loan]
self.payment = findPayment(loan, self.rate, self.months)
self.legend = None

def makePayment(self):
self.paid.append(self.payment)
reduction = self.payment - self.owed[-1] * self.rate
self.owed.append(self.owed[-1] - reduction)

def getTotalPaid(self):
return sum(self.paid)

def __str__(self):
return str(self.legend)


class Fixed(Mortgage):
def __init__(self, loan, r, months):
# 请在此添加代码,补全函数__init__
#********** Begin *********#
Mortgage.__init__(self,loan,r,months)
self.legend = 'Fixed, ' + str(r*100) + '%'
#********** End *********#
self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months'


class FixedWithPoints(Mortgage):
def __init__(self, loan, r, months, pts):
# 请在此添加代码,补全函数__init__
#********** Begin *********#
Mortgage.__init__(self,loan,r,months)
self.pts = pts
self.paid = [loan*(pts/100.0)]
self.legend = 'Fixed, ' + str(r*100) +'%, ' + str(pts) + 'points'

#********** End *********#
self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months'


class TwoRate(Mortgage):
def __init__(self, loan, r, months, teaserRate, teaserMonths):
# 请在此添加代码,补全函数__init__
#********** Begin *********#
Mortgage.__init__(self,loan,teaserRate,months)
self.teaserMonths = teaserMonths
self.teaserRate = teaserRate
self.nextRate = r/1200.0
self.legend = str(teaserRate*100) + '%for' +str(self.teaserMonths) + ' months,then ' + str(r*100) + '%'

#********** End *********#
self.legend = str(teaserRate)\
+ '% for ' + str(self.teaserMonths)\
+ ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months'

def makePayment(self):
# 请在此添加代码,补全函数makePayment
#********** Begin *********#
if len(self.paid) == self.teaserMonths + 1:
self.rate = self.nextRate
self.payment = findPayment(self.owed[-1],self.rate,self.months - self.teaserMonths)


#********** End *********#
Mortgage.makePayment(self)

if __name__=="__main__":

print(Fixed(100000, 6.5, 36))
print(Fixed(100000, 6.5, 120))

print(FixedWithPoints(100000, 6.5, 36, 20))
print(FixedWithPoints(100000, 6.5, 120, 20))

print(TwoRate(100000, 9.0, 36, 4.8, 12))
print(TwoRate(100000, 7.0, 120, 4.8, 36))

part 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def findPayment(loan, r, m):
return loan * ((r * (1 + r) ** m) / ((1 + r) ** m - 1))

class Mortgage(object):
def __init__(self, loan, annRate, months):
self.loan = loan
self.rate = annRate / 1200.0
self.months = months
self.paid = [0.0]
self.owed = [loan]
self.payment = findPayment(loan, self.rate, self.months)
self.legend = None

def makePayment(self):
self.paid.append(self.payment)
reduction = self.payment - self.owed[-1] * self.rate
self.owed.append(self.owed[-1] - reduction)

def getTotalPaid(self):
return sum(self.paid)

def __str__(self):
return str(self.legend)


class Fixed(Mortgage):
def __init__(self, loan, r, months):
Mortgage.__init__(self, loan, r, months)
self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months'


class FixedWithPoints(Mortgage):
def __init__(self, loan, r, months, pts):
Mortgage.__init__(self, loan, r, months)
self.pts = pts
self.paid = [loan * (pts / 100.0)]
self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months'


class TwoRate(Mortgage):
def __init__(self, loan, r, months, teaserRate, teaserMonths):
Mortgage.__init__(self, loan, teaserRate, months)
self.teaserMonths = teaserMonths
self.teaserRate = teaserRate/1200
self.nextRate = r / 1200.0
self.legend = str(teaserRate)\
+ '% for ' + str(self.teaserMonths)\
+ ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months'

def makePayment(self):
if len(self.paid) == self.teaserMonths + 1:
self.rate = self.nextRate
self.payment = findPayment(self.owed[-1], self.rate, self.months - self.teaserMonths)
Mortgage.makePayment(self)


def compareMortgages(amt, years, fixedRate, pts, ptsRate, varRate1, varRate2, varMonths):
# 请在此添加代码,补全函数compareMortgages
#********** Begin *********#
totMonths = years*12
fixed1 = Fixed(amt, fixedRate, totMonths)
fixed2 = FixedWithPoints(amt, ptsRate, totMonths, pts)
twoRate = TwoRate(amt, varRate2, totMonths, varRate1, varMonths)
morts = [fixed1, fixed2, twoRate]
#********** End *********#
for m in range(totMonths):
# 请在此添加代码,补全函数compareMortgages
#********** Begin *********#
for mort in morts:
mort.makePayment()
#********** End *********#
for m in morts:
print(m)
print('Loan ' + str(amt) + ' Total payments = ' + str(int(m.getTotalPaid())))

if __name__=="__main__":
compareMortgages(200000, 30, 7, 3.25, 5, 4.5, 9.5, 48)
print('*'*40)
compareMortgages(1000000, 30, 7, 20, 5, 4.5, 9.5, 48)
print('*' * 40)
compareMortgages(500000, 10, 7, 20, 5, 4.5, 9.5, 48)

FROM :b0urne.top | Author:b0urne

特别标注: 本站(CN-SEC.COM)所有文章仅供技术研究,若将其信息做其他用途,由用户承担全部法律及连带责任,本站不承担任何法律及连带责任,请遵守中华人民共和国安全法.
  • 我的微信
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年1月6日01:52:52
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                  Python Encoder练习 http://cn-sec.com/archives/722900.html

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: