Adding Scala examples for Chapter 4 and Chapter 5 (#30)
* Add files via upload * Update 01_countdown.scala * Update 02_greet.scala * Update 02_greet.scala * Create scala * Delete scala * Add files via upload * Add files via upload
This commit is contained in:
6
04_quicksort/scala/01_loop_sum.scala
Normal file
6
04_quicksort/scala/01_loop_sum.scala
Normal file
@@ -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)))
|
||||
4
04_quicksort/scala/02_recursive_sum.scala
Normal file
4
04_quicksort/scala/02_recursive_sum.scala
Normal file
@@ -0,0 +1,4 @@
|
||||
def sum(list: List[Int]): Int = {
|
||||
if(list.isEmpty) return 0
|
||||
else return list(0) + sum(list.drop(1))
|
||||
}
|
||||
4
04_quicksort/scala/03_recursive_count.scala
Normal file
4
04_quicksort/scala/03_recursive_count.scala
Normal file
@@ -0,0 +1,4 @@
|
||||
def count(list: List[Int]): Int = {
|
||||
if(list.isEmpty) return 0
|
||||
return 1 + count(list.drop(1))
|
||||
}
|
||||
9
04_quicksort/scala/04_recursive_max.scala
Normal file
9
04_quicksort/scala/04_recursive_max.scala
Normal file
@@ -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
|
||||
}
|
||||
16
04_quicksort/scala/05_quicksort.scala
Normal file
16
04_quicksort/scala/05_quicksort.scala
Normal file
@@ -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))
|
||||
6
05_hash_tables/scala/01_price_of_groceries.scala
Normal file
6
05_hash_tables/scala/01_price_of_groceries.scala
Normal file
@@ -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)
|
||||
12
05_hash_tables/scala/02_check_voter.scala
Normal file
12
05_hash_tables/scala/02_check_voter.scala
Normal file
@@ -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")
|
||||
Reference in New Issue
Block a user