Advertisement
mehedi2022

chapter_7_Exercise

Sep 15th, 2022 (edited)
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.96 KB | None | 0 0
  1. val flightsDatabase = List(
  2.   ("Geneva", "Vienna", 200),
  3.   ("Vienna", "London", 150),
  4.   ("London", "Geneva", 300),
  5.   ("Geneva", "Zurich", 100),
  6.   ("Vienna", "London", 300),
  7.   ("Vienna", "Helsinki", 200)
  8. )
  9.  
  10. //1. Exercise: Find all the flights arriving to London.
  11.  
  12.   println(flightsDatabase.filter(f => f._2 == "London"))
  13.  
  14. //2. Exercise: given a val numbers = List(1, 2, 3, 4), use the map function to make a new list where each number is squared.
  15.  
  16. val numbers = List(1, 2, 3, 4)
  17. val squareNumber = numbers.map(n => n*n)
  18. println(squareNumber)
  19.  
  20. //**why did we get a nested list?
  21.     Because of we get a new list for connecting flight. For creating new list from the original list there created the nested list.
  22.  
  23. //3. Exercise: use foreach to print every pair of connecting flights nicely, so that the end user of your search engine doesn't see the parentheses or the List.
  24.  
  25. flightsDatabase.foreach(f=>println(s"Deptutare : ${f._1}; Arrival : ${f._2}; Cost : ${f._3}"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement