added midterm preparation
This commit is contained in:
		
							
								
								
									
										29
									
								
								src/MidTermPrep1/Ex1.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/MidTermPrep1/Ex1.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| import scala.annotation.tailrec | ||||
|  | ||||
| def foo(x: Int): Int = { | ||||
|   println("Foo !") | ||||
|   x + 1 | ||||
| } | ||||
|  | ||||
| def bar(x: => Int): Unit = { | ||||
|   println("x1=" + x) | ||||
|   println("x2=" + x) | ||||
| } | ||||
|  | ||||
| bar(foo(3)) | ||||
| // Foo ! | ||||
| // x1=4 | ||||
| // Foo ! | ||||
| // x2=4 | ||||
|  | ||||
|  | ||||
| def toUpper(s: String): String = { | ||||
|   @tailrec | ||||
|   def helper(s: String, res: String): String = { | ||||
|     if (s.isEmpty) res | ||||
|     else helper(s.tail, res + s.head.toUpper) | ||||
|   } | ||||
|   helper(s, "") | ||||
| } | ||||
|  | ||||
| toUpper("hello") | ||||
							
								
								
									
										20
									
								
								src/MidTermPrep1/Ex2.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/MidTermPrep1/Ex2.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| import scala.annotation.tailrec | ||||
|  | ||||
| def balanceMatch(chars: List[Char]): Boolean = { | ||||
|   @tailrec | ||||
|   def checkStep(chars: List[Char], n: Int): Boolean = { | ||||
|     if (chars.isEmpty) n == 0 | ||||
|     else if (n < 0) false | ||||
|     else checkStep(chars.tail, chars.head match { | ||||
|       case '(' => n + 1 | ||||
|       case ')' => n - 1 | ||||
|       case _ => n | ||||
|     }) | ||||
|   } | ||||
|   checkStep(chars, 0) | ||||
| } | ||||
|  | ||||
| balanceMatch("(if (x == 0) then max (1, x))".toList) | ||||
| balanceMatch("I told him (that it's not (yet) done). (But he wasn't listening)".toList) | ||||
| balanceMatch(")".toList) | ||||
| balanceMatch("())()".toList) | ||||
							
								
								
									
										36
									
								
								src/MidTermPrep1/Ex3.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/MidTermPrep1/Ex3.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| import scala.annotation.tailrec | ||||
|  | ||||
| def countTrue(bools: List[Boolean]): Int = { | ||||
|   bools.foldLeft(0)((n, bool) => if (bool) n + 1 else n) | ||||
| } | ||||
|  | ||||
| countTrue(List(true, true, false, true, false, false)) | ||||
|  | ||||
| def remDup[T](list: List[T]): List[T] = { | ||||
|   list.foldRight(List.empty[T])((elem: T, res: List[T]) => { | ||||
|     if (res.contains(elem)) res | ||||
|     else elem::res | ||||
|   }) | ||||
| } | ||||
|  | ||||
| def remDup2[T](list: List[T]): List[T] = { | ||||
|   list.foldLeft(List.empty[T])((res: List[T], elem: T) => { | ||||
|     if (res.contains(elem)) res | ||||
|     else res :+ elem | ||||
|   }) | ||||
| } | ||||
|  | ||||
| def remDup3[T](list: List[T]): List[T] = { | ||||
|   @tailrec | ||||
|   def helper(list: List[T], distinct: List[T]): List[T] = { | ||||
|     list match { | ||||
|       case head::rest if distinct.contains(head) => helper(rest, distinct) | ||||
|       case head::rest => helper(rest, distinct :+ head) | ||||
|       case Nil => distinct | ||||
|     } | ||||
|   } | ||||
|   helper(list, List.empty[T]) | ||||
| } | ||||
|  | ||||
| remDup(List(5,3,2,4,3,2,3,3)) // List(5,3,2,4) | ||||
| remDup(List("hello", "youpi", "hello", "hello")) // List("hello", "youpi") | ||||
		Reference in New Issue
	
	Block a user