add: string quiz + execises

This commit is contained in:
Inanc Gumus
2019-04-10 23:14:38 +03:00
parent 2cd646b092
commit 1cf3f4b13e
8 changed files with 328 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package main
// ---------------------------------------------------------
// EXERCISE: Convert the strings
//
// 1. Loop over the words slice
//
// 2. In the loop:
// 1. Convert each string value to a byte slice
// 2. Print the byte slice
// 3. Append the byte slice to the `bwords`
//
// 3. Print the words using the `bwords`
//
// EXPECTED OUTPUT
// [103 111 112 104 101 114]
// [112 114 111 103 114 97 109 109 101 114]
// [103 111 32 108 97 110 103 117 97 103 101]
// [103 111 32 115 116 97 110 100 97 114 100 32 108 105 98 114 97 114 121]
// gopher
// programmer
// go language
// go standard library
// ---------------------------------------------------------
func main() {
// Please uncomment the code below
// words := []string{
// "gopher",
// "programmer",
// "go language",
// "go standard library",
// }
// var bwords [][]byte
}

View File

@ -0,0 +1,32 @@
// 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() {
words := []string{
"gopher",
"programmer",
"go language",
"go standard library",
}
var bwords [][]byte
for _, w := range words {
bw := []byte(w)
fmt.Println(bw)
bwords = append(bwords, bw)
}
for _, w := range bwords {
fmt.Println(string(w))
}
}

View File

@ -0,0 +1,28 @@
package main
// ---------------------------------------------------------
// EXERCISE: Print the runes
//
// 1. Loop over the "console" word and print its runes one by one,
// in decimals, hexadecimals and binary.
//
// 2. Manually put the runes of the "console" word to a byte slice, one by one.
//
// As the elements of the byte slice use only the rune literals.
//
// Print the byte slice.
//
// 3. Repeat the step 2 but this time, as the elements of the byte slice,
// use only decimal numbers.
//
// 4. Repeat the step 2 but this time, as the elements of the byte slice,
// use only hexadecimal numbers.
//
//
// EXPECTED OUTPUT
// Run the solution to see the expected output.
// ---------------------------------------------------------
func main() {
const word = "console"
}

View File

@ -0,0 +1,33 @@
// 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() {
const word = "console"
for _, w := range word {
fmt.Printf("%c\n", w)
fmt.Printf("\tdecimal: %[1]d\n", w)
fmt.Printf("\thex : 0x%[1]x\n", w)
fmt.Printf("\tbinary : 0b%08[1]b\n", w)
}
// print the word manually using runes
fmt.Printf("with runes : %s\n",
string([]byte{'c', 'o', 'n', 's', 'o', 'l', 'e'}))
// print the word manually using decimals
fmt.Printf("with decimals : %s\n",
string([]byte{99, 111, 110, 115, 111, 108, 101}))
// print the word manually using hexadecimals
fmt.Printf("with hexadecimals: %s\n",
string([]byte{0x63, 0x6f, 0x6e, 0x73, 0x6f, 0x6c, 0x65}))
}

View File

@ -0,0 +1,47 @@
package main
// ---------------------------------------------------------
// EXERCISE: Rune Manipulator
//
// Please read the comments inside the following code.
//
// EXPECTED OUTPUT
// Please run the solution.
// ---------------------------------------------------------
func main() {
strings := []string{
"cool",
"güzel",
"jīntiān",
"今天",
"read 🤓",
}
_ = strings
// Print the byte and rune length of the strings
// Hint: Use len and utf8.RuneCountInString
// Print the bytes of the strings in hexadecimal
// Hint: Use % x verb
// Print the runes of the strings in hexadecimal
// Hint: Use % x verb
// Print the runes of the strings as rune literals
// Hint: Use for range
// Print the first rune and its byte size of the strings
// Hint: Use utf8.DecodeRuneInString
// Print the last rune of the strings
// Hint: Use utf8.DecodeLastRuneInString
// Slice and print the first two runes of the strings
// Slice and print the last two runes of the strings
// Convert the string to []rune
// Print the first and last two runes
}

View File

