Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0074d86

Browse files
committed
week 6 course
1 parent f9e528f commit 0074d86

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed

src/main/scala/week6/Maps.sc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
val capitalOfCountry = Map("US" ->"Washington",
2+
"France" -> "Paris",
3+
"Germany" -> "Berlin",
4+
"Netherland" -> "Den Haag")
5+
6+
capitalOfCountry("US")
7+
capitalOfCountry("Belgium")
8+
9+
capitalOfCountry get "Belgium"
10+
capitalOfCountry get "France"
11+
12+
13+
14+
def showCapital(country: String) : String =
15+
capitalOfCountry.get(country) match {
16+
case Some(capital) => capital
17+
case None => "missing data"
18+
}
19+
20+
showCapital("France")
21+
showCapital("US")
22+
showCapital("Belgium")
23+
24+
25+
26+
val fruits = List("apple", "banana","pear", "pineapple", "orange")
27+
28+
fruits.sortWith(_.length < _.length)
29+
fruits.sorted
30+
31+
32+
fruits.groupBy(_.head)

src/main/scala/week6/aroundSet.sc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
val fruits = Set("bananan", "apple", "pear")
2+
3+
val s = (1 to 6).toSet
4+
val s10 = (1 to 10).toSet
5+
6+
s map (_ +2)
7+
s.nonEmpty
8+
9+
fruits filter (_ startsWith("app"))
10+
11+
def isSafe(col: Int, queens: List[Int]) : Boolean = {
12+
//queens contains col
13+
val row = queens.length
14+
val queensWithRow = (row -1 to 0 by -1) zip queens
15+
16+
queensWithRow forall{
17+
case (r, c) => col != c && math.abs(col - c) != row - r
18+
19+
}
20+
21+
22+
}
23+
24+
25+
26+
def queens(n: Int) : Set[List[Int]] = {
27+
def placeQueens(k: Int) : Set[List[Int]] =
28+
29+
if (k == 0) Set(List())
30+
else
31+
for {
32+
queens <- placeQueens(k -1)
33+
col <- 0 until n
34+
if isSafe(col, queens)
35+
36+
} yield col :: queens
37+
38+
39+
placeQueens(n)
40+
41+
42+
43+
44+
}
45+
46+
def show (queens: List[Int]) = {
47+
48+
val lines =
49+
for (col <- queens.reverse)
50+
yield Vector.fill(queens.length) ("* ").updated(col, "X ").mkString
51+
"\n" + (lines mkString "\n")
52+
}
53+
54+
55+
queens(4)
56+
queens(4) map show
57+
(queens(4) map show) mkString "\n"
58+
//(queens(12) map show) mkString "\n"
59+
60+

src/main/scala/week6/pairs.sc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
//object pairs {
3+
val n = 7
4+
(1 until n) map (i =>
5+
(1 until i) map (j => (i,j)))
6+
7+
// Vector(Vector(),
8+
// Vector((2,1)),
9+
// Vector((3,1), (3,2)),
10+
// Vector((4,1), (4,2), (4,3)),
11+
// Vector((5,1), (5,2), (5,3), (5,4)),
12+
// Vector((6,1), (6,2), (6,3), (6,4), (6,5)))
13+
14+
//}
15+
16+
17+
18+
((1 until n) map (i =>
19+
(1 until i) map (j => (i,j)))).flatten
20+
21+
22+
//with flatMap == Map + flatten
23+
24+
(1 until n) flatMap (i =>
25+
(1 until i) map (j => (i,j)))
26+
27+
28+
29+
//now we filter the pair
30+
def isPrime(n: Int) = (2 until n) forall (n % _ != 0)
31+
32+
(1 until n) flatMap (i =>
33+
(1 until i) map (j => (i,j))) filter (pair =>
34+
isPrime(pair._1 + pair._2))
35+
36+
37+
38+
//for expresseion in order to make more readable the use
39+
// of HOF such as map, filter , flatMap
40+
41+
case class Person(name: String, age: Int)
42+
43+
//List[Person]
44+
val persons = List(Person("tot", 2), Person("papa", 40), Person("mama", 35), Person("child1", 21))
45+
46+
for (p <- persons if p.age > 20) yield p.name
47+
48+
49+
50+
for {
51+
i <- 1 until n //generator
52+
j <- 1 until i
53+
if isPrime(i + j) //filter here
54+
} yield (i,j) // always result as a List
55+
56+
57+
58+
def scalarProduct(xs: List[Double], ys: List[Double]): Double =
59+
(for ((x, y) <- xs zip ys ) yield x*y )
60+
.sum
61+
62+
val abs = List(1.3,3.2, 45.3)
63+
val ord = List(76,4, 56.3, 23.1)
64+
65+
scalarProduct(abs,ord)
66+
67+
68+

src/main/scala/week6/polynom.sc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
class Poly(val terms: Map[Int, Double]) {
3+
def + (other: Poly) = new Poly(terms ++ other.terms)
4+
5+
override def toString =
6+
(for ((exp, coef) <- terms.toList.sorted.reverse) yield coef + "x^"+ exp) mkString " + "
7+
8+
}
9+
10+
val p1 = new Poly(Map(1->2.0, 3 -> 4.0, 5->6.2))
11+
val p2 = new Poly(Map(0->3.0, 3->7.0))
12+
13+
p1 + p2
14+

0 commit comments

Comments
 (0)