Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
cb16eadda1 | |||
|
95733aa1aa |
32
homework/task07.py
Normal file
32
homework/task07.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#!/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):
|
||||||
|
suma_ascii = 0
|
||||||
|
lista_ascii = []
|
||||||
|
# lista_znaków = list(text)
|
||||||
|
for znak in text:
|
||||||
|
znak_ascii = ord(znak)
|
||||||
|
lista_ascii.append(znak_ascii)
|
||||||
|
for znak2 in lista_ascii:
|
||||||
|
suma_ascii = suma_ascii + znak2
|
||||||
|
return suma_ascii
|
||||||
|
# print('suma ASCII: ', suma_ascii)
|
||||||
|
|
||||||
|
|
||||||
|
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))
|
31
homework/task08.py
Normal file
31
homework/task08.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#!/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):
|
||||||
|
lista_wejsciowa = range(n)
|
||||||
|
lista_wyjsciowa = []
|
||||||
|
suma = 0
|
||||||
|
for i in lista_wejsciowa:
|
||||||
|
if i % 3 == 0 or i % 5 == 0:
|
||||||
|
lista_wyjsciowa.append(i)
|
||||||
|
for j in lista_wyjsciowa:
|
||||||
|
suma = suma + j
|
||||||
|
return suma
|
||||||
|
|
||||||
|
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))
|
41
homework/task09.py
Normal file
41
homework/task09.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#!/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(text):
|
||||||
|
strin = []
|
||||||
|
for znak in text:
|
||||||
|
if znak == 'e':
|
||||||
|
znak1 = '3'
|
||||||
|
strin.append(znak1)
|
||||||
|
elif znak == 'l':
|
||||||
|
znak1 = '1'
|
||||||
|
strin.append(znak1)
|
||||||
|
elif znak == 'o':
|
||||||
|
znak1 = '0'
|
||||||
|
strin.append(znak1)
|
||||||
|
elif znak == 't':
|
||||||
|
znak1 = '7'
|
||||||
|
strin.append(znak1)
|
||||||
|
else:
|
||||||
|
strin.append(znak)
|
||||||
|
return ''.join(strin)
|
||||||
|
|
||||||
|
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))
|
34
homework/task10.py
Normal file
34
homework/task10.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#!/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):
|
||||||
|
i = 0
|
||||||
|
new_string = []
|
||||||
|
for j in text:
|
||||||
|
if i % 2 == 0:
|
||||||
|
new_string.append(j.upper())
|
||||||
|
else:
|
||||||
|
new_string.append(j)
|
||||||
|
i = i + 1
|
||||||
|
return ''.join(new_string)
|
||||||
|
|
||||||
|
|
||||||
|
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))
|
42
homework/task11.py
Normal file
42
homework/task11.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#!/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):
|
||||||
|
output_list1 = []
|
||||||
|
output_list2 = []
|
||||||
|
for j in string1:
|
||||||
|
if j in string2:
|
||||||
|
output_list1.append(j)
|
||||||
|
#print(output_list1)
|
||||||
|
for i in output_list1:
|
||||||
|
if i not in output_list2:
|
||||||
|
output_list2.append(i)
|
||||||
|
for k in output_list2:
|
||||||
|
if k == ' ':
|
||||||
|
output_list2.remove(k)
|
||||||
|
#return output_list2
|
||||||
|
output_list3 = sorted(output_list2)
|
||||||
|
#print(output_list3)
|
||||||
|
return output_list3
|
||||||
|
|
||||||
|
#common_chars("this is a string", "ala ma kota")
|
||||||
|
|
||||||
|
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))
|
@ -8,7 +8,7 @@ która zawiera tylko elementy z list o parzystych indeksach.
|
|||||||
|
|
||||||
def even_elements(lista):
|
def even_elements(lista):
|
||||||
pass
|
pass
|
||||||
|
return lista[::2]
|
||||||
|
|
||||||
def tests(f):
|
def tests(f):
|
||||||
inputs = [[[1, 2, 3, 4, 5, 6]], [[]], [[41]]]
|
inputs = [[[1, 2, 3, 4, 5, 6]], [[]], [[41]]]
|
||||||
|
@ -5,8 +5,14 @@
|
|||||||
Napisz funkcję days_in_year zwracającą liczbę dni w roku (365 albo 366).
|
Napisz funkcję days_in_year zwracającą liczbę dni w roku (365 albo 366).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def days_in_year(days):
|
def days_in_year(year):
|
||||||
pass
|
pass
|
||||||
|
if year % 400 == 0:
|
||||||
|
return 366
|
||||||
|
elif year % 4 == 0 and year % 100 != 0:
|
||||||
|
return 366
|
||||||
|
else:
|
||||||
|
return 365
|
||||||
|
|
||||||
def tests(f):
|
def tests(f):
|
||||||
inputs = [[2015], [2012], [1900], [2400], [1977]]
|
inputs = [[2015], [2012], [1900], [2400], [1977]]
|
||||||
|
@ -11,12 +11,18 @@ litery. (OOV = out of vocabulary) (W pythonie istnieje struktura danych tak
|
|||||||
jak 'set', która przechowuje elementy bez powtórzeń.)
|
jak 'set', która przechowuje elementy bez powtórzeń.)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
text = "this is a string , which i will use for string testing"
|
||||||
|
vocab = [',', 'this', 'is', 'a', 'which', 'for', 'will', 'i']
|
||||||
|
|
||||||
def oov(text, vocab):
|
def oov(text, vocab):
|
||||||
pass
|
pass
|
||||||
|
temp_text=text.split(' ')
|
||||||
|
temp_vocab=vocab.split(' ')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
def tests(f):
|
def tests(f):
|
||||||
inputs = [("this is a string , which i will use for string testing",
|
inputs = [("this is a string , which i will use for string testing",
|
||||||
[',', 'this', 'is', 'a', 'which', 'for', 'will', 'i'])]
|
[',', 'this', 'is', 'a', 'which', 'for', 'will', 'i'])]
|
||||||
@ -30,3 +36,4 @@ def tests(f):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(tests(oov))
|
print(tests(oov))
|
||||||
|
"""
|
@ -7,6 +7,7 @@ def suma(a, b):
|
|||||||
Napisz funkcję, która zwraca sumę elementów.
|
Napisz funkcję, która zwraca sumę elementów.
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
return a + b
|
||||||
|
|
||||||
def tests(f):
|
def tests(f):
|
||||||
inputs = [(2, 3), (0, 0), (1, 1)]
|
inputs = [(2, 3), (0, 0), (1, 1)]
|
||||||
|
Loading…
Reference in New Issue
Block a user