add: array exercises
This commit is contained in:
@ -1,2 +0,0 @@
|
|||||||
- [ ] add challenge link to the moodly's resources
|
|
||||||
- [ ] add exercises 1 and 2 after the array basics quiz
|
|
@ -1,15 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// EXERCISE: Name
|
|
||||||
// ?
|
|
||||||
//
|
|
||||||
// NOTE
|
|
||||||
// ?
|
|
||||||
//
|
|
||||||
// EXPECTED OUTPUT
|
|
||||||
// ?
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
27
14-arrays/exercises/05-fix/main.go
Normal file
27
14-arrays/exercises/05-fix/main.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Fix
|
||||||
|
//
|
||||||
|
// 1. Uncomment the code
|
||||||
|
// 2. And fix the problems
|
||||||
|
// 3. BONUS: Simplify the code
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// var names [3]string = [3]string{
|
||||||
|
// "Einstein" "Shepard"
|
||||||
|
// "Tesla"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// var books [5]string = [5]string{
|
||||||
|
// "Kafka's Revenge",
|
||||||
|
// "Stay Golden",
|
||||||
|
// "",
|
||||||
|
// "",
|
||||||
|
// ""
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fmt.Printf("%q\n", names)
|
||||||
|
// fmt.Printf("%q\n", books)
|
||||||
|
}
|
11
14-arrays/exercises/05-fix/solution/main.go
Normal file
11
14-arrays/exercises/05-fix/solution/main.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
names := [...]string{"Einstein", "Shepard", "Tesla"}
|
||||||
|
books := [5]string{"Kafka's Revenge", "Stay Golden"}
|
||||||
|
|
||||||
|
fmt.Printf("%q\n", names)
|
||||||
|
fmt.Printf("%q\n", books)
|
||||||
|
}
|
34
14-arrays/exercises/06-compare/main.go
Normal file
34
14-arrays/exercises/06-compare/main.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Compare the Arrays
|
||||||
|
//
|
||||||
|
// 1. Uncomment the code
|
||||||
|
// 2. Fix the problems so that arrays become comparable
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// week := [...]string{"Monday", "Tuesday"}
|
||||||
|
// wend := [4]string{"Saturday", "Sunday"}
|
||||||
|
|
||||||
|
// fmt.Println(week != wend)
|
||||||
|
|
||||||
|
// evens := [...]int{2, 4, 6, 8, 10}
|
||||||
|
// evens2 := [...]int32{2, 4, 6, 8, 10}
|
||||||
|
|
||||||
|
// fmt.Println(evens == evens2)
|
||||||
|
|
||||||
|
// Use : uint8 for one of the arrays instead of byte here.
|
||||||
|
// Remember: Aliased types are the same types.
|
||||||
|
image := [5]byte{'h', 'i'}
|
||||||
|
var data [5]byte
|
||||||
|
|
||||||
|
fmt.Println(data == image)
|
||||||
|
}
|
22
14-arrays/exercises/06-compare/solution/main.go
Normal file
22
14-arrays/exercises/06-compare/solution/main.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
week := [...]string{"Monday", "Tuesday"}
|
||||||
|
wend := [...]string{"Saturday", "Sunday"}
|
||||||
|
|
||||||
|
fmt.Println(week != wend)
|
||||||
|
|
||||||
|
evens := [...]int{2, 4, 6, 8, 10}
|
||||||
|
evens2 := [...]int{2, 4, 6, 8, 10}
|
||||||
|
|
||||||
|
fmt.Println(evens == evens2)
|
||||||
|
|
||||||
|
// Use : uint8 for one of the arrays instead of byte here.
|
||||||
|
// Remember: Aliased types are the same types.
|
||||||
|
image := [5]uint8{'h', 'i'}
|
||||||
|
var data [5]byte
|
||||||
|
|
||||||
|
fmt.Println(data == image)
|
||||||
|
}
|
31
14-arrays/exercises/07-assign/main.go
Normal file
31
14-arrays/exercises/07-assign/main.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Assign the Arrays
|
||||||
|
//
|
||||||
|
// 1. Create an array named books
|
||||||
|
// 2. Add book titles to the array
|
||||||
|
// 3. Create two more copies of the array named: upper and lower
|
||||||
|
// 4. Change the book titles to uppercase in the upper array only
|
||||||
|
// 5. Change the book titles to lowercase in the lower array only
|
||||||
|
// 6. Print all the arrays
|
||||||
|
// 7. Observe that the arrays are not connected when they're copied.
|
||||||
|
//
|
||||||
|
// NOTE
|
||||||
|
// Check out the strings package, it has functions to convert letters to
|
||||||
|
// upper and lower cases.
|
||||||
|
//
|
||||||
|
// BONUS
|
||||||
|
// Invent your own arrays with different types other than string,
|
||||||
|
// and do some manipulations on them.
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// Note: Don't worry about the book titles here, you can use any title.
|
||||||
|
//
|
||||||
|
// books: ["Kafka's Revenge" "Stay Golden" "Everythingship"]
|
||||||
|
// upper: ["KAFKA'S REVENGE" "STAY GOLDEN" "EVERYTHINGSHIP"]
|
||||||
|
// lower: ["kafka's revenge" "stay golden" "everythingship"]
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
21
14-arrays/exercises/07-assign/solution/main.go
Normal file
21
14-arrays/exercises/07-assign/solution/main.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
books := [...]string{"Kafka's Revenge", "Stay Golden", "Everythingship"}
|
||||||
|
|
||||||
|
upper, lower := books, books
|
||||||
|
|
||||||
|
for i := range books {
|
||||||
|
upper[i] = strings.ToUpper(upper[i])
|
||||||
|
lower[i] = strings.ToLower(lower[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("books: %q\n", books)
|
||||||
|
fmt.Printf("upper: %q\n", upper)
|
||||||
|
fmt.Printf("lower: %q\n", lower)
|
||||||
|
}
|
25
14-arrays/exercises/08-wizard-printer/main.go
Normal file
25
14-arrays/exercises/08-wizard-printer/main.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Wizard Printer
|
||||||
|
//
|
||||||
|
// In this exercise, your goal is to display a few famous scientists
|
||||||
|
// in a pretty table.
|
||||||
|
//
|
||||||
|
// 1. Create a multi-dimensional array
|
||||||
|
// 2. In each inner array, store the scientist's name, lastname and his/her
|
||||||
|
// nickname
|
||||||
|
// 3. Print their information in a pretty table using a loop.
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// First Name Last Name Nickname
|
||||||
|
// ==================================================
|
||||||
|
// Albert Einstein time
|
||||||
|
// Isaac Newton apple
|
||||||
|
// Stephen Hawking blackhole
|
||||||
|
// Marie Curie radium
|
||||||
|
// Charles Darwin fittest
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
26
14-arrays/exercises/08-wizard-printer/solution/main.go
Normal file
26
14-arrays/exercises/08-wizard-printer/solution/main.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
names := [...][3]string{
|
||||||
|
{"First Name", "Last Name", "Nickname"},
|
||||||
|
{"Albert", "Einstein", "emc2"},
|
||||||
|
{"Isaac", "Newton", "apple"},
|
||||||
|
{"Stephen", "Hawking", "blackhole"},
|
||||||
|
{"Marie", "Curie", "radium"},
|
||||||
|
{"Charles", "Darwin", "fittest"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range names {
|
||||||
|
n := names[i]
|
||||||
|
fmt.Printf("%-15s %-15s %-15s\n", n[0], n[1], n[2])
|
||||||
|
|
||||||
|
if i == 0 {
|
||||||
|
fmt.Println(strings.Repeat("=", 50))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
41
14-arrays/exercises/09-currency-converter/main.go
Normal file
41
14-arrays/exercises/09-currency-converter/main.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Currency Converter
|
||||||
|
//
|
||||||
|
// In this exercise, you're going to display currency exchange ratios
|
||||||
|
// against USD.
|
||||||
|
//
|
||||||
|
// 1. Declare a few constants with iota. They're going to be the keys
|
||||||
|
// of the array.
|
||||||
|
//
|
||||||
|
// 2. Create an array that contains the conversion ratios.
|
||||||
|
//
|
||||||
|
// You should use keyed elements and the contants you've declared before.
|
||||||
|
//
|
||||||
|
// 3. Get the USD amount to be converted from the command line.
|
||||||
|
//
|
||||||
|
// 4. Handle the error cases for missing or invalid input.
|
||||||
|
//
|
||||||
|
// 5. Print the exchange ratios.
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// go run main.go
|
||||||
|
// Please provide the amount to be converted.
|
||||||
|
//
|
||||||
|
// go run main.go invalid
|
||||||
|
// Invalid amount. It should be a number.
|
||||||
|
//
|
||||||
|
// go run main.go 10.5
|
||||||
|
// 10.50 USD is 9.24 EUR
|
||||||
|
// 10.50 USD is 8.19 GBP
|
||||||
|
// 10.50 USD is 1186.71 JPY
|
||||||
|
//
|
||||||
|
// go run main.go 1
|
||||||
|
// 1.00 USD is 0.88 EUR
|
||||||
|
// 1.00 USD is 0.78 GBP
|
||||||
|
// 1.00 USD is 113.02 JPY
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
37
14-arrays/exercises/09-currency-converter/solution/main.go
Normal file
37
14-arrays/exercises/09-currency-converter/solution/main.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
const (
|
||||||
|
EUR = iota
|
||||||
|
GBP
|
||||||
|
JPY
|
||||||
|
)
|
||||||
|
|
||||||
|
rates := [...]float64{
|
||||||
|
EUR: 0.88,
|
||||||
|
GBP: 0.78,
|
||||||
|
JPY: 113.02,
|
||||||
|
}
|
||||||
|
|
||||||
|
args := os.Args[1:]
|
||||||
|
if len(args) != 1 {
|
||||||
|
fmt.Println("Please provide the amount to be converted.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
amount, err := strconv.ParseFloat(args[0], 64)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Invalid amount. It should be a number.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%.2f USD is %.2f EUR\n", amount, rates[EUR]*amount)
|
||||||
|
fmt.Printf("%.2f USD is %.2f GBP\n", amount, rates[GBP]*amount)
|
||||||
|
fmt.Printf("%.2f USD is %.2f JPY\n", amount, rates[JPY]*amount)
|
||||||
|
}
|
@ -1,24 +0,0 @@
|
|||||||
// 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
|
|
||||||
|
|
||||||
// search for books in hipster's love
|
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// EXERCISE: Name
|
|
||||||
// ?
|
|
||||||
//
|
|
||||||
// NOTE
|
|
||||||
// ?
|
|
||||||
//
|
|
||||||
// EXPECTED OUTPUT
|
|
||||||
// ?
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
56
14-arrays/exercises/10-hipsters-love-search/main.go
Normal file
56
14-arrays/exercises/10-hipsters-love-search/main.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Hipster's Love Search Engine
|
||||||
|
//
|
||||||
|
// Your goal is to let people search for books of Hipster's Love Bookstore.
|
||||||
|
//
|
||||||
|
// 1. Create an array with these book titles:
|
||||||
|
// Kafka's Revenge
|
||||||
|
// Stay Golden
|
||||||
|
// Everythingship
|
||||||
|
// Kafka's Revenge 2nd Edition
|
||||||
|
//
|
||||||
|
// 2. Get the search query from the command-line argument
|
||||||
|
//
|
||||||
|
// 3. Search for the books in the books array
|
||||||
|
//
|
||||||
|
// 4. When the programs find the book, print it.
|
||||||
|
// 5. Otherwise, print that the book doesn't exist.
|
||||||
|
//
|
||||||
|
// 6. Handle the errors.
|
||||||
|
//
|
||||||
|
// RESTRICTION:
|
||||||
|
// + The search should be case insensitive.
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// go run main.go
|
||||||
|
// Tell me a book title
|
||||||
|
//
|
||||||
|
// go run main.go STAY
|
||||||
|
// Search Results:
|
||||||
|
// + Stay Golden
|
||||||
|
//
|
||||||
|
// go run main.go sTaY
|
||||||
|
// Search Results:
|
||||||
|
// + Stay Golden
|
||||||
|
//
|
||||||
|
// go run main.go "Kafka's Revenge"
|
||||||
|
// Search Results:
|
||||||
|
// + Kafka's Revenge
|
||||||
|
// + Kafka's Revenge 2nd Edition
|
||||||
|
//
|
||||||
|
// go run main.go void
|
||||||
|
// Search Results:
|
||||||
|
// We don't have the book: "void"
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
@ -13,27 +13,24 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const yearly = 4
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var books [yearly]string
|
books := [4]string{
|
||||||
|
"Kafka's Revenge",
|
||||||
|
"Stay Golden",
|
||||||
|
"Everythingship",
|
||||||
|
"Kafka's Revenge 2nd Edition",
|
||||||
|
}
|
||||||
|
|
||||||
books[0] = "Kafka's Revenge"
|
args := os.Args[1:]
|
||||||
books[1] = "Stay Golden"
|
if len(args) != 1 {
|
||||||
books[2] = "Everythingship"
|
|
||||||
books[3] += books[0] + " 2nd Edition"
|
|
||||||
|
|
||||||
if len(os.Args) != 2 {
|
|
||||||
fmt.Println("Tell me a book title")
|
fmt.Println("Tell me a book title")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
query := os.Args[1]
|
query := strings.ToLower(args[0])
|
||||||
|
|
||||||
fmt.Println("Search Results:")
|
fmt.Println("Search Results:")
|
||||||
fmt.Println("---------------")
|
|
||||||
|
|
||||||
var found bool
|
var found bool
|
||||||
|
|
||||||
for _, v := range books {
|
for _, v := range books {
|
||||||
if strings.Contains(strings.ToLower(v), query) {
|
if strings.Contains(strings.ToLower(v), query) {
|
||||||
fmt.Println("+", v)
|
fmt.Println("+", v)
|
||||||
@ -42,6 +39,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
fmt.Printf("We don't have that book: %q\n", query)
|
fmt.Printf("We don't have the book: %q\n", query)
|
||||||
}
|
}
|
||||||
}
|
}
|
42
14-arrays/exercises/11-average/main.go
Normal file
42
14-arrays/exercises/11-average/main.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Find the Average
|
||||||
|
//
|
||||||
|
// Your goal is to fill an array with numbers and find the average.
|
||||||
|
//
|
||||||
|
// 1. Get the numbers from the command-line.
|
||||||
|
//
|
||||||
|
// 2. Create an array and assign the given numbers to that array.
|
||||||
|
//
|
||||||
|
// 3. Print the given numbers and their average.
|
||||||
|
//
|
||||||
|
// RESTRICTION
|
||||||
|
// + Maximum 5 numbers can be provided
|
||||||
|
// + If one of the arguments are not a valid number, skip it
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// go run main.go
|
||||||
|
// Please tell me numbers (maximum 5 numbers).
|
||||||
|
//
|
||||||
|
// go run main.go 1 2 3 4 5 6
|
||||||
|
// Please tell me numbers (maximum 5 numbers).
|
||||||
|
//
|
||||||
|
// go run main.go 1 2 3 4 5
|
||||||
|
// Your numbers: [1 2 3 4 5]
|
||||||
|
// Average: 3
|
||||||
|
//
|
||||||
|
// go run main.go 1 a 2 b 3
|
||||||
|
// Your numbers: [1 0 2 0 3]
|
||||||
|
// Average: 2
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
42
14-arrays/exercises/11-average/solution/main.go
Normal file
42
14-arrays/exercises/11-average/solution/main.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// 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"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
args := os.Args[1:]
|
||||||
|
if l := len(args); l == 0 || l > 5 {
|
||||||
|
fmt.Println("Please tell me numbers (maximum 5 numbers).")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
sum float64
|
||||||
|
nums [5]float64
|
||||||
|
total float64
|
||||||
|
)
|
||||||
|
|
||||||
|
for i, v := range args {
|
||||||
|
n, err := strconv.ParseFloat(v, 64)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
total++
|
||||||
|
nums[i] = n
|
||||||
|
sum += n
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Your numbers:", nums)
|
||||||
|
fmt.Println("Average:", sum/total)
|
||||||
|
}
|
41
14-arrays/exercises/12-sorter/main.go
Normal file
41
14-arrays/exercises/12-sorter/main.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Number Sorter
|
||||||
|
//
|
||||||
|
// Your goal is to sort the given numbers from the command-line.
|
||||||
|
//
|
||||||
|
// 1. Get the numbers from the command-line.
|
||||||
|
//
|
||||||
|
// 2. Create an array and assign the given numbers to that array.
|
||||||
|
//
|
||||||
|
// 3. Sort the given numbers and print them.
|
||||||
|
//
|
||||||
|
// RESTRICTION
|
||||||
|
// + Maximum 5 numbers can be provided
|
||||||
|
// + If one of the arguments are not a valid number, skip it
|
||||||
|
//
|
||||||
|
// HINTS
|
||||||
|
// + You can use the bubble-sort algorithm to sort the numbers.
|
||||||
|
// Please watch this: https://youtu.be/nmhjrI-aW5o?t=7
|
||||||
|
//
|
||||||
|
// + When swapping for elements, do not check for the last element.
|
||||||
|
// Or, you will receive this error:
|
||||||
|
// "panic: runtime error: index out of range"
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// go run main.go
|
||||||
|
// Please give me up to 5 numbers.
|
||||||
|
//
|
||||||
|
// go run main.go 6 5 4 3 2 1
|
||||||
|
// Sorry. Go arrays are fixed. So, for now, I'm only supporting sorting 5 numbers...
|
||||||
|
//
|
||||||
|
// go run main.go 5 4 3 2 1
|
||||||
|
// [1 2 3 4 5]
|
||||||
|
//
|
||||||
|
// go run main.go 5 4 a c 1
|
||||||
|
// [0 0 1 4 5]
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
58
14-arrays/exercises/12-sorter/solution/main.go
Normal file
58
14-arrays/exercises/12-sorter/solution/main.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
// 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"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
args := os.Args[1:]
|
||||||
|
|
||||||
|
switch l := len(args); {
|
||||||
|
case l == 0:
|
||||||
|
fmt.Println("Please give me up to 5 numbers.")
|
||||||
|
return
|
||||||
|
case l > 5:
|
||||||
|
fmt.Println("Sorry. Go arrays are fixed.",
|
||||||
|
"So, for now, I'm only supporting sorting 5 numbers...")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var nums [5]float64
|
||||||
|
|
||||||
|
// fill the array with the numbers
|
||||||
|
for i, v := range args {
|
||||||
|
n, err := strconv.ParseFloat(v, 64)
|
||||||
|
if err != nil {
|
||||||
|
// skip if it's not a valid number
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
nums[i] = n
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
check whether it's the last element or not:
|
||||||
|
i < len(nums)-1
|
||||||
|
|
||||||
|
check whether the next number is greater than the current one, if so, swap it:
|
||||||
|
v > nums[i+1]
|
||||||
|
*/
|
||||||
|
for range nums {
|
||||||
|
for i, v := range nums {
|
||||||
|
if i < len(nums)-1 && v > nums[i+1] {
|
||||||
|
nums[i], nums[i+1] = nums[i+1], nums[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(nums)
|
||||||
|
}
|
48
14-arrays/exercises/13-word-finder/main.go
Normal file
48
14-arrays/exercises/13-word-finder/main.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Word Finder
|
||||||
|
//
|
||||||
|
// Your goal is to search for the words inside the corpus.
|
||||||
|
//
|
||||||
|
// Note: This exercise is similar to the previous word finder program:
|
||||||
|
// https://github.com/inancgumus/learngo/tree/master/13-loops/10-word-finder-labeled-switch
|
||||||
|
//
|
||||||
|
// 1. Get the search query from the command-line (it can be multiple words)
|
||||||
|
//
|
||||||
|
// 2. Filter these words, do not search for them:
|
||||||
|
// and, or, was, the, since, very
|
||||||
|
//
|
||||||
|
// To do this, use an array for the filtered words.
|
||||||
|
//
|
||||||
|
// 3. Print the words found.
|
||||||
|
//
|
||||||
|
// RESTRICTION
|
||||||
|
// + The search and the filtering should be case insensitive
|
||||||
|
//
|
||||||
|
// HINT
|
||||||
|
// + strings.Fields function converts a given string a slice.
|
||||||
|
// You can find its example in the worder finder program that I've mentioned
|
||||||
|
// above.
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// go run main.go
|
||||||
|
// Please give me a word to search.
|
||||||
|
//
|
||||||
|
// go run main.go and was
|
||||||
|
//
|
||||||
|
// go run main.go AND WAS
|
||||||
|
//
|
||||||
|
// go run main.go cat beginning
|
||||||
|
// #2 : "cat"
|
||||||
|
// #11: "beginning"
|
||||||
|
//
|
||||||
|
// go run main.go Cat Beginning
|
||||||
|
// #2 : "cat"
|
||||||
|
// #11: "beginning"
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
const corpus = "lazy cat jumps again and again and again since the beginning this was very important"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
48
14-arrays/exercises/13-word-finder/solution/main.go
Normal file
48
14-arrays/exercises/13-word-finder/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"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const corpus = "lazy cat jumps again and again and again since the beginning this was very important"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
query := os.Args[1:]
|
||||||
|
if len(query) == 0 {
|
||||||
|
fmt.Println("Please give me a word to search.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filter := [...]string{
|
||||||
|
"and", "or", "was", "the", "since", "very",
|
||||||
|
}
|
||||||
|
|
||||||
|
words := strings.Fields(strings.ToLower(corpus))
|
||||||
|
|
||||||
|
queries:
|
||||||
|
for _, q := range query {
|
||||||
|
q = strings.ToLower(q)
|
||||||
|
|
||||||
|
for _, v := range filter {
|
||||||
|
if q == v {
|
||||||
|
continue queries
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, w := range words {
|
||||||
|
if q == w {
|
||||||
|
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
# Array Exercises
|
|
||||||
|
|
||||||
- get data from command-line
|
|
||||||
- into a fixed array; see how it blows beyond its len
|
|
||||||
|
|
||||||
- add items
|
|
||||||
- get items
|
|
||||||
- check the length
|
|
||||||
- reverse the array
|
|
||||||
- shuffle the items
|
|
||||||
- find the first item that contains x
|
|
||||||
- find the last item that contains y
|
|
||||||
- find the duplicate items
|
|
@ -1,6 +1,6 @@
|
|||||||
# Array Exercises
|
# Array Exercises
|
||||||
|
|
||||||
## Basic Exercises
|
## Exercises Level I - Basics
|
||||||
|
|
||||||
1. **[Declare Empty Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/01-declare-empty)**
|
1. **[Declare Empty Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/01-declare-empty)**
|
||||||
|
|
||||||
@ -10,10 +10,24 @@
|
|||||||
|
|
||||||
4. **[Refactor to Ellipsis](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/04-ellipsis)**
|
4. **[Refactor to Ellipsis](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/04-ellipsis)**
|
||||||
|
|
||||||
|
5. **[Fix](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/05-fix)**
|
||||||
|
|
||||||
|
6. **[Compare the Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/06-compare)**
|
||||||
|
|
||||||
|
7. **[Assign the Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/07-assign)**
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Program Exercises
|
## Exercises Level II
|
||||||
|
|
||||||
????. **[text](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/)**
|
1. **[Wizard Printer](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/08-wizard-printer)**
|
||||||
|
|
||||||
?
|
2. **[Currency Converter](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/09-currency-converter)**
|
||||||
|
|
||||||
|
3. **[Hipster's Bookstore Search Engine](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/10-hipsters-love-search)**
|
||||||
|
|
||||||
|
4. **[Find the Average](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/11-average)**
|
||||||
|
|
||||||
|
5. **[Number Sorter](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/12-sorter)**
|
||||||
|
|
||||||
|
6. **[Word Finder](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/13-word-finder)**
|
||||||
|
@ -1,42 +1,4 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
students := [...][3]float64{
|
|
||||||
{5, 6, 1},
|
|
||||||
{9, 8, 4},
|
|
||||||
}
|
|
||||||
|
|
||||||
var sum float64
|
|
||||||
for _, grades := range students {
|
|
||||||
for _, grade := range grades {
|
|
||||||
sum += grade
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const N = float64(len(students) * len(students[0]))
|
|
||||||
fmt.Printf("Avg Grade: %g\n", sum/N)
|
|
||||||
|
|
||||||
// students := [2][3]float64{
|
|
||||||
// [3]float64{5, 6, 1},
|
|
||||||
// [3]float64{9, 8, 4},
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var sum float64
|
|
||||||
// sum += students[0][0] + students[0][1] + students[0][2]
|
|
||||||
// sum += students[1][0] + students[1][1] + students[1][2]
|
|
||||||
|
|
||||||
// const N = float64(len(students) * len(students[0]))
|
|
||||||
// fmt.Printf("Avg Grade: %g\n", sum/N)
|
|
||||||
|
|
||||||
// student1 := [3]float64{5, 6, 1}
|
|
||||||
// student2 := [3]float64{9, 8, 4}
|
|
||||||
|
|
||||||
// var sum float64
|
|
||||||
// sum += student1[0] + student1[1] + student1[2]
|
|
||||||
// sum += student2[0] + student2[1] + student2[2]
|
|
||||||
|
|
||||||
// const N = float64(len(student1) * 2)
|
|
||||||
// fmt.Printf("Avg Grade: %g\n", sum/N)
|
|
||||||
}
|
}
|
||||||
|
52
x-tba/17-strings-revisited/exercises/_exp/fromarrays/main.go
Normal file
52
x-tba/17-strings-revisited/exercises/_exp/fromarrays/main.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Assign the Arrays
|
||||||
|
//
|
||||||
|
// 1. Create an array named books
|
||||||
|
// 2. Add book titles to the array
|
||||||
|
// 3. Create two more copies of the array named: upper and lower
|
||||||
|
// 4. Change the book titles to uppercase in the upper array only
|
||||||
|
// 5. Change the book titles to lowercase in the lower array only
|
||||||
|
// 6. Print all the arrays
|
||||||
|
//
|
||||||
|
// NOTE
|
||||||
|
// Check out the strings package, it has function to convert cases to
|
||||||
|
// upper and lower cases.
|
||||||
|
//
|
||||||
|
// BONUS
|
||||||
|
// 1. Invent your own arrays with different types other than string,
|
||||||
|
// and do some manipulations on them.
|
||||||
|
//
|
||||||
|
// 👉 THISSSS--------------------------------------------------------
|
||||||
|
// 2. Find some Turkish book titles and do the same upper, lowercase conversion
|
||||||
|
// for them.
|
||||||
|
//
|
||||||
|
// Here are some books: https://www.goodreads.com/group/bookshelf/417154-bilimkurgu-kul-b?shelf=read
|
||||||
|
//
|
||||||
|
// Note: You'd need to use special functions to convert the Turkish letters.
|
||||||
|
// They're in the strings package as well.
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// ?
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
books := [...]string{"Kafka's Revenge", "Stay Golden", "Everythingship"}
|
||||||
|
|
||||||
|
upper, lower := books, books
|
||||||
|
|
||||||
|
for i := range books {
|
||||||
|
upper[i] = strings.ToUpper(upper[i])
|
||||||
|
lower[i] = strings.ToLower(lower[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("books: %q\n", books)
|
||||||
|
fmt.Printf("upper: %q\n", upper)
|
||||||
|
fmt.Printf("lower: %q\n", lower)
|
||||||
|
}
|
Reference in New Issue
Block a user