Skip to content

Commit 4d0249c

Browse files
Guilherme DiegoGuilherme Diego
authored andcommitted
First Examples
1 parent 724c7aa commit 4d0249c

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

cap1/.problem4.py.swp

12 KB
Binary file not shown.

cap1/problem1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
grades = (1, 2, 3, 4, 5, 6, 7, 8, 9)
2+
3+
def drop_first_n_last(grades):
4+
first, *middle, last = grades
5+
return sum(middle) / len(middle)
6+
7+
result = drop_first_n_last(grades)
8+
print(result)
9+

cap1/problem2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def create_values():
2+
sales_record = [155, 182, 192, 220, 320, 110, 166, 185, 190]
3+
*traiding_qtr, current_qtr = sales_record
4+
middle_qtr = sum(traiding_qtr) / len(traiding_qtr)
5+
print(middle_qtr, current_qtr)
6+
7+
create_values()

cap1/problem3.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
records = [
2+
('foo', 1, 2),
3+
('bar', 'hello'),
4+
('foo', 3, 4),
5+
]
6+
7+
def do_foo(x, y):
8+
print('foo', x, y)
9+
10+
def do_bar(s):
11+
print('bar', s)
12+
13+
for tag, *args in records:
14+
if tag == 'foo':
15+
do_foo(*args)
16+
elif tag == 'bar':
17+
do_bar(*args)
18+

cap1/problem4.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import deque
2+
3+
def search(lines, pattern, history=5):
4+
previous_lines = deque(maxlen=history)
5+
import pdb; pdb.set_trace()
6+
for line in lines:
7+
if pattern in line:
8+
yield line, previous_lines
9+
previous_lines.append(line)
10+
11+
if __name__ == '__main__':
12+
with open('somefile.txt') as f:
13+
results = search(f, 'python', 5)
14+
import pdb; pdb.set_trace()
15+
for line, prevlines in results:
16+
for pline in prevlines:
17+
print(pline, end='')
18+
print(line, end='')
19+
print('-' * 20)

cap1/somefile.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Go
2+
Pascal
3+
PHP
4+
R
5+
Java
6+
Javascript
7+
Ruby
8+
C++
9+
python
10+
C
11+
C#
12+
Fortran

0 commit comments

Comments
 (0)