-
Notifications
You must be signed in to change notification settings - Fork 0
/
findLuckyNumbers.sc
123 lines (110 loc) · 2.83 KB
/
findLuckyNumbers.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.Optional
sealed trait Expr
case class Val(v: Double) extends Expr
case object Sum extends Expr
case object Diff extends Expr
case object Prod extends Expr
case object Div extends Expr
def evaluate(list: List[Expr]): Optional[Double] = {
var fun: (Double, Expr)=>Double = (a,b: Val) => a+b.v
var lastexpr: (Double, Double)=>Double = (a: Double, b: Double)=>a+b
def ev(list: List[Expr]):Optional[Double] = {
list.last match {
case Diff => lastexpr = (a: Double, b: Double) => a-b
case Sum => lastexpr = (a: Double, b: Double) => a+b
case Prod => lastexpr = (a: Double, b: Double) => a*b
case Div => lastexpr = (a: Double, b: Double) => a/b
case Val(v) => las
}
ev(list.tail)
}
}
//2
sealed trait Expr
case class Sum(a: Double, b: Double) extends Expr
case class Neg(a: Double) extends Expr
def evaluate(expr: Expr) : Double = expr match{
case Sum(a,b) => a+b
case Neg(a) => -a
}
evaluate(Sum(1.2, -5.4))
evaluate(Neg(-3))
evaluate(Neg(3))
//2
sealed trait Bool
case object True extends Bool
case object False extends Bool
def and(b: Bool, b2: Bool): Bool = {
(b,b2) match {
case (True, True) => True
case (_,_) => False
}
}
def or(b: Bool, b2: Bool): Bool = {
(b,b2) match {
case (False,False) => False
case (_, _) => True
}
}
def xor(b: Bool, b2: Bool): Bool = {
(b,b2) match {
case (True, True) => False
case (False, False) => False
case (_,_) => True
}
}
def nor(b: Bool, b2: Bool): Bool = {
(b,b2) match {
case (False,False) => True
case (_, _) => False
}
}
def nand(b: Bool, b2: Bool): Bool = {
(b,b2) match {
case (True,True) => False
case (_, _) => True
}
}
or(True,False)
or(False,False)
or(True, True)
nand(False, True)
and(False, True)
and(True, True)
and(True, False)
and(False, False)
//5 not tested
def foldLeft[A, B, C](l: List[(A,B)], z: C)(f: (C, (A, B)) => C): C =
l.isEmpty match {
case true => z
case false => foldLeft(l.tail, f(z, l.head))(f)
}
//def foldRight[A,B,C](l:List[(A,B)], z: C)(f: ((A,B), C) => C): C= {
// l.isEmpty match {
// case true => z
// case false => foldRight(l.tail, f(z, l.head))(f)
// }
//}
//4
def findLuckyNumbers() : List[Int] = {
def isLucky(value: Int) : Boolean = {
def sumThisShit(n : Int, result : Int) : Int = {
if(n == 0 ) result
else sumThisShit(n/10, result + n%10)
}
val right = value%1000
val left = value/1000 % 1000
(sumThisShit(left, 0) == sumThisShit(right, 0))
}
val list = List.range(0, 999999)
list.filter(x => isLucky(x))
}
def divide[A](s: Stream[A]): (Stream[A], Stream[A]) = {
if(s.isEmpty) (Stream(), Stream())
else if(s.size==1) (Stream(s.head), Stream())
else{
val div = divide(s.tail.tail)
(s.head#::div._1, s.tail.head#::div._2)}
}
print(divide(Stream(1))._1.toList)
print(divide(Stream(1))._2.toList)