1
0
forked from tdwojak/Python2017

tasks 08-10 revised upload

This commit is contained in:
s45153 2018-01-21 23:50:22 +00:00
parent e5791bef6c
commit 1cdc7bbede
3 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,29 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Napisz funkcję sum_div35(n), która zwraca sumę wszystkich liczb podzielnych
przez 3 lub 5 mniejszych niż n.
"""
#pythonowy zakres dla funkcji range: range(3) == [0, 1, 2], czyli <n to range(n)
#example sum in range: for x in range(100, 2001, 3); 100 - 2001 range, divided by 3
#n = 100
def sum_div35(n):
return sum(range(3, n, 3)) + sum(range(5, n, 5)) - sum(range(15, n, 15))
print(sum_div35(n))
def tests(f):
inputs = [[10], [100], [3845]]
outputs = [23, 2318, 3446403]
for input, output in zip(inputs, outputs):
if f(*input) != output:
return "ERROR: {}!={}".format(f(*input), output)
break
return "TESTS PASSED"
if __name__ == "__main__":
print(tests(sum_div35))

View File

@ -0,0 +1,18 @@
## final version L02T09
def leet_speak(words):
return words.replace("e","3").replace("l","1").replace("o","0").replace("t","7")
def tests(f):
inputs = [['leet'], ['do not want']]
outputs = ['1337', 'd0 n07 wan7']
for input, output in zip(inputs, outputs):
if f(*input) != output:
return "ERROR: {}!={}".format(f(*input), output)
break
return "TESTS PASSED"
if __name__ == "__main__":
print(tests(leet_speak))

View File

@ -0,0 +1,30 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Napisz funkcję pokemon_speak, która zamienia w podanym napisie co drugą literę
na wielką. Np. pokemon_speak('pokemon') powinno zwrócić 'PoKeMoN'.
"""
def pokemon_speak(words):
words = str()
for i, l in enumerate(text): #zwraca element i index <- enumerate(iterable, start=0)
if i % 2 == 0:
words += l.upper()
else:
words += l
return words
def tests(f):
inputs = [['pokemon'], ['do not want'], ['POKEMON']]
outputs = ['PoKeMoN', 'Do nOt wAnT', 'POKEMON']
for input, output in zip(inputs, outputs):
if f(*input) != output:
return "ERROR: {}!={}".format(f(*input), output)
break
return "TESTS PASSED"
if __name__ == "__main__":
print(tests(pokemon_speak))