Python练习题答案: Scramblies【难度:3级】--景越Python编程实例训练营,1000道上机题等你来挑战

Scramblies【难度:3级】:

答案1:

def scramble(s1,s2):
    for c in set(s2):
        if s1.count(c) < s2.count(c):
            return False
    return True

答案2:

def scramble(s1,s2):
    #your code here
    for c in set(s2):
        if s1.count(c) < s2.count(c):
            return False
    return True

答案3:

from collections import Counter
def scramble(s1,s2):
    # Counter basically creates a dictionary of counts and letters
    # Using set subtraction, we know that if anything is left over,
    # something exists in s2 that doesn't exist in s1
    return len(Counter(s2)- Counter(s1)) == 0

答案4:

def scramble(s1, s2):
    return not any(s1.count(char) < s2.count(char) for char in set(s2))

答案5:

(condition for element in iterable)

答案6:

def scramble(s1,s2):
    return all( s1.count(x) >= s2.count(x) for x in set(s2))

答案7:

def scramble(s1,s2):
    return all((s1.count(x) >= s2.count(x) for x in set(s2)))

答案8:

def scramble(s1, s2):
    # your code here
    return all(s1.count(x) >= s2.count(x) for x in set(s2))

答案9:

def scramble(s1, s2):
    return all( s1.count(x) >= s2.count(x) for x in set(s2))
    # your code here​

答案10:

def scramble(s1,s2):
  dct={}
  for l in s1:
    if l not in dct:
      dct[l]=1
    else:
      dct[l] +=1
  for l in s2:
    if l not in dct or dct[l] < 1:
      return False
    else:
      dct[l] -= 1
  return True

答案11:

from collections import Counter

def scramble(s1,s2):
    c1 = Counter(s1)
    c2 = Counter(s2)
    return not (c2 -c1).values()

答案12:

from collections import Counter
scramble = lambda s1, s2: not Counter(s2) - Counter(s1)

答案13:

scramble=lambda a,b,c=__import__('collections').Counter:not c(b)-c(a)

答案14:

from collections import Counter
from operator import sub

def scramble(s1,s2):
    return not sub(*map(Counter, (s2,s1)))

答案15:

from collections import Counter
def scramble(s1,s2): return len(Counter(s2) - Counter(s1)) == 0

答案16:

from collections import Counter
def scramble(s1, s2):
    return (len(Counter (s2)-Counter (s1)) == 0)

答案17:

def scramble(s1, s2):
    from collections import Counter  
    return len(Counter(s2) - Counter(s1)) == 0



Python基础训练营景越Python基础训练营QQ群

在这里插入图片描述
欢迎各位同学加群讨论,一起学习,共同成长!

你可能感兴趣的:(Python编程高级练习题,python面试题库和答案,python编程练习,算法,字符串,数据类型)