Welcome! In this unit, we have an exciting and practical task that will test your Kotlin programming skills. We will be parsing strings and making type conversions. So, let's dive into it!
Our task for the day involves creating a Kotlin function called parseAndMultiplyNumbers()
. This function is designed to accept a string as an input. However, it's not just any string — the input we'll consider is a playful mix of numbers and words.
The purpose of this function is to analyze the input string, extract all the numbers, convert these numbers (currently string types) into integer data types, and then multiply all these numbers together. The final output? It's the product of all those numbers!
Here's an illustration for clarification. Given the input string "I have 2 apples and 5 oranges," our function should return the product of 2 and 5, which is 10.
The primary task is to parse the string and identify the numbers. To do that, let's create an empty string, num
, to accumulate digits and a MutableList<Int>
to collect all the numbers we find:
Kotlin1val inputString = "I have 2 apples and 5 oranges" 2var num = "" 3val numbers = mutableListOf<Int>()
The next step requires 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, add it to the numbers
list, and reset num
to an empty string. If the character isn’t a digit and num
is empty, we simply skip and progress.
Kotlin1for (ch in inputString) { 2 if (ch.isDigit()) { 3 num += ch 4 } else if (num.isNotEmpty()) { 5 numbers.add(num.toInt()) 6 num = "" 7 } 8} 9// After the loop, we must check if 'num' is not empty 10// because it indicates that the last part of the string contains a number. 11if (num.isNotEmpty()) { 12 numbers.add(num.toInt()) 13} 14for (number in numbers) { 15 print("$number ") 16}
After running this code, the output should be 2 5
.
Finally, we multiply all the numbers
in the numbers
list together. The multiplication result gets stored in the result
variable.
Kotlin1var result = 1 2for (number in numbers) { 3 result *= number 4} 5println(result)
After executing this code, the console output should be 10
.
Bringing together all the steps, our final Kotlin solution manifests as follows:
Kotlin1fun parseAndMultiplyNumbers(inputString: String): Int { 2 var num = "" 3 val numbers = mutableListOf<Int>() 4 5 for (ch in inputString) { 6 if (ch.isDigit()) { 7 num += ch 8 } else if (num.isNotEmpty()) { 9 numbers.add(num.toInt()) 10 num = "" 11 } 12 } 13 if (num.isNotEmpty()) { 14 numbers.add(num.toInt()) 15 } 16 17 var result = 1 18 for (number in numbers) { 19 result *= number 20 } 21 return result 22} 23 24fun main() { 25 // Call the function 26 println(parseAndMultiplyNumbers("I have 2 apples and 5 oranges")) 27}
This solution also caters to numbers situated at the end of the input string.
Applaud yourself! You've successfully developed a Kotlin function that deftly navigates strings to identify numbers, performs a data type conversion, and then conducts an arithmetic operation on those numbers. You've truly demonstrated admirable skill in orchestrating these coding concepts!
However, as always in coding, practice is key to improvement. With this solution, you could try to perform different operations on the numbers or change the condition for identifying valid numbers, thereby further sharpening your Kotlin skills. Here's to coding greatness!