Thuta Learning
ExercisesProgrammingbeginner

Exercises: Core Basics Review

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Practice Exercises: Core Basics Review on your own
  • Practice the skills you've already learned to make them stick
  • Learn to find bugs, fix them, and check your own work

Let's think about this for a second

This lesson isn't new teaching content - it's meant to help you confidently review the function, slice, map, and loop skills you already learned in the Basic and Intermediate chapters. We recommend writing the code yourself for each task before comparing it against the solution. Finishing this practice set will make the next project chapter a lot smoother to work through. The difficulty is roughly medium, focused mainly on testing slice manipulation and map counting logic.

Exercises

Task 1: Write a sum(nums []int) int function that takes an int slice and returns the sum of all its elements. Task 2: Write a countChars(s string) map[rune]int function that takes a string and counts how many times each character appears, as a map[rune]int. Task 3: Write a filterEven(nums []int) []int function that takes an int slice as a parameter and returns a new slice containing only the even numbers. Task 4: Create a struct Person{Name string; Age int}, then write an oldest(people []Person) Person function that takes a Person slice and finds and returns the person with the highest Age.

Code Example

go
package main

import "fmt"

// Task 1
func sum(nums []int) int {
	// TODO: implement
	return 0
}

// Task 2
func countChars(s string) map[rune]int {
	// TODO: implement
	return nil
}

// Task 3
func filterEven(nums []int) []int {
	// TODO: implement
	return nil
}

// Task 4
type Person struct {
	Name string
	Age  int
}

func oldest(people []Person) Person {
	// TODO: implement
	return Person{}
}

func main() {
	fmt.Println(sum([]int{1, 2, 3, 4, 5}))
	fmt.Println(countChars("gopher"))
	fmt.Println(filterEven([]int{1, 2, 3, 4, 5, 6}))
	people := []Person{{"Aye", 25}, {"Bo", 30}, {"Cho", 22}}
	fmt.Println(oldest(people))
}
You should see
The terminal should show sum() as 15, filterEven() as [2 4 6], and oldest() as {Bo 30} (the countChars output will be a character count map).

Try it in 5 minutes

Within 5 minutes, fill in all 4 TODO comments yourself, then run go run and check whether the output is correct.

One quick word of caution

When practicing on your own, don't peek at the solution right away - try debugging compile errors and logic errors yourself first. That's how the concepts actually stick.

Easy traps

  • Trying to assign directly to an index instead of appending from a nil slice inside filterEven(), which triggers an index out of range panic
  • Looping over a string byte by byte in countChars(), which gets the character count wrong when multi-byte characters like Myanmar text show up - you need to loop with range over runes instead

Now try it yourself

Within 5 minutes, fill in all 4 TODO comments yourself, then run go run and check whether the output is correct.

You'll know it worked when: The terminal should show sum() as 15, filterEven() as [2 4 6], and oldest() as {Bo 30} (the countChars output will be a character count map).

Exercises: Core Basics Review | Thuta Learning