Ad
Code
Diff
  • def find_max(arr):
        return sorted(arr)[-1]
    • def find_max(arr):
    • return 0
    • return sorted(arr)[-1]

Your task is make a calculator.
its rule is like polish notation.
you only need to deal with add, minum, multiply and divide.
e.g.
run(add,1,2) -> 3
run(minum,10,2,2) -> 6
run(multiply,2,25,2) -> 100
(The number of elements to be calculated is unlimited)

add = lambda *args:sum(args)
def minum(*args):
    xxx = args[0]
    for i in range(1,len(args)):
        xxx = xxx - args[i]
    return xxx
def multiply(*args):
    xxx = 1
    for i in args:
        xxx = xxx * i
    return xxx
def divide(*args):
    xxx = args[0]
    for i in args[1:]:
        xxx = xxx / i
    return xxx
def run(func,*args):
    return func(*args)

Write a function return the index of the given number in a list.(no repeated numbers)

import functools
def where(arr,n):
    for i in range(len(arr)):
        if arr[i] == n:
            return i+1
    return "QAQ"
Code
Diff
  • import functools
    find_max = lambda arr:functools.reduce(lambda x,y:x if x > y else y,arr)
    • def find_max(arr):
    • return 0
    • import functools
    • find_max = lambda arr:functools.reduce(lambda x,y:x if x > y else y,arr)

That might make me feel comfortable ...

Code
Diff
  • flip_the_number = lambda num:num[::-1]
    • def flip_the_number(num):
    • return ''.join(reversed(num))
    • flip_the_number = lambda num:num[::-1]
Code
Diff
  • return_hundred = lambda:len(__import__("__future__").all_feature_names) ** 2
    • return_hundred = lambda: __import__('this').i * len('that')
    • return_hundred = lambda:len(__import__("__future__").all_feature_names) ** 2

There are some volleyballs and basketballs in a school. You get know how many of them there are in total, and the difference of quantities between them. Find the respective quantities.
Write a function that returns the number of volleyballs and the number of basketballs as a list.
Remember that basketballs are always more than volleyballs and the quantities will never be zero.

def how_many(sum,difference):
    return [(sum-difference)/2,(sum+difference)/2]