Zad 2 and 3

This commit is contained in:
s45158 2017-12-14 16:45:47 +01:00
parent 5444026df7
commit 151e3a09b6
6 changed files with 179 additions and 198 deletions

View File

@ -13,7 +13,13 @@ jak 'set', która przechowuje elementy bez powtórzeń.)
def oov(text, vocab):
pass
def test(self):
"""Prosty test."""
text = "This is a string , which I will use for string testing"
vocab = [',', 'this', 'is', 'a', 'which', 'for', 'will', 'I']
oo_voc = ['string', 'testing', 'use']
self.assertEqual(set(oov(text, vocab)), set(oo_voc))

View File

@ -10,7 +10,21 @@ np. odległość pomiędzy punktami (0, 0, 0) i (3, 4, 0) jest równa 5.
"""
def euclidean_distance(x, y):
pass
def test_special_cases(self):
"""Testy przypadków szczególnych."""
self.assertAlmostEqual(
euclidean_distance((0.0, 0.0, 0.0), (0.0, 0.0, 0.0)), 0.0)
self.assertAlmostEqual(
euclidean_distance((3.6, -1.7, 0.3), (3.6, -1.7, 0.3)), 0.0)
self.assertAlmostEqual(
euclidean_distance((2.3, 4.3, -7.5), (2.3, 8.5, -7.5)), 4.2)
def test_regular_cases(self):
"""Testy dla zwykłych przypadków"""
self.assertAlmostEqual(
euclidean_distance((0.0, 0.0, 0.0), (0.0, 4.0, 3.0)), 5.0)
self.assertAlmostEqual(
euclidean_distance((2.4, -5.1, 3.0), (5.3, 2.1, 10.0)), 10.4523, 3)
def tests(f):
inputs = [[(2.3, 4.3, -7.5), (2.3, 8.5, -7.5)]]

View File

@ -10,7 +10,21 @@ ma być zwracany napis "It's not a Big 'No!'".
"""
def big_no(n):
pass
def test_special_cases(self):
"""Testy przypadków szczególnych."""
self.assertEqual(big_no(0), "It's not a Big 'No!'")
self.assertEqual(big_no(1), "It's not a Big 'No!'")
self.assertEqual(big_no(2), "It's not a Big 'No!'")
self.assertEqual(big_no(4), "It's not a Big 'No!'")
self.assertEqual(big_no(5), "NOOOOO!")
def test_regular_cases(self):
"""Testy dla zwykłych przypadków"""
self.assertEqual(big_no(6), "NOOOOOO!")
self.assertEqual(big_no(33), "N" + ("O" * 33) + "!")
def tests(f):
inputs = [[5], [6], [2]]

View File

