Giter Site home page Giter Site logo

scala-exercises's Introduction

scala-exercises

Introduction

The purpose of this repository is to provide an skeleton to solve a series of scala exercises.

Exercises

Exercise 1

Provide a generic method to process a list of any type and apply a function to the elements of the list.

Example 1: Transform each element into an arbitrary string representation.

val l = List(1,2,3,4,5)
val r1 = processList(l, transformToString)
println(r1)

Output:

 Map(5 -> string: 5, 1 -> string: 1, 2 -> string: 2,
      3 -> string: 3, 4 -> string: 4) 

Example 2: Transform each number obtain its square value.

val r2 = processList(l, square)
println(r2)`

Output:

Map(5 -> 25, 1 -> 1, 2 -> 4, 3 -> 9, 4 -> 16)

Notes

  • Provide two implementations. One of them must use foldLeft.
  • The elements of the list can be of any type A.

Exercise 2

Define a Person case class that contains the following attributes:

  • name
  • age
  • mail

Code a method to obtain a map of users indexed by age.

def groupByAge(persons: List[Person]): Map[Int, List[Person]]

Notes

  • Provide two implementations. One of them must use foldLeft.
  • Generate a list of 50 persons using random names, mails, and ages.

Exercise 3

  1. Define a superclass called Vehicle.
  2. Define two traits: AirVehicle, and GroundVehicle.
  3. Define a class Car, Bus, Helicopter and Plane that extend both Vehicle and the previous traits.
  4. Each vehicle is identified by a name.
  5. An air vehicle has the following attributes: airspeed, and maxSpeed.
  6. A ground vehicle has a maxSpeed.

Provide a method to filter ground vehicles.

def filterGroundVehicles(vehicles: List[Vehicle]): List[GroundVehicle]

Provide a method to obtain the average maximum speed.

def getAverageMaxSpeed(vehicles: List[Vehicle]): Double

Notes

  • Use pattern matching in the filter function.
  • Generate a list of 50 vehicles using random values.

Extensions

  • Generate a new vehicle that is both AirVehicle and GroundVehicle.

Exercise 4

Define a linked list data structure with the following public methods:

  • head: Obtain the first element of the list.
  • last: Obtain the last element of the list.
  • isEmpty: Whether the list is empty.
  • get(index): Get the element at possition index if possible. Return an option.
  • add: Add a new element to the list.
  • size: Get the size of the list.

Notes

  • The list must accept generic types.
  • The list is mutable but it is implemented using in-mutable structures.
  • The list must be implemented as a linked list.
  • Use of scala or Java collections as internal structures is forbidden.

scala-exercises's People

Contributors

dhiguero avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.