Skip to content
Snippets Groups Projects
Commit 8e982b9b authored by joel.vonderwe's avatar joel.vonderwe
Browse files

Exos

parent 41092d38
Branches
No related tags found
No related merge requests found
*~
target/
organization := "ch.hepia"
name := "tpscala"
version := "2020"
scalaVersion := "2.13.1"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.1.0" % "test"
)
fork in run := true
javaOptions in run += "-Xmx2G"
scalacOptions ++= Seq(
"-deprecation", // Emit warning and location for usages of deprecated APIs.
"-encoding", "utf-8", // Specify character encoding used by source files.
"-explaintypes", // Explain type errors in more detail.
"-feature", // Emit warning and location for usages of features that should be imported explicitly.
"-language:existentials", // Existential types (besides wildcard types) can be written and inferred
"-language:higherKinds", // Allow higher-kinded types
"-unchecked", // Enable additional warnings where generated code depends on assumptions.
"-Xcheckinit", // Wrap field accessors to throw an exception on uninitialized access.
"-Xfatal-warnings", // Fail the compilation if there are any warnings.
// "-Xlint:adapted-args", // Warn if an argument list is modified to match the receiver.
// "-Xlint:by-name-right-associative", // By-name parameter of right associative operator.
// "-Xlint:constant", // Evaluation of a constant arithmetic expression results in an error.
// "-Xlint:delayedinit-select", // Selecting member of DelayedInit.
// "-Xlint:doc-detached", // A Scaladoc comment appears to be detached from its element.
// "-Xlint:inaccessible", // Warn about inaccessible types in method signatures.
// "-Xlint:infer-any", // Warn when a type argument is inferred to be `Any`.
// "-Xlint:missing-interpolator", // A string literal appears to be missing an interpolator id.
// "-Xlint:nullary-override", // Warn when non-nullary `def f()' overrides nullary `def f'.
// "-Xlint:nullary-unit", // Warn when nullary methods return Unit.
// "-Xlint:option-implicit", // Option.apply used implicit view.
// "-Xlint:package-object-classes", // Class or object defined in package object.
// "-Xlint:poly-implicit-overload", // Parameterized overloaded implicit methods are not visible as view bounds.
// "-Xlint:private-shadow", // A private field (or class parameter) shadows a superclass field.
// "-Xlint:stars-align", // Pattern sequence wildcard must align with sequence component.
// "-Xlint:type-parameter-shadow", // A local type parameter shadows a type already in scope.
// "-Xlint:unsound-match", // Pattern match may not be typesafe.
// "-Yno-adapted-args", // Do not adapt an argument list (either by inserting () or creating a tuple) to match the receiver.
// "-Ypartial-unification", // Enable partial unification in type constructor inference
// "-Ywarn-dead-code", // Warn when dead code is identified.
// "-Ywarn-extra-implicit", // Warn when more than one implicit parameter section is defined.
// "-Ywarn-inaccessible", // Warn about inaccessible types in method signatures.
// "-Ywarn-infer-any", // Warn when a type argument is inferred to be `Any`.
// "-Ywarn-nullary-override", // Warn when non-nullary `def f()' overrides nullary `def f'.
// "-Ywarn-nullary-unit", // Warn when nullary methods return Unit.
// "-Ywarn-numeric-widen", // Warn when numerics are widened.
// "-Ywarn-unused:implicits", // Warn if an implicit parameter is unused.
// "-Ywarn-unused:imports", // Warn if an import selector is not referenced.
// "-Ywarn-unused:locals", // Warn if a local definition is unused.
// "-Ywarn-unused:params", // Warn if a value parameter is unused.
// "-Ywarn-unused:patvars", // Warn if a variable bound in a pattern is unused.
// "-Ywarn-unused:privates", // Warn if a private member is unused.
// "-Ywarn-value-discard" // Warn when non-Unit expression results are unused.
)
scalacOptions in (Compile, console) --= Seq("-Ywarn-unused:imports", "-Xfatal-warnings")
scalaSource in Compile := baseDirectory.value / "src"
javaSource in Compile := baseDirectory.value / "java" / "src"
scalaSource in Test := baseDirectory.value / "test"
sbt.version=1.3.7
\ No newline at end of file
/*
* Implémenter les fonctions suivantes en suivant les commentaires.
* Respectez également les consignes suivantes:
* - Toutes les fonctions doivent être pures
* - Tout doit être immutable (val au lieu de var)
* - Utiliser la recursion terminale si possible
* - Utiliser le pattern matching si possible
*/
object Serie3 {
/*
* Donne la longueur d'une liste. Votre implémentation ne peut
* utiliser aucune fonction de List excepté isEmpty()
*/
def len[A]( as: List[A] ): Int = {
def lenRec(as: List[A], size: Int): Int = {
as match {
case Nil => size
case _ :: rest => lenRec(rest, size+1)
}
}
lenRec(as, 0)
}
/*
* Donne la longueur d'une liste. Votre implémentation ne peut
* utiliser aucune fonction de List excepté: -
* - isEmpty
* - ::
* - head
* - tail
*/
def rev[A]( as: List[A] ): List[A] = {
def revRec(as: List[A], newL: List[A]): List[A] = {
as match {
case Nil => newL
case v :: rest => revRec(rest, v :: newL)
}
}
revRec(as, List.empty[A])
}
/*
* Donne la longueur d'une liste. Votre implémentation ne peut
* utiliser aucune fonction de List excepté: -
* - isEmpty
* - head
* - tail
*/
def sum( xs: List[Int] ): Int = {
def sumRec(xs: List[Int], sum: Int): Int = {
xs match {
case Nil => sum
case v :: rest => sumRec(rest, sum+v)
}
}
sumRec(xs, 0)
}
/*
* Retourne vrai si et seulement si la liste xs ne
* comprend que des valeures vraies. Votre implémentation
* ne peutcutiliser aucune fonction de List excepté:
* - isEmpty
* - head
* - tail
*/
final def and( xs: List[Boolean] ): Boolean = xs match {
case Nil => true
case false :: _ => false
case _ :: rest => and(rest)
}
/*
* Applatit une liste. Votre implémentation
* ne peut utiliser aucune fonction de List excepté:
* - isEmpty
* - head
* - tail
* - ++
*/
def flat[A]( las: List[List[A]] ): List[A] = {
def flatRec(las: List[List[A]], flatten: List[A]): List[A] = {
las match {
case Nil => flatten
case as :: rest => flatRec(rest, flatten ++ as)
}
}
flatRec(las, List.empty[A])
}
/*
* Applatit une liste. Votre implémentation
* ne peut utiliser aucune fonction de List !
* Vous devez utiliser le pattern matching.
*/
final def even[A]( as: List[A] ): Boolean = {
as match {
case Nil => true
case _ :: _ :: rest => evenRec(rest)
case _ :: Nil => false
}
}
}
object Serie3Main extends App {
import Serie3._
val is = List( 1, 2, 3, 4, 5 )
val bs1 = List( true, true, false, true )
val bs2 = List( true, true, true )
val las1 = List.empty[List[Int]]
val las2 = List( List(1,2), List(3), List(4,5) )
require( rev(is) == List( 5, 4, 3, 2, 1 ) )
require( len(is) == 5 )
require( len( las1 ) == 0 )
require( len( bs1 ) == 4 )
require( flat(las1) == Nil )
require( flat(las2) == is )
require( sum(is) == 15 )
require( sum(flat(las2)) == sum(is) )
require( rev( bs1 ) == List( true, false, true, true ) )
require( rev( bs2 ) == bs2 )
require( even( is ) == false )
require( even( bs1 ) == true )
require( even( las1 ) == true )
}
\ No newline at end of file
package ch.unige.hepia.tp
object Euclid extends App {
// Implémenter correctement cette fonction
def gcd( a: Int, b: Int ): Int = {
if (b == 0) {
a
} else {
gcd(b, a % b)
}
}
println( gcd( 100, 30 ) ) //=> 10
println( gcd( 30, 100 ) ) //=> 10
println( gcd( 12, 3732 ) ) //=> 12
println( gcd( 1, 3732 ) ) //=> 1
println( gcd( 25, 3732 ) ) //)> 1
}
class Stack[A] {
import java.util.{ArrayList => JArrayList};
private var stack = new JArrayList[A];
//Retourne vrai si la pile est vide
def isEmpty: Boolean = stack.isEmpty
// Retourne le nombre d'éléments de la pile
def size: Int = stack.size
// Empile un élément sur la pile
def push( a: A ): Unit = stack.add(0, a)
// Dépile et retourne le sommet de la pile
def pop: A = {
if (stack.isEmpty) {
throw new Exception
} else {
stack.get(0)
stack.remove(0)
}
}
//Inverse les deux éléments au sommet de la pile
//ne fait rien si moins de deux éléments
def swap: Unit = {
if (stack.size >= 2) {
val v = pop
stack.add(1, v)
}
}
}
object Stack extends App {
println("Stack testing...")
var s = new Stack[Int]
assert(s.isEmpty)
assert(s.size == 0)
s.push(1)
assert(s.pop == 1)
s.push(1)
s.push(2)
s.push(3)
assert(s.size == 3)
assert(!s.isEmpty)
assert(s.pop == 3)
s.swap
assert(s.pop == 1)
assert(s.pop == 2)
println("Stack tested !")
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment