2020.06.27

1. 

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b.

array_diff([1,2],[1]) == [2]

If a value is present in b, all of its occurrences must be removed from the other:

array_diff([1,2,2,2,3],[2]) == [1,3]

def array_diff(a, b):
    c = []
    for i in a:
        if i not in b:
            c.append(i)
    return c


or
def array_diff(a, b):
    return [i for i in a if i not in b]

2.

Your job is to write a function which increments a string, to create a new string.

  • If the string already ends with a number, the number should be incremented by 1.
  • If the string does not end with a number. the number 1 should be appended to the new string.

Examples:

foo -> foo1

foobar23 -> foobar24

foo0042 -> foo0043

foo9 -> foo10

foo099 -> foo100

不会

def increment_string(strng):
    head = strng.rstrip('0123456789')
    tail = strng[len(head):]
    print(head,tail)
    if tail == "": return strng + "1"
    return head + str(int(tail) + 1).zfill(len(tail))

3. 输入n  输出xy 是x^y == n

import math
def isPP(n):
    i=2
    while i < n:
        if i ** round(math.log(n,i)) == n:
            return [i,round(math.log(n,i))]
        else:
            i+=1
    else:
        return None

4. h bounce window     输出   从窗户可以看到多少次球

h: 扔球的高度, 应大于0

bounce: 球重新弹起来的高度是原来的多少 (0,1)

window : 看球的高度

def bouncingBall(h, bounce, window):
    if h >0 and bounce >0 and bounce<1 and window < h:
        a = 1
        while h * bounce > window:
            a += 2
            h = h*bounce
        else:
            return a
    else:
        return -1

5. 钞票25 50 100, 门票一张25   输入一个列表 ,看看能不能找钱给顾客

test.assert_equals(tickets([25, 25, 50]), "YES")
test.assert_equals(tickets([25, 100]), "NO")

def tickets(people):
  till = {100.0:0, 50.0:0, 25.0:0}

  for paid in people:
    till[paid] += 1
    change = paid-25.0
    
    for bill in (50,25):
      while (bill <= change and till[bill] > 0):
        till[bill] -= 1
        change -= bill

    if change != 0:
      return 'NO'
        
  return 'YES'

猜你喜欢

转载自www.cnblogs.com/adelinebao/p/13200926.html