# The Decryption Function

You must write a program that takes an encrypted string and decrypt it according to specific rules and then print it out. Decryption will occur by performing two operations.

Sample Input Argument 1 (the encrypted string)

HGJILKBADCFE


Operation 1:

Swap the first half of the string with the second half, which should leave you with:

BADCFEHGJILK


Operation 2:

Swap every two characters with each other such as swapping character 1 with 2, 3 with 4, etc., which should leave you with the decrypted string:

ABCDEFGHIJKL


Guidelines:

• Input Argument 1 will contain only uppercase letters

• Input Argument 1's length will be between 2 and 100 characters

• Input Argument 1's length will always be an even number

• Preferably the input will be taken from the command line (like below).

• Another Test Case MPORQTSVUXWZYBADCFEHGJILKN is the input, Output is ABCDEFGHIJKLMNOPQRSTUVWXYZ

My Attempt

import sys
_,a=sys.argv
b=len(a)//2
s=a[b:]+a[:b]
print(''.join(x+y for x,y in zip(s[1::2],s[::2])))

• Welcome to code golf! Your challenge is well specified, but I'd suggest removing the wording 'and then print it out' to allow users to submit a function. Sep 21, 2020 at 2:36
• We have a list of allowed I/O methods by default, and as a code golf challenge, most answers will just ignore the "preferably..." part to save as much bytes as possible. (Also note that some languages simply cannot take command line args.) Sep 21, 2020 at 3:50
• Yes but languages like python, ruby and more can do that, and people have submitted answers in that, so they can try that also, right? Sep 21, 2020 at 4:18
• Of course they could if they feel like it. But accessing command line arguments is verbose in most languages, so they don't have incentives to do so. (Don't think of giving a bonus for that. It's even worse.) Sep 21, 2020 at 4:24
• You might like to add a test case where the input length is still even but not a multiple of 4, such as ABCDEF. A few submissions haven't handled this correctly (including mine, initially). Sep 21, 2020 at 8:04

# J, 15 bytes

