2
\$\begingroup\$

I have been going to puzzling.se, and people over there write stuff in rot13 a lot, sot this gave me an idea:
Write a program that takes 2 inputs, a string and integer:

  1. the message to encode or decode
  2. how far to shift

You can input in a function call, and output in any legal way twist:
it must work with any printable ascii character. Example code:

def ceasar_cipher(message,shift):
    o = ""
    for n in message:
        o+=(chr(ord(n)+shift))
    return o

python 3 examples:

input:

ceasar_cipher(“hello, world”,13):

output:

"uryy|9-\x84|\x7fyq."

This is code-golf, so shortest code in bytes wins.
The output should be a string
-Can raise an error if it will go past ASCII-characters, or just not shift said characters
Good luck!

\$\endgroup\$
4
  • 9
    \$\begingroup\$ You can input in a function call, and output in any legal way twist: it must work with any printable ascii character, meaning that it will still shift by the same shift as everything else. Get quite lost here. Could you please rewrite this more clearly. Also competitions of this sort usually don't have to handle input errors. \$\endgroup\$
    – Noodle9
    Nov 29, 2020 at 1:29
  • 4
    \$\begingroup\$ What happens if the shift puts us past the largest character value, or a negative shift puts the character value below zero? Do we wrap around? The example code doesn't handle this. \$\endgroup\$
    – xnor
    Nov 29, 2020 at 7:11
  • \$\begingroup\$ @xnor you may raise an error, or just not shift it. \$\endgroup\$
    – pyton
    Nov 29, 2020 at 18:04
  • \$\begingroup\$ @pyton Thanks, you should add this info it's the challenge. \$\endgroup\$
    – xnor
    Nov 29, 2020 at 20:45

12 Answers 12

9
\$\begingroup\$

BQN, 1 byte

Anonymous tacit infix function. Order of arguments doesn't matter.

+

Try BQN!

\$\endgroup\$
2
  • \$\begingroup\$ was BQN made for this? \$\endgroup\$
    – pyton
    Nov 29, 2020 at 1:12
  • 1
    \$\begingroup\$ @pyton Not at all. Read the explanation here. \$\endgroup\$
    – Adám
    Nov 29, 2020 at 1:13
5
\$\begingroup\$

05AB1E, 4 3 bytes

crossed out 4 is still regular 4 ;(

-1 byte thanks to @Kevin Cruijssen.

Ç+ç

Try it online!

Ç+ç  # full program
 +   # increment...
Ç    # charcodes of...
     # implicit input
 +   # by...
     # implicit input
  ç  # convert to chars
     # implicit output
\$\endgroup\$
1
  • 1
    \$\begingroup\$ It's fine to leave the J out and just output as a list of characters. A string is a sequence of characters in most languages anyway, so when a string is asked you're also allowed to output as a list/array/stream of characters instead of an actual string. \$\endgroup\$ Nov 30, 2020 at 8:08
4
\$\begingroup\$

K, 4 bytes

Anonymous tacit function taking two arguments in any order.

`c$+

Try K!

+ add the arguments (this implicitly converts the characters to code points

$ cast to…

`c character

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 32 bytes

f(s,n)char*s;{for(;*s;*s+++=n);}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Wolfram Language (Mathematica), 40 bytes

FromCharacterCode[ToCharacterCode@#+#2]&

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Vyxal, s, 3 bytes

C+C

Explained

C     # Convert input string to a list of ord values
 +    # Add implicit number shift
  C   # Convert back to a list of chars and output

If it must absolutely be a function:

7 bytes

λ2|C+C;

Explained

λ2|C+C;
λ2|      # Start a lambda with two arguments
   C+C   # Same as the full program
      ;  # Close lambda
\$\endgroup\$
2
\$\begingroup\$

Stax, 4 bytes

{n+m

Run and debug it

a downside of m printing stuff with newline is that you need an explicit bracket.

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

Jelly, 3 bytes

O+Ọ

Try it online!

Explanation

O+Ọ  Main Link
O    chr->ord of left argument
 +   add (left with right; implicit vectorization)
  Ọ  ord->chr of left argument
\$\endgroup\$
1
  • \$\begingroup\$ I don't like the way this looks at me. \$\endgroup\$
    – lyxal
    Dec 21, 2020 at 0:18
1
\$\begingroup\$

Charcoal, 8 bytes

⭆η℅⁺Iθ℅ι

Try it online! Link is to verbose version of code. Explanation:

 η          Second input
⭆           Map over characters and join
       ι    Current character
      ℅     Ordinal
   ⁺        Plus
     θ      First input
    I       Cast to integer
  ℅         Character
            Implicitly print
\$\endgroup\$
1
\$\begingroup\$

Retina, 18 bytes

"$+"+T` -þ`!-ÿ
0A`

Try it online! Works on all ISO-8859-1 characters as long as the result does not exceed U+00FF. Add 2 bytes to extend the output range to U+07FF. Add a further 2 bytes to extend the output range to UCS-2 characters up to U+FFFF. Explanation:

"$+"+`

Repeat the given number of times.

T` -þ`!-ÿ

Bump all the supported code points up by 1.

0A`

Delete the count.

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

Husk, 6 bytes

mȯc+⁰c

Try it online!

m       # map over each element in arg 2
 ȯ      # 3 functions:
     c  # convert character to value
   +⁰   # add arg 1
  c     # convert back to character
\$\endgroup\$
1
\$\begingroup\$

Ruby 2.7, 33 30 bytes

->w,s{w.bytes.map{(_1+s).chr}}

Try it online!

Returns an array of chars. TIO uses an older version of Ruby, whereas in Ruby 2.7, we've numbered parameters, which saves two bytes.

\$\endgroup\$

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