fix: naming convention. Min. refactor (#116)
* fix: naming convention. Min. refactor * add: 05 for Kotlin
This commit is contained in:
10
05_hash_tables/kotlin/01_price_of_groceries.kt
Normal file
10
05_hash_tables/kotlin/01_price_of_groceries.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
val book = hashMapOf<String, Double>(
|
||||
"apple" to 0.67,
|
||||
"milk" to 1.49,
|
||||
"avocado" to 1.49
|
||||
)
|
||||
|
||||
println(book)
|
||||
}
|
||||
19
05_hash_tables/kotlin/02_check_voter.kt
Normal file
19
05_hash_tables/kotlin/02_check_voter.kt
Normal file
@@ -0,0 +1,19 @@
|
||||
val voted:HashMap<String,Boolean> = HashMap<String, Boolean>()
|
||||
|
||||
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<String>) {
|
||||
|
||||
checkVoter("tom")
|
||||
checkVoter("mike")
|
||||
checkVoter("mike")
|
||||
}
|
||||
14
05_hash_tables/kotlin/readme.md
Normal file
14
05_hash_tables/kotlin/readme.md
Normal file
@@ -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`
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user