Introduction

Welcome to an exciting exploration of string manipulation and array operations! In this Unit, we’ll tackle a unique challenge: processing a string and an array in parallel while adhering to specific conditions. This engaging task is part of a mysterious book club's cryptic coding adventure.

Get ready to uncover the secrets of combining string and numerical logic into one seamless solution!

Task Statement

Our goal is to create a unique encoded message and manage an array of numbers simultaneously. Here's what we need to do:

  1. Replace each letter in a string with the next letter alphabetically. If the letter is 'z', wrap around to 'a'.
  2. For an accompanying array of numbers, divide each number by 2, round the result, and accumulate these rounded values.
  3. Stop processing once the accumulated sum exceeds 20.
  4. Reverse the updated string to complete the cryptic transformation.
  5. Return the reversed string and the unprocessed numbers from the array.

For example, given the input string "books" and the array [10, 20, 30, 50, 100]:

  • Start with an empty string and a sum of 0.
  • Process 'b' (becomes 'c') and 10 (half is 5), sum becomes 5.
  • Process 'o' (becomes 'p') and 20 (half is 10), sum becomes 15.
  • Process 'o' (becomes 'p') and 30 (half is 15), sum becomes 30, exceeding 20. Stop processing.

The resulting string is "cpp", reversed to "ppc". The unprocessed numbers are [50, 100].

The output will be:

["ppc", [50, 100]]
Solution Building: Step 1 - Initialization

We begin by setting up our components: the result string to store the transformed characters, a variable to track the cumulative sum, and an index to keep track of how many elements have been processed.

def generate_cryptic_message(input_string, numbers)
  result = ''         # Holds the processed characters
  sum_so_far = 0      # Tracks the cumulative sum of half numbers
  index = 0           # Tracks how many elements have been processed

With these initialized, we are ready to start processing the input.

Solution Building: Step 2 - Processing the String and Array

Next, we iterate over the characters in the input string along with their corresponding numbers. For each character:

  • Replace it with the next alphabetical character, wrapping around from 'z' to 'a'.
  • Append the transformed character to the result string.
  • Divide the corresponding number by 2, round it, and add the result to the cumulative sum.
  • Stop processing as soon as the sum exceeds 20.
  input_string.each_char.with_index do |char, i|
    shifted_char = char == 'z' ? 'a' : (char.ord + 1).chr
    result += shifted_char

    half_number = (numbers[i] / 2.0).round
    sum_so_far += half_number
    index = i + 1  # Update the index to reflect processed characters

    break if sum_so_far > 20  # Stop processing if the sum exceeds 20
  end

At this stage, we have a partially processed string and a cumulative sum that determines how far we’ve gone.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal