String Challenge - Calculate Days and Hours

October 22, 2024

String Challenge

Have the function string_challenge(num) take the num parameter being passed and return the number of days and hours the parameter converts to (i.e., if num = 49, then the output should be 2:1). Separate the number of days and hours with a colon.

Once your function is working, take the final output string and concatenate it with your ChallengeToken, and then replace every 4th character with a capital 'Y'.

Your ChallengeToken: xyz987abc

Examples

Input: 50
Output: 2:2
Final Output: 2:Y2xYz9Y7aYc

Input: 25
Output: 1:1
Final Output: 1:Y1xYz9Y7aYc

Input: 123
Output: 5:3
Final Output: 5:Y3xYz9Y7aYc


def string_challenge(num):
    # Calculate days and hours
    days = num // 24
    hours = num % 24

    # Format string
    time_str = f"{days}:{hours}"

    # Define Challenge Token
    challenge_token = 'xyz987abc'
    final_output = time_str + challenge_token

    # Replace every 4th character with 'Y'
    final_output = ''.join(['Y' if (i + 1) % 4 == 0 else char for i, char in enumerate(final_output)])

    return final_output


# Example usage
print(string_challenge(50))  # Output: 2:Y2xYz9Y7aYc
print(string_challenge(25))  # Output: 1:Y1xYz9Y7aYc
print(string_challenge(123)) # Output: 5:Y3xYz9Y7aYc

Explanation:

  1. Input: The function takes an integer num which represents total hours.
  2. Processing:
    • First, the total hours are converted into days and remaining hours.
    • The result is then concatenated with the ChallengeToken.
    • Every 4th character in the final concatenated string is replaced with 'Y'.
  3. Output: The transformed string is returned as the final result.

Time Complexity:

The time complexity of the string_challenge function is O(n), where n is the length of the output string.

Breakdown

  1. Time calculation: O(1) (constant time)
  2. String formatting: O(n) (proportional to the length of the output string)
  3. Concatenation: O(n) (proportional to the length of the ChallengeToken)
  4. Character replacement: O(n) (proportional to the length of the output string)

Dominant Factor The dominant factor is the character replacement operation, which iterates over the entire output string.

Space Complexity The space complexity is also O(n), as a new string is created for the output.

This solution is efficient, with a linear time complexity.


Profile picture

Written by Marylene Sawyer is a web developer dedicated to building useful and impactful solutions. With a passion for technology and creativity, she enjoys crafting applications that enhance user experiences. Marylene combines her technical expertise with a keen eye for design, striving to create intuitive and engaging interfaces that meet the needs of users.