So guys, here we are! Finally you're facing your first REAL homework. Are you ready to fight? We're going to apply all the Pythonic stuff seen before AND EVEN MORE...
Guys the life is hard...in this Master it's even worse... Soooo, since that you seem so smart I want to test you before the start of all the courses.
.
.
.
But not now.
You have to come prepared to the challenge, so right now solve these first 6 exercises, then it will be the time for FIGHTING and (for one of you) DRINKING.
In [19]:
for x in range(13):
if x==0:
fact = 1
else:
fact = fact * x
fact
Out[19]:
In [25]:
','.join([str(x) for x in range(1001) if (x%7 == 0) and (x%5 != 0)])
Out[25]:
In this exercises you're going to deal with YOUR DATA. Indeed, in the list below there are stored your Favorite Tv Series. But, as you can see, there is something weird. There are too much CaPITal LeTTErs. Your task is to count the capital letters in all the strings and then print the total number of capital letters in all the list.
In [28]:
tv_series = ['Game of THRroneS',
'big bang tHeOrY',
'MR robot',
'WesTWorlD',
'fIRefLy',
"i haven't",
'HOW I MET your mothER',
'friENds',
'bRon broen',
'gossip girl',
'prISon break',
'breaking BAD']
In [39]:
# write here your code
import re
pattern = re.compile("[A-Z]")
capitals = 0
for c in ''.join(tv_series):
if pattern.match(c):
capitals+=1
capitals
Out[39]:
Using the list above, create a dictionary where the keys are Unique IDs and values the TV Series. You have to do the exercise keeping in mind these 2 constraints:
In [100]:
# write here your code
series_dict = {}
titled = [t.title() for t in tv_series]
for idx,movie_title in enumerate(sorted(titled)):
series_dict[idx]=movie_title.title()
series_dict
Out[100]:
In [60]:
# write here your code
series_dict = dict(zip(series_dict.values(),series_dict.keys()))
series_dict
Out[60]:
Have you done in one line of code? If not, try now!
In [61]:
# write here your code
print(dict(zip(series_dict.values(),series_dict.keys())))
In [4]:
exams = [('BIOINFORMATICS', 29),
('DATA MANAGEMENT FOR DATA SCIENCE', 30),
('DIGITAL EPIDEMIOLOGY', 26),
('NETWORKING FOR BIG DATA AND LABORATORY',28),
('QUANTITATIVE MODELS FOR ECONOMIC ANALYSIS AND MANAGEMENT','30 e lode'),
('DATA MINING TECHNOLOGY FOR BUSINESS AND SOCIETY', 30),
('STATISTICAL LEARNING',30),
('ALGORITHMIC METHODS OF DATA MINING AND LABORATORY',30),
('FUNDAMENTALS OF DATA SCIENCE AND LABORATORY', 29)]
cfu = sum([6,6,6,9,6,6,6,9,9])
In [5]:
cfu_ex = [6,6,6,9,6,6,6,9,9]
In [8]:
# write here your code
grades_sum = 0
mean = 0
for idx,w in enumerate(cfu_ex):
try:
mean += int(exams[idx][1])/cfu*w
grades_sum += int(exams[idx][1])
except ValueError:
if exams[idx][1] == "30 e lode":
mean += 30/cfu*w
grades_sum += 30
else:
pass
print(mean)
In [27]:
N = cfu * grades_sum
palindromic = ','.join([str(x) for x in range(N+1) if list(str(x)) == list(reversed(list(str(x))))])
print(palindromic)
Let's start using your new best friend. Now I'm going to give other task, slightly more difficult BUT this time, just googling, you will find easily the answer on the www.stackoverflow.com. You can use the code there for solving the exercise BUT you have to understand the solution there COMMENTING the code, showing me you understood the thinking process behind the code.
In [34]:
# write here your code
from random import randint
while True:
try:
randint(0, 9) / randint(0, 9) # execute some random division
except ZeroDivisionError: # until a division by zero occurs
break
['theory', 'of', 'bron', 'firefly', 'thrones', 'break', 'bad', 'mother', 'firefly', "haven't", 'prison', 'big', 'friends', 'girl', 'westworld', 'bad', "haven't", 'gossip', 'thrones', 'your', 'big', 'how', 'friends', 'theory', 'your', 'bron', 'bad', 'bad', 'breaking', 'met', 'breaking', 'breaking', 'game', 'bron', 'your', 'breaking', 'met', 'bang', 'how', 'mother', 'bad', 'theory', 'how', 'i', 'friends', "haven't", 'of', 'of', 'gossip', 'i', 'robot', 'of', 'prison', 'bad', 'friends', 'friends', 'i', 'robot', 'bang', 'mother', 'bang', 'i', 'of', 'bad', 'friends', 'theory', 'i', 'friends', 'thrones', 'prison', 'theory', 'theory', 'big', 'of', 'bang', 'how', 'thrones', 'bang', 'theory', 'friends', 'game', 'bang', 'mother', 'broen', 'bad', 'game', 'break', 'break', 'bang', 'big', 'gossip', 'robot', 'met', 'i', 'game', 'your', 'met', 'bad', 'firefly', 'your']
In [44]:
# write here your code
corpus = ['theory', 'of', 'bron', 'firefly', 'thrones', 'break', 'bad', 'mother', 'firefly', "haven't", 'prison', 'big', 'friends', 'girl', 'westworld', 'bad', "haven't", 'gossip', 'thrones', 'your', 'big', 'how', 'friends', 'theory', 'your', 'bron', 'bad', 'bad', 'breaking', 'met', 'breaking', 'breaking', 'game', 'bron', 'your', 'breaking', 'met', 'bang', 'how', 'mother', 'bad', 'theory', 'how', 'i', 'friends', "haven't", 'of', 'of', 'gossip', 'i', 'robot', 'of', 'prison', 'bad', 'friends', 'friends', 'i', 'robot', 'bang', 'mother', 'bang', 'i', 'of', 'bad', 'friends', 'theory', 'i', 'friends', 'thrones', 'prison', 'theory', 'theory', 'big', 'of', 'bang', 'how', 'thrones', 'bang', 'theory', 'friends', 'game', 'bang', 'mother', 'broen', 'bad', 'game', 'break', 'break', 'bang', 'big', 'gossip', 'robot', 'met', 'i', 'game', 'your', 'met', 'bad', 'firefly', 'your']
BoW = {}
for idx,word in enumerate(corpus):
BoW[word] = corpus.count(word)
BoW
Out[44]:
In [57]:
# write here your code
fibonacci = []
for x in range(10):
if x<2:
fibonacci.append(1)
else:
fibonacci.append(fibonacci[x-1] + fibonacci[x-2])
fibonacci
Out[57]:
In [ ]: