Compare commits

...

4 Commits

Author SHA1 Message Date
s45165 1f09589b05 Merge branch 'master' of https://git.wmi.amu.edu.pl/tdwojak/Python2017
# Conflicts:
#	labs02/test_task.py
2017-12-16 09:13:23 +01:00
s45165 e31fe96cbf Merge branch 'master' of https://git.wmi.amu.edu.pl/tdwojak/Python2017
# Conflicts:
#	labs02/test_task.py
2017-12-16 09:11:17 +01:00
s45165 e89d2834ac done 2017-11-27 21:23:07 +01:00
s45165 9e7ad31f19 done 2017-11-19 15:40:25 +01:00
17 changed files with 281 additions and 24 deletions

View File

@ -7,7 +7,8 @@ która zawiera tylko elementy z list o parzystych indeksach.
"""
def even_elements(lista):
pass
return lista[::2]
def tests(f):

View File

@ -5,9 +5,11 @@
Napisz funkcję days_in_year zwracającą liczbę dni w roku (365 albo 366).
"""
def days_in_year(days):
pass
def days_in_year(year):
if year % 4==0 and year % 100!=0 or year % 400 == 0:
return 366
else:
return 365
def tests(f):
inputs = [[2015], [2012], [1900], [2400], [1977]]
outputs = [365, 366, 365, 366, 365]
@ -20,3 +22,8 @@ def tests(f):
if __name__ == "__main__":
print(tests(days_in_year))
#jest podzielny przez 4, ale nie jest podzielny przez 100
#jest podzielny przez 400

View File

