r/dailyprogrammer • u/jnazario 2 0 • Feb 15 '16
[2016-02-16] Challenge #254 [Easy] Atbash Cipher
Description
Atbash is a simple substitution cipher originally for the Hebrew alphabet, but possible with any known alphabet. It emerged around 500-600 BCE. It works by substituting the first letter of an alphabet for the last letter, the second letter for the second to last and so on, effectively reversing the alphabet. Here is the Atbash substitution table:
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: ZYXWVUTSRQPONMLKJIHGFEDCBAAmusingly, some English words Atbash into their own reverses, e.g., "wizard" = "draziw."
This is not considered a strong cipher but was at the time.
For more information on the cipher, please see the Wikipedia page on Atbash.
Input Description
For this challenge you'll be asked to implement the Atbash cipher and encode (or decode) some English language words. If the character is NOT part of the English alphabet (a-z), you can keep the symbol intact. Examples:
foobar
wizard
/r/dailyprogrammer
gsrh rh zm vcznkov lu gsv zgyzhs xrksviOutput Description
Your program should emit the following strings as ciphertext or plaintext:
ullyzi
draziw
/i/wzrobkiltiznnvi
this is an example of the atbash cipherBonus
Preserve case.
4
u/jnazario 2 0 Feb 15 '16
Go solution, without bonus
2
u/jnazario 2 0 Feb 15 '16 edited Feb 18 '16
F# solution, no bonus. simple conversion to a Map<string, string>.
EDIT bonus support comes from a simple
tblchange:19
u/NarcissusGray Feb 17 '16 edited Feb 17 '16
Brainfuck (with challenge), returns on newline
Explained:
23
u/casualfrog Feb 15 '16 edited Feb 15 '16
JavaScript (edit: with bonus!)
without bonus:
8
u/darthjoey91 Feb 16 '16
Sed one-liner with bonus
Presumes inputs have been placed in file labeled input.
8
u/chunes 1 2 Feb 16 '16
This is a great use case for
Befunge-93:
6
u/Juerd Feb 16 '16 edited Feb 16 '16
Perl 6, with bonus:
The killer feature here is
which will preserve case in a regex substitution.
Without bonus or even uppercase, the following one-liner does the trick:
10
u/skatashit Feb 15 '16
C, with bonus. Also my first post here.
5
u/[deleted] Feb 15 '16 edited Feb 15 '16
C++ with bonus, using no other string than the input. Solved by calculating the replacement letter:
And one with shorter but very ugly calculations:
9
u/k1ll3rpanda Feb 15 '16 edited Feb 15 '16
MIPS assembly with bonus (updated to not need word length) .data
4
u/Bur_Sangjun Feb 15 '16 edited Feb 15 '16
Rust with bonus
Online Demo
4
u/hutsboR 3 0 Feb 15 '16
Elixir one liner:
Usage:
4
u/Rekumaru Feb 16 '16 edited Feb 16 '16
Prolog compiler abuse - read the errors for output;)
[Solution Description]
4
u/nrebeps Feb 17 '16
Julia: (I am just getting started in this language :-) )
1
u/Azphael Feb 16 '16
Decided to learn python this week. Here's my first piece of python and submission with bonus. Let me know if I'm not doing things "pythonic" here.
6
u/0x0dea Feb 15 '16
$ ruby -pe '$_.tr! "A-Za-z", [*?a..?z, *?A..?Z].join.reverse' input6
u/boxofkangaroos 1 0 Feb 15 '16
I didn't follow the directions, but here's one that finds all English words that Atbash into their reverses.
Output:
Note: 'trig' and 'girt' are the only pair of words that are semordnilaps and Atbash into each other!
3
u/fazxyll Feb 15 '16 edited Feb 15 '16
Java with bonus
Input:
Output:
3
u/bemmu Feb 15 '16
Python, with bonus
3
u/TheHelgeSverre Feb 15 '16 edited Feb 15 '16
C#
PHP
3
u/[deleted] Feb 15 '16
Python, no bonus:
3
u/[deleted] Feb 15 '16
Visual Basic 2015 - with bonus Someone has to!
Input/Output
http://imgur.com/dXDXtCk
3
u/Minolwa Feb 15 '16
Java 8 Solution:
Output:
3
u/groundisdoom Feb 15 '16
First time trying Elm:
Go here to run.
3
u/[deleted] Feb 15 '16
First time here! I welcome any and all feedback as I want to improve my C++
C++ With bonus!
3
u/G33kDude 1 1 Feb 16 '16 edited Feb 16 '16
Done in one line of python 2, with bonus
+/u/CompileBot python
3
u/papertrailz Feb 16 '16
LUA solution. Cipher challenges are fun!
3
u/[deleted] Feb 16 '16
C# one liner with bonus:
3
u/dbittman Feb 16 '16 edited Feb 17 '16
C, with bonus:
5
u/cym13 Feb 15 '16
D, with bonus:
4
u/gabyjunior 1 2 Feb 15 '16
Bash script with bonus
Output
2
u/Oblivious_Eyelid Feb 15 '16
Haskell with bonus
2
u/5k17 Feb 15 '16
Factor, with bonus:
2
u/supercodes Feb 15 '16
Python 2.7 (with bonus)
Example output:
2
u/__DavidBowman__ Feb 15 '16
Solution in Python; any feedback wellcome :)
Python 3
2
u/cheers- Feb 15 '16 edited Feb 15 '16
Java
functional approach + bonus
Edit:
Scala
2
u/Godspiral 3 3 Feb 15 '16 edited Feb 15 '16
in J,
2
u/YOLO_Ma Feb 15 '16
Clojure solution - with bonus
2
u/fvandepitte 0 0 Feb 15 '16 edited Feb 17 '16
Haskell, feedback welcome
After the feedback of /u/wizao
2
u/svgwrk Feb 15 '16 edited Feb 15 '16
Favorite puzzle in months. :)
Lazy-ass rust solution:
2
u/themagicalcake 1 0 Feb 16 '16
Go with bonus
I figured I might as well practice golang on this easy challenge
2
u/franza73 Feb 16 '16
2
u/JasonPandiras Feb 16 '16 edited Feb 16 '16
F# (simple dictionary translation with bonus)
Output:
The joining of the alphabets in bondage to the cipher variable can't help but look a bit awkward I'm afraid, but hey, I finally got to use the double pipe operator :D
I actually hadn't chanced to notice before that fsharp's string came with it's own collection functions, no .ToCharArray() necessary. That's what I get for pretty much always defaulting to System.String, I guess.
2
u/nrebeps Feb 17 '16 edited Mar 09 '16
Perl6 with bonus
2
u/chunes 1 2 Feb 17 '16
Case-preserving Java:
2
u/hyrulia Feb 18 '16 edited Feb 18 '16
Kotlin
Output
2
u/atomicpenguin12 Feb 19 '16
Python (with bonus, derived from this Javascript version: https://www.reddit.com/r/dailyprogrammer/comments/45w6ad/20160216_challenge_254_easy_atbash_cipher/d068p9a)
2
u/SirRust Mar 03 '16
Here's my
Rustsolution.I've recently started learning Rust so any feedback on how to write idiomatic Rust feedback is appreciated.
1
u/Torgard Feb 15 '16
Java, without bonus
1
u/Oops_TryAgain Feb 15 '16 edited Feb 23 '16
Javascript (ECMAScript 6) solution with bonus; handles arrays as input too.
Sample session:
1
u/HDean_ Feb 15 '16
Python 3
1
u/[deleted] Feb 15 '16 edited Feb 16 '16
Updated Go solution with bonus
1
u/Primo37 Feb 15 '16 edited Feb 15 '16
C#
static void Main(string[] args) {
fuck this im to stupid for this strange spoiler thing :/
1
u/j_random0 Feb 15 '16
Shell script
1
u/j_random0 Feb 15 '16 edited Feb 15 '16
C
Oops. This code has a bug. Bad things could happen.
1
u/Little0Smit Feb 15 '16
Java with bonus. first reply to any challenges, any tips welcome.
1
u/franza73 Feb 15 '16
Python
1
u/Calaer Feb 15 '16
C++ with Bonus
1
u/JakDrako Feb 15 '16
C# Functional style...
1
u/Specter_Terrasbane Feb 15 '16
Python 2.7, simple + bonus (Added another test input with caps to illustrate bonus)
Output
1
u/fibonacci__ 1 0 Feb 15 '16 edited Feb 15 '16
Python
1
u/[deleted] Feb 15 '16
Python 3.5 Feedback is always appreciated
Output
1
u/hfdias Feb 15 '16
ABAP with bonus:
1
u/EvgeniyZh 1 0 Feb 15 '16
C++ with bonus
1
u/[deleted] Feb 15 '16 edited Feb 15 '16
Python, in no hurry. (Edited to create the dictionary using the tuple-based constructor)
1
u/DownloadReddit Feb 15 '16 edited Feb 15 '16
C++14
Edit: Shortened the solution a bit.
1
u/sarinth Feb 15 '16
C#
1
u/[deleted] Feb 15 '16 edited Feb 15 '16
C with bonus
Input:
Output:
Edit:
1
u/shayhtfc Feb 15 '16
Ruby, with bonus
1
u/renaldorini Feb 15 '16
So I did this in Qt5 as I'm trying to learn it. It preserves case and line breaks. Github Link
1
u/FlammableMarshmallow Feb 15 '16
Python (with bonus!)
Output
Not much to say about it, this is just a simple use of
str.translate; Only thing I might change isALPHABETtostring.ascii_lowercase1
u/fvandepitte 0 0 Feb 15 '16
C++ with bonus, feedback is welcome
Result:
Explenation
1
u/changed_perspective Feb 15 '16 edited Feb 15 '16
Python 3 ridiculous one liner I wrote (no bonus)
With bonus:
1
u/ckrausko Feb 15 '16
PHP.
1
u/FlaconPunch Feb 15 '16
I'm sorry but what does the bonus mean?
1
u/[deleted] Feb 16 '16
Java Preserves case and anything not A-Za-z (includes Unicode multi-char sequences).
1
u/ADreamByAnyOtherName Feb 16 '16
Solution, with bonus, language: java
feedback is welcome.
1
u/vastronaut Feb 16 '16 edited Feb 16 '16
first post, featuring super-beginner-level c++
1
u/TheSpongeGod Feb 16 '16
Haskell - generic substitution cipher, with bonus:
1
u/emberstream Feb 16 '16 edited Feb 16 '16
Python 3.5.0 No Bonus yet.
1
u/RoadieRich Feb 16 '16
Python (with bonus)
Why? I felt like it.
1
u/Nyxisto Feb 16 '16 edited Feb 16 '16
Python 3:
1
u/VikingofRock Feb 16 '16 edited Feb 16 '16
Haskell
(case preserving)
output:
1
u/[deleted] Feb 16 '16
1
u/failtolaunch28 Feb 16 '16
C
First ever c program. I'm a sophomore in college, took ap cs in high school and a data structures class last year that I got nothing out of. I can't take another cs class until senior year, so I'm trying to improve to basic competency until then. Please, please criticize this.
1
u/jacsonW Feb 16 '16
C# with bonus
1
u/Moklomi Feb 16 '16
R - I'm ashamed to say this took so long
R has a funny way of dealing with string splits and my if else statement broke when iterating over that.... turns out the simple ( read took me a while to realize I needed a vector) solution was to coerce the type via unlist...... I'll deal with the bonus later
1
u/JakDrako Feb 16 '16
VB.NET with bonus.
Uses the fact that the lowercase range has an identical bit pattern to the uppercase range, except that the 6th bit is set.
Output
1
u/[deleted] Feb 16 '16
C++, with bonus
1
u/happy-elephant Feb 16 '16
In C. It's longer than needed, because I wanted to practice writing a function that would return a char pointer, and also wanted to practice splitting code across files.
But works, bonus included.
1
u/tcbenkhard Feb 16 '16
Java
Output
1
u/AdmissibleHeuristic 0 1 Feb 16 '16
Some R5RS, which could certainly be more LISP-y:
1
u/[deleted] Feb 16 '16 edited Feb 16 '16
Python:
This is my first time posting so hopefully this is up to par.
1
u/n2468txd Feb 17 '16
Kotlin (with bonus)
(my first program in Kotlin...)
Using / Output:
1
u/danneu Feb 17 '16
Elm
Example
1
u/oddolatry Feb 17 '16
Clojure, with bonus
1
u/AnnieBruce Feb 17 '16
I have a feeling this could be done in one list comprehension or regex but that seemed like too much work, and might be too opaque as to the purpose of the code.
Additionally, I realized that this is really a simple substitution cipher, and once I had the key I could just write a quick function to map the key to the input. This gives me a general program, and I just need to provide keys to work with basically any simple substitution cipher. And then, I realized that the Python dev team has already written the hard(or at least annoying) parts of the mapping function for me.
I might come back to this with a ROT13 function to demonstrate(and prove I didn't mess up on) the generic quality of the ciphering/deciphering engine.
edit- Removed a commented out relic of an earlier version.
Python 3.4
1
u/Kansoku Feb 17 '16
Python 3
1
u/RebootForAwesome Feb 17 '16 edited Feb 17 '16
Python 3.5
with bonus. Tried to do it in one line because I've never tried to do that kind of stuff (Not sure if it's the best challenge to start).
Not insane version:
1
u/[deleted] Feb 17 '16
Ruby:
1
u/IWieldTheFlameOfAnor Feb 17 '16
Java
1
u/JulianDeclercq Feb 17 '16 edited Feb 17 '16
Quick C++
1
u/[deleted] Feb 17 '16
Crystal
https://play.crystal-lang.org/#/r/sjl
1
u/omission9 Feb 17 '16
Perl 5 (with bonus):
1
u/draegtun Feb 17 '16 edited Feb 19 '16
Rebol (with bonus)
Example usage:
1
u/ViridianHominid Feb 18 '16
Python 2, with bonus:
If you're a fan of minimizing number of lines:
1
u/LordJackass Feb 18 '16
C++ solution:
1
u/Sock_Ninja Feb 18 '16
This is the first time I've posted a response to a challenge! It only took me 4 hours. Actually, this is only the second program I've ever made. The first was a magic eight ball program. Let me tell ya, it was a doozy. Anyway, I'm excited to have completed it!
1
u/[deleted] Feb 18 '16
Python 2.7 with bonus
1
u/speedster217 Feb 19 '16
Haskell with Bonus:
1
u/zachtower Feb 19 '16
Python 2.7 Solution with bonus. I decided to use ASCII values instead of generating a dictionary. Very fun little challenge.
1
u/primaryobjects Feb 19 '16
R
Run | Gist
1
u/[deleted] Feb 19 '16 edited Feb 19 '16
Python 3.x (with bonus)
I would be grateful for feedback of any sort.
1
u/dbittman Feb 19 '16
Thue, with bonus:
Output:
Limitations: The only non-alpha characters supported are / and space.
1
u/[deleted] Feb 19 '16
Python with bonus. Just started learning Python last week, so any feedback on how to improve would be appreciated:
1
u/xtcriott Feb 19 '16
vb.net with bonus!
Suggestions welcomed. I am sure there are better ways to do some of this. Going to ponder and rework it a bit if I figure anything out.
Output:
1
u/quests Feb 19 '16
Scala
1
u/CrunchyChewie Feb 19 '16 edited Feb 19 '16
Python3
No bonus(at the moment).
1
u/chunes 1 2 Feb 19 '16
I'm learning Clojure. Any input is welcome.
1
u/porphyro Feb 19 '16
CJam, 14 bytes
Try it online!
Only works for lowercase characters.
1
u/ashish2199 0 2 Feb 19 '16
Language Used: Java
1
u/IWannaFuckLarryPage Feb 19 '16
Pyth
http://pyth.herokuapp.com/?code=VwKxGNp%3FqK_1N%40G-25K&input=%2Fr%2Fdailyprogrammer%0A&debug=0
1
u/blanonymous Feb 19 '16
Haskell no bonus
1
u/atomicpenguin12 Feb 19 '16
Javascript (with bonus)
};
1
u/kangaroo_king Feb 19 '16
XSLT1.0 bonus
1
u/imTgv Feb 19 '16
Java, with bonus.
import java.util.Scanner;
public class Atbash_Cypher {
}
1
u/scul86 Feb 20 '16 edited Feb 20 '16
Python 2 with bonus
My first submission, and would love criticism and comments.
ETA: and the output:
1
u/danneu Feb 20 '16 edited Feb 20 '16
First time using Swift:
1
u/tajjet Feb 20 '16 edited Feb 21 '16
Java because I can't sleep.
Output:
Edit: extra output to show off that it keeps special characters:
1
u/defregga Feb 20 '16 edited Feb 20 '16
C++ Not a studied coder by any means and I took a long break since starting as a hobbyist, but these will be a welcome break and exercise while working through the C++ Primer 5th Ed. to learn it proper this time. Solution is somewhat clunky and I am sure as soon as I get back into strings and iterators I'll also be able to work out a solution that doesn't lose the spaces. (Having peaked at other C++ solutions after finishing mine, I already got the idea.)
Edit: proper solution incl. bonus with what I learned in the last hour.
1
u/fredrikaugust Feb 21 '16 edited Feb 21 '16
Trying to learn ruby, so here's my solution!
Also throwing in a one-line for good measures :)
EDIT: bonus
1
u/frederic777 Feb 21 '16 edited Feb 21 '16
i know it's a little late, but here it is, and i promise i haven't peeked at the already existing solutions. Oh and it only works for lower case letters and not special characters or uppercase:
Here's the same function but with recursion :
1
u/smapti Feb 21 '16
C++ with bonus, first post! Feedback welcome
1
u/Superheroguy89 Feb 21 '16
Java (With Bonus and RecursionbecauseIlikerecursion)
This may be long and stupid but atleast I did it :D
1
u/serkanh Feb 21 '16
Any advises from seasoned pythonistas appreciated: import unittest #https://www.reddit.com/r/dailyprogrammer/comments/45w6ad/20160216_challenge_254_easy_atbash_cipher/
1
u/[deleted] Feb 21 '16
Haskell w/ bonus
1
u/SportingSnow21 Feb 21 '16
Golang w/bonus
stdlib makes this one pretty easy. :)
1
u/[deleted] Feb 21 '16 edited Feb 21 '16
C# with bonus
first time posting, total noob approach
1
u/[deleted] Feb 21 '16 edited Feb 21 '16
Four days into Python, my solution with bonus
1
u/Oscarfromparis Feb 22 '16 edited Feb 22 '16
Python with bonus. Any advice on how to improve it? a= "/r/dailyprogrammer" atbash ="" for i in a: if 65<=ord(i)<=90: atbash += chr(65 + (90 - ord(i))) elif 97<=ord(i)<=122: atbash += chr(97 + (122 - ord(i))) else: atbash += i print(atbash)
1
u/[deleted] Feb 22 '16 edited Feb 22 '16
Kotlin with Bonus:
1
u/quincyjh Feb 22 '16
Python 3 (with bonus):
1
u/shlomocomputer Feb 22 '16
Haskell. I know, very overrepresented in /r/dailyprogrammer. HOWEVER, I wanted to point out that many are using
isLowerand related functions together withData.Char.ordarithmetic. This is incorrect, because non-ASCII characters test positive withisLower. There is a different function that will work as expected:1
u/[deleted] Feb 22 '16
Python2.7 (with bonus)
Output:
1
u/mudien Feb 23 '16
Python 3.5 with bonus
Output (with bonus):
1
u/CHURLZ_ Feb 23 '16
First post! This channel is awesome xD Started learning Python and all the sweet tricks that come with it, hence I tend to toy with one-liners rather than prdouce "good" code. Any feedback is welcome! Python:
1
u/lalago1 Feb 23 '16
ruby OOO, with bonus
1
u/iheatu Feb 24 '16
clojure: with bonus
1
u/Kenya151 Feb 24 '16
Java
1
u/sopa89 Feb 25 '16
Java with bonus package com.tc689.atbash;
1
u/lop3rt Feb 25 '16
Java programmer looking to get into Ruby
https://github.com/lopert/daily-programmer/blob/master/254-easy/atbash_cypher.rb
1st submission, feedback welcome / sought!
1
u/Grygon Feb 25 '16 edited Feb 25 '16
My newbie submission in Python:
Doesn't do the bonus yet, I just got a simple implementation working.
Any input would be much appreciated, still learning Python.
EDIT: Small change makes it compatible with the bonus!
1
u/Anarch33 Feb 26 '16
JavaScript, couldn't figure out the bonus, Im never good with regexs and stuff
1
u/IAmZeUsername Feb 26 '16
Python solution (tried with 3, should work with 2): ALPHABET = "abcdefghijklmnopqrstuvwxyz"
1
u/daviegravee Feb 28 '16
Python 2.7
1
u/OutputStream Feb 28 '16 edited Feb 28 '16
Scala:
1
u/x1147x Feb 29 '16
Haxe
1
u/waterskier2007 Mar 02 '16
Swift (w/ bonus included)
edit: output
ullyzi
draziw
/i/wzrobkiltiznnvi
this is an example of the atbash cipher
Ullyzi
Draziw
/i/wzrobkiltiznnvi
This is an example of the atbash cipher
1
u/HDInfinity Mar 03 '16
C++, feedback appreciated
1
u/frankperez87 Mar 04 '16
PHP with Bonus using Test Driven Approach
Usage: https://github.com/frankperez87/DailyProgrammerChallenges/tree/master/challenges/Challenge254
Solution: https://github.com/frankperez87/DailyProgrammerChallenges/blob/master/challenges/Challenge254/AtbashCipher.php
Unit Test: https://github.com/frankperez87/DailyProgrammerChallenges/blob/master/tests/Challenge254/AtbashCipherTest.php
1
u/bigolpete Mar 17 '16
C#
I did it! Very new here. Case included!
1
u/Lev1a Mar 18 '16
C++ with bonus
1
u/aerialB Mar 28 '16
Haskell With bonus.
Then just do
1
u/SynonymOfHeat Mar 28 '16
Python 3.5
1
u/[deleted] Mar 31 '16
Java (With Challenge)
Returns:
1
u/line_over Mar 31 '16
My take on this using Python 3.4
Output:
1
u/rjckE Apr 08 '16
C++
1
u/Gobbedyret 1 0 Apr 12 '16 edited Apr 12 '16
Python 3.5
With bonus.
Man, I love Python! This code reads almost like a description of the problem.
1
u/pacificjunction Apr 16 '16
Go. I know its been two months. I'm just trying to learn Go.
OUTPUT
1
u/marcelo_rocha Apr 17 '16
Dart
1
u/Tryer1234 Apr 30 '16
A solution in C, no bonus.
1
u/VerifiedMyEmail May 12 '16 edited May 12 '16
Java
1
u/[deleted] May 27 '16 edited May 27 '16
Another JS version with bonus I did quickly on my phone using array and map. The JS solution at the top using replace is still more elegant though.
1
u/4kpics Jun 30 '16
C++
1
u/SusuKacangSoya Jul 03 '16 edited Jul 03 '16
Java. Preserving the case was the only way I could do it (ignoring case seems harder due to the symbols in the way).
Bonus... Questionable Hiragana Atbash!
Questionable because that's a really poor order I used there (literally copied the Unicode), can be vastly improved by kicking out the small Kana, and then forming the cipher by laying first normal Hiragana, then Dakutens, then Handakuten, or even Atbashing them separately like how we do with uppercase and lowercase for the English alphabet.
Protip: You can substitute the Hiragana solution for other scripts as long as you make sure both plain and cipher have same length, or you modify it to be tolerant for different lengths.
1
u/vishal_jaiswal Aug 04 '16
My first post on Reddit. (Works for Bonus case also)