2020.06.27

Posted adelinebao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于2020.06.27的主要内容,如果未能解决你的问题,请参考以下文章

leetcode每日一题(2020-06-27):41. 缺失的第一个正数

JS - 2020-01-01T00:00:00.000000Z 日期格式转换

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?