标签:__next__ def lambda += not editor port ken function
Your team is writing a fancy new text editor and you‘ve been tasked with implementing the line numbering.
Write a function which takes a list of strings and returns each line prepended by the correct number.
The numbering starts at 1. The format is n: string
. Notice the colon and space in between.
Examples:
number([]) # => []
number(["a", "b", "c"]) # => ["1: a", "2: b", "3: c"]
1 def number(lines): 2 a=enumerate(lines,1) 3 print(a.__next__(),a.__next__(),a.__next__()) # (1, ‘a‘) (2, ‘b‘) (3, ‘c‘) 4 return [‘%d: %s‘ % v for v in enumerate(lines, 1)] # 因为返回是是先数字,在lines 里面的元素
The Fibonacci numbers are the numbers in the following integer sequence (Fn):
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...
such as
F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.
Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying
F(n) * F(n+1) = prod.
Your function productFib takes an integer (prod) and returns an array:
[F(n), F(n+1), true] or {F(n), F(n+1), 1} or (F(n), F(n+1), True)
depending on the language if F(n) * F(n+1) = prod.
If you don‘t find two consecutive F(m) verifying F(m) * F(m+1) = prod
you will return
[F(m), F(m+1), false] or {F(n), F(n+1), 0} or (F(n), F(n+1), False)
F(m) being the smallest one such as F(m) * F(m+1) > prod
.
productFib(800) # should return (34, 55, false),
1 def productFib(prod): 2 x = [0,1] 3 # print(x[-1]) 4 while True: 5 if prod == x[-1] * x [-2]: 6 return [x[-2],x[-1],True] 7 if prod < x[-1] * x [-2]: 8 return [x[-2],x[-1],False] 9 else: 10 x.append(x[-1] + x[-2]) 11 continue
def productFib(prod): a, b = 0, 1 while prod > a * b: a, b = b, a + b return [a, b, prod == a * b]
def productFib(prod): func = lambda a, b: func(b, a+b) if a*b < prod else [a, b, a*b == prod] return func(0, 1)
def productFib(prod, f1=0, f2=1): return [f1, f2, True] if prod == f1 * f2 else [f1, f2, False] if prod < f1 * f2 else productFib(prod, f2, f1+f2)
3. 数字乘方相加
Some numbers have funny properties. For example:
89 --> 8¹ + 9² = 89 * 1
695 --> 6² + 9³ + 5?= 1390 = 695 * 2
46288 --> 4³ + 6?+ 2? + 8? + 8? = 2360688 = 46288 * 51
Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p
In other words:
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case we will return k, if not return -1.
Note: n and p will always be given as strictly positive integers.
dig_pow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1 dig_pow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k dig_pow(695, 2) should return 2 since 6² + 9³ + 5?= 1390 = 695 * 2 dig_pow(46288, 3) should return 51 since 4³ + 6?+ 2? + 8? + 8? = 2360688 = 46288 * 51
test.assert_equals(dig_pow(46288, 3), 51)
def dig_pow(n, p): a = 0 for x in str(n): a += int(x) ** p p += 1 if a % n == 0: return a//n else: return -1
def dig_pow(n, p): s = 0 for i,c in enumerate(str(n)): s += pow(int(c),p+i) return s/n if s%n==0 else -1
def dig_pow(n, p): t = sum( int(d) ** (p+i) for i, d in enumerate(str(n)) ) return t//n if t%n==0 else -1
ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.
If the function is passed a valid PIN string, return true, else return false.
eg:
validate_pin("1234") == True
validate_pin("12345") == False
validate_pin("a234") == False
def validate_pin(pin): return pin.isdecimal() and (len(pin) == 4 or len(pin) == 6)
def validate_pin(pin): return len(pin) in (4, 6) and pin.isdigit()
1 import re 2 def validate_pin(pin): 3 return bool(re.match(r‘^(\d{4}|\d{6})$‘,pin)) # ^开头,$结尾,中间要严格符合这个格式,digit 4个或6个
标签:__next__ def lambda += not editor port ken function
原文地址:https://www.cnblogs.com/adelinebao/p/13193718.html