[:,_2|.\-:@#|.]


Try it online!

Straightforward implementation of the formula.

Slightly more interesting (not helpful for J golfing, but maybe for another lang) is that the algorithm can be solved with a scan sum:

1. First take a 1 followed by -1 3, with -1 3 repeated up to the length of the list.
2. Scan sum that list.
3. Rotate the numbers half the list length.
4. Sort the original according those numbers.

See the TIO for a demo in J.

• Your 14 byter is wrong, just like Dingus' previous answer. (Operations 1 and 2 cannot be swapped: try 0123456789.) Sep 21, 2020 at 4:04

# Ruby-p, 48 47 bytes

$_=$_[l= ~/$//2,l]+$_[0,l];gsub /(.)(.)/,'\2\1'


Try it online!

Operation 1 is handled by splitting $_ (the predefined global variable that contains the input) into two substrings of equal length; ~/$/ gives the length of the input. Then gsub /(.)(.)/,'\2\1' completes Operation 2 by swapping each pair of characters. (With the -p flag, gsub without a receiver implicitly acts on $_.) • @Sachin To take input from the command line you could do something like this. Sep 21, 2020 at 4:46 • I tried swapping like, $_=$_.slice!($_.size/2..-1)+$_, but couldn't understand what you're doing here -> l= ~/$//2,l x_x Dec 5, 2020 at 7:36

## Explanation

ŒHṚFs2U  Main Link
ŒH       Split into two halves of similar length
Ṛ      Reverse the order (swap the two halves)
F     Flatten back into a single string
s2   Slice into chunks of length 2
U  Reverse each chunk
Output is implicitly as one string


The other one ṙotates it by $(Half of the string's Length) and then does the same thing for the second part of the challenge. # Python 3, 75 bytes Exactly as asked: input from command line, output to STDOUT. import sys _,s=sys.argv i=1 while s[i:]:print(end=s[i-len(s)//2]);i+=3|i%-2  Try it online! ## Python 2, 52 bytes If we can use a function: f=lambda s,i=1:s[i:]and s[i-len(s)/2]+f(s,i+3-i%2*4)  Try it online! The idea of all of these is that the index of the $$\i\$$'th output character in a string of length $$\n\$$ is: $$i - \frac{n}{2} + (-1)^i$$ Subject to the usual Python indexing semantics. If we rewrite this in an iterative fashion, then the $$\i\$$'th output character is: $$a_i - \frac{n}{2} \text{ where } a_0 = 1 \text{ and } a_i = a_{i-1} + 3 - 4(i\%2)$$ Shorter with certain precedence is: $$a_i - \frac{n}{2} \text{ where } a_0 = 1 \text{ and } a_i = a_{i-1} + 3|(i\%-2)$$ # Python 2, 50 bytes f=lambda s,i=0:s[i:]and s[(i^1)-len(s)/2]+f(s,i+1)  Try it online! Borrowing ideas from Sisyphus, the i'th character of the output is the character at index (i^1)-len(s)/2 of the input. Here, i^1 is XOR with 1, which flips the last bit and so swaps even/odd pairs 0<->1, 2<->3, 4<->5, ... Here's a non-recursive alternative for the same length, though it outputs a list of characters which I'm not sure is allowed. lambda s:[s[(i^1)-len(s)/2]for i in range(len(s))]  Try it online! • Very nice. I think this may be optimal. Sep 21, 2020 at 10:22 # C (gcc), 7372 70 bytes Saved 2 bytes thanks to Dominic van Essen!!! i;l;f(char*s){for(i=1;(l=strlen(s))/i;)putchar(s[(i+l/2-++i%2*2)%l]);}  Try it online! Inputs a string and outputs the decryption. ### Explanation Maps the index (starting at $$\0\$$ to the end), of the input string, $$\s\$$ of length $$\l\$$, to the correct place by shifting it over $$\\frac{l}{2}+1\$$ places and then back $$\2\$$ for odd indices. Using this $$\\mod{l}\$$ gives the correct index of $$\s\$$ for the next output character. • 70 bytes Sep 21, 2020 at 14:14 • @DominicvanEssen Nice one, thought I had too many parentheses - thanks! :-) Sep 21, 2020 at 15:18 # JavaScript (Node.js), 71 65 bytes s=>(s.slice(l=s.length/2)+s.slice(0,l)).replace(/(.)(.)/g,'$2$1')  Try it online! Saved 6 bytes thanks to @Shaggy. Original 71 byte solution: s=>(l=>s.slice(l)+s.slice(0,l))(s.length/2).replace(/(\w)(\w)/g,'$2$1')  Pretty simple stuff here - I used an inner function because I had to surround the slice calls anyway - this saves 4 bytes. • 65 bytes Sep 21, 2020 at 20:10 # Brachylog, 8 bytes ḍ↔cġ₂↔ᵐc  Try it online! ### How it works ḍ↔cġ₂↔ᵐc ḍ split in two halves ↔ reverse c join ġ₂ group with length two ↔ᵐ reverse each c join  # Python 3, 91 bytes a=input();x=len(a)//2;b=a[x:]+a[:x];c='' for i in range(0,len(b),2):c+=b[i+1]+b[i] print(c)  Try it online! • It should be taking input from the command line, like sys.argv Sep 21, 2020 at 3:13 • @Sachin usual convention in code golf is that input can be taken from STDIN, the command line, or as function parameters, and output can be to STDOUT or as a function return value, in any reasonable format. Of course, in some cases, it would make sense to restrict the input format, but consider whether or not it would improve the challenge to do so. In this case, I recommend leaving I/O as flexible as possible; generally, problems like this allow I/O as a string or list of characters as input, CL arguments, or function I/O. Sep 21, 2020 at 3:18 # Pip, 21 bytes RV_M(JRV(a<>#a/2)<>2)  Try it online! ## Explanation RV_M(JRV(a<>#a/2)<>2) (a<>#a/2) split input into parts of size length/2 JRV reverse the list, and join it to string <>2 split the joined string into parts of size 2 RV_M reverse each of those parts implicit output  # C (gcc), 787678 74 bytes Thanks to ceilingcat for the -4! Edit: Reverted to use addition instead of OR to avoid operator precedence issues. Rather than splitting the string, the function starts at the middle of the string and wraps around the entire string has been processed. To flip every other character, the index inverts the 1s position of the counter. f(s,i,j)char*s;{for(i=strlen(s),j=0;write(1,s+(i/2+j++/2*2+j%2)%i,j<i););}  Try it online! If the program absolutely must take from the command line: 82 bytes main(i,s,j)char**s;{for(i=strlen(*++s),j=0;write(1,*s+(i/2+j++/2*2+j%2)%i,j<i););}  Try it online! • @Dingus Thank you for the heads-up. I've reverted the code to my original submission. Sep 21, 2020 at 7:24 # Stax, 10 bytes ü♠yαæ♠╟«ºñ  Run and debug it What a wonderful online interpreter. Link is to unpacked version of code. ## Explanation ;%h/r$2/Frp
;           copy input
%          get it's length
h         halve it
/        split input into parts of that size
r       reverse

# bash+sed, 57 bytes

Takes input as a command line argument.

<<<"${1:${#1}/2}${1:0:${#1}/2}" sed 's/$$.$$$$.$$/\2\1/g'


Try it online!

# Burlesque, 17 bytes

iRsa2./!!2co)<-++


Try it online!

Description:

iR               # Generate all rotations of the input string
sa             # Duplicate and get length (which equals string length)
2./          # Divide by two
!!        # And grab the string that's been rotated that many times
2co     # Split the rotated string into chunks of two
<-   # Reverse each chunk
++ # Join together and implicitly output


# Japt-P, 9 bytes

éUÊz)ò mw


Try it

éUÊz)ò mw     :Implicit input of string U
é             :Rotate right by
UÊ           :  Length of U
z          :  Floor divided by 2
)         :End rotate
ò        :Partitions of length 2
m      :Map
w     :  Reverse
:Implicitly join and output