@ -0,0 +1,78 @@
// 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"
"unicode/utf8"
)
func main() {
strings := []string{
"cool",
"güzel",
"jīntiān",
"今天",
"read 🤓",
}
for _, s := range strings {
fmt.Printf("%q\n", s)
// Print the byte and rune length of the strings
// Hint: Use len and utf8.RuneCountInString
fmt.Printf("\thas %d bytes and %d runes\n",
len(s), utf8.RuneCountInString(s))
// Print the bytes of the strings in hexadecimal
// Hint: Use % x verb
fmt.Printf("\tbytes : % x\n", s)
// Print the runes of the strings in hexadecimal
// Hint: Use % x verb
fmt.Print("\trunes :")
for _, r := range s {
fmt.Printf("% x", r)
}
fmt.Println()
// Print the runes of the strings as rune literals
// Hint: Use for range
fmt.Print("\trunes :")
for _, r := range s {
fmt.Printf("%q", r)
}
fmt.Println()
// Print the first rune and its byte size of the strings
// Hint: Use utf8.DecodeRuneInString
r, size := utf8.DecodeRuneInString(s)
fmt.Printf("\tfirst : %q (%d bytes)\n", r, size)
// Print the last rune of the strings
// Hint: Use utf8.DecodeLastRuneInString
r, size = utf8.DecodeLastRuneInString(s)
fmt.Printf("\tfirst : %q (%d bytes)\n", r, size)
// Slice and print the first two runes of the strings
_, first := utf8.DecodeRuneInString(s)
_, second := utf8.DecodeRuneInString(s[first:])
fmt.Printf("\tfirst 2 : %q\n", s[:first+second])
// Slice and print the last two runes of the strings
_, last1 := utf8.DecodeLastRuneInString(s)
_, last2 := utf8.DecodeLastRuneInString(s[:len(s)-last1])
fmt.Printf("\tlast 2 : %q\n", s[len(s)-last2-last1:])
// Convert the string to []rune
// Print the first and last two runes
rs := []rune(s)
fmt.Printf("\tfirst 2 : %q\n", string(rs[:2]))
fmt.Printf("\tlast 2 : %q\n", string(rs[len(rs)-2:]))
}
}

View File

@ -0,0 +1,13 @@
# Strings, bytes, and runes exercises
1. **[Convert](https://github.com/inancgumus/learngo/tree/master/19-strings-runes-bytes/exercises/01-convert)**
Use []byte <-> string conversions.
2. **[Print the Runes](https://github.com/inancgumus/learngo/tree/master/19-strings-runes-bytes/exercises/02-print-the-runes)**
Use Printf verbs.
3. **[Rune Manipulator](https://github.com/inancgumus/learngo/tree/master/19-strings-runes-bytes/exercises/03-rune-manipulator)**
Use utf8 package, indexing and slicing.

View File

@ -0,0 +1,60 @@
# Strings, Runes and Bytes Quiz
## Which byte slice below equals to the "keeper" string?
```go
// Here are the corresponding code points for the runes of "keeper":
// k => 107
// e => 101
// p => 112
// r => 114
```
1. []byte{107, 101, 101, 112, 101, 114} *CORRECT*
2. []byte{112, 101, 101, 112, 114, 101}
3. []byte{114, 101, 112, 101, 101, 112}
4. []byte{112, 101, 101, 114, 107, 101}
## What does this code print?
```go
// Code points:
// g => 103
// o => 111
fmt.Println(string(103), string(111))
```
1. 103 111
2. g o *CORRECT*
3. n o
4. "103 111"
## What does this code print?
```go
const word = "gökyüzü"
bword := []byte(word)
// ö => 2 bytes
// ü => 2 bytes
fmt.Println(utf8.RuneCount(bword), len(word), len(string(word[1])))
```
1. 7 10 2 *CORRECT*
2. 10 7 1
3. 10 7 2
4. 7 7 1
## Which one below is true?
1. for range loops over the bytes of a string
2. for range loops over the runes of a string *CORRECT*
## For a utf-8 encoded string value, which one below is true?
1. runes always start and end in the same indexes
2. runes may start and end in different indexes *CORRECT*
3. bytes may start and end in different indexes
## Why can't you change the bytes of a string value?
1. Strings values are immutable byte slices
2. Strings are used a lot so they are being shared behind the scenes
3. All of above *CORRECT*