Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exercises

Exercise 1: Basic List Operations

val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

val firstElement = list.head          // Output: 1
val lastElement = list.last           // Output: 10
val allButFirst = list.tail           // Output: List(2, 3, 4, 5, 6, 7, 8, 9, 10)
val allButLast = list.init            // Output: List(1, 2, 3, 4, 5, 6, 7, 8, 9)
val containsFive = list.contains(5)   // Output: true

Exercise 2: Concatenation and Addition

val evenList = List(2, 4, 6, 8, 10)
val oddList = List(1, 3, 5, 7, 9)

val concatenatedList = evenList ++ oddList  // Output: List(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)
val finalList = 0 :: concatenatedList       // Output: List(0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9)

Exercise 3: Mapping and Filtering

val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

val multipliedList = list.map(_ * 2)        // Output: List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
val filteredList = multipliedList.filter(_ > 10)  // Output: List(12, 14, 16, 18, 20)

Exercise 4: Folding and Reducing

val list = List(1, 2, 3, 4, 5)

val sum = list.foldLeft(0)(_ + _)   // Output: 15
val product = list.reduce(_ * _)    // Output: 120

Exercise 5: Using ListBuffer

import scala.collection.mutable.ListBuffer

val buffer = ListBuffer(1, 2, 3)

buffer += 4         // ListBuffer(1, 2, 3, 4)
buffer -= 2         // ListBuffer(1, 3, 4)

val immutableList = buffer.toList  // Output: List(1, 3, 4)

Exercise 6: Working with Arrays

val array = Array(1, 2, 3, 4, 5)

array(2) = 10         // Array(1, 2, 10, 4, 5)

val length = array.length  // Output: 5

array.foreach(println)  // Output: 1 2 10 4 5

Exercise 7: Zipping and Unzipping

val numbers = List(1, 2, 3)
val words = List("one", "two", "three")

val zipped = numbers.zip(words)  // Output: List((1,"one"), (2,"two"), (3,"three"))

val (unzippedNumbers, unzippedWords) = zipped.unzip
// unzippedNumbers: List(1, 2, 3)
// unzippedWords: List("one", "two", "three")

Exercise 8: Using Range to Create Lists

val rangeList = Range(1, 11).toList            // Output: List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val evenRangeList = Range(2, 21, 2).toList     // Output: List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
val reverseRangeList = Range(10, 0, -1).toList // Output: List(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

Exercise 9: LazyList

val lazyList = LazyList.from(1).take(10)  // Output: LazyList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

val evenNumbers = LazyList.from(2, 2)  // Infinite LazyList of even numbers

val firstTenEvens = evenNumbers.take(10)
firstTenEvens.foreach(println)  // Output: 2 4 6 8 10 12 14 16 18 20

Exercise 10: Grouping and Partitioning

val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

val grouped = list.groupBy(_ % 2)  // Output: Map(0 -> List(2, 4, 6, 8, 10), 1 -> List(1, 3, 5, 7, 9))

val (lessThanOrEqualFive, greaterThanFive) = list.partition(_ <= 5)
// lessThanOrEqualFive: List(1, 2, 3, 4, 5)
// greaterThanFive: List(6, 7, 8, 9, 10)