1
\$\begingroup\$
  1. Given an input string, first convert each character to its ASCII value.
  2. Generate a reversed Fibonacci sequence of the same length as the input string.
  3. Encode each character by adding its ASCII value to the corresponding number in the reversed Fibonacci sequence.
  4. 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:

  1. The input string will contain only printable ASCII characters.
  2. 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"

\$\endgroup\$
6
  • 3
    \$\begingroup\$ What happens if you add 13 to z? \$\endgroup\$
    – chunes
    Commented Jun 13 at 5:39
  • \$\begingroup\$ a-1, b-2, c-3, d-4, e-5, f-6, g-7, h-8, i-9, j-10, k-11, l-12, m-13, n-14, o-15, p-16, q-17, r-18, s-19, t-20, u-21, v-22, w-23, x-24, y-25, z-26 it'll be 'm' \$\endgroup\$
    – Dhanush
    Commented Jun 13 at 5:57
  • 2
    \$\begingroup\$ Why does Code return Epee but Hello returns Jfnnp (off-by-one)? Shouldn't Hello result in Kgmmo? \$\endgroup\$ Commented Jun 13 at 6:44
  • \$\begingroup\$ @Dhanush your most recent comment does not align with the specification in the question in any way. \$\endgroup\$ Commented Jun 13 at 8:18
  • 2
    \$\begingroup\$ @Dhanush: You kind of give conflicting statements: "The input string will contain only printable ASCII characters" and then "What happens if you add 13 to z? it'll be 'm' ". So, now, which is correct? All printable characters? or only small letters, no digits, no symbols, no capital letters? \$\endgroup\$
    – virolino
    Commented Jun 13 at 8:49

2 Answers 2

3
\$\begingroup\$

05AB1E, 8 bytes

Çā<ÅfR+ç

Output as a list of characters.

Try it online or verify all test cases.

Explanation:

Ç        # Convert the (implicit) input-string to a list of codepoint-integers
 ā       # Push a list in the range [1,length] (without popping the list)
  <      # Decrease each by 1 to make the range [0,length)
   Åf    # Map each value to its 0-based n'th Fibonacci number
     R   # Reverse this list
      +  # Add the values in the lists at the same positions together
       ç # Convert it from integers to ASCII characters with these codepoint
         # (after which this list of characters is output implicitly as result)
\$\endgroup\$
2
\$\begingroup\$

Vyxal, 51 bitsv2, 6.375 bytes

C:ẏṘ∆F+C

Try it Online!

Bitstring:

010101101100110100101110001100110100000001001011110

Outputs a list of characters.

Explained

C:ẏṘ∆F+C­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌­
C         # ‎⁡Convert the input to character codes
 :ẏ       # ‎⁢And push the range [0, len(input)), without popping the character codes
   Ṙ∆F    # ‎⁣Generate the fibonacci numbers in reversed order as required
      +   # ‎⁤Add each number to the corresponding char code
       C  # ‎⁢⁡And convert back to characters.
💎

Created with the help of Luminespire.

\$\endgroup\$
1
  • \$\begingroup\$ Impressive use of Vyxal \$\endgroup\$
    – Dhanush
    Commented Jun 13 at 6:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.