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)
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(run(add,100,100,1000,500,300),2000)
test.assert_equals(run(minum,1000,100),900)
test.assert_equals(run(multiply,12,2),24)
test.assert_equals(run(divide,100,25),4)
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"
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(where([1,2,3,6,7,8,0],3),3)
That might make me feel comfortable ...
flip_the_number = lambda num:num[::-1]
def flip_the_number(num):return ''.join(reversed(num))- flip_the_number = lambda num:num[::-1]
import codewars_test as Test Test.assert_equals(flip_the_number("12345"), "54321"); Test.assert_equals(flip_the_number("1973572"), "2753791");
- import codewars_test as Test
- Test.assert_equals(flip_the_number("12345"), "54321");
- Test.assert_equals(flip_the_number("1973572"), "2753791");
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]
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(how_many(10,2),[4,6])
test.assert_equals(how_many(21,3),[9,12])