forked from tdwojak/Python2018
labs02
labs02
This commit is contained in:
parent
41207a9a35
commit
4d8e263857
22
zadaniedomowe1/labs02/task07.py
Normal file
22
zadaniedomowe1/labs02/task07.py
Normal file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Napisz funkcję char_sum, która dla zadanego łańcucha zwraca
|
||||
sumę kodów ASCII znaków.
|
||||
"""
|
||||
def char_sum(text):
|
||||
return sum(ord(i) for i in text)
|
||||
|
||||
def tests(f):
|
||||
inputs = [["this is a string"], ["this is another string"]]
|
||||
outputs = [1516, 2172]
|
||||
|
||||
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(char_sum))
|
29
zadaniedomowe1/labs02/task08.py
Normal file
29
zadaniedomowe1/labs02/task08.py
Normal 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.
|
||||
"""
|
||||
|
||||
def sum_div35(n):
|
||||
i, suma = 1, 0
|
||||
for i in range(1, n):
|
||||
if not (i % 3 and i % 5):
|
||||
suma += i
|
||||
i += 1
|
||||
return suma
|
||||
sum_div35(10)
|
||||
|
||||
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))
|
25
zadaniedomowe1/labs02/task09.py
Normal file
25
zadaniedomowe1/labs02/task09.py
Normal file
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Napisz funkcję leet_speak, która podmienia w podanym napisie niektóre litery
|
||||
na podobnie wyglądające cyfry: 'e' na '3', 'l' na '1', 'o' na '0', 't' na '7'.
|
||||
Np. leet('leet') powinno zwrócić '1337'.
|
||||
"""
|
||||
|
||||
|
||||
def leet_speak(tekst):
|
||||
return tekst.replace('o', '0').replace('l', '1').replace('e', '3').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))
|
38
zadaniedomowe1/labs02/task10.py
Normal file
38
zadaniedomowe1/labs02/task10.py
Normal file
@ -0,0 +1,38 @@
|
||||
#!/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(text):
|
||||
value = ''
|
||||
b = True
|
||||
for i in text:
|
||||
value += i.upper() if b else i.lower()
|
||||
b = not b
|
||||
if i.isupper() == True:
|
||||
return text
|
||||
else:
|
||||
return value
|
||||
|
||||
|
||||
pokemon_speak('pokemon')
|
||||
pokemon_speak('do not want')
|
||||
pokemon_speak('POKEMON')
|
||||
|
||||
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))
|
28
zadaniedomowe1/labs02/task11.py
Normal file
28
zadaniedomowe1/labs02/task11.py
Normal file
@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
"""
|
||||
Napisz funkcję common_chars(string1, string2), która zwraca alfabetycznie
|
||||
uporządkowaną listę wspólnych liter z lańcuchów string1 i string2.
|
||||
Oba napisy będą składać się wyłacznie z małych liter.
|
||||
"""
|
||||
|
||||
def common_chars(string1, string2):
|
||||
txt1 = string1.replace(' ','')
|
||||
txt2 = string2.replace(' ','')
|
||||
|
||||
return list(''.join(sorted(set(txt1) & set(txt2))))
|
||||
|
||||
def tests(f):
|
||||
inputs = [["this is a string", "ala ma kota"]]
|
||||
outputs = [['a', 't']]
|
||||
|
||||
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(common_chars))
|
Loading…
Reference in New Issue
Block a user