Lesson 2
Parsing and Multiplying Numbers in Go
Introduction

Welcome! Today, we have a fascinating and practical task that will test your Go programming skills. We'll be working on parsing strings and performing type conversions. Let's get started!

Task Statement and Description

Our task for the day involves creating a Go function named ParseAndMultiplyNumbers(). This function is designed to accept a string as input. The input is a playful mix of numbers and words. The goal of this function is to analyze the string, extract all the numbers, convert these numbers (currently string types) into integer data types, and then multiply these numbers together to produce a final result.

For example, given the input string "I have 2 apples and 5 oranges," our function should return the product of 2 and 5, which is 10.

Step 1 - Initialize Variables

The primary task is to parse the string and identify the numbers. To do this in Go, let's create an empty string, num, to accumulate digits and a slice numbers to collect all the numbers we find:

Go
1inputString := "I have 2 apples and 5 oranges" 2num := "" 3var numbers []int
Step 2 - Parse and Extract Numbers

The next step involves iterating through the input string character by character. When we encounter a digit, we append it to our num string. If a character isn't a digit and num isn't empty, it means we've reached the end of a number.

At this point, we convert num to an integer using strconv.Atoi, add it to the numbers slice, and reset num to an empty string. If the character isn't a digit and num is empty, we simply skip it and continue.

Go
1import ( 2 "fmt" 3 "strconv" 4 "unicode" 5) 6 7for _, ch := range inputString { 8 if unicode.IsDigit(ch) { 9 num += string(ch) 10 } else if num != "" { 11 number, err := strconv.Atoi(num) 12 if err == nil { 13 numbers = append(numbers, number) 14 } 15 num = "" 16 } 17} 18if num != "" { 19 number, err := strconv.Atoi(num) 20 if err == nil { 21 numbers = append(numbers, number) 22 } 23}

After running this code, the output should be a slice of integers [2, 5].

Step 3 - Multiply Extracted Numbers

Finally, we multiply all the numbers in the numbers slice together. The multiplication result is stored in the result variable.

Go
1result := 1 2for _, number := range numbers { 3 result *= number 4} 5fmt.Println(result)

After executing this code, the console output should be 10.

Complete Solution

Bringing together all the steps, our final Go solution is as follows:

Go
1package main 2 3import ( 4 "fmt" 5 "strconv" 6 "unicode" 7) 8 9func ParseAndMultiplyNumbers(inputString string) int { 10 num := "" 11 var numbers []int 12 for _, ch := range inputString { 13 if unicode.IsDigit(ch) { 14 num += string(ch) 15 } else if num != "" { 16 number, err := strconv.Atoi(num) 17 if err == nil { 18 numbers = append(numbers, number) 19 } 20 num = "" 21 } 22 } 23 // Check if there was a number at the end of the string 24 if num != "" { 25 number, err := strconv.Atoi(num) 26 if err == nil { 27 numbers = append(numbers, number) 28 } 29 } 30 31 result := 1 32 for _, number := range numbers { 33 result *= number 34 } 35 return result 36} 37 38func main() { 39 // Call the function 40 fmt.Println(ParseAndMultiplyNumbers("I have 2 apples and 5 oranges")) 41}

This solution also caters to numbers situated at the end of the input string.

Lesson Summary

Congratulations! You've successfully developed a Go function that navigates through strings to identify numbers, performs data type conversion, and then conducts an arithmetic operation on those numbers. This achievement is a testament to your growing proficiency in Go programming. Remember, coding improvement comes with practice. With this solution, you can try different operations or introduce new conditions for identifying valid numbers to further sharpen your Go skills. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.