- Given an input string, first convert each character to its ASCII value.
- Generate a reversed Fibonacci sequence of the same length as the input string.
- Encode each character by adding its ASCII value to the corresponding number in the reversed Fibonacci sequence.
- Convert the resulting values back to characters and form the encoded string.
Example Input:
"Code"
Convert each character to its ASCII value:
'C' -> 67
'o' -> 111
'd' -> 100
'e' -> 101
Generate the reversed Fibonacci sequence of length 4:
Fibonacci sequence (first 4 values): 0, 1, 1, 2
Reversed: 2, 1, 1, 0
Add the corresponding Fibonacci number to each ASCII value:
67 + 2 = 69 -> 'E'
111 + 1 = 112 -> 'p'
100 + 1 = 101 -> 'e'
101 + 0 = 101 -> 'e'
Form the encoded string:
def reverse_fibonacci_cipher(input_str: str) -> str:
# Your implementation here
Rules:
- The input string will contain only printable ASCII characters.
- The length of the input string will be between 1 and 100 characters. Your Task: Write the shortest code possible to implement the reverse_fibonacci_cipher function that performs the encoding as described above. Example Test Cases: reverse_fibonacci_cipher("Code") should return
"Epee"
reverse_fibonacci_cipher("Hello") should return
"Jfnnp"
CodereturnEpeebutHelloreturnsJfnnp(off-by-one)? Shouldn'tHelloresult inKgmmo? \$\endgroup\$