Our mission for 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 an array of numbers simultaneously and stop once a given condition is satisfied.
Consider the input string "books" and array [10, 20, 30, 50, 100].
We start our process with an empty string and a sum of 0.
So the output should be ('ppc', [50, 100]).
Let's begin our journey by setting up two crucial components: our resultant string and a variable to keep track of the cumulative sum.
from typing import List
def solution(input_string: str, numbers: List[int]):
result = ''
sum_so_far = 0
With the setup complete, it's time to roll up our sleeves and process the string and array. We need to iterate over the input_string and update each character to its next alphabetical character. Simultaneously, we'll keep tabs on our array condition - if the sum of half of the numbers crosses our threshold of 20, we should stop the process.
from typing import List
def solution(input_string: str, numbers: List[int]):
result = ''
sum_so_far = 0
i = 0
while i < len(input_string) and sum_so_far <= 20:
result += 'a' if input_string[i] == 'z' else chr(ord(input_string[i]) + 1)
half_number = round(numbers[i] / 2)
sum_so_far += half_number
i += 1
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 array.
from typing import List
def solution(input_string: str, numbers: List[int]):
result = ''
sum_so_far = 0
i = 0
while i < len(input_string) and sum_so_far <= 20:
result += 'a' if input_string[i] == 'z' else chr(ord(input_string[i]) + 1)
half_number = round(numbers[i] / 2)
sum_so_far += half_number
i += 1
return result[::-1], numbers[i:]