-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvanced_Python.py
More file actions
173 lines (131 loc) · 4.82 KB
/
Advanced_Python.py
File metadata and controls
173 lines (131 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# coding: utf-8
##################################################################################################
"""
Exercise #1
You are the manager of a supermarket.
You have a list of N items together with their prices that consumers bought on a
particular day. Your task is to print each item_name and net_price in order
of its first occurrence.
item_name = Name of the item.
net_price = Quantity of the item sold multiplied by the price of each item.
Input Format
The first line contains the number of items, .
The next lines contain the item's name and price, separated by a space.
Constraints
N <= 100
Output Format
Print the item_name and net_price in order of its first occurrence.
"""
from collections import OrderedDict
output = OrderedDict()
with open("input_file.txt", "r") as f:
lines = f.read().splitlines()
for line in lines[1:]:
count = int(line.split(" ")[-1])
name = " ".join(line.split(" ")[:-1])
output[name] = count
for name, count in output.items():
print(name, count)
##################################################################################################
"""
Exercise #2
Rahue is a shoe shop owner. His shop has X number of shoes.
He has a list containing the size of each shoe he has in his shop.
There are N number of customers who are willing to pay xi amount of money only
if they get the shoe of their desired size.
Your task is to compute how much money Rahue earned.
Input Format
The first line contains X , the number of shoes.
The second line contains a space separated list of all the shoe sizes in the shop.
The third line contains N, the number of customers.
The next N lines contain the space separated values of the shoe size desired
by the customer and xi the price of the shoe.
Output Format
Print the amount of money earned by Rahue.
"""
from collections import Counter
with open("input_file_2.txt", "r") as f:
lines = f.read().splitlines()
sizes = Counter(lines[1].split(" "))
output = sum([Counter({int(a): int(b)}) for a,b in
[i.split(" ") for i in lines[4:]] if a in sizes], Counter())
print(sum(output.values()))
##################################################################################################
"""
Exercise #3
Create an iterator that given a filename will return an object that on every iteration
will return a single character. As an option let the user skip newlines, or maybe
any pre-defined character.
"""
class iter_letters():
def __init__(self, file_path, skip_char=None):
self._skip_char = skip_char
with open(file_path, 'r') as f:
self._file_data = f.read()
self._index = 0
def __next__(self):
while True:
if self._index == len(self._file_data):
raise StopIteration()
x = self._file_data[self._index]
self._index += 1
if self._skip_char is not None and x == self._skip_char:
continue
return x
def __iter__(self):
return self
a = iter_letters("nice.txt", 'm')
''.join(list(iter(a)))
##################################################################################################
"""
Exercise #4
Create a generator that yields recursively all files paths in a specified
path (you can use the os or pathlib modules for that).
"""
from pathlib import Path
def path_gen(file_path, skip_char=None):
skip_char = skip_char
with open(file_path, 'r') as f:
file_data = f.read()
for l in file_data:
if l is not skip_char:
yield l
''.join(list(path_gen("nice.txt", 'r')))
##################################################################################################
"""
Exercise #5
Write a simple decorator (@hello) that prints “hello world” in every function it decorates
Write a parameterized decorator (@debug) that returns the decorated function
if and only if the decorator gets a True parameter and a function that
prints “Debug” otherwise.
"""
import time
def say(text):
def _say(func):
def call_func(*args, **kwargs):
print(text)
return func(*args, **kwargs)
return call_func
return _say
def debug(enabled):
def _debug(func):
def call_func(*args, **kwargs):
a = time.time()
x = func(*args, **kwargs)
b = time.time()
if enabled:
print("Function time: {}".format(b-a))
return x
return call_func
return _debug
DEBUG = False
@say("Hello")
@debug(DEBUG)
def add(a,b,c):
return a+b+c
@debug(DEBUG)
def break_sha1(a):
time.sleep(4)
add(1,2,3)
##################################################################################################