Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
lfa:lab01-python-intro [2021/09/14 09:40] pdmatei |
lfa:lab01-python-intro [2021/10/07 11:32] (current) ioana.georgescu [List comprehensions] |
||
|---|---|---|---|
| Line 45: | Line 45: | ||
| l[:x] | l[:x] | ||
| # the last element of a list: | # the last element of a list: | ||
| - | l[:-1] | + | l[-1] |
| # the last three elements of a list: | # the last three elements of a list: | ||
| - | l[:-3] | + | l[-3:] |
| # the slice from n-3 to n-1, where n is the number of elements from the list | # the slice from n-3 to n-1, where n is the number of elements from the list | ||
| l[-3:-1] | l[-3:-1] | ||
| Line 197: | Line 197: | ||
| | | ||
| # computing the average ages (a map could have also been used) | # computing the average ages (a map could have also been used) | ||
| - | avg = reduce(lambda a,b:a+b, [age(x) for x in l]) / len(l) | + | avg = reduce(lambda a,b:a+b, [age(x[-1]) for x in l]) / len(l) |
| | | ||
| # we return the last name and the age of the filtered list l | # we return the last name and the age of the filtered list l | ||
| Line 281: | Line 281: | ||
| */ | */ | ||
| + | |||
| + | ===== Practice ===== | ||
| + | |||
| + | A labelled graph is encoded as a file where: | ||
| + | * **the first line** consists of the number of nodes | ||
| + | * **each subsequent line** is an edge ''<from> <label> <to>'' | ||
| + | Example: | ||
| + | <code> | ||
| + | 5 | ||
| + | 0 X 1 | ||
| + | 1 O 2 | ||
| + | 1 X 3 | ||
| + | 1 O 4 | ||
| + | 4 X 1 | ||
| + | 3 O 2 | ||
| + | </code> | ||
| + | |||
| + | * Suppose we encode streets as labelled graphs, where each label 'X' or 'O' denotes if a street is closed or open. | ||
| + | * Compute the set of accessible nodes from a given **source**, via open streets. | ||
| + | * (Hint1: google //Python read lines// to see how to read from a file; also, google ''split'' in Python) | ||
| + | * (Hint2: you will need a dictionary to store, for each node and label l, the list of its l-successors) | ||
| + | |||
| + | |||
| + | ===== Haskell practice ===== | ||
| + | |||
| + | Solve the same exercise, only build your own input as a string, instead of a file. | ||