diff --git a/04_quicksort/scala/01_loop_sum.scala b/04_quicksort/scala/01_loop_sum.scala new file mode 100644 index 0000000..1f27254 --- /dev/null +++ b/04_quicksort/scala/01_loop_sum.scala @@ -0,0 +1,6 @@ +def sum(arr: List[Int]): Int = { + var total: Int = 0 + for(x <- arr) total += x + total +} +println(sum(List(1,2,3,4))) \ No newline at end of file diff --git a/04_quicksort/scala/02_recursive_sum.scala b/04_quicksort/scala/02_recursive_sum.scala new file mode 100644 index 0000000..65af080 --- /dev/null +++ b/04_quicksort/scala/02_recursive_sum.scala @@ -0,0 +1,4 @@ +def sum(list: List[Int]): Int = { + if(list.isEmpty) return 0 + else return list(0) + sum(list.drop(1)) +} \ No newline at end of file diff --git a/04_quicksort/scala/03_recursive_count.scala b/04_quicksort/scala/03_recursive_count.scala new file mode 100644 index 0000000..0db63a7 --- /dev/null +++ b/04_quicksort/scala/03_recursive_count.scala @@ -0,0 +1,4 @@ +def count(list: List[Int]): Int = { + if(list.isEmpty) return 0 + return 1 + count(list.drop(1)) +} \ No newline at end of file diff --git a/04_quicksort/scala/04_recursive_max.scala b/04_quicksort/scala/04_recursive_max.scala new file mode 100644 index 0000000..d69ad29 --- /dev/null +++ b/04_quicksort/scala/04_recursive_max.scala @@ -0,0 +1,9 @@ +def max(list: List[Int]): Int = { + if(list.length == 2){ + if(list(0) > list(1)) return list(0) + else return list(1) + } + val sub_max = max(list.drop(1)) + if(list(0) > sub_max) list(0) + else sub_max +} \ No newline at end of file diff --git a/04_quicksort/scala/05_quicksort.scala b/04_quicksort/scala/05_quicksort.scala new file mode 100644 index 0000000..eb98dfc --- /dev/null +++ b/04_quicksort/scala/05_quicksort.scala @@ -0,0 +1,16 @@ +def quicksort(array: Array[Int]): Array[Int] = { + if(array.length < 2){ + //base case, arrays with 0 or 1 element are already "sorted" + return array + } + else { + //recursive case + val pivot: Int = array(0) + //sub-array of all the elements less than the pivot + val less: Array[Int] = array.drop(1).filter(x => x < pivot) + //sub-array of all the elements greater than the pivot + val greater: Array[Int] = array.drop(1).filter(x => x > pivot) + return (quicksort(less) :+ pivot) ++ quicksort(greater) + } +} +quicksort(Array(10,5,2,3)) \ No newline at end of file diff --git a/05_hash_tables/scala/01_price_of_groceries.scala b/05_hash_tables/scala/01_price_of_groceries.scala new file mode 100644 index 0000000..d279c76 --- /dev/null +++ b/05_hash_tables/scala/01_price_of_groceries.scala @@ -0,0 +1,6 @@ +import scala.collection.mutable +val book = mutable.Map[String, Double]() +book += ("apple" -> 0.67) +book += ("milk" -> 1.49) +book += ("avocado" -> 1.49) +println(book) \ No newline at end of file diff --git a/05_hash_tables/scala/02_check_voter.scala b/05_hash_tables/scala/02_check_voter.scala new file mode 100644 index 0000000..b995e20 --- /dev/null +++ b/05_hash_tables/scala/02_check_voter.scala @@ -0,0 +1,12 @@ +import scala.collection.mutable +val voted = mutable.Map[String, Boolean]() +def check_voter(name: String): Unit = { + if(voted.exists(_ == (name,true))){ + println("kick them out!") + } else{ + voted += (name -> true) + } +} +check_voter("tom") +check_voter("mike") +check_voter("mike") \ No newline at end of file