diff --git a/add.py b/add.py new file mode 100644 index 0000000..c744d31 --- /dev/null +++ b/add.py @@ -0,0 +1,4 @@ +def add(a, b): + print a + b + +add(1, -2) \ No newline at end of file diff --git a/dict.py b/dict.py new file mode 100644 index 0000000..6a5f147 --- /dev/null +++ b/dict.py @@ -0,0 +1,20 @@ +D = {} + +def makeD(s): + a = s.split(";") + for i in a: + key, val = i.split("=") + D[key] = val + +def makeDshort(s): + return dict([i.split("=") for i in s.split(";")]) + +def dictocs(d): + s = '' + for i in d: + s += str(i) + '=' + str(d[i]) + return s + +makeD("a=bc;d=efg;h=ijkl;m=n") +print D +print dictocs({'a': 'bc', 'h': 'ijkl', 'm': 'n', 'd': 'efg'}) \ No newline at end of file diff --git a/files.py b/files.py new file mode 100644 index 0000000..4cd03f9 --- /dev/null +++ b/files.py @@ -0,0 +1,7 @@ +try: + with open('first.txt', 'r') as inp: + with open('second.txt', 'w') as out: + for line in inp: + out.write(line) +except IOError: + print "Wrong file or file path" diff --git a/inherit dict.py b/inherit dict.py new file mode 100644 index 0000000..d6c84b1 --- /dev/null +++ b/inherit dict.py @@ -0,0 +1,60 @@ + +class ITEMNOTINMENU(Exception): + pass +class WrongPrice(Exception): + pass + +class Menu(dict): + def __getitem__(self, i): + if self.get(i) is None: + raise ITEMNOTINMENU + else: + return self.get(i) + +class Order(dict): + def __init__(self, m): + self.d = m + dict.__init__(self) + + def __setitem__(self, key, item): + if key in self.d: + if self.d[key] == item: + self.update({key : item}) + else: + raise WrongPrice + else: + raise ITEMNOTINMENU + + def show(self): + print 'order :' + for i in self.items(): + print '\t' + str(i[0]) + ' costs ' + str(i[1]) + + +c = Menu() +c['vada'] = 10 +c['idly'] = 30 +print c['vada'] +try: + print c['unknown'] +except ITEMNOTINMENU: + print 'not in menu' + +o1 = Order(c) +o2 = Order(c) +try: + o1['vada'] = 10 +except ITEMNOTINMENU: + print 'not in menu' +except WrongPrice: + print 'wrong price' + +try: + o2['idly'] = 30 +except ITEMNOTINMENU: + print 'not in menu' +except WrongPrice: + print 'wrong price' + +o1.show() +o2.show() diff --git a/menu.py b/menu.py new file mode 100644 index 0000000..eebbdac --- /dev/null +++ b/menu.py @@ -0,0 +1,12 @@ +class Menu: + def __init__(self): + self.d = {'vada' : 10} + def show(self): + for i in self.d.items(): + print str(i[0]) + ' costs ' + str(i[1]) + def add(self, a, b): + self.d[a] = b + +c = Menu() +c.add('idly', 20) +c.show() \ No newline at end of file diff --git a/order.py b/order.py new file mode 100644 index 0000000..966151c --- /dev/null +++ b/order.py @@ -0,0 +1,76 @@ +class ITEMNOTINMENU(Exception): + pass +class WrongPrice(Exception): + pass + +class Menu: + def __init__(self): + self.d = {'vada' : 10, 'idly' : 20} + + def __contains__(self, key): + return key in self.d + + def __getitem__(self, i): + if i in self.d: + return self.d[i] + else: + raise ITEMNOTINMENU + + def __setitem__(self, key, item): + self.d[key] = item + + def show(self): + print 'menu :' + for i in self.d.items(): + print '\t' + str(i[0]) + ' costs ' + str(i[1]) + + def add(self, a, b): + self.d[a] = b + +class Order(): + d = Menu() + def __init__(self, m): + self.d = m + self.ord = {} + + def __getitem__(self, i): + if i in self.ord: + return self.ord[i] + else: + raise ITEMNOTINMENU + + def __setitem__(self, key, item): + if key in self.d: + if self.d[key] == item: + self.ord[key] = item + else: + raise WrongPrice + else: + raise ITEMNOTINMENU + + def show(self): + print 'order :' + for i in self.ord.items(): + print '\t' + str(i[0]) + ' costs ' + str(i[1]) + + +c = Menu() +c['idly'] = 20 +try: + print c['unknown'] +except ITEMNOTINMENU: + print 'not in menu' + +c.show() +o1 = Order(c) +o2 = Order(c) +try: + o1['vada'] = 10 + o2['idly'] = 20 +except ITEMNOTINMENU: + print 'not in menu' +except WrongPrice: + print 'wrong price' + +o1.show() +o2.show() diff --git a/orderInheritance.py b/orderInheritance.py new file mode 100644 index 0000000..81ec9eb --- /dev/null +++ b/orderInheritance.py @@ -0,0 +1,78 @@ +class ITEMNOTINMENU(Exception): + pass +class WrongPrice(Exception): + pass + +class Help(object): + def getitem(self, i, a): + if i in a: + return a[i] + else: + raise ITEMNOTINMENU + + def show(self, a): + for i in a.items(): + print '\t' + str(i[0]) + ' costs ' + str(i[1]) + +class Menu(Help): + def __init__(self): + self.d = {'vada' : 10, 'idly' : 20} + + def __getitem__(self, i): + return super(Menu, self).getitem(i, self.d) + + def __contains__(self, key): + return key in self.d + + def __setitem__(self, key, item): + self.d[key] = item + + def show(self): + print 'menu :' + super(Menu, self).show(self.d) + + def add(self, a, b): + self.d[a] = b + +class Order(Help): + def __init__(self, m): + self.d = m + self.ordL = {} + + def __getitem__(self, i): + return super(Order, self).getitem(i, self.ordL) + + def __setitem__(self, key, item): + if key in self.d: + if self.d[key] == item: + self.ordL[key] = item + else: + raise WrongPrice + else: + raise ITEMNOTINMENU + + def show(self): + print 'order :' + super(Order, self).show(self.ordL) + + +c = Menu() +c['idly'] = 20 +try: + print c['unknown'] +except ITEMNOTINMENU: + print 'not in menu' + +c.show() +o1 = Order(c) +o2 = Order(c) +try: + o1['vada'] = 10 + o2['idly'] = 20 +except ITEMNOTINMENU: + print 'not in menu' +except WrongPrice: + print 'wrong price' + +o1.show() +o2.show() \ No newline at end of file diff --git a/root.py b/root.py new file mode 100644 index 0000000..ec6c878 --- /dev/null +++ b/root.py @@ -0,0 +1,10 @@ +def root(a, b, c): + sq = b * b - 4 * a * c + if sq < 0: + r1 = str(pow(abs(b * b - 4 * a * c), 0.5)) + 'i' + r2 = -b / 2 * a + return str(r2) + ' + ' + r1, str(r2) + ' - ' + r1 + else: + return (-b + pow(b * b - 4 * a * c, 0.5)) / (2 * a), (-b - pow(b * b - 4 * a * c, 0.5)) / (2 * a) + +print root(2, 3, 5) \ No newline at end of file diff --git a/rootV2.py b/rootV2.py new file mode 100644 index 0000000..a43de1e --- /dev/null +++ b/rootV2.py @@ -0,0 +1,6 @@ +import cmath + +def root(a, b, c): + return (-b + cmath.sqrt(b * b - 4 * a * c)) / (2 * a), (-b - cmath.sqrt(b * b - 4 * a * c)) / (2 * a) + +print root(2, 3, 5)