Exercises
Exercise 1: Basic List Operations
Create a list of integers from 1 to 10. Then, perform the following tasks:
- Extract the first element.
- Extract the last element.
- Get all elements except the first.
- Get all elements except the last.
- Check if the list contains the number 5.
Exercise 2: Concatenation and Addition
- Create two lists: one with even numbers from 2 to 10, and another with odd numbers from 1 to 9.
- Concatenate these lists into a single list.
- Add the number 0 to the beginning of the concatenated list.
Exercise 3: Mapping and Filtering
Given a list of numbers from 1 to 10:
- Multiply each number by 2.
- Filter the resulting list to keep only numbers greater than 10.
Exercise 4: Folding and Reducing
- Create a list of integers from 1 to 5.
- Compute the sum of all elements using
foldLeft. - Compute the product of all elements using
reduce.
Exercise 5: Using ListBuffer
- Create a mutable
ListBufferand add elements 1, 2, and 3 to it. - Append the element 4 to the list buffer.
- Remove the element 2 from the list buffer.
- Convert the
ListBufferto an immutable list.
Exercise 6: Working with Arrays
- Create an array of integers from 1 to 5.
- Update the third element (index 2) to 10.
- Print the length of the array.
- Iterate over the array and print each element.
Exercise 7: Zipping and Unzipping
- Create two lists: one with numbers 1 to 3, and another with the strings "one", "two", and "three".
- Zip these lists together.
- Unzip the zipped list back into two separate lists.
Exercise 8: Using Range to Create Lists
- Create a list of numbers from 1 to 10 using
Range. - Create a list of even numbers from 2 to 20 using
Range. - Create a list of numbers from 10 to 1 (in reverse order) using
Range.
Exercise 9: LazyList
- Create a
LazyListof the first 10 positive integers. - Define a
LazyListthat generates an infinite sequence of even numbers. - Take the first 10 elements from the infinite
LazyListand print them.
Exercise 10: Grouping and Partitioning
- Create a list of integers from 1 to 10.
- Group the integers by whether they are even or odd.
- Partition the list into two lists: one containing numbers less than or equal to 5, and another containing numbers greater than 5.