-4
\$\begingroup\$

Your task is to make a script with the shortest byte count, that reverses a word then replaces each letter with the next letter next in the alphabet.

Examples:

input: hi
output: ji

input: hello
output: pmmfi

input: test
output: utfu

A few notes:

  • Should the letter's value be 27 (z+1), assume it is an a.
  • Symbols, numbers (and any other characters not found in the alphabet) as inputs should error
\$\endgroup\$
6
  • \$\begingroup\$ What does 'z' map to? \$\endgroup\$
    – hakr14
    Feb 22, 2021 at 1:43
  • \$\begingroup\$ z will be a, i forgot to put that in it \$\endgroup\$ Feb 22, 2021 at 1:45
  • 2
    \$\begingroup\$ This challenge has multiple problems. What should we do with uppercase/lowercase letters? How about non-ASCII Unicode letters? What is a "word"? What should we do when the input contains spaces? What should we do with 'z' and 'Z' (z->a and Z->A, or z->A and Z->a)? \$\endgroup\$
    – Bubbler
    Feb 22, 2021 at 1:56
  • 2
    \$\begingroup\$ Also, it is discouraged to validate inputs as part of the task ("cause an error if a symbol or number is given"), and we usually score a source code in bytes, not characters. \$\endgroup\$
    – Bubbler
    Feb 22, 2021 at 1:56
  • 1
    \$\begingroup\$ Nice attempt, but try to explain your question well next time, and instead, post in the sandbox before posting here. \$\endgroup\$
    – user100752
    Feb 22, 2021 at 6:42

2 Answers 2

2
\$\begingroup\$

Whispers v1, 349 343 bytes

>> #2
> Input
>> [13]
>> LⁿR
>> Each 20 27
>> Each 4 29 30
> 0
> 65
> 90
> 97
> 122
>> 8…9
>> 10…11
>> [12]
>> Then 14 3
>> Then 3
>> Then 15
>> 7-L
>> ≻L
>> ?L
>> 'L
>> L∈R
>> 2ⁿL
>> LⁱR
>> (1]
>> Each 18 25
>> Each 23 26
>> 1⋅16
>> 1⋅17
>> Each 22 5 28
>> Each 24 6 5
>> Each 19 31
>> Each 4 6 32
>> Each 21 33
>> Output 34

Try it online!

In Whispers, the last line is executed first. The other lines are executed when they are referenced in an executing line. Since this program contains many nested operations, I will try to explain it in a bottom-up approach. For better understanding, I will go through the explanation using the example input HelloWorld.

Input:

2 >> Input

Takes the first line of input. Example: HelloWorld

Constants:

7 > 0 8 > 65 9 > 90 10 > 97 11 > 122

Simple integers.

12 >> 8…9

Returns the set {result of line 8 .. result of line 9}, so we get {65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90}.

13 >> 10…11

We get {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122}.

14 >> [12]

Converts the result of line 12 into a list. We get [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]. Since this array occurs several times in this explanation, I will call it A.

3 >> [13]

[97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]. We call this array B.

15 >> Then 14 3

[A, B]

16 >> Then 3

[B]

17 >> Then 15

[[A, B]]

Functions:

Functions take an argument L and/or an argument R from a previously executed line.

4 >> LⁿR

Returns the Rth position of L.

18 >> 7-L

Takes the result from line 7, subtracts it with L and returns it. Since the result of line 7 is 0 line 18 simply negates its argument.

19 >> ≻L

Increments L.

20 >> ?L

Returns the integer representation of L.

21 >> 'L

Returns the string representation of L.

22 >> L∈R

Returns true if L is in R, else false.

23 >> 2ⁿL

Returns the Lth position of the result of line 2. Since line 2 is our example input HelloWorld it will return the Lth character of this word.

24 >> LⁱR

Returns the index of R in L.

Variables:

1 >> #2

Returns the length of result of line 2. In this example it is 10.

25 >> (1]

Returns the set {1 .. result of line 1}, so we get {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.

The Each command iterates over the second argument and applies the function given by the first argument. If two arrays are passed, they are zipped.

26 >> Each 18 25

[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]

27 >> Each 23 26

dlroWolleH

5 >> Each 20 27

[100, 108, 114, 111, 87, 111, 108, 108, 101, 72]

28 >> 1⋅16

[B, B, B, B, B, B, B, B, B, B]

29 >> 1⋅17

[[A, B], [A, B], [A, B], [A, B], [A, B], [A, B], [A, B], [A, B], [A, B], [A, B]]

30 >> Each 22 5 28

[True, True, True, True, False, True, True, True, True, False]

6 >> Each 4 29 30

[B, B, B, B, A, B, B, B, B, A]

31 >> Each 24 6 5

[3, 11, 17, 14, 22, 14, 11, 11, 4, 7]

32 >> Each 19 31

[4, 12, 18, 15, 23, 15, 12, 12, 5, 8]

33 >> Each 4 6 32

[101, 109, 115, 112, 88, 112, 109, 109, 102, 73]

34 >> Each 21 33

emspXpmmfI

Output:

35 >> Output 34

Outputs the result of line 34.

But why the strange order? Because the order is essential. The lines 1-6 are called several times. So they should better be on a small number line.

\$\endgroup\$
1
\$\begingroup\$

Python 3, 109 bytes

p=input()
print(''.join(chr(ord(c)+1)for c in p[::-1]).replace('{','a').replace('[','A')if p.isalpha()else 0)

Try it online!

This is working also for uppercase letters, since also them are included in the alphabet.

Code explanation:

p=input()
# takes the input from the prompt
              
                                                                              if p.isalpha()
# main check: if the string contains all alphabetic characters

              chr(ord(c)+1)for c in p[::-1]
# reverse the word and returns a list of the next characters in the alphabet (ascii value +1)

      ''.join
# creates a string from the list
             
                                            .replace('{','a').replace('[','A')
# substitutes in the new string the characters '{' (='z' + 1) and '[' (='Z' + 1), to remap 'Z' 'z' to 'A' 'a'

print
# returns the modified word or 0 if a non letter character is present in the input string 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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