Video of previous lectures:
All the exam sessions are uploaded on AlmaEsami
You have to subscribe to the session you want to attend
She was a physicist and entrepreneur
She started to work in a company that produced digital computers, and then she founded her own company: Redactron Corporation
In this new company, she started to work on computer systems to simplify the work of secretaries
The main product was Data Secretary, the very first word processor in history
At every stage of execution of a particular algorithm when we are seeking for possible candidates for constructing the solution to a computational problem, this approach makes always the choice that is optimal (i.e. the best one) in that particular moment
It is easy to come up with a greedy algorithm for a problem
Computational problem: to give the change of a certain amount of euros in coins
A possible greedy approach:
Consider the coins to choose for the change as ordered in a decrescent way, from the highest value (i.e. 2 euros) to the lowest one (i.e. 1 cent)
For each kind of value, add in the candidate set of the solution as much coins of that value as possible until their sum is lesser than the remaining of the change to give
Return the selected coins for the change
Sometimes the solution found, while it provides a correct solution to the problem, is just suboptimal - i.e. it is not the best one for solving the problem
For instance, if you are driving from Florence to Bologna, and, at a certain crossroad you find two signs indicating two different routes to get to Bologna
left route: get to Bologna by travelling for 42 kilometers
right route: get to Bologna by travelling for 56 kilometers
Greedy approach: go to left (1)
It cannot predict the existence of traffic in the left route
Greedy choice property: at a certain step, we can choose the best candidate for improving the set of candidates bringing to a solution - this chioce can be somehow driven by all the previous choices
The problem has an optimal substructure, i.e. optimal solution to a computational problem can be built by considering the optimal solutions to its subproblems
Computational problem: break a text into lines such that it will fit in the available width of a page
<string>.split(<string_separator>)
It returns a list of strings where each string in the list represents a word (or a token)
Example: "a b c".split(" ")
returns the list ["a", "b", "c"]
<string_separator>.join(<list_of_strings>)
It returns a string composed by all the strings in the list separated by <string_separator>
Example: " ".join(["a", "b", "c"])
returns the string "a b c"
def line_wrap(text, line_width):
result = []
space_left = line_width
line = []
for word in text.split(" "):
word_len = len(word)
if word_len + 1 > space_left:
result.append(" ".join(line))
line = [word]
space_left = line_width - word_len
else:
line.append(word)
space_left = space_left - (word_len + 1)
result.append(" ".join(line))
return "\n".join(result)