@ -49,34 +49,22 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"[1, 2, 3, 1, 2, 3]\n",
"123\n"
]
}
],
"outputs": [],
"source": [
"def dwojak(x): \n",
" x *= 2\n",
" return x\n",
"def dwojak(x): x *= 2\n",
" \n",
"l = [1, 2, 3]\n",
"s = \"123\"\n",
"\n",
"dwojak(l)\n",
"dwojak(s)\n",
"print(dwojak(1))\n",
"\n",
"print(l)\n",
"print(s)"
]
@ -116,28 +104,16 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 1, 2, 3]\n",
"F: [1, 2, 3, 1, 2, 3]\n",
"[1, 2, 3]\n"
]
}
],
"outputs": [],
"source": [
"def dwojak1(x): x *= 2\n",
"def dwojak2(x): \n",
" x = x * 2\n",
" print(\"F:\", x)\n",
"def dwojak2(x): x = x * 2\n",
"\n",
"l = [1,2, 3]\n",
"dwojak1(l)\n",
@ -150,47 +126,29 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3]\n",
"[1, 2, 3, 4]\n"
]
}
],
"outputs": [],
"source": [
"l = [1, 2, 3]\n",
"e = l[:]\n",
"e = l\n",
"e.append(4)\n",
"print(l)\n",
"print(e)"
"print(l)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1], [1], [1]]\n"
]
}
],
"outputs": [],
"source": [
"e = []\n",
"f = [e for i in range(3)]\n",
@ -214,39 +172,18 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 'napis', [0])\n",
"3\n"
]
},
{
"ename": "TypeError",
"evalue": "unhashable type: 'list'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-25-2bd2fa17fbf5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
],
"outputs": [],
"source": [
"t = (1, \"napis\", [])\n",
"t[-1].append(0)\n",
"t = (1, \"napis\", None)\n",
"elem = t[0]\n",
"print(t)\n",
"print(len(t))\n",
"print({t: None})"
"print(len(t))"
]
},
{
@ -262,29 +199,19 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n",
"a == 1\n",
"b == (3, 4)\n"
]
}
],
"outputs": [],
"source": [
"def suma(*args):\n",
" return sum(args)\n",
"print(suma(1,2,3,4,5))\n",
"\n",
"def greet_me(z=None,**kwargs):\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",
@ -304,32 +231,16 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"97\n",
"a\n",
"98\n",
"b\n",
"99\n",
"c\n",
"100\n",
"d\n"
]
}
],
"outputs": [],
"source": [
"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",
@ -349,74 +260,40 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"W Paryżu najlepsze kasztany są na placu Pigalle\n",
"Zuzanna lubi je tylko jesienią.\n",
"\n",
">>\n"
]
}
],
"outputs": [],
"source": [
"plik = open(\"haslo.txt\", 'r')\n",
"for linia in plik.readlines():\n",
" print(linia.strip())\n",
"print(plik.read())\n",
"print(\">>\")\n",
"plik.close()"
]
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"W Paryżu najlepsze kasztany są na placu Pigalle\n",
"\n",
"Zuzanna lubi je tylko jesienią.\n",
"\n"
]
},
{
"ename": "ValueError",
"evalue": "I/O operation on closed file.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-47-f06513c1bbec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mlinia\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mplik\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreadlines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlinia\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mplik\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: I/O operation on closed file."
]
}
],
"outputs": [],
"source": [
"with open(\"haslo.txt\", 'r') as plik:\n",
" for linia in plik.readlines():\n",
" print(linia)\n",
"print(plik.read())"
"# print(plik.read())"
]
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": null,
"metadata": {
"collapsed": true,
"slideshow": {
@ -457,22 +334,13 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"posix\n",
"Nazwa uzytkownika: tomaszd\n"
]
}
],
"outputs": [],
"source": [
"import os\n",
"print(os.name)\n",
@ -483,31 +351,13 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counter({'o': 4, 'n': 4, 'a': 4, 'k': 3, 't': 3, 'y': 2, 'i': 2, 'c': 2, 'z': 2, 's': 1, 'p': 1, 'l': 1, 'ń': 1, 'w': 1, 'e': 1})\n"
]
},
{
"data": {
"text/plain": [
"array([[ 1., 3., 4., 5.]], dtype=float32)"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"from collections import *\n",
"print(Counter(\"konstantynopolitańczykowianeczka\"))\n",
@ -544,23 +394,13 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What's your name?\n",
"Tomasz\n",
"Welcome home, Tomasz.\n"
]
}
],
"outputs": [],
"source": [
"name = input(\"What's your name?\\n\")\n",
"print(\"Welcome home, {}.\".format(name))"

45
labs04/zad2_lab4.py Normal file
View File

@ -0,0 +1,45 @@
'''
**ćwiczenie 2**
Napisz prostą hierarchię klas:
* Klasa bazowa ``Employee``, która będzie zawierać informacje o imieniu i nazwisku pracownika. Ponadto każdy pracownik otrzyma numer ``id``, który będzie unikatowy.
Wykorzystaj do tego atrybut statyczny. Napisz metodę ``get_id``, która zwraca identyfikator pracownika.
* Klasy pochodna: ``Recruiter``, która ma dodatkową mtodę ``recruit``, która jako parament przyjmuje obiekt ``Employee`` i zapisuje jego ``id`` w liście ``self.recruited``.
* Klasa pochodna ``Programmer``. Klasa ``Programmer`` ma przyjąć w konstruktorze podstawowe informacje (imię i nazwisko) oraz obiekt rekturera.
Ponadto stwórz atrybut ``recruiter``, który będzie przechowywać ``id`` rekrutera.
'''
class Employee:
ID_COUNTER = 1000
def __init__(self, first_name, second_name):
self.first_name = first_name
self.second_name = second_name
self.id = Employee.ID_COUNTER
Employee.ID_COUNTER += 1
def get_id(self):
return self.id
class Recruiter(Employee):
def __init__(self, first_name, second_name):
Employee.__init__(self, first_name, second_name)
self.recruited = []
def recruit(self, employee):
self.recruited.append(employee.get_id())
class Programmer(Employee):
def __init__(self, first_name, second_name, recruiter):
Employee.__init__(self, first_name, second_name)
self.recruiter = recruiter.get_id()
employee1 = Employee('John', 'Smith')
print(employee1.get_id())
employee2 = Employee('Bob', 'Marley')
print(employee2.get_id())
recruiter1 = Recruiter('Recruiter', 'Andy')
recruiter1.recruit(employee1)
programmer1 = Programmer('Programmer', 'Frank', recruiter1)

62
labs04/zad3_lab4.py Normal file
View File

@ -0,0 +1,62 @@
'''
**ćwiczenie 3 (zadanie domowe) **
Stwórz klasę ``Point``, która będzie reprezentować punkt w przestrzeni wielowymiarowej:
* Konstruktor ma przyjąc tylko 1 parametr: listę współrzednych. Wykorzystaj funkcję z pierwszego zadania, żeby sprawdzić, czy lista zawiera wyłącznie liczby.
* Napisz metodę add, która dida dwa punkty (dwa wektory) po współrzędnych i zwróci obiekt typu ``Punkt``. Zaimplementuj własny wyjątek ``DimensionError``, który zostaje wyrzucony, jeżeli dodawany punkt ma inny wymiar.
* Napisz metodę ``to\_string``, która zwróci łancuch znakowy, który w czytelny sposób przedstawi punkt.
* Napisz metodę __len__, która zwróci liczbę współrzędnych punktu. Zobacz, czy możesz teraz wywołać funkcję len na obiekcie typy punkt.
* Napisz metodę __str__, która bedzie działać dokładnie tak samo jak metoda ``to_string``. Wyświetl obiekt typy Point korzystając z funkcji print.
'''
class DimensionError(Exception):
pass
def validate_list(list_arg):
return sum([not (type(element) is int or type(element) is float) for element in list_arg]) == 0
class Point:
def __init__(self, coords):
if validate_list(coords):
self.coords = coords[:]
else:
self.coords = [0]
def add(self, point_right):
n = len(self.coords)
if n != len(point_right):
raise DimensionError("Wymiary punktów się nie zgadzają.")
return Point([self.coords[i] + point_right.coords[i] for i in range(n)])
def to_string(self):
return '(' + ', '.join(map(str, self.coords)) + ')'
def __len__(self):
return len(self.coords)
def __str__(self):
return self.to_string()
# testowanie
p1 = Point([1, 2, 3])
p2 = Point([4, 5, 6])
print(p1.to_string())
print(p2.to_string())
print(str(p1.add(p2)))
print(len(p1))
print(Point(['1']))
try:
p1.add(Point([1]))
except:
print('wyjątek')