-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_1.html
152 lines (149 loc) · 4.41 KB
/
ex_1.html
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#157878">
<link rel="stylesheet" href="assets/style.css">
<style>
.hidden {
display: none;
}
</style>
</head>
<body>
<section class="page-header">
<h1 class="project-name">Intro To Python</h1>
<h2 class="project-tagline">Part 1 and 2 Exercises</h2>
</section>
<section class="main-content">
<h1><a></a>Exercise 1</h1>
<p>Write a function that takes in 1 number x and prints out a x by
x times table nicely formatted, for example if i called f(x) i'd get </p>
<pre><code> | 1 2 3 4 5
=====================
1 | 1 2 3 4 5
2 | 2 4 6 8 10
3 | 3 6 9 12 15
4 | 4 8 12 16 20
5 | 5 10 15 20 25
</code></pre>
<blockquote>You can assume the highest number you'll get is 99</blockquote>
<div class="item hidden">
<h2><a></a>Solution</h2>
<pre><code>def print_table(size):
header = ' |' +''.join([f'{i:5}' for i in range(1,size+1)])
print(header)
print('='*(len(header)+2))
for i in range(1,size+1):
row = [f'{i*j:5}' for j in range(1,size+1)]
print(f'{i:<3}|'+''.join(row))
print_table(7)
</code></pre>
</div>
<h1><a></a>Exercise 2</h1>
<p>Write a function that takes in a list of <i>tuples</i> where each tuple contains a fruit,
it's cost and the selling price of the fruit </p>
<p> Calculate the optimal set of fruits to buy which will maximise your profits
BUT you can't buy more then n fruits because that's all the space you have in your store,
i.e write a function f(fruits, max) which returns the set of fruits to buy</p>
<pre><code>fruits = []
fruits.append(("apple",12,5)) # an apple worth 12 bucks with a selling price of 5 bucks
fruits.append(("bannana",3,100)
l = f(fruits,1) # l is ["bannana"] which is the most profitable
</code></pre>
<div class="item hidden">
<h2><a></a>Solution</h2>
<pre><code>def get_items_to_buy(fruits, max):
profits = [(fruit[0],fruit[2] - fruit[1]) for fruit in fruits]
profits = sorted(profits, key=lambda x: x[1], reverse=True)
return profits[:max]
</code></pre>
</div>
<h1><a></a>Exercise 3</h1>
<p>Write a function that takes in a movie title such as "need for speed" and
generates every possible play on the title that involves changing 1 character,
such as "seed for speed" or "need far speed",
but to narrow down the amount of results that are garbage like 'xeed for speed',
don't replace any letter with "x" "y" or "z".
</p>
<p></p>
<pre><code>l = f("need for speed")
print(l) # ["seed for speed",...]
</code></pre>
<div class="item hidden">
<h2><a></a>Solution</h2>
<pre><code>def movie_titles(title):
title = title.lower()
for l in range(len(title)):
if list(title)[l] == ' ':
continue
for c in range(ord('a'),ord('x')):
copy = list(title)
copy[l] = chr(c)
print(''.join(copy))
movie_titles("need for speed")
</code></pre>
</div>
<h1>Exercise 4</h1>
<p>Write a function that given a file name will read the file and print out
how often each letter is used as a percentage (case insensitve)</p>
<pre><code>count_letters("hello.txt") # assume hello.txt is a file with 1 line in it "HHH"
a : 0.00%
b : 0.00%
c : 0.00%
d : 0.00%
e : 0.00%
f : 0.00%
g : 0.00%
h : 100.00%
i : 0.00%
j : 0.00%
k : 0.00%
l : 0.00%
m : 0.00%
n : 0.00%
o : 0.00%
p : 0.00%
q : 0.00%
r : 0.00%
s : 0.00%
t : 0.00%
u : 0.00%
v : 0.00%
w : 0.00%
x : 0.00%
y : 0.00%
z : 0.00%
</code></pre>
<blockquote> Aim to match the 2 decimal places shown above </blockquote>
<div class="item hidden">
<h2><a></a>Solution</h2>
<pre><code>def count_file(file):
counts = {}
with open(file) as f:
for c in f.read():
c = c.lower()
if c == ' ':
continue
if c not in counts:
counts[c] = 0
counts[c] += 1
for k in counts.keys():
print(f"k : {round(counts[k],2)}%")
</code></pre>
</div>
</section>
<script>
window.onload = () => {
if(window.location.href.split("#")[1] === "sol") {
const l = document.getElementsByClassName("item");
for (elem of l) {
console.log(elem);
elem.classList.remove("hidden");
}
}
}
</script>
</body>
</html>