@ -13,9 +13,19 @@ jak 'set', która przechowuje elementy bez powtórzeń.)
def oov(text, vocab):
pass
s = set()
for czajnik in text.split():
if czajnik not in vocab:
s.add(czajnik.lower())
return s
text = [("this is a string , which i will use for string testing",
[',', 'this', 'is', 'a', 'which', 'for', 'will', 'i'])]
vocab = [['string', 'testing', 'use']]
oov("this is a string , which i will use for string testing",
[',', 'this', 'is', 'a', 'which', 'for', 'will', 'i'])
def tests(f):
inputs = [("this is a string , which i will use for string testing",

View File

@ -7,7 +7,10 @@ Jeśli podany argument jest mniejszy od 1 powinna być zwracana wartość 0.
"""
def sum_from_one_to_n(n):
pass
if n >= 1:
return sum(range(1,n+1))
else:
return 0
def tests(f):

View File

@ -9,8 +9,13 @@ trzyelementowe listy liczb zmiennoprzecinkowych.
np. odległość pomiędzy punktami (0, 0, 0) i (3, 4, 0) jest równa 5.
"""
x=[0,0,0]
y=[3,4,0]
def euclidean_distance(x, y):
pass
return ((x[0]-y[0])**2+(x[1]-y[1])**2+(x[2]-y[2])**2)**0.5
euclidean_distance(x,y)
def tests(f):
inputs = [[(2.3, 4.3, -7.5), (2.3, 8.5, -7.5)]]

View File

@ -5,12 +5,22 @@
Napisz funkcję big_no zwracającej tzw. "Big 'NO!'"
(zob. http://tvtropes.org/pmwiki/pmwiki.php/Main/BigNo)
dla zadanej liczby tj. napis typu "NOOOOOOOOOOOOO!", gdzie liczba 'O' ma być
równa podanemu argumentem, przy czym jeśli argument jest mniejszy niż 5,
równa podanemu argumentu, przy czym jeśli argument jest mniejszy niż 5,
ma być zwracany napis "It's not a Big 'No!'".
"""
def big_no(n):
pass
if n<5:
return "It's not a Big 'No!'"
else:
return "N" + 'O'*n + '!'
#big_no(4)
def tests(f):
inputs = [[5], [6], [2]]

View File

@ -5,8 +5,24 @@
Napisz funkcję char_sum, która dla zadanego łańcucha zwraca
sumę kodów ASCII znaków.
"""
#def char_sum(text):
#sum([ord(litera) for litera in list(text)])
tablica=[]
def char_sum(text):
pass
tablica.clear()
for litera in list(text):
tablica.append(ord(litera))
return(sum(tablica))
#tablica.clear()
#char_sum("this is a string")
#char_sum('a')
def tests(f):
inputs = [["this is a string"], ["this is another string"]]

View File

@ -6,8 +6,16 @@ Napisz funkcję sum_div35(n), która zwraca sumę wszystkich liczb podzielnych
przez 3 lub 5 mniejszych niż n.
"""
tablica=[]
def sum_div35(n):
pass
tablica.clear()
for i in range(1,n):
if (i % 3 ==0 or i% 5 ==0):
tablica.append(i)
return(sum(tablica))
#sum_div35(6)
def tests(f):
inputs = [[10], [100], [3845]]

View File

@ -9,7 +9,11 @@ Np. leet('leet') powinno zwrócić '1337'.
def leet_speak(text):
pass
for litera in ["e", "l", "o", "t"]:
if litera in text:
return text.replace("e", "3").replace("l", "1").replace("o", "0").replace("t", "7")
def tests(f):

View File

@ -6,12 +6,62 @@
Napisz funkcję pokemon_speak, która zamienia w podanym napisie co drugą literę
na wielką. Np. pokemon_speak('pokemon') powinno zwrócić 'PoKeMoN'.
"""
#tekst=("edyta")
#def pokemon_speak(tekst):
#for litera in tekst:
#if litera in tekst[::2]:
#return litera.upper()
#def pokemon_speak(text):
#return [text.upper() for x in text[::2]]
#def pokemon_speak(text):
#return [x.upper() for x in text[::2]]
#if litera in text[::2]:
#return text[::2].upper()
#else:
#return text[::1].lower()
#pokemon_speak("edytarenkjacek")
def pokemon_speak(text):
pass
indices=set([0,2,4,6,8,10,12,14,16,18])
#indices=set(index(text[::2]))
return("".join(c.upper() if i in indices else c for i, c in enumerate(text)))
# def fold(s):
# uppers = s[0::2].upper()
# lowers = s[1::2].lower()
# return zip(uppers, lowers)
# def fold(s):
# time_to_upper = True
# result = ""
# for ch in s:
# if time_to_upper:
# result += ch.upper()
# else:
# result += ch.lower()
# time_to_upper = not time_to_upper
# return result
#
# def fold(s):
# time_to_upper = True
# result = ""
# for ch in s:
# if time_to_upper:
# result += ch.upper()
# else:
# result += ch.lower()
# time_to_upper = not time_to_upper
# return result
#
# s="edyta"
#indices=set([text[::2]])
def tests(f):
inputs = [['pokemon'], ['do not want'], ['POKEMON']]
outputs = ['PoKeMoN', 'Do nOt wAnT', 'POKEMON']

View File

@ -7,10 +7,23 @@ 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.
"""
# tablica=[]
# def common_chars(string1, string2):
# for x in string1:
# for x in string2:
# tablica.append(x)
# return set(tablica)
# common_chars("this is a string", "ala ma kota")
def common_chars(string1, string2):
pass
s=set(string1)
t=set(string2)
intersect1 = s.intersection(t).difference("' '")
intersect1 = list(intersect1)
intersect1.sort()
return intersect1
#print(type(intersect1))
def tests(f):
inputs = [["this is a string", "ala ma kota"]]

View File

@ -3,10 +3,16 @@
def suma(a, b):
<<<<<<< HEAD
return a+b
=======
"""
Napisz funkcję, która zwraca sumę elementów.
"""
return a + b
>>>>>>> 8c76a052001eb7efe6cb0610a238cad5181c7325
def tests(f):
inputs = [(2, 3), (0, 0), (1, 1)]
@ -18,4 +24,10 @@ def tests(f):
break
return "TESTS PASSED"
<<<<<<< HEAD
if __name__ == "__main__":
print(tests(suma))
=======
print(tests(suma))
>>>>>>> 8c76a052001eb7efe6cb0610a238cad5181c7325

View File

@ -232,13 +232,13 @@
{
"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'"
]
],
"output_type": "error"
}
],
"source": [
@ -398,13 +398,13 @@
{
"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."
]
],
"output_type": "error"
}
],
"source": [

View File

@ -309,13 +309,13 @@
{
"ename": "AttributeError",
"evalue": "'Parser' object has no attribute '__parse'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-80ee186598d3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mparser\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mParser\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mparser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mparser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__parse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'Parser' object has no attribute '__parse'"
]
],
"output_type": "error"
}
],
"source": [
@ -465,13 +465,13 @@
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'nieistniejący_plik.txt'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-20-41928d542bef>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"nieistniejący_plik.txt\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mplik\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \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[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'nieistniejący_plik.txt'"
]
],
"output_type": "error"
}
],
"source": [
@ -614,13 +614,13 @@
{
"ename": "MyError",
"evalue": "Coś poszło nie tak!",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mMyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-36-4fb306b42ebc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mMyError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Coś poszło nie tak!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mMyError\u001b[0m: Coś poszło nie tak!"
]
],
"output_type": "error"
}
],
"source": [

View File

@ -1,3 +1,14 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#**ćwiczenie 1**
#Napisz funckję ``is_numeric``, która sprawdzi, czy każdy element z przekazanej listy jest typu int lub float
#Wykorzystaj funcję ``isinstance()`` (#isinstance).https://docs.python.org/2/library/functions.html
def is_numeric(lista):
for x in lista:
return isinstance(x, (float, int))
is_numeric([1.0,2.0,3.0])

View File

@ -1,3 +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.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
class Employee:
class_counter=1
def __init__(self, Imię, Nazwisko):
self.Imię=Imię
self.Nazwisko=Nazwisko
self.Id= Employee.class_counter
Employee.class_counter+=1
def get_id(self):
print(self.Id)
class Recruiter (Employee):
def __init__(self, Id, lista):
super().__init__(Id)
self.lista=lista
def recruit(self, Id, lista):
lista.append(Id)
print(len(lista))
#print(self)
print(help(Recruiter))
print(Recruiter.recruit(emp3.Id))
print(Recruiter.lista)
emp1=Employee("Edyta","Renk")
emp2=Employee("Jacek", "Placek")
emp3=Employee("Jacek", "Macek")
print(emp2.id)
Employee.get_id(emp3)

View File

@ -1,3 +1,68 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#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 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 k# orzystając z funkcji print.
class DimensionError(Exception):
pass
class Point:
def __init__(self, coordinates):
self.coordinates = coordinates
def is_numeric(coordinates):
for coordinate in coordinates:
if isinstance(coordinate, (float, int, complex)) == True:
pass
else:
return "{} is a not number".format(coordinate)
def number_of_coordinates(self):
return len(self.coordinates)
def add(self, point):
# check if this instance of class Point and the instance that was passed(parameter) has the same number of coordinates
if self.number_of_coordinates() == point.number_of_coordinates():
new_coordinates = []
for pos, coordinate in enumerate(self.coordinates):
new_coordinates.append(coordinate + point.coordinates[pos])
return Point(new_coordinates)
else:
raise DimensionError("Two points do not have the same number of dimensions")
def __str__(self):
string = ""
for pos, coordinate in enumerate(self.coordinates):
if len(self.coordinates) - 1 != pos:
string = string + str(coordinate) + ", "
else:
string = string + str(coordinate)
return "({})".format(string)
def __len__(self):
return len(self.coordinates)
coord = [1, 2, 4, 5, 6, 7]
p1 = Point(coord)
print(p1)