1. 数不属于a-m 的字母,并返回个数/ 总字母个数
mein: 应该为109 不是110
def printer_error(s): a = 0 for i in s: if ord(i) >110: # a = a + 1 return str(a)+'/'+str(len(s))
key:
from re import sub def printer_error(s): return "{}/{}".format(len(sub("[a-m]",'',s)),len(s)) # 正则表达
def printer_error(s): return "{}/{}".format(len([x for x in s if x not in "abcdefghijklm"]), len(s))
def printer_error(s): errors = 0 count = len(s) for i in s: if i > "m": errors += 1 return str(errors) + "/" + str(count) # 和自己的好像,不知道哪里出问题
def printer_error(s): return '{}/{}'.format(sum(color > 'm' for color in s), len(s))
2. 计算列表中数字的和, 再返回单双数
mein:
def odd_or_even(arr): s = 0 for i in arr: s += i if s % 2 == 0: return 'even' return 'odd'
key: sum(可直接加代码)
def oddOrEven(arr): return 'even' if sum(arr) % 2 == 0 else 'odd'
3.
西部郊区槌球俱乐部有两类会员资格:高级会员和公开会员。他们希望获得您的帮助,并提供申请表,该表将告诉准会员他们将被放置的类别。
要成为高年级成员,年龄必须至少55岁,并且让分差大于7。在这个槌球俱乐部中,让分范围是-2到+26;球员越好,让分越低。
输入将包含一个列表列表,每个列表包含两个项目。每个列表都包含单个潜在成员的信息。信息由一个人的年龄的整数和一个人的障碍的整数组成。
F#的注意事项:输入将是(int列表)的一个List <List>
openOrSenior([[45, 12],[55,21],[19, -2],[104, 20]]), ['Open', 'Senior', 'Open', 'Senior'])
mein:
def openOrSenior(data): a=[] for i in data: print(i) if i[0]> 54 and i[1] > 7: a.append('Senior') else: a.append('Open') return a
key:
def openOrSenior(data): return ["Senior" if age >= 55 and handicap >= 8 else "Open" for (age, handicap) in data]
4. AT,GC 相互转换
不会
import string def DNA_strand(dna): return dna.translate(string.maketrans("ATCG","TAGC")) # string.maketrans(a,b) 是制作翻译表,然后dna.translate 进行转化 # or pairs = {'A':'T','T':'A','C':'G','G':'C'} def DNA_strand(dna): return ''.join([pairs[x] for x in dna]) # or def DNA_strand(dna): reference = { "A":"T", "T":"A", "C":"G", "G":"C" } return "".join([reference[x] for x in dna]) 与上一个是一样的
5. 89 = 8^1 + 9^2, 135 = 1^1 + 3^2 + 5^3 等于自己 类似的函数
mein:
def sum_dig_pow(a, b): c = [] d = 0 for i in range(a,b+1): for n in range(1,len(str(i))+1): d += int(str(i)[n-1])**n if d == i: c.append(i) d = 0 return c
key:
def sum_dig_pow(a, b): return [x for x in range(a, b+1) if sum(int(d)**i for i, d in enumerate(str(x), 1)) == x] # enumerate 返回 标号和成分
6. 排队时间(不会)
queue_time([5,3,4], 1) # should return 12 # because when n=1, the total time is just the sum of the times q
ueue_time([10,2,3,3], 2) # should return 10 # because here n=2 and the 2nd, 3rd, and 4th people in the # queue finish before the 1st person has finished.
queue_time([2,3,10], 2) # should return 12
def queue_time(customers, n): l=[0]*n print(l) for i in customers: l[l.index(min(l))]+=i # l.index(min(l)) 寻找最短的队伍 return max(l) print(queue_time([1,2,3,4,5], 1))
7. 将名字 连城字符,但是最后两个名字用& 连,前面都用, 连
Test.assert_equals(namelist([{'name': 'Bart'},{'name': 'Lisa'},{'name': 'Maggie'},{'name': 'Homer'},{'name': 'Marge'}]),
'Bart, Lisa, Maggie, Homer & Marge',
mein:
def namelist(names): a = ' & '.join(i['name'] for i in names) return a.replace(' & ',', ',a.count('&')-1)
key:
def namelist(names): return ", ".join([name["name"] for name in names])[::-1].replace(",", "& ",1)[::-1] # 先转个方向,再转个方向
8. P be the Principal = 1000.00 Let I be the Interest Rate = 0.05 Let T be the Tax Rate = 0.18 Let D be the Desired Sum = 1100.00 投资什么时候能得到到期望的回报。
mein:
def calculate_years(principal, interest, tax, desired): i = 0 while True: if principal < desired: principal = principal + principal * interest * (1-tax) i += 1 continue else: break return i
key :
# 与自己的逻辑类似 def calculate_years(principal, interest, tax, desired): years = 0 while principal < desired: principal += (interest * principal) * (1 - tax) years += 1 return years
# 来自math 模块的专门算法 from math import ceil, log def calculate_years(principal, interest, tax, desired): if principal >= desired: return 0 return ceil(log(float(desired) / principal, 1 + interest * (1 - tax)))
浙公网安备 33010602011771号