Lesson 3
Mystical Coding: Parallel String and List Processing in Kotlin
Introduction

Welcome to a new session, where we are embarking on a journey into the mystical territory of combined string and list operations. Have you ever thought about how to update a string and a list in parallel while a specific condition holds true? That's precisely what we'll explore today, all in the context of a real-world scenario related to a mystery novel book club. Get ready to dive in!

Task Statement

Our mission today is to generate a unique encoded message for a book club. Here's the fun part: to create a cryptic message, we will process a string and a list of numbers simultaneously and stop once a given condition is satisfied.

For the string, our task is to replace each letter with the next alphabetical letter and then reverse the entire updated string. For the list of numbers, our task is to divide each number by 2, round the result, and accumulate the rounded numbers until their total exceeds 20.

When the accumulated total exceeds 20, we immediately stop the process and return the updated string and the as-yet-unprocessed numbers in their original order.

Example

Consider the input string "books" and the list listOf(10, 20, 30, 50, 100).

We start our process with an empty string and a sum of 0.

  • For the first character 'b' in "books", we replace it with the next alphabet 'c'. For the corresponding number 10 in the list, we divide it by 2 and round it. The result is 5. The sum after the first operation is 5, which is less than 20, so we continue to the next character.
  • For the next character 'o', we replace it with 'p'. For the corresponding number 20 in the list, half and rounded is 10. The sum after the second operation is 15 (5 + 10). The sum still doesn't exceed 20, so we move to the third character.
  • For the next character 'o', we replace it with 'p'. For the corresponding number 30 in the list, half and rounded is 15. When we add this 15 to the previously calculated sum of 15, it totals 30, which is more than 20. So, we stop the process here.
  • We have processed 'b', 'o', and 'o' from the word "books" and replaced them with 'c', 'p', and 'p' respectively to get "cpp". After reversing, we get "ppc".
  • For the list, we exclude any numbers that we have processed. Hence, we exclude the first three numbers, and the list becomes [50, 100].

So the output should be "ppc 50, 100".

Solution Building: Step 1 - String and List Initialization

Let's begin our journey by setting up two crucial components: our resultant string and a variable to keep track of the cumulative sum.

Kotlin
1fun solution(inputString: String, numbers: List<Int>): String { 2 var result = "" 3 var sumSoFar = 0
Solution Building: Step 2 - Iteration and Updates

With the setup complete, it's time to roll up our sleeves and process the inputString and list. We need to iterate over the inputString and update each character to its next alphabetical character. Simultaneously, we'll keep tabs on our list condition — if the sum of half of the numbers crosses our threshold of 20, we should stop the process.

Kotlin
1import kotlin.math.roundToInt 2 3fun solution(inputString: String, numbers: List<Int>): String { 4 val result = StringBuilder() 5 var sumSoFar = 0 6 var i = 0 7 while (i < inputString.length && sumSoFar <= 20) { 8 result.append(if (inputString[i] == 'z') 'a' else inputString[i] + 1) 9 val halfNumber = (numbers[i] / 2.0).roundToInt() 10 sumSoFar += halfNumber 11 i++ 12 }
Solution Building: Step 3 - Final Touch-Up and Return

With the updates complete, we're one step away from solving this mystery. We must reverse our string to generate the final encoded message! At the end, we return the processed string and the remaining list as a single concatenated string separated by a space.

Kotlin
1import kotlin.math.roundToInt 2 3fun solution(inputString: String, numbers: List<Int>): String { 4 val result = StringBuilder() 5 var sumSoFar = 0 6 var i = 0 7 while (i < inputString.length && sumSoFar <= 20) { 8 result.append(if (inputString[i] == 'z') 'a' else inputString[i] + 1) 9 val halfNumber = (numbers[i] / 2.0).roundToInt() 10 sumSoFar += halfNumber 11 i++ 12 } 13 result.reverse() 14 val remainingNumbers = numbers.subList(i, numbers.size) 15 val remainingNumbersString = remainingNumbers.joinToString(", ") 16 return "$result $remainingNumbersString" 17} 18 19fun main() { 20 val numbers = listOf(10, 20, 30, 50, 100) 21 val result = solution("books", numbers) 22 println(result) 23 24 // Output: 25 // ppc 50, 100 26}
Lesson Summary

Congratulations! You have successfully navigated and implemented a complex process that involved string manipulation, list processing, and cumulative conditions. This computational challenge has given you the perspective on how to apply these programming elements in real-world scenarios.

Up next, I encourage you to solve more problems that require you to iterate and update lists based on certain conditions. We will meet again soon to crack another problem and delve deeper into the world of coding. Keep practicing and happy coding!

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