Skip to content

Commit 69c7288

Browse files
Guilherme DiegoGuilherme Diego
authored andcommitted
2 parents b6ff50c + fca2db9 commit 69c7288

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

forloop.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
s = "a string to examine"
2+
for i in range(len(s)):
3+
for j in range(i+1, len(s)):
4+
if s[i] == s[j]:
5+
answer = (i, j)

p14-2.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import heapq
2+
3+
4+
portfolio = [
5+
{'name': 'IBM', 'shares': 100, 'price': 91.1},
6+
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
7+
{'name': 'FB', 'shares': 200, 'price': 21.09},
8+
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
9+
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
10+
{'name': 'ACME', 'shares': 75, 'price': 115.65},
11+
]
12+
13+
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
14+
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
15+
16+
print('Cheap shares: ', cheap, '\n')
17+
print('Expensive shares: ', expensive)

p14.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import heapq
2+
3+
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
4+
5+
print(heapq.nlargest(3, nums))
6+
print(heapq.nsmallest(3, nums))

p15.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import heapq
2+
3+
4+
class PriorityQueue:
5+
def __init__(self):
6+
self.queue = []
7+
self._index = 0
8+
9+
def push(self, item, priority):
10+
heapq.heappush(self.queue, (-priority, self._index, item))
11+
self._index += 1
12+
13+
def pop(self):
14+
return heapq.heappop(self.queue)[-1]
15+
16+
if __name__ == '__main__':
17+
18+
class Item:
19+
def __init__(self, name):
20+
self.name = name
21+
22+
def __repr__(self):
23+
return 'Item({!r})'.format(self.name)
24+
25+
q = PriorityQueue()
26+
q.push(Item('foo'), 1)
27+
q.push(Item('bar'), 5)
28+
q.push(Item('spam'), 4)
29+
q.push(Item('grok'), 1)
30+
31+
print(q.pop())
32+
print(q.pop())
33+
print(q.pop())
34+
print(q.pop())

p19.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
a = {
2+
'x': 1,
3+
'y': 2,
4+
'z': 3
5+
}
6+
7+
b = {
8+
'w': 10,
9+
'x': 11,
10+
'y': 2
11+
}
12+
13+
c = {
14+
'x': 4,
15+
'u': 2
16+
}
17+
18+
print('a: ', a)
19+
print('b: ', b)
20+
print('c: ', c)
21+
22+
print('Commons items (items in a in b)')
23+
print(a.items() & b.items())
24+
print('Common keys (keys in a in b)')
25+
print(a.keys() & b.keys())
26+
print('Intersection (keys in a not in b)')
27+
print(a.keys() - b.keys())
28+
print('Intersection (keys in b not in a)')
29+
print(b.keys() - a.keys())
30+
print('Intersection (items in a not in c)')
31+
print(a.items() - c.items())
32+
print('Intersection (keys in a not in c)')
33+
print(a.keys() - c.keys())
34+
35+
36+
# cria um novo dicionario com determindas chaves removidas
37+
c = {key: a[key] for key in a.keys() - {'z', 'w'}}
38+
print('Removing specific keys {\'z\', \'w\'} from a')
39+
print(c)

0 commit comments

Comments
 (0)