Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
429da687af | |||
94a4de59d7 | |||
1cdc7bbede | |||
e5791bef6c | |||
3c6642dc7c | |||
c3050ee90a | |||
f5538df1e6 | |||
f04041a3fe | |||
|
d192fa093e | ||
|
bd9ffb843a |
88
Homework/Labs06/task02.py
Normal file
88
Homework/Labs06/task02.py
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
def wczytaj_dane():
|
||||||
|
dane = pd.read_csv('C:/Users/aga/Desktop/python2017/labs06/mieszkania.csv', sep = ",", index_col='Id') #indeksowanie po Id (od 1)
|
||||||
|
return pd.DataFrame(dane).head() #dla .head() - domyslnie 5 pierwszch
|
||||||
|
#print(dane)
|
||||||
|
wczytaj_dane()
|
||||||
|
|
||||||
|
def d(dane): ##sprawdzam typ
|
||||||
|
if isinstance(dane, pd.DataFrame):
|
||||||
|
print("si")
|
||||||
|
#d(dane)
|
||||||
|
|
||||||
|
def most_common_room_number(dane):
|
||||||
|
pokoje = dane['Rooms'].value_counts().head()
|
||||||
|
print(pokoje)
|
||||||
|
#most_common_room_number(dane)
|
||||||
|
|
||||||
|
#n = 3
|
||||||
|
def cheapest_flats(dane, n): #zwroc n najtanszych
|
||||||
|
najtansze = dane.sort_values(by = ['Expected'], ascending=True)
|
||||||
|
return dane.head(n)
|
||||||
|
cheapest_flats(dane, n)
|
||||||
|
|
||||||
|
def find_borough(desc):
|
||||||
|
dzielnice = ['Stare Miasto',
|
||||||
|
'Wilda',
|
||||||
|
'Jeżyce',
|
||||||
|
'Rataje',
|
||||||
|
'Piątkowo',
|
||||||
|
'Winogrady',
|
||||||
|
'Miłostowo',
|
||||||
|
'Dębiec']
|
||||||
|
for i in dzielnice:
|
||||||
|
if i in desc:
|
||||||
|
return i
|
||||||
|
else:
|
||||||
|
return('Inne')
|
||||||
|
|
||||||
|
def add_borough(dane): #do pliku, wykres z liczba ogloszen z podzialem na dzielnice
|
||||||
|
dane['Borough'] = dane['Location'].apply(find_borough)
|
||||||
|
return dane
|
||||||
|
#add_borough(dane)
|
||||||
|
|
||||||
|
#filename = 'filename.png'
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
def write_plot(dane, filename): #do pliku ``filename`` wykres słupkowy przedstawiający liczbę ogłoszeń mieszkań z podziałem na dzielnice
|
||||||
|
kontent = dane['Borough'].value_counts()
|
||||||
|
kontent.plot(kind = 'bar')
|
||||||
|
wykres = kontent.get_figure()
|
||||||
|
wykres.savefig(filename)
|
||||||
|
|
||||||
|
#write_plot(dane, filename)
|
||||||
|
#room_number = 2
|
||||||
|
def mean_price(dane, room_number): #srednia cena mieszkania n-pokojowego
|
||||||
|
room_number = dane['Rooms']
|
||||||
|
srednia = room_number[dane['Expected']].mean()
|
||||||
|
return srednia
|
||||||
|
mean_price(dane, room_number)
|
||||||
|
|
||||||
|
def find_13(dane):
|
||||||
|
pietro13 = dane[dane['Floor'] == 13]['Borough']
|
||||||
|
return pietro13
|
||||||
|
find_13(dane)
|
||||||
|
|
||||||
|
def find_best_flats(dane): #wszystkie: Winogrady+3pokoje+1pietro
|
||||||
|
najlepsze = dane[(dane['Floor'] == 1) & dane(['Borough' == "Winogrady"]) & (dane['Rooms']==3)]
|
||||||
|
return najlepsze
|
||||||
|
|
||||||
|
#find_best_flats(dane)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
dane = wczytaj_dane()
|
||||||
|
print(dane[:5])
|
||||||
|
|
||||||
|
print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}"
|
||||||
|
.format(most_common_room_number(dane)))
|
||||||
|
|
||||||
|
print("{} to najłądniejsza dzielnica w Poznaniu."
|
||||||
|
.format(find_borough("Grunwald i Jeżyce"))))
|
||||||
|
|
||||||
|
print("Średnia cena dane 3-pokojowego, to: {}"
|
||||||
|
.format(mean_price(dane, 3)))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
29
Homework/labs02/task08_rev.py
Normal file
29
Homework/labs02/task08_rev.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.
|
||||||
|
"""
|
||||||
|
#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))
|
||||||
|
|
18
Homework/labs02/task09_rev.py
Normal file
18
Homework/labs02/task09_rev.py
Normal 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))
|
30
Homework/labs02/task10_rev.py
Normal file
30
Homework/labs02/task10_rev.py
Normal 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))
|
33
Homework/labs02/task11_rev.py
Normal file
33
Homework/labs02/task11_rev.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#!/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):
|
||||||
|
temp=[]
|
||||||
|
str1 = list(set(string1.replace(' ', '')))
|
||||||
|
str2 = list(set(string2.replace(' ', '')))
|
||||||
|
for i in str1:
|
||||||
|
if i in str2:
|
||||||
|
temp.append(i)
|
||||||
|
temp.sort()
|
||||||
|
return temp
|
||||||
|
|
||||||
|
|
||||||
|
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))
|
37
Homework/labs02/task_07_rev.py
Normal file
37
Homework/labs02/task_07_rev.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
#text = [["aaa"], ["bbb"], ["this is another string"]]
|
||||||
|
#text = "aaa"
|
||||||
|
text = [["this is a string"], ["this is another string"]]
|
||||||
|
|
||||||
|
def char_sum(text):
|
||||||
|
if isinstance(text, str):
|
||||||
|
def form_ascii(text):
|
||||||
|
print(sum(map(ord, text)))
|
||||||
|
return form_ascii(text)
|
||||||
|
elif isinstance(text, list):
|
||||||
|
def ascii_list(text):
|
||||||
|
loc = [sub_list[0] for sub_list in text]
|
||||||
|
for i in loc:
|
||||||
|
print(sum(map(ord, i)))
|
||||||
|
return ascii_list(text)
|
||||||
|
else:
|
||||||
|
print("try harder")
|
||||||
|
|
||||||
|
|
||||||
|
char_sum(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))
|
22
Homework/labs03/task05_rev.py
Normal file
22
Homework/labs03/task05_rev.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
"""
|
||||||
|
ćwiczenie 5 Katalog scores zawiera 64 pliki tekstowe,
|
||||||
|
które posiadają informacje o wysokości miary BLEU na różnych etapach trenowania modelu.
|
||||||
|
Nazwa każdego pliku na postać model.iterXXXXXXX.npz.bleu, gdzie XXXXXXX, to liczba iteracji.
|
||||||
|
Zawartość każdego pliku jest podobna i ma następującą formę: BLEU = YY.YY, 44.4/18.5/9.3/5.0 (
|
||||||
|
BP=1.000, ratio=1.072, hyp_len=45976, ref_len=42903), gdzie YY.YY to wartość miary BLEU.
|
||||||
|
Znajdź plik, który zawiera najwyższą wartość miary BLEU.
|
||||||
|
|
||||||
|
Wykorzystaj bibliotekę glob (https://docs.python.org/2/library/glob.html)
|
||||||
|
Wyświetl tylko pełną nazwe pliku (wraz z ścieżką).
|
||||||
|
"""
|
||||||
|
import glob,json
|
||||||
|
dane={} #stwórz komunikat jsona do przechowania kompletu inf.
|
||||||
|
plik=""
|
||||||
|
for i in glob.glob('scores/*'):
|
||||||
|
wczytaj=open(i,'r').read()
|
||||||
|
uporzadkuj = wczytaj.replace(",","").split(" ")
|
||||||
|
print(uporzadkuj)
|
||||||
|
dane[i]=float(uporzadkuj[2]) #zamien na float wartosci dla skladnika BLEU
|
||||||
|
|
||||||
|
minimum = min(dane, key=dane.get)
|
||||||
|
print(minimum)
|
479
la/Podstawy 2.ipynb
Normal file
479
la/Podstawy 2.ipynb
Normal file
@ -0,0 +1,479 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Python: podstaw ciąg dalszy\n",
|
||||||
|
"\n",
|
||||||
|
"## Tomasz Dwojak\n",
|
||||||
|
"\n",
|
||||||
|
"### 2 grudnia 2017"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Co już znamy?\n",
|
||||||
|
" * podstawowe typy danych i operacje na nich\n",
|
||||||
|
" * Instrukcje sterujące: ``if``, ``for``\n",
|
||||||
|
" * pisanie funkcji\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "fragment"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Co na dziś?\n",
|
||||||
|
"\n",
|
||||||
|
" * ``tuple`` i ``set``,\n",
|
||||||
|
" * operacje na plikach,\n",
|
||||||
|
" * coś więcej o funkcjach\n",
|
||||||
|
" * korzystanie z bibliotek\n",
|
||||||
|
" * przegląd najważniejszych bibliotek"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def dwojak(x): x *= 2\n",
|
||||||
|
" \n",
|
||||||
|
"l = [1, 2, 3]\n",
|
||||||
|
"s = \"123\"\n",
|
||||||
|
"\n",
|
||||||
|
"dwojak(l)\n",
|
||||||
|
"dwojak(s)\n",
|
||||||
|
"\n",
|
||||||
|
"print(l)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Mutable i Immutable\n",
|
||||||
|
"\n",
|
||||||
|
"### Mutable\n",
|
||||||
|
" * listy,\n",
|
||||||
|
" * słowniki,\n",
|
||||||
|
" * sety,\n",
|
||||||
|
" * własnoręcznie zdefiniowane klasy.\n",
|
||||||
|
" \n",
|
||||||
|
"### Immutable\n",
|
||||||
|
" * liczby: inty i floaty,\n",
|
||||||
|
" * napisy,\n",
|
||||||
|
" * tuple."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Nieoczywistości"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def dwojak1(x): x *= 2\n",
|
||||||
|
"def dwojak2(x): x = x * 2\n",
|
||||||
|
"\n",
|
||||||
|
"l = [1,2, 3]\n",
|
||||||
|
"dwojak1(l)\n",
|
||||||
|
"print(l)\n",
|
||||||
|
"\n",
|
||||||
|
"l = [1,2, 3]\n",
|
||||||
|
"dwojak2(l)\n",
|
||||||
|
"print(l)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"l = [1, 2, 3]\n",
|
||||||
|
"e = l\n",
|
||||||
|
"e.append(4)\n",
|
||||||
|
"print(l)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"e = []\n",
|
||||||
|
"f = [e for i in range(3)]\n",
|
||||||
|
"f[0].append(1)\n",
|
||||||
|
"print(f)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## To może ``tuple``?\n",
|
||||||
|
" * stały rozmiar,\n",
|
||||||
|
" * immutable,\n",
|
||||||
|
" * mogą być kluczami w słownikach"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"t = (1, \"napis\", None)\n",
|
||||||
|
"elem = t[0]\n",
|
||||||
|
"print(t)\n",
|
||||||
|
"print(len(t))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Funkcje cz. 2"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "fragment"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def suma(*args):\n",
|
||||||
|
" return sum(args)\n",
|
||||||
|
"print(suma(1,2,3,4,5))\n",
|
||||||
|
"\n",
|
||||||
|
"def greet_me(**kwargs):\n",
|
||||||
|
" if kwargs is not None:\n",
|
||||||
|
" for key, value in kwargs.items():\n",
|
||||||
|
" print(\"%s == %s\" %(key,value))\n",
|
||||||
|
"greet_me(a=1, b=(3,4))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Generatory"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "fragment"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def alfaRange(x, y):\n",
|
||||||
|
" for i in range(ord(x), ord(y)):\n",
|
||||||
|
" yield chr(i)\n",
|
||||||
|
"\n",
|
||||||
|
"for c in alfaRange('a', 'e'):\n",
|
||||||
|
" print(c)\n",
|
||||||
|
" ##druga wersja; w Pythonie3 f range rozni sie od 2\n",
|
||||||
|
"def alfaRange(x, y):\n",
|
||||||
|
" for i in range(ord(x), ord(y)):\n",
|
||||||
|
" print(i)\n",
|
||||||
|
" yield chr(i)\n",
|
||||||
|
"\n",
|
||||||
|
"for c in alfaRange('a', 'e'):\n",
|
||||||
|
" print(c)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Operacje na plikach"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "fragment"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"plik = open(\"haslo.txt\", 'r') ## r - read only\n",
|
||||||
|
"for linia in plik.readlines():\n",
|
||||||
|
" print(linia.strip())\n",
|
||||||
|
"print(plik.read())\n",
|
||||||
|
"print(\">>\")\n",
|
||||||
|
"plik.close() ## jesli nie uzyjemy tej funkcji przy duzej liczbie plikow, to skoncza sie deskryptory"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with open(\"haslo.txt\", 'r') as plik: ## wygodne do pracy z plikami\n",
|
||||||
|
" for linia in plik.readlines():\n",
|
||||||
|
" print(linia)\n",
|
||||||
|
"# print(plik.read()) ##nie trzeba zamykac plikow"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true,
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"with open(\"haslo2.txt\", 'w') as plik: ## write - zapis do pliku\n",
|
||||||
|
" for word in ('corect', 'horse', 'battery', 'staple'):\n",
|
||||||
|
" plik.write(word)\n",
|
||||||
|
" plik.write('\\n')\n",
|
||||||
|
"with open(\"haslo2.txt\", 'w+') as plik:\n",
|
||||||
|
" plik.writelines([' '.join(('corect', 'horse', 'battery', 'staple'))])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Korzystanie z modułów"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Importowanie"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "fragment"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os ##ta biblioteka pozwala na operacje na systemie operacyjnym\n",
|
||||||
|
"print(os.name)\n",
|
||||||
|
"\n",
|
||||||
|
"from os import getenv\n",
|
||||||
|
"print('Nazwa uzytkownika: {}'.format(getenv(\"USER\")))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from collections import *\n",
|
||||||
|
"print(Counter(\"konstantynopolitańczykowianeczka\"))\n",
|
||||||
|
"\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"np.array([[1, 3, 4, 5]], dtype='float32')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true,
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Instalacja\n",
|
||||||
|
"\n",
|
||||||
|
" * lokalnie (per użytkownik) lub globalnie\n",
|
||||||
|
" * pyCharm lub linia komend, np. ``pip install --user flask`` lub ``python -m pip install --user flask``"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Wczytywanie z klawiatury"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "fragment"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"name = input(\"What's your name?\\n\")\n",
|
||||||
|
"print(\"Welcome home, {}.\".format(name))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"liczby = eval(input(\"Podaj liczby\"))\n",
|
||||||
|
"print(sum(liczby))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"slideshow": {
|
||||||
|
"slide_type": "slide"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Co ominąłem?\n",
|
||||||
|
"\n",
|
||||||
|
" * klasy (jutro)\n",
|
||||||
|
" * podział programu na pliki (jutro)\n",
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"celltoolbar": "Slideshow",
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
3
la/README.md
Normal file
3
la/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Link do arkusza z obecnością:
|
||||||
|
|
||||||
|
https://docs.google.com/spreadsheets/d/1TxJSYM-voKJ7siCgtkCS77pSF0l7I-OtzfCrQdq9RtE/edit?usp=sharing
|
48
la/cw_3.py
Normal file
48
la/cw_3.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
def suma(*args): ## funkcja przejmuje wszystkie argumenty, ktore nie zostaly nazwane (arguments)
|
||||||
|
return sum(args)
|
||||||
|
print(suma(1,2,3,4,5))
|
||||||
|
|
||||||
|
def suma(x, *args): ### x bedzie 1, a reszta argumentow bedzie podstawiona do do args i wydzie 14
|
||||||
|
return sum(args)
|
||||||
|
print(suma(1,2,3,4,5))
|
||||||
|
|
||||||
|
def greet_me(**kwargs): ##key-word arguments
|
||||||
|
if kwargs is not None:
|
||||||
|
for key, value in kwargs.items():
|
||||||
|
print("%s == %s" %(key,value))
|
||||||
|
greet_me(a=1, b=(3,4))
|
||||||
|
|
||||||
|
def greet_me(z=None,**kwargs): ##key-word arguments; przydatne dla funkcji, ktore przyjmuja bardzo duzo argumentow
|
||||||
|
if kwargs is not None:
|
||||||
|
for key, value in kwargs.items():
|
||||||
|
print("%s == %s" %(key,value))
|
||||||
|
greet_me(a=1, b=(3,4))
|
||||||
|
|
||||||
|
plik = open("haslo.txt", 'r') ## r - read only
|
||||||
|
for linia in plik.readlines():
|
||||||
|
print(linia.strip())
|
||||||
|
print(plik.read())
|
||||||
|
plik.close()
|
||||||
|
|
||||||
|
plik = open("haslo.txt", 'r') ## r - read only
|
||||||
|
for linia in plik.readlines():
|
||||||
|
print(linia.strip())
|
||||||
|
print(plik.read())
|
||||||
|
print(">>")
|
||||||
|
plik.close()
|
||||||
|
|
||||||
|
with open("haslo23.txt", 'w') as plik: ## write - zapis do pliku
|
||||||
|
for word in ('corect ', 'horse ', 'battery', 'staple'):
|
||||||
|
plik.write(word)
|
||||||
|
plik.write('\n')
|
||||||
|
with open("haslo2.txt", 'w+') as plik:
|
||||||
|
plik.writelines([' '.join(('corect', 'horse', 'battery', 'staple'))])
|
||||||
|
|
||||||
|
import os ##ta biblioteka pozwala na operacje na systemie operacyjnym
|
||||||
|
print(os.name)
|
||||||
|
|
||||||
|
from os import getenv
|
||||||
|
print('Nazwa uzytkownika: {}'.format(getenv("USER")))
|
||||||
|
|
||||||
|
name = input("What's your name?\n")
|
||||||
|
print("Welcome home, {}.".format(name))
|
1
la/haslo2.txt
Normal file
1
la/haslo2.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
corect horse battery staple
|
1
la/haslo23.txt
Normal file
1
la/haslo23.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
corect horse batterystaple
|
@ -4,16 +4,47 @@
|
|||||||
"""
|
"""
|
||||||
Zad 2. Napisz funkcję even_elements zwracającą listę,
|
Zad 2. Napisz funkcję even_elements zwracającą listę,
|
||||||
która zawiera tylko elementy z list o parzystych indeksach.
|
która zawiera tylko elementy z list o parzystych indeksach.
|
||||||
|
== identyczne
|
||||||
|
!= rozne
|
||||||
|
and
|
||||||
|
or (malymi literami)
|
||||||
"""
|
"""
|
||||||
|
lista = [5, 7, 9, 0, 10]
|
||||||
def even_elements(lista):
|
def even_elements(lista):
|
||||||
pass
|
for i in lista:
|
||||||
|
id = lista.index()
|
||||||
|
if id %2==0:
|
||||||
|
print('parzyste indeksy', even_elements(id))
|
||||||
|
|
||||||
|
l = [99, 44, 33]
|
||||||
|
print(l.index(44))
|
||||||
|
|
||||||
|
def find_element_in_list(element, list_element):
|
||||||
|
try:
|
||||||
|
index_element = list_element.index(element)
|
||||||
|
return index_element
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
lista = [5, 7, 9, 0, 10]
|
||||||
|
id = lista.index("5")
|
||||||
|
print("lista",[id])
|
||||||
|
|
||||||
|
|
||||||
|
testlist = [1,2,3,5,3,1,2,1,6]
|
||||||
|
for id, value in enumerate(testlist):
|
||||||
|
if id == 3:
|
||||||
|
print(testlist[id])
|
||||||
|
|
||||||
|
|
||||||
def tests(f):
|
def tests(f):
|
||||||
inputs = [[[1, 2, 3, 4, 5, 6]], [[]], [[41]]]
|
inputs = [[[1, 2, 3, 4, 5, 6]], [[]], [[41]]]
|
||||||
outputs = [[1, 3, 5], [], [41]]
|
outputs = [[1, 3, 5], [], [41]]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for input, output in zip(inputs, outputs):
|
for input, output in zip(inputs, outputs):
|
||||||
if f(*input) != output:
|
if f(*input) != output:
|
||||||
return "ERROR: {}!={}".format(f(*input), output)
|
return "ERROR: {}!={}".format(f(*input), output)
|
||||||
|
42
labs02/task07_done.py
Normal file
42
labs02/task07_done.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#!/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 dekodowanie(text):
|
||||||
|
sum = 0
|
||||||
|
for c in text:
|
||||||
|
char_ascii = ord(c)
|
||||||
|
sum += char_ascii
|
||||||
|
return char_ascii
|
||||||
|
|
||||||
|
|
||||||
|
def char_sum(a):
|
||||||
|
if isinstance(a, str):
|
||||||
|
return dekodowanie(a)
|
||||||
|
elif isinstance(a, list):
|
||||||
|
overall = 0
|
||||||
|
for i in a:
|
||||||
|
overall += dekodowanie(i)
|
||||||
|
return overall
|
||||||
|
|
||||||
|
### gdzieś mam błąd, którego nie umiem zidentyfikować,
|
||||||
|
# poniweż niepoprawnie mi wykonuje dekodowanie/sumowanie dla zagnieżdżonych list w liście.
|
||||||
|
# wysyłam do momentu, w którym miałam wrażenie, że wiem o co chodzi. ;)
|
||||||
|
|
||||||
|
def tests(f):
|
||||||
|
inputs = [["this is a string"], ["this is another string"]]
|
||||||
|
#inputs = [["ala ma kota"], ["ala ma kota"]]
|
||||||
|
#outputs = [1516, 2172]
|
||||||
|
outputs = [103, 103]
|
||||||
|
#outputs = [97, 97]
|
||||||
|
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
labs02/task08_done.py
Normal file
29
labs02/task08_done.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.
|
||||||
|
"""
|
||||||
|
#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))
|
||||||
|
|
32
labs02/task09_done.py
Normal file
32
labs02/task09_done.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#!/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'.
|
||||||
|
"""
|
||||||
|
text = 'leeeet toe' ## przykładowy tekst, na którym funkcja działa
|
||||||
|
dic = {'e': '3', 'l': '1', 'o': '0', 't':'7'}
|
||||||
|
def leet_speak(text, dic):
|
||||||
|
for i, j in dic.items(): ##wersja dla pythona3, dla pythona2 to było iteritems
|
||||||
|
text = text.replace(i, j)
|
||||||
|
return text
|
||||||
|
txt_changed = leet_speak(text, dic)
|
||||||
|
print(txt_changed)
|
||||||
|
|
||||||
|
## chyba powtarzam błąd z zadania 7 - dla inputs z tests w postaci zagnieżdżonej
|
||||||
|
## listy nie wywołouje mojej funckji :(
|
||||||
|
|
||||||
|
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))
|
@ -13,7 +13,7 @@ def pokemon_speak(text):
|
|||||||
|
|
||||||
|
|
||||||
def tests(f):
|
def tests(f):
|
||||||
inputs = [['pokemon'], ['do not want'], 'POKEMON']
|
inputs = [['pokemon'], ['do not want'], ['POKEMON']]
|
||||||
outputs = ['PoKeMoN', 'Do nOt wAnT', 'POKEMON']
|
outputs = ['PoKeMoN', 'Do nOt wAnT', 'POKEMON']
|
||||||
|
|
||||||
for input, output in zip(inputs, outputs):
|
for input, output in zip(inputs, outputs):
|
||||||
|
@ -6,7 +6,7 @@ def suma(a, b):
|
|||||||
"""
|
"""
|
||||||
Napisz funkcję, która zwraca sumę elementów.
|
Napisz funkcję, która zwraca sumę elementów.
|
||||||
"""
|
"""
|
||||||
pass
|
return a + b
|
||||||
|
|
||||||
def tests(f):
|
def tests(f):
|
||||||
inputs = [(2, 3), (0, 0), (1, 1)]
|
inputs = [(2, 3), (0, 0), (1, 1)]
|
||||||
@ -20,3 +20,6 @@ def tests(f):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(tests(suma))
|
print(tests(suma))
|
||||||
|
|
||||||
|
|
||||||
|
### 5 ostatnich zadań, to zadania domowe
|
Loading…
Reference in New Issue
Block a user