add: map exercises and quiz

This commit is contained in:
Inanc Gumus
2019-05-05 00:41:44 +03:00
parent 03c753fe56
commit c6ec3af17e
8 changed files with 390 additions and 3 deletions

View 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() {
}

View 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)
}