Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lfa:2022:lab01-programming-intro [2022/10/07 15:34] pdmatei |
lfa:2022:lab01-programming-intro [2022/10/14 13:13] (current) mihai.udubasa add solutions |
||
---|---|---|---|
Line 2: | Line 2: | ||
===== 1. Scala introduction ===== | ===== 1. Scala introduction ===== | ||
- | |||
**Exercise 1.1.** Write a function ''startStop'' which retrieves the elements of a list from within bounds. Use patterns in your implementation. Define an object ''Main'' as shown below. | **Exercise 1.1.** Write a function ''startStop'' which retrieves the elements of a list from within bounds. Use patterns in your implementation. Define an object ''Main'' as shown below. | ||
- | Any code within the object can be directly executed. | + | Any code within the object can be directly executed. (Hint: use a simply-recursive function)/ |
<code scala> | <code scala> | ||
Line 16: | Line 15: | ||
</code> | </code> | ||
- | **Exercise 1.2.** | + | **Exercise 1.2.** Write a function which determines the number of occurrences of each character in a string. (Hint: use a tail-recursive function) |
+ | <code scala> | ||
+ | def countChars (s:String): Map[Char,Int] = { | ||
+ | def aux(l: List[Char], acc: Map[Char,Int]): Map[Char,Int] = ??? | ||
+ | ??? | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | **Exercise 1.3** Write a function which takes a list of records first name, last name, CNP (encoded as tuples), and returns a list of the last names and ages of all females which are younger than the average of the entire list. E.g. ''List( (“Mary”, “2030694123456”), (“Anne”,“2121092123456”), (“Matei”, “5121202123456”), (“Maggie”, “2121078123456”) )'' yields ''List( (“Mary”,28), (“Anne”,30) )''. Maggie was born in '78, whereas Mary, Anne and Matei were born in '94, '92 and 2002, respectively. (Hint: use combinations of **map** with **filter**) | ||
+ | |||
+ | <code scala> | ||
+ | val example = List(("Mary", "2030694123456"), ("Anne", "2121092123456"), ("Matei", "5121202123456"), ("Maggie", "2121078123456")) | ||
+ | |||
+ | def youngerThanAverage(l: List[(String,String)]): List[(String,Int)] = { | ||
+ | def getAge(s:String):Int = ??? | ||
+ | // female gender = True | ||
+ | def getGender(s:String):Boolean = ??? | ||
+ | ??? | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | **Exercise 1.4** | ||
+ | A labelled graph is encoded as a String text where **each line** is an edge ''<from> <label> <to>'' | ||
+ | Example: | ||
+ | <code> | ||
+ | 0 r 1 | ||
+ | 0 r 2 | ||
+ | 0 g 1 | ||
+ | 1 g 2 | ||
+ | 1 b 3 | ||
+ | 1 r 4 | ||
+ | 4 g 1 | ||
+ | 3 b 2 | ||
+ | 2 r 2 | ||
+ | 2 g 3 | ||
+ | </code> | ||
+ | |||
+ | * Suppose we encode streets as labelled graphs, where each label 'r', 'g' or 'b' denotes a street color. These colors can be any alphabetical character, not necessarily just rgb. | ||
+ | * Given a **start node** and a **sequence of colors**, find all the destination nodes that can be accessed by traversing streets in that particular color. | ||
+ | * Example: Starting from 0, on the sequence ''rrg'', the destination node are ''1'' (0 red to 1 red to 4 green to 1) and ''3'' (0 red to 2, red to 2, red to 3) | ||
+ | * Hint: use **foldLeft** as well as **for expressions**. | ||
+ | |||
+ | <code scala> | ||
+ | val example = """0 r 1 | ||
+ | |0 r 2 | ||
+ | |0 g 1 | ||
+ | |1 g 2 | ||
+ | |1 b 3 | ||
+ | |1 r 4 | ||
+ | |4 g 1 | ||
+ | |3 b 2 | ||
+ | |2 r 2 | ||
+ | |2 g 3""".stripMargin | ||
+ | |||
+ | type Streets = Map[(Int,Char),Set[Int]] | ||
+ | def readStreets(s: String): Streets = ??? | ||
+ | |||
+ | def findDestination(start: Int, colors: String, streets: Streets): Set[Int] = ??? | ||
+ | </code> | ||
+ | |||
===== 2. Python introduction ===== | ===== 2. Python introduction ===== | ||
Line 31: | Line 90: | ||
==== List traversal ==== | ==== List traversal ==== | ||
- | **Exercise 1.1.** Write a function ''f(l)'' which determines the maximum of a list ''l'' of integers. | + | **Exercise 2.1.** Write a function ''f(l)'' which determines the maximum of a list ''l'' of integers. |
<hidden The pythonic way> | <hidden The pythonic way> | ||
Line 42: | Line 101: | ||
</hidden> | </hidden> | ||
- | **Exercise 1.2.** Modify the previous function to ''f(l,start,stop)'' and determine the maximum between positions ''start'' and ''stop''. | + | **Exercise 2.2.** Modify the previous function to ''f(l,start,stop)'' and determine the maximum between positions ''start'' and ''stop''. |
<hidden The pythonic way> | <hidden The pythonic way> | ||
Line 54: | Line 113: | ||
==== Slicing lists ==== | ==== Slicing lists ==== | ||
- | **Exercise 1.3.** Find the **longest** sequence of positive integers from a list. E.g. for the list ''[1,3,-1,2,0,1,5,4,-2,4,5,-3,0,1,2]'' the answer is ''[2,0,1,5,4]''. | + | **Exercise 2.3.** Find the **longest** sequence of positive integers from a list. E.g. for the list ''[1,3,-1,2,0,1,5,4,-2,4,5,-3,0,1,2]'' the answer is ''[2,0,1,5,4]''. |
<hidden The pythonic way> | <hidden The pythonic way> | ||
Line 72: | Line 131: | ||
</hidden> | </hidden> | ||
- | **Exercise 1.4.** Write a pythonic function to check if a list is palindrome. | + | **Exercise 2.4.** Write a pythonic function to check if a list is palindrome. |
===== Other datatypes ===== | ===== Other datatypes ===== | ||
Line 88: | Line 147: | ||
</code> | </code> | ||
- | **Exercise 1.5.** Write a function which determines (and returns) the **longest** sequence of consecutive integers from a list. | + | **Exercise 2.5.** Write a function which determines (and returns) the **longest** sequence of consecutive integers from a list. |
==== Dictionaries ==== | ==== Dictionaries ==== | ||
Line 388: | Line 447: | ||
* ''None'': represents the lack of a value (or an explicit None value); useful to mark functions which have no return value, or that a value may be purposefully missing (in combination with the ''|'' operator), and a None check may be necessary | * ''None'': represents the lack of a value (or an explicit None value); useful to mark functions which have no return value, or that a value may be purposefully missing (in combination with the ''|'' operator), and a None check may be necessary | ||
full documentation of the python typing module: [[https://docs.python.org/3/library/typing.html]] | full documentation of the python typing module: [[https://docs.python.org/3/library/typing.html]] | ||
+ | |||
+ | |||
+ | |||
+ | ===== Solutions ===== | ||
+ | <note important> | ||
+ | [[https://github.com/NethDR/LFA-LAB1-solutions|click here for spoilers/solutions]] | ||
+ | </note> |