# Charcoal, 13 bytes

⭆⪪⪫⮌⪪θ⊘Ｌθω²⮌ι


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

        θ       Input string
Ｌ        Length
⊘         Halved
⪪θ          Split input string into substrings of this length
⮌            Reverse
⪫      ω      Join together
⪪        ²     Split into substrings of length 2
⭆               Map over substrings and join
ι   Current substring
⮌    Reversed
Implicitly print


((.)+?)((?<-2>.)+)3$1 (.)(.)$2$1  Try it online! Explanation: The first stage uses a .NET balancing group to match as few characters as possible into $1 while still matching the same number of characters into $3. $#2 increments for each character matched into $1 and decrements for each character matched into $3 but it cannot decrement below zero, so $1 is forced to consume the first half of the string to allow the end of the string to be reached. The second stage then flips pairs of adjacent characters. (Also ASCII art bewbs.) # MathGolf, 10 bytes h½/xy2/mx~  Try it online. Explanation: h # Get the length of the (implicit) input-string (without popping) # i.e. "HGJILKBADCFE" → "HGJILKBADCFE" and 12 ½ # Halve this length # → "HGJILKBADCFE" and 6 / # Split the string into parts of that size # → ["HGJILK","BADCFE"] x # Reverse this pair # → ["BADCFE","HGJILK"] y # Join it back together to a string # → "BADCFEHGJILK" 2/ # Split it into parts of size 2 # → ["BA","DC","FE","HG","JI","LK"] m # Map over each pair: x # Reverse the pair # → ["AB","CD","EF","GH","IJ","KL"] ~ # Pop and push all strings separated to the stack # → "AB", "CD", "EF", "GH", "IJ", and "KL" # (after which the entire stack joined together is output implicitly) # → "ABCDEFGHIJKL"  # Red, 89 bytes func[s][move/part s tail s(length? s)/ 2 rejoin collect[foreach[b a]s[keep rejoin[a b]]]]  Try it online! # Lua, 67 bytes a=...b=#a//2print(((a:sub(b+1)..a:sub(1,b)):gsub('(.)(.)','%2%1')))  Try it online! First, string is cut in two using sub functions and then concatenated back in reverse order. Then, gsub is used to swap pairs of characters. # Wolfram Language (Mathematica), 64 51 bytes #[[#+UnitStep@#&@Array[#+(-1)^#&,L=Tr[1^#],-L/2]]]&  Try it online! Port of Sisyphus's Python solution # CJam, 13 bytes q2/::\_,2//:\  Try it online! # Perl 5-pF, 40 bytes for$p("."x(@F/2),"."){s/($p)($p)/$2$1/g}


Try it online!

# Poetic, 472 bytes

DECODING THINGS:A BRIEFING
o,o,hello!i am agent Q
nah,Qs chosen by an infamous phoney
a misprint!oh,sorry!am i sorry
i am agent J.W,tech/A.I hacker
i see a piece o code,i am trying at a means on how i decode it
what i am doing:i am laying all A-Z clearly along a pathway
midway,put in zeros(O,O)cause J.W needs it to split em
i shift em in tandem,i get B,A
lastly,if it leaves you a letter,it is moved
o,then i am doing A-Z again,it is taken to a shift
ah ha!spying is EZ


Try it online!