From 11901b678e61c8a52b760a33b857dde6e84639fd Mon Sep 17 00:00:00 2001 From: Matheus Rosa Date: Tue, 30 Aug 2016 18:36:39 -0300 Subject: [PATCH 1/3] Add more code examples --- forloop.py | 5 +++++ p14-2.py | 17 +++++++++++++++++ p14.py | 6 ++++++ p15.py | 34 ++++++++++++++++++++++++++++++++++ p19.py | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 forloop.py create mode 100644 p14-2.py create mode 100644 p14.py create mode 100644 p15.py create mode 100644 p19.py diff --git a/forloop.py b/forloop.py new file mode 100644 index 0000000..43b7074 --- /dev/null +++ b/forloop.py @@ -0,0 +1,5 @@ +s = "a string to examine" +for i in range(len(s)): + for j in range(i+1, len(s)): + if s[i] == s[j]: + answer = (i, j) diff --git a/p14-2.py b/p14-2.py new file mode 100644 index 0000000..9228cce --- /dev/null +++ b/p14-2.py @@ -0,0 +1,17 @@ +import heapq + + +portfolio = [ + {'name': 'IBM', 'shares': 100, 'price': 91.1}, + {'name': 'AAPL', 'shares': 50, 'price': 543.22}, + {'name': 'FB', 'shares': 200, 'price': 21.09}, + {'name': 'HPQ', 'shares': 35, 'price': 31.75}, + {'name': 'YHOO', 'shares': 45, 'price': 16.35}, + {'name': 'ACME', 'shares': 75, 'price': 115.65}, +] + +cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price']) +expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price']) + +print('Cheap shares: ', cheap, '\n') +print('Expensive shares: ', expensive) diff --git a/p14.py b/p14.py new file mode 100644 index 0000000..a1309de --- /dev/null +++ b/p14.py @@ -0,0 +1,6 @@ +import heapq + +nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] + +print(heapq.nlargest(3, nums)) +print(heapq.nsmallest(3, nums)) diff --git a/p15.py b/p15.py new file mode 100644 index 0000000..18ccd9a --- /dev/null +++ b/p15.py @@ -0,0 +1,34 @@ +import heapq + + +class PriorityQueue: + def __init__(self): + self.queue = [] + self._index = 0 + + def push(self, item, priority): + heapq.heappush(self.queue, (-priority, self._index, item)) + self._index += 1 + + def pop(self): + return heapq.heappop(self.queue)[-1] + +if __name__ == '__main__': + + class Item: + def __init__(self, name): + self.name = name + + def __repr__(self): + return 'Item({!r})'.format(self.name) + + q = PriorityQueue() + q.push(Item('foo'), 1) + q.push(Item('bar'), 5) + q.push(Item('spam'), 4) + q.push(Item('grok'), 1) + + print(q.pop()) + print(q.pop()) + print(q.pop()) + print(q.pop()) diff --git a/p19.py b/p19.py new file mode 100644 index 0000000..4701f99 --- /dev/null +++ b/p19.py @@ -0,0 +1,39 @@ +a = { + 'x': 1, + 'y': 2, + 'z': 3 +} + +b = { + 'w': 10, + 'x': 11, + 'y': 2 +} + +c = { + 'x': 4, + 'u': 2 +} + +print('a: ', a) +print('b: ', b) +print('c: ', c) + +print('Commons items (items in a in b)') +print(a.items() & b.items()) +print('Common keys (keys in a in b)') +print(a.keys() & b.keys()) +print('Intersection (keys in a not in b)') +print(a.keys() - b.keys()) +print('Intersection (keys in b not in a)') +print(b.keys() - a.keys()) +print('Intersection (items in a not in c)') +print(a.items() - c.items()) +print('Intersection (keys in a not in c)') +print(a.keys() - c.keys()) + + +# cria um novo dicionario com determindas chaves removidas +c = {key: a[key] for key in a.keys() - {'z', 'w'}} +print('Removing specific keys {\'z\', \'w\'} from a') +print(c) From b6ff50c3273f492fdcfd365ec9833e02e38e895b Mon Sep 17 00:00:00 2001 From: Guilherme Diego Date: Mon, 5 Sep 2016 19:37:29 -0300 Subject: [PATCH 2/3] New Examples --- cap1/problem19-1.py | 26 ++++++++++++++++++++++++++ cap1/problem19.py | 28 ++++++++++++++++++++++++++++ cap1/problem20.py | 17 +++++++++++++++++ cap2/problem1.py | 24 ++++++++++++++++++++++++ cap2/problem2.py | 15 +++++++++++++++ cap2/problem3.py | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+) create mode 100644 cap1/problem19-1.py create mode 100644 cap1/problem19.py create mode 100644 cap1/problem20.py create mode 100644 cap2/problem1.py create mode 100644 cap2/problem2.py create mode 100644 cap2/problem3.py diff --git a/cap1/problem19-1.py b/cap1/problem19-1.py new file mode 100644 index 0000000..1ee4312 --- /dev/null +++ b/cap1/problem19-1.py @@ -0,0 +1,26 @@ +from collections import ChainMap + + +values = ChainMap() +values['x'] = 1 + +# Adiciona um novo mapeamento +values = values.new_child() +values['x'] = 2 + +# Adiciona um novo mapeamento +values = values.new_child() +values['x'] = 3 + +print(values) +print(values['x']) + +# Descarta o último mapeamento +values = values.parents +print(values['x']) + +# Descarta o último mapeamento +values = values.parents +print(values['x']) + +print(values) diff --git a/cap1/problem19.py b/cap1/problem19.py new file mode 100644 index 0000000..f3c0cb3 --- /dev/null +++ b/cap1/problem19.py @@ -0,0 +1,28 @@ +import os + +nums = [1,2,3,4,5] +s = sum(x * x for x in nums) + +print('SUM') +print(s) + +files = os.listdir('./') + +if any(name.endswith('.py') for name in files): + print('There be python!') +else: + print('Sorry, no py') + +s = ('ACME', 50, 123.45) +print(','.join(str(x) for x in s)) + +portifolio = [ + {'name': 'GOOG', 'shares': 50}, + {'name': 'YHOO', 'shares': 75}, + {'name': 'AOL', 'shares': 20}, + {'name': 'SCOX', 'shares': 65} +] +min_shares = min(s['shares'] for s in portifolio) +print(min_shares) + + diff --git a/cap1/problem20.py b/cap1/problem20.py new file mode 100644 index 0000000..3b92423 --- /dev/null +++ b/cap1/problem20.py @@ -0,0 +1,17 @@ +from collections import ChainMap + +a = {'x': 1, 'z': 3} +b = {'y': 2, 'z': 4} + +c = ChainMap(a, b) + +print('< x >', c['x']) +print('< y > ', c['y']) +print('< z >: ', c['z']) + +print('< Length >', len(c)) +print('< Keys >', list(c.keys())) +print('< Values >', list(c.values())) + +c['z'] = 10 +print('New A', a) diff --git a/cap2/problem1.py b/cap2/problem1.py new file mode 100644 index 0000000..ffac8b3 --- /dev/null +++ b/cap2/problem1.py @@ -0,0 +1,24 @@ +import re + +line = 'asdf asd;alfre, michael, viado é foo' + +splited = re.split(r'[;,\s]\s*', line) + +print('SPLITED STRING') +print(splited) + +splited_for_remake = re.split(r'(;|,|\s)\s*', line) +print('SPLITED WITH DELIMITERS') +print(splited_for_remake) + +values = splited_for_remake[::2] +print('ONLY VALUES') +print(values) + +delimiters = splited_for_remake[1::2] + [''] +print('ONLY DELIMITERS') +print(delimiters) + +line_again = ''.join(v+d for v,d in zip(values, delimiters)) +print('LINE AGAIN MODAFCK') +print(line_again) diff --git a/cap2/problem2.py b/cap2/problem2.py new file mode 100644 index 0000000..0da82ca --- /dev/null +++ b/cap2/problem2.py @@ -0,0 +1,15 @@ +import os + +filenames = os.listdir('../cap1') + +print('VERBOSE FILENAMES VARIABLE') +print(filenames) + +txt_files = [filename for filename in filenames if filename.endswith('.txt') ] +print('TXT FILES') +print(txt_files) + +any_py_on_filename = any(filename.endswith('.py') for filename in filenames) +print('ANY EXAMPLE') +print(any_py_on_filename) + diff --git a/cap2/problem3.py b/cap2/problem3.py new file mode 100644 index 0000000..533bdf8 --- /dev/null +++ b/cap2/problem3.py @@ -0,0 +1,35 @@ +""" + In OSX + fnmatch('michael.txt', '*.TXT') + >> FALSE + + In windows + fnmatch('michael.txt', '*.TXT')) + >> TRUE + + FOR THAT FUCKIN SHIT (WINDOW) OF SYSTEM YOU CAN USE FNMATCHCASE HUR DURRRRR +""" +from fnmatch import fnmatch, fnmatchcase + +print('Everything endswith .txt', fnmatch('michael.txt', '*.txt')) + +print('Start with F', fnmatch('foo.txt', '?oo.txt')) + +print('Complex Regex', fnmatch('Dat45.csv', 'Dat[0-9]*')) + +names = ['Dat1.csv', 'Dat2.csv', 'config.ini', 'foo.py'] + +print('Filter with Generator', [name for name in names if fnmatch(name, 'Dat*.csv')]) + + +#------------------------------------------------------------------------------------ +addresses = [ + '5412 N CLARK ST', + '1060 W ADDISON ST', + '1039 W GRANVILLE AVE', + '2122 N CLARK ST', + '4802 N BROADWAY' +] + +print([addr for addr in addresses if fnmatchcase(addr, '* ST')]) +print([addr for addr in addresses if fnmatchcase(addr, '54[0-9][0-9] *CLARK*')]) From 8c51e5b4cea57f3c50ada4a9de02b8df4b507eee Mon Sep 17 00:00:00 2001 From: Guilherme Diego Date: Tue, 6 Sep 2016 09:22:24 -0300 Subject: [PATCH 3/3] Reorganize --- forloop.py => cap1/forloop.py | 0 p14.py => cap1/problem14-1.py | 0 p14-2.py => cap1/problem14-2.py | 0 p15.py => cap1/problem15-2.py | 0 p19.py => cap1/problema19-2.py | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename forloop.py => cap1/forloop.py (100%) rename p14.py => cap1/problem14-1.py (100%) rename p14-2.py => cap1/problem14-2.py (100%) rename p15.py => cap1/problem15-2.py (100%) rename p19.py => cap1/problema19-2.py (100%) diff --git a/forloop.py b/cap1/forloop.py similarity index 100% rename from forloop.py rename to cap1/forloop.py diff --git a/p14.py b/cap1/problem14-1.py similarity index 100% rename from p14.py rename to cap1/problem14-1.py diff --git a/p14-2.py b/cap1/problem14-2.py similarity index 100% rename from p14-2.py rename to cap1/problem14-2.py diff --git a/p15.py b/cap1/problem15-2.py similarity index 100% rename from p15.py rename to cap1/problem15-2.py diff --git a/p19.py b/cap1/problema19-2.py similarity index 100% rename from p19.py rename to cap1/problema19-2.py