add: map exercises and quiz
This commit is contained in:
38
22-maps/exercises/01-warm-up/main.go
Normal file
38
22-maps/exercises/01-warm-up/main.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Warm-up
|
||||
//
|
||||
// Create and print the following maps.
|
||||
//
|
||||
// 1. Phone numbers by last name
|
||||
// 2. Product availability by Product ID
|
||||
// 3. Multiple phone numbers by last name
|
||||
// 4. Shopping basket by Customer ID
|
||||
//
|
||||
// Each item in the shopping basket has a Product ID and
|
||||
// quantity. Through the map, you can tell:
|
||||
// "Mr. X has bought Y bananas"
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// Hint: Store phone numbers as text
|
||||
|
||||
// #1
|
||||
// Key : Last name
|
||||
// Element : Last name
|
||||
|
||||
// #2
|
||||
// Key : Product ID
|
||||
// Element : Available / Unavailable
|
||||
|
||||
// #3
|
||||
// Key : Last name
|
||||
// Element : Phone numbers
|
||||
|
||||
// #4
|
||||
// Key : Customer ID
|
||||
// Element Key:
|
||||
// Key: Product ID Element: Quantity
|
||||
}
|
36
22-maps/exercises/01-warm-up/solution/main.go
Normal file
36
22-maps/exercises/01-warm-up/solution/main.go
Normal file
@ -0,0 +1,36 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var (
|
||||
phones map[string]string
|
||||
// Key : Last name
|
||||
// Element : Last name
|
||||
|
||||
// Key : Product ID
|
||||
// Element : Available / Unavailable
|
||||
products map[int]bool
|
||||
|
||||
multiPhones map[string][]string
|
||||
// Key : Last name
|
||||
// Element : Phone numbers
|
||||
|
||||
basket map[int]map[int]int
|
||||
// Key : Customer ID
|
||||
// Element Key:
|
||||
// Key: Product ID Element: Quantity
|
||||
)
|
||||
|
||||
fmt.Printf("phones : %#v\n", phones)
|
||||
fmt.Printf("products : %#v\n", products)
|
||||
fmt.Printf("multiPhones: %#v\n", multiPhones)
|
||||
fmt.Printf("basket : %#v\n", basket)
|
||||
}
|
65
22-maps/exercises/02-populate/main.go
Normal file
65
22-maps/exercises/02-populate/main.go
Normal file
@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Populate and Lookup
|
||||
//
|
||||
// Add elements to the maps that you've declared in the
|
||||
// first exercise, and try them by looking up for the keys.
|
||||
//
|
||||
// Use map literals.
|
||||
//
|
||||
// After completing the exercise, remove the data and check
|
||||
// that your program still works.
|
||||
//
|
||||
//
|
||||
// 1. Phone numbers by last name
|
||||
// --------------------------
|
||||
// bowen 202-555-0179
|
||||
// dulin 03.37.77.63.06
|
||||
// greco 03489940240
|
||||
//
|
||||
// Print the dulin's phone number.
|
||||
//
|
||||
//
|
||||
// 2. Product availability by Product ID
|
||||
// ----------------
|
||||
// 617841573 true
|
||||
// 879401371 false
|
||||
// 576872813 true
|
||||
//
|
||||
// Is Product ID 879401371 available?
|
||||
//
|
||||
//
|
||||
// 3. Multiple phone numbers by last name
|
||||
// ------------------------------------------------------
|
||||
// bowen [202-555-0179]
|
||||
// dulin [03.37.77.63.06 03.37.70.50.05 02.20.40.10.04]
|
||||
// greco [03489940240 03489900120]
|
||||
//
|
||||
// What is Greco's second phone number?
|
||||
//
|
||||
//
|
||||
// 4. Shopping basket by Customer ID
|
||||
// -------------------------------
|
||||
// 100 [617841573:4 576872813:2]
|
||||
// 101 [576872813:5 657473833:20]
|
||||
// 102 []
|
||||
//
|
||||
// How many of 576872813 the customer 101 is going to buy?
|
||||
// (Product ID) (Customer ID)
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
//
|
||||
// 1. Run the solution to see the output
|
||||
// 2. Here is the output with empty maps:
|
||||
//
|
||||
// dulin's phone number: N/A
|
||||
// Product ID #879401371 is not available
|
||||
// greco's 2nd phone number: N/A
|
||||
// Customer #101 is going to buy 0 from Product ID #576872813.
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
63
22-maps/exercises/02-populate/solution/main.go
Normal file
63
22-maps/exercises/02-populate/solution/main.go
Normal file
@ -0,0 +1,63 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
phones := map[string]string{
|
||||
"bowen": "202-555-0179",
|
||||
"dulin": "03.37.77.63.06",
|
||||
"greco": "03489940240",
|
||||
}
|
||||
|
||||
products := map[int]bool{
|
||||
617841573: true,
|
||||
879401371: false,
|
||||
576872813: true,
|
||||
}
|
||||
|
||||
multiPhones := map[string][]string{
|
||||
"bowen": []string{"202-555-0179"},
|
||||
"dulin": []string{
|
||||
"03.37.77.63.06", "03.37.70.50.05", "02.20.40.10.04",
|
||||
},
|
||||
"greco": []string{"03489940240", "03489900120"},
|
||||
}
|
||||
|
||||
basket := map[int]map[int]int{
|
||||
100: {617841573: 4, 576872813: 2},
|
||||
101: {576872813: 5, 657473833: 20},
|
||||
102: {},
|
||||
}
|
||||
|
||||
// Print dulin's phone number.
|
||||
who, phone := "dulin", "N/A"
|
||||
if v, ok := phones[who]; ok {
|
||||
phone = v
|
||||
}
|
||||
fmt.Printf("%s's phone number: %s\n", who, phone)
|
||||
|
||||
// Is Product ID 879401371 available?
|
||||
id, status := 879401371, "available"
|
||||
if !products[id] {
|
||||
status = "not " + status
|
||||
}
|
||||
fmt.Printf("Product ID #%d is %s\n", id, status)
|
||||
|
||||
// What is Greco's second phone number?
|
||||
who, phone = "greco", "N/A"
|
||||
if phones := multiPhones[who]; len(phones) >= 2 {
|
||||
phone = phones[1]
|
||||
}
|
||||
fmt.Printf("%s's 2nd phone number: %s\n", who, phone)
|
||||
|
||||
// How many of 576872813 the customer 101 is going to buy?
|
||||
cid, pid := 101, 576872813
|
||||
fmt.Printf("Customer #%d is going to buy %d from Product ID #%d.\n", cid, basket[cid][pid], pid)
|
||||
}
|
68
22-maps/exercises/03-students/main.go
Normal file
68
22-maps/exercises/03-students/main.go
Normal file
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Students
|
||||
//
|
||||
// Create a program that returns the students by the given
|
||||
// Hogwarts house name (see the data below).
|
||||
//
|
||||
// Print the students sorted by name.
|
||||
//
|
||||
// "bobo" doesn't belong to Hogwarts, remove it by using
|
||||
// the delete function.
|
||||
//
|
||||
//
|
||||
// RESTRICTIONS
|
||||
//
|
||||
// + Add the following data to your map as is.
|
||||
// Do not sort it manually and do not modify it.
|
||||
//
|
||||
// + Slices in the map shouldn't be sorted (changed).
|
||||
// HINT: Copy them.
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
//
|
||||
// go run main.go
|
||||
//
|
||||
// Please type a Hogwarts house name.
|
||||
//
|
||||
//
|
||||
// go run main.go bobo
|
||||
//
|
||||
// Sorry. I don't know anything about "bobo".
|
||||
//
|
||||
//
|
||||
// go run main.go hufflepuf
|
||||
//
|
||||
// ~~~ hufflepuf students ~~~
|
||||
//
|
||||
// + diggory
|
||||
// + helga
|
||||
// + scamander
|
||||
// + wenlock
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// House Student Name
|
||||
// ---------------------------
|
||||
// gryffindor weasley
|
||||
// gryffindor hagrid
|
||||
// gryffindor dumbledore
|
||||
// gryffindor lupin
|
||||
// hufflepuf wenlock
|
||||
// hufflepuf scamander
|
||||
// hufflepuf helga
|
||||
// hufflepuf diggory
|
||||
// ravenclaw flitwick
|
||||
// ravenclaw bagnold
|
||||
// ravenclaw wildsmith
|
||||
// ravenclaw montmorency
|
||||
// slytherin horace
|
||||
// slytherin nigellus
|
||||
// slytherin higgs
|
||||
// slytherin scorpius
|
||||
// bobo wizardry
|
||||
// bobo unwanted
|
||||
}
|
48
22-maps/exercises/03-students/solution/main.go
Normal file
48
22-maps/exercises/03-students/solution/main.go
Normal file
@ -0,0 +1,48 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func main() {
|
||||
houses := map[string][]string{
|
||||
"gryffindor": {"weasley", "hagrid", "dumbledore", "lupin"},
|
||||
"hufflepuf": {"wenlock", "scamander", "helga", "diggory", "bobo"},
|
||||
"ravenclaw": {"flitwick", "bagnold", "wildsmith", "montmorency"},
|
||||
"slytherin": {"horace", "nigellus", "higgs", "bobo", "scorpius"},
|
||||
"bobo": {"wizardry", "unwanted"},
|
||||
}
|
||||
|
||||
// remove "bobo" house
|
||||
delete(houses, "bobo")
|
||||
|
||||
args := os.Args[1:]
|
||||
if len(args) < 1 {
|
||||
fmt.Println("Please type a Hogwarts house name.")
|
||||
return
|
||||
}
|
||||
|
||||
house, students := args[0], houses[args[0]]
|
||||
if students == nil {
|
||||
fmt.Printf("Sorry. I don't know anything about %q.\n", house)
|
||||
return
|
||||
}
|
||||
|
||||
// only sort the clone
|
||||
clone := append([]string(nil), students...)
|
||||
sort.Strings(clone)
|
||||
|
||||
fmt.Printf("~~~ %s students ~~~\n\n", house)
|
||||
for _, student := range clone {
|
||||
fmt.Printf("\t+ %s\n", student)
|
||||
}
|
||||
}
|
@ -1,5 +1,15 @@
|
||||
# IDEAS
|
||||
# Header
|
||||
|
||||
* Retrieve sorted map keys
|
||||
What you will learn?
|
||||
|
||||
* Roman literals
|
||||
1. **[Warm Up](https://github.com/inancgumus/learngo/tree/master/22-maps/exercises/01-warm-up)**
|
||||
|
||||
Create and print the maps.
|
||||
|
||||
2. **[Populate and Lookup](https://github.com/inancgumus/learngo/tree/master/22-maps/exercises/02-populate)**
|
||||
|
||||
Add elements to the maps that you've declared in the first exercise, and try them by looking up for the keys.
|
||||
|
||||
3. **[Hogwarts Students](https://github.com/inancgumus/learngo/tree/master/22-maps/exercises/03-students)**
|
||||
|
||||
Create a program that returns the students by the given Hogwarts house name.
|
59
22-maps/questions/README.md
Normal file
59
22-maps/questions/README.md
Normal file
@ -0,0 +1,59 @@
|
||||
## Why are maps used?
|
||||
For example, here is a program that uses a slice to find an element among millions of elements.
|
||||
```go
|
||||
millions := []int{/* millions of elements */}
|
||||
for _, v := range millions {
|
||||
if v == userQuery {
|
||||
// do something
|
||||
}
|
||||
}
|
||||
```
|
||||
1. Maps allow fast-lookup for map keys in O(1) time *CORRECT*
|
||||
2. Maps allow fast-lookup for map keys in O(n) time
|
||||
3. Maps allow fast-traversal on map keys in O(1) time
|
||||
|
||||
> **1:** That's right. Maps work in O(1) in average for fast-lookup.
|
||||
>
|
||||
> **2:** Map doesn't work in O(n) time for key lookup.
|
||||
>
|
||||
|
||||
## When shouldn't you use a map?
|
||||
1. To find an element through a key
|
||||
2. To loop over the map keys *CORRECT*
|
||||
3. To add structured data to your program
|
||||
|
||||
> **1:** That is when you use a map.
|
||||
>
|
||||
> **2:** Right! Looping over map keys happen in O(n) time. So, maps are the worst data structures for key traversing.
|
||||
>
|
||||
> **3:** Maps don't allow you to add structured data to your program.
|
||||
|
||||
|
||||
## Which type below cannot be a map key?
|
||||
1. map[string]int
|
||||
2. []string
|
||||
3. []int
|
||||
4. []bool
|
||||
5. All of them *CORRECT*
|
||||
|
||||
> **5:** Slices, maps, and function values are not comparable. So, they cannot be map keys.
|
||||
>
|
||||
|
||||
## Which are the following map's key and element types?
|
||||
```go
|
||||
map[string]map[int]bool
|
||||
```
|
||||
1. Key: string Element: bool
|
||||
2. Key: string Element: int
|
||||
3. Key: string Element: map[int]
|
||||
4. Key: string Element: map[int]string *CORRECT*
|
||||
|
||||
> **4:** The map contains other maps. The element type of a map can be of any type.
|
||||
>
|
||||
|
||||
## What is a map behind the scenes?
|
||||
1. A map header
|
||||
2. A pointer to a map header *CORRECT*
|
||||
3. Tiny data structure with 3 fields: Pointer, Length and Capacity
|
||||
|
||||
> **2**: That's right. Maps are complex data structures. However, each map value is only a pointer to a map header (which is a more complex data structure).
|
Reference in New Issue
Block a user