From 6b9faa9cab921b93b086cd5623a4e206a014ba19 Mon Sep 17 00:00:00 2001 From: bigpas Date: Tue, 12 Nov 2019 15:18:17 +0000 Subject: [PATCH] fix: naming convention. Min. refactor (#116) * fix: naming convention. Min. refactor * add: 05 for Kotlin --- .../kotlin/01_price_of_groceries.kt | 10 +++ 05_hash_tables/kotlin/02_check_voter.kt | 19 +++++ 05_hash_tables/kotlin/readme.md | 14 ++++ 06_breadth-first_search/golang/bfs.go | 71 ++++++++++--------- 4 files changed, 80 insertions(+), 34 deletions(-) create mode 100644 05_hash_tables/kotlin/01_price_of_groceries.kt create mode 100644 05_hash_tables/kotlin/02_check_voter.kt create mode 100644 05_hash_tables/kotlin/readme.md diff --git a/05_hash_tables/kotlin/01_price_of_groceries.kt b/05_hash_tables/kotlin/01_price_of_groceries.kt new file mode 100644 index 0000000..3de68db --- /dev/null +++ b/05_hash_tables/kotlin/01_price_of_groceries.kt @@ -0,0 +1,10 @@ +fun main(args: Array) { + + val book = hashMapOf( + "apple" to 0.67, + "milk" to 1.49, + "avocado" to 1.49 + ) + + println(book) +} diff --git a/05_hash_tables/kotlin/02_check_voter.kt b/05_hash_tables/kotlin/02_check_voter.kt new file mode 100644 index 0000000..97acaee --- /dev/null +++ b/05_hash_tables/kotlin/02_check_voter.kt @@ -0,0 +1,19 @@ +val voted:HashMap = HashMap() + +fun checkVoter(name: String) { + + if(!voted.containsKey(name)){ + voted.put(name, true) + println("let them vote!") + } else { + println("kick them out!") + } + +} + +fun main(args: Array) { + + checkVoter("tom") + checkVoter("mike") + checkVoter("mike") +} diff --git a/05_hash_tables/kotlin/readme.md b/05_hash_tables/kotlin/readme.md new file mode 100644 index 0000000..bc2c951 --- /dev/null +++ b/05_hash_tables/kotlin/readme.md @@ -0,0 +1,14 @@ +# HOW TO: + +## Example 01: + +### Compile +`kotlinc 01_price_of_groceries.kt -include-runtime -d price_of_groceries.jar` +### Execute +`java -jar price_of_groceries.jar` + +## Example 02: +### Compile +`kotlinc 02_check_voter.kt -include-runtime -d check_voter.jar` +### Execute +`java -jar check_voter.jar` diff --git a/06_breadth-first_search/golang/bfs.go b/06_breadth-first_search/golang/bfs.go index a362431..274c727 100644 --- a/06_breadth-first_search/golang/bfs.go +++ b/06_breadth-first_search/golang/bfs.go @@ -5,11 +5,45 @@ import ( ) func PersonIsSeller(name string) bool { - /* Assuming the person whose name ends with m as the mango seller as per the book */ return string(name[len(name)-1]) == "m" } -func search(name string) bool { +func search(aGraph map[string][]string, name string) bool { + + //search queue + var searchQueue []string + searchQueue = append(searchQueue, aGraph[name]...) + var searched []string + + for len(searchQueue) > 0 { + var person = searchQueue[0] + searchQueue = searchQueue[1:] + personAlreadySearched := false + + /* Checking to see if this person has already been searched */ + for i := 0; i < len(searched); i++ { + if searched[i] == person { + personAlreadySearched = true + + } + } + + if personAlreadySearched == false { + if PersonIsSeller(person) { + fmt.Println(person, "is a mango seller!") + return true + } + + searchQueue = append(searchQueue, aGraph[person]...) + searched = append(searched, person) + } + + } + return false + +} + +func main() { graph := make(map[string][]string) graph["you"] = []string{"alice", "bob", "claire"} graph["bob"] = []string{"anuj", "peggy"} @@ -20,36 +54,5 @@ func search(name string) bool { graph["thom"] = []string{} graph["jonny"] = []string{} - //search queue - var search_queue []string - search_queue = append(search_queue, graph[name]...) - var searched []string - for len(search_queue) > 0 { - var person = search_queue[0] - search_queue = search_queue[1:] - var person_already_searched = false - /* Checking to see if this person has already been searched */ - for i := 0; i < len(searched); i++ { - if searched[i] == person { - person_already_searched = true - - } - } - if person_already_searched == false { - if PersonIsSeller(person) { - fmt.Println(person + " is the mango seller!") - return true - } - - search_queue = append(search_queue, graph[person]...) - searched = append(searched, person) - } - - } - return false - -} - -func main() { - search("you") + search(graph, "you") }