Похожие презентации:
Checkpoint2_Go
1.
Checkpoint 2Go practice
These are study examples, NOT verified Tomorrow School answers
2.
01 onlyaTASK · illustrative interpretation
Print the letter a.
EXPECTED RESULT · example
a
GO · example solution
func OnlyA() {
z01.PrintRune('a')
}
3.
02 onlyzTASK · illustrative interpretation
Print the letter z.
EXPECTED RESULT · example
z
GO · example solution
func OnlyZ() {
z01.PrintRune('z')
}
4.
03 onlyfTASK · illustrative interpretation
Print the letter f.
EXPECTED RESULT · example
f
GO · example solution
func OnlyF() {
z01.PrintRune('f')
}
5.
04 only1TASK · illustrative interpretation
Print the digit 1.
EXPECTED RESULT · example
1
GO · example solution
func Only1() {
z01.PrintRune('1')
}
6.
05 onlybTASK · illustrative interpretation
Print the letter b.
EXPECTED RESULT · example
b
GO · example solution
func OnlyB() {
z01.PrintRune('b')
}
7.
06 rectperimeterTASK · illustrative interpretation
Calculate rectangle perimeter from
length and width.
EXPECTED RESULT · example
RectPerimeter(3, 5) → 16
GO · example solution
func RectPerimeter(a, b int) int {
return 2 * (a + b)
}
8.
07 countcharacterTASK · illustrative interpretation
Count occurrences of a rune in a
string.
EXPECTED RESULT · example
CountCharacter("banana", 'a') → 3
GO · example solution
func CountCharacter(s string, c rune) int {
n := 0
for _, v := range s {
if v == c { n++ }
}
return n
}
9.
08 checknumberTASK · illustrative interpretation
Check whether an integer is
positive, negative or zero.
EXPECTED RESULT · example
CheckNumber(-2) → negative
GO · example solution
func CheckNumber(n int) string {
if n > 0 { return "positive" }
if n < 0 { return "negative" }
return "zero"
}
10.
09 countalphaTASK · illustrative interpretation
Count alphabetic Latin letters in a
string.
EXPECTED RESULT · example
CountAlpha("Hi 123!") → 2
GO · example solution
func CountAlpha(s string) int {
n := 0
for _, c := range s {
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' { n++ }
}
return n
}
11.
10 retainfirsthalfTASK · illustrative interpretation
Return the first half of a string
(runes).
EXPECTED RESULT · example
RetainFirstHalf("abcdef") → abc
GO · example solution
func RetainFirstHalf(s string) string {
r := []rune(s)
return string(r[:len(r)/2])
}
12.
11 digitlenTASK · illustrative interpretation
Return the number of decimal digits
of an integer.
EXPECTED RESULT · example
DigitLen(-1234) → 4
GO · example solution
func DigitLen(n int) int {
x := int64(n)
if x < 0 { x = -x }
count := 1
for x >= 10 { x /= 10; count++ }
return count
}
13.
12 cameltosnakecaseTASK · illustrative interpretation
Convert CamelCase into
snake_case.
GO · example solution
func CamelToSnakeCase(s string) string {
out := ""
for i, c := range s {
if c >= 'A' && c <= 'Z' {
if i > 0 { out += "_" }
out += string(c + 32)
} else { out += string(c) }
}
return out
}
EXPECTED RESULT · example
CamelToSnakeCase("HelloWorld") → hello_world
14.
13 gcdTASK · illustrative interpretation
Find the greatest common divisor
(Euclidean algorithm).
EXPECTED RESULT · example
Gcd(24, 18) → 6
GO · example solution
func Gcd(a, b int) int {
if a < 0 { a = -a }; if b < 0 { b = -b }
for b != 0 { a, b = b, a%b }
return a
}
15.
14 hashcodeTASK · illustrative interpretation
Compute a simple rolling hash of a
string.
EXPECTED RESULT · example
HashCode("ab") → 3105
GO · example solution
func HashCode(s string) int {
h := 0
for _, c := range s { h = h*31 + int(c) }
return h
}
16.
15 repeatalphaTASK · illustrative interpretation
Repeat each alphabetic character
by its 1-based position.
EXPECTED RESULT · example
RepeatAlpha("abc") → abbccc
GO · example solution
func RepeatAlpha(s string) string {
out := ""
for _, c := range s {
x := c
if x >= 'A' && x <= 'Z' {
x += 32
}
if x >= 'a' && x <= 'z' {
for i := 0; i < int(x-'a'+1); i++ {
out += string(c)
} else {
out += string(c)
}
}
}
return out
}
17.
16 findprevprimeTASK · illustrative interpretation
Find the largest prime strictly less
than n.
EXPECTED RESULT · example
FindPrevPrime(12) → 11
GO · example solution
func FindPrevPrime(n int) int {
for x := n-1; x >= 2; x-- {
prime := true
for d := 2; d*d <= x; d++ {
if x%d == 0 { prime = false; break }
}
if prime { return x }
}
return 0
}
18.
17 printrevcombTASK · illustrative interpretation
Print three-digit combinations in
reverse order.
GO · example solution
func PrintRevComb() {
for a := '7'; a >= '0'; a-- {
for b := '8'; b > a; b-- {
for c := '9'; c > b; c-- {
z01.PrintRune(a); z01.PrintRune(b); z01.PrintRune(c)
z01.PrintRune('\n')
}
}
}
}
EXPECTED RESULT · example
Example: 789 … 012 (ordering depends on spec)
19.
18 printmemoryTASK · illustrative interpretation
Print bytes as hexadecimal and
printable characters.
EXPECTED RESULT · example
[]byte{65,66} → 41 42 (simplified)
GO · example solution
func PrintMemory(b []byte) {
for _, v := range b {
const hex = "0123456789abcdef"
z01.PrintRune(rune(hex[v>>4]))
z01.PrintRune(rune(hex[v&15]))
z01.PrintRune(' ' )
}
z01.PrintRune('\n')
}
20.
19 weareuniqueTASK · illustrative interpretation
Check whether all runes in a string
are unique.
EXPECTED RESULT · example
WeAreUnique("abc") → true
GO · example solution
func WeAreUnique(s string) bool {
seen := map[rune]bool{}
for _, c := range s {
if seen[c] { return false }
seen[c] = true
}
return true
}
21.
20 itoaTASK · illustrative interpretation
Convert integer to decimal string
without strconv.
EXPECTED RESULT · example
Itoa(-42) → "-42"
GO · example solution
func Itoa(n int) string {
x := int64(n)
if x == 0 { return "0" }
neg := x < 0
if neg { x = -x }
s := ""
for x > 0 { s = string(rune('0'+x%10)) + s; x /= 10 }
if neg { s = "-" + s }
return s
}
22.
21 iscapitalizedTASK · illustrative interpretation
Check whether every word begins
with an uppercase letter.
EXPECTED RESULT · example
IsCapitalized("Hello World") → true
GO · example solution
func IsCapitalized(s string) bool {
wordStart := true
for _, c := range s {
letter := c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
if !letter { wordStart = true; continue }
if wordStart && (c < 'A' || c > 'Z') { return false }
wordStart = false
}
return true
}
23.
22 concatalternateTASK · illustrative interpretation
Interleave characters from two
strings.
EXPECTED RESULT · example
ConcatAlternate("abc", "12") → a1b2c
GO · example solution
func ConcatAlternate(a, b string) string {
x, y := []rune(a), []rune(b)
out := ""
for i := 0; i < len(x) || i < len(y); i++ {
if i < len(x) { out += string(x[i]) }
if i < len(y) { out += string(y[i]) }
}
return out
}
24.
23 saveandmissTASK · illustrative interpretation
Remove a character from a string by
position.
EXPECTED RESULT · example
SaveAndMiss("hello", 1) → hllo
GO · example solution
func SaveAndMiss(s string, i int) string {
r := []rune(s)
if i < 0 || i >= len(r) { return s }
return string(append(r[:i:i], r[i+1:]...))
}
25.
24 canjumpTASK · illustrative interpretation
Determine if the last index is
reachable using jumps.
EXPECTED RESULT · example
CanJump([]int{2,3,1,1,4}) → true
GO · example solution
func CanJump(nums []int) bool {
reach := 0
for i, jump := range nums {
if i > reach { return false }
if i+jump > reach { reach = i+jump }
}
return true
}
26.
25 notdecimalTASK · illustrative interpretation
Check if a string contains no
decimal digits.
EXPECTED RESULT · example
NotDecimal("abc!") → true
GO · example solution
func NotDecimal(s string) bool {
for _, c := range s {
if c >= '0' && c <= '9' { return false }
}
return true
}
27.
26 sliceTASK · illustrative interpretation
Return a substring between rune
indices.
EXPECTED RESULT · example
Slice("abcdef", 1, 4) → bcd
GO · example solution
func Slice(s string, start, end int) string {
r := []rune(s)
if start < 0 { start = 0 }
if end > len(r) { end = len(r) }
if start > end || end < 0 { return "" }
return string(r[start:end])
}
28.
27 revconcatalternateTASK · illustrative interpretation
Interleave two strings starting from
their ends.
EXPECTED RESULT · example
RevConcatAlternate("abc", "12") → c2b1a
GO · example solution
func RevConcatAlternate(a, b string) string {
x, y := []rune(a), []rune(b)
out := ""
for i, j := len(x)-1, len(y)-1; i >= 0 || j >= 0; i, j = i-1, j-1 {
if i >= 0 { out += string(x[i]) }
if j >= 0 { out += string(y[j]) }
}
return out
}
29.
28 wordflipTASK · illustrative interpretation
Reverse the order of words in a
string.
EXPECTED RESULT · example
WordFlip("go is fun") → fun is go
GO · example solution
func WordFlip(s string) string {
w := strings.Fields(s)
for i,j := 0,len(w)-1; i<j; i,j=i+1,j-1 {
w[i],w[j]=w[j],w[i]
}
return strings.Join(w, " ")
}
30.
29 revwstrTASK · illustrative interpretation
Reverse words, separated by
whitespace.
EXPECTED RESULT · example
RevWstr("one two three") → three two one
GO · example solution
func RevWstr(s string) string {
words := strings.Fields(s)
for i,j := 0,len(words)-1; i<j; i,j=i+1,j-1 {
words[i],words[j]=words[j],words[i]
}
return strings.Join(words, " ")
}
31.
30 fishandchipsTASK · illustrative interpretation
Print integers 1–100; multiples of 3 →
fish, 5 → chips.
EXPECTED RESULT · example
1, 2, fish, 4, chips, fish, …
GO · example solution
func FishAndChips() {
for n := 1; n <= 100; n++ {
if n%15 == 0 { fmt.Println("fishandchips")
} else if n%3 == 0 { fmt.Println("fish")
} else if n%5 == 0 { fmt.Println("chips")
} else { fmt.Println(n) }
}
}