Skip to content

Commit b6ff50c

Browse files
Guilherme DiegoGuilherme Diego
Guilherme Diego
authored and
Guilherme Diego
committed
New Examples
1 parent b48d0dc commit b6ff50c

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed

cap1/problem19-1.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from collections import ChainMap
2+
3+
4+
values = ChainMap()
5+
values['x'] = 1
6+
7+
# Adiciona um novo mapeamento
8+
values = values.new_child()
9+
values['x'] = 2
10+
11+
# Adiciona um novo mapeamento
12+
values = values.new_child()
13+
values['x'] = 3
14+
15+
print(values)
16+
print(values['x'])
17+
18+
# Descarta o último mapeamento
19+
values = values.parents
20+
print(values['x'])
21+
22+
# Descarta o último mapeamento
23+
values = values.parents
24+
print(values['x'])
25+
26+
print(values)

cap1/problem19.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
3+
nums = [1,2,3,4,5]
4+
s = sum(x * x for x in nums)
5+
6+
print('SUM')
7+
print(s)
8+
9+
files = os.listdir('./')
10+
11+
if any(name.endswith('.py') for name in files):
12+
print('There be python!')
13+
else:
14+
print('Sorry, no py')
15+
16+
s = ('ACME', 50, 123.45)
17+
print(','.join(str(x) for x in s))
18+
19+
portifolio = [
20+
{'name': 'GOOG', 'shares': 50},
21+
{'name': 'YHOO', 'shares': 75},
22+
{'name': 'AOL', 'shares': 20},
23+
{'name': 'SCOX', 'shares': 65}
24+
]
25+
min_shares = min(s['shares'] for s in portifolio)
26+
print(min_shares)
27+
28+

cap1/problem20.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from collections import ChainMap
2+
3+
a = {'x': 1, 'z': 3}
4+
b = {'y': 2, 'z': 4}
5+
6+
c = ChainMap(a, b)
7+
8+
print('< x >', c['x'])
9+
print('< y > ', c['y'])
10+
print('< z >: ', c['z'])
11+
12+
print('< Length >', len(c))
13+
print('< Keys >', list(c.keys()))
14+
print('< Values >', list(c.values()))
15+
16+
c['z'] = 10
17+
print('New A', a)

cap2/problem1.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import re
2+
3+
line = 'asdf asd;alfre, michael, viado é foo'
4+
5+
splited = re.split(r'[;,\s]\s*', line)
6+
7+
print('SPLITED STRING')
8+
print(splited)
9+
10+
splited_for_remake = re.split(r'(;|,|\s)\s*', line)
11+
print('SPLITED WITH DELIMITERS')
12+
print(splited_for_remake)
13+
14+
values = splited_for_remake[::2]
15+
print('ONLY VALUES')
16+
print(values)
17+
18+
delimiters = splited_for_remake[1::2] + ['']
19+
print('ONLY DELIMITERS')
20+
print(delimiters)
21+
22+
line_again = ''.join(v+d for v,d in zip(values, delimiters))
23+
print('LINE AGAIN MODAFCK')
24+
print(line_again)

cap2/problem2.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
filenames = os.listdir('../cap1')
4+
5+
print('VERBOSE FILENAMES VARIABLE')
6+
print(filenames)
7+
8+
txt_files = [filename for filename in filenames if filename.endswith('.txt') ]
9+
print('TXT FILES')
10+
print(txt_files)
11+
12+
any_py_on_filename = any(filename.endswith('.py') for filename in filenames)
13+
print('ANY EXAMPLE')
14+
print(any_py_on_filename)
15+

cap2/problem3.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
In OSX
3+
fnmatch('michael.txt', '*.TXT')
4+
>> FALSE
5+
6+
In windows
7+
fnmatch('michael.txt', '*.TXT'))
8+
>> TRUE
9+
10+
FOR THAT FUCKIN SHIT (WINDOW) OF SYSTEM YOU CAN USE FNMATCHCASE HUR DURRRRR
11+
"""
12+
from fnmatch import fnmatch, fnmatchcase
13+
14+
print('Everything endswith .txt', fnmatch('michael.txt', '*.txt'))
15+
16+
print('Start with F', fnmatch('foo.txt', '?oo.txt'))
17+
18+
print('Complex Regex', fnmatch('Dat45.csv', 'Dat[0-9]*'))
19+
20+
names = ['Dat1.csv', 'Dat2.csv', 'config.ini', 'foo.py']
21+
22+
print('Filter with Generator', [name for name in names if fnmatch(name, 'Dat*.csv')])
23+
24+
25+
#------------------------------------------------------------------------------------
26+
addresses = [
27+
'5412 N CLARK ST',
28+
'1060 W ADDISON ST',
29+
'1039 W GRANVILLE AVE',
30+
'2122 N CLARK ST',
31+
'4802 N BROADWAY'
32+
]
33+
34+
print([addr for addr in addresses if fnmatchcase(addr, '* ST')])
35+
print([addr for addr in addresses if fnmatchcase(addr, '54[0-9][0-9] *CLARK*')])

0 commit comments

Comments
 (0)