30
\$\begingroup\$

Consider the Atbash transformation:

A|B|C|D|E|F|G|H|I|J|K|L|M
Z|Y|X|W|V|U|T|S|R|Q|P|O|N

Where A ⇔ Z and L ⇔ O, e.g. There is an interesting property that some words share. When some strings are translated to their atbash-equivalent, said translation is the original word reversed. I call these Atbash Self Palindromes.

As an example, let's translate WIZARD:

W → D
I → R
Z → A
A → Z
R → I
D → W

The result is DRAZIW, which is WIZARD reversed. Thus, WIZARD is an atbash self palindrome.

Objective Given a string of printable ASCII characters, output or return a truthy value if that string is an atbash self palindrome, and a falsey value otherwise. (This is done through STDIN, closest equivalent, functional input, etc. If your language cannot do any of these, consider choosing a different language you may hardcode the input.) You should do this case-insensitively. If the input is a palindrome and is unaffected by the atbash seqeunce, you should still output true, since a palindrome + itself is a palindrome. This is a , so the shortest program in bytes wins.

Test cases

"Input" => true, false

"WIZARD" => true
"Wizard" => true // case doesn't matter
"wIzArD" => true 
"W I Z A R D" => true
"W IZ ARD" => false // the atbash of this is D RA ZIW, which is not a palindrome of W IZ ARD
"ABCXYZ" => true // ZYXCBA
"345 09%" => false // is not a palindrome
"ev" => true // ve
"AZGDFSSF IJHSDFIU HFIA" => false
"Zyba" => true
"-AZ" => false // -ZA is not a reverse of -AZ
"Tree vvig" => true // Givv eert 
"$%%$" => true // palindrome
"A$&$z" => true // z$&$A

Leaderboard

The Stack Snippet at the bottom of this post generates the catalog from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=68757,OVERRIDE_USER=44713;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
10
  • \$\begingroup\$ Related: Swap the Alphabet. \$\endgroup\$
    – nicael
    Jan 7, 2016 at 8:07
  • 5
    \$\begingroup\$ AtBash is actually not a new thing. It is the Kabala (Jewish Mysticism) Hebrew letter transformation equivalent to this. Since Hebrew is written only with with wovels, any letter string can be read by inserting random wovels. A-T-B(a)SH is a mnemonic for transforming Alef (first Hebrew letter) to Tav (last), Beis (second) to SHin (next-to-last). \$\endgroup\$
    – Adám
    Jan 7, 2016 at 9:17
  • 1
    \$\begingroup\$ Consider giving -1000000 points if someone's solution code is itself an atbash self palindrome? :p \$\endgroup\$
    – kojiro
    Jan 7, 2016 at 12:06
  • 3
    \$\begingroup\$ @kojiro Non-trivial as opposed to code {Comment-symbol}{Atbash'ed Comment-symbol} Atbash'ed code... \$\endgroup\$
    – Adám
    Jan 7, 2016 at 13:20
  • 1
    \$\begingroup\$ @mbomb007 I said I may offer a bounty if such a nontrivial program is found \$\endgroup\$ Jan 8, 2016 at 17:28

29 Answers 29

11
\$\begingroup\$

Pyth, 10 9 bytes

qJrz0_XJG

Try this fiddle online or verify all test cases at once.

Explanation

qJrz0_XJG
  rz0      Lowercase input
 J         Store a copy in J
     _XJG  Translate J with the reverse alphabet and reverse
q          Compare
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Since you're using rz0 twice, isn't it shorter to save it to a variable? \$\endgroup\$
    – xnor
    Jan 7, 2016 at 5:13
  • 1
    \$\begingroup\$ Like @xnor suggests, q_Jrz0XJG is one byte shorter. \$\endgroup\$ Jan 7, 2016 at 7:15
8
\$\begingroup\$

RX, 9 8 bytes

Heavily inspired by Retina, I made this a few days ago. Code:

prR`w$rM

Explanation:

prR`w$rM

p         # Start pattern
 r        # Reversed lowercase alphabet
  R       # Reversed uppercase alphabet
   `      # Next pattern
    w     # Equivalent to a-zA-Z_0-9 (word pattern)
     $    # End pattern and compute regex
      r   # Reverse input
       M  # Change mode to Match mode, compares the atbash string with the reversed string.

Try it here!

\$\endgroup\$
5
  • \$\begingroup\$ So how does the language itself actually work? Is it some sort of stack-based string processing language? This is really impressive, but as far as I can tell there is no way to loop in the language yet which means it's highly unlikely that this meets our standards of a programming language at this stage. \$\endgroup\$ Jan 7, 2016 at 15:24
  • \$\begingroup\$ @MartinBüttner This language is primarily based on the processing of the input using a stack model. It does not use integers (and probably never will). I do have implemented a loop, but that version has not been released yet. \$\endgroup\$
    – Adnan
    Jan 7, 2016 at 15:50
  • \$\begingroup\$ @Martin Regexes are capable of primality testing on their own, so I'm pretty sure this is valid. \$\endgroup\$
    – lirtosiast
    Jan 7, 2016 at 17:15
  • \$\begingroup\$ @ThomasKwa As far as I can see, the interpreter does not use any actual regular expressions. \$\endgroup\$ Jan 7, 2016 at 17:19
  • \$\begingroup\$ @Martin Hmm, you're right. \$\endgroup\$
    – lirtosiast
    Jan 7, 2016 at 17:22
6
\$\begingroup\$

Julia, 96 bytes

s->join([get(Dict(zip([u=map(Char,65:90);],reverse(u))),c,c)for c=(S=uppercase(s))])==reverse(S)

This is a lambda function that accepts a string and returns a string. To call it, assign it to a variable.

Ungolfed:

function f(s::AbstractString)
    # Get all of the uppercase letters A-Z
    u = map(Char, 65:90)

    # Create a dictionary for the transformation
    D = Dict(zip(u, reverse(u)))

    # Uppercase the input
    S = uppercase(s)

    return join([get(D, c, c) for c in S]) == reverse(S)
end
\$\endgroup\$
0
5
\$\begingroup\$

Bash + Linux utils, 56

tr a-z `printf %s {z..a}`<<<${1,,}|cmp - <(rev<<<${1,,})

Outputs the empty string for Truthy and something like - /dev/fd/63 differ: byte 1, line 1 for Falsey. If this is not acceptable then we can add -s for an extra 3 bytes and use the standard Unix return codes of 0 for Success (Truthy) and 1 for Failure (Falsey).

\$\endgroup\$
5
\$\begingroup\$

Retina, 44 bytes

$
¶$_
T`lL`Ro`.+$
+`(¶.*)(.)
$2$1
i`^(.+)\1$

Prints 1 or 0. The byte count assumes that the file is encoded as ISO 8859-1.

Try it online!

This answer was largely inspired by DigitalTrauma's sed answer but then again I guess there aren't that many approaches to this challenge in the first place.

Explanation

Whenever you see a , the first thing Retina does after splitting the code into lines is to replace all of those pilcrows with linefeeds. This allows the inclusion of linefeeds for a single byte even though linefeeds are Retina's stage separator.

$
¶$_

We start by duplicating the input. We match the end of the input with $ and insert a linefeed along with the input itself (using $_).

T`lL`Ro`.+$

A transliteration stage. Let's start with the regex: .+$. It matches the second copy of the input (by ensuring the match goes until the end of the string). So only characters in the second copy will be transliterated. The transliteration itself makes use of some very recent features. l and L are character classes for lower and upper case letters, respectively. o refers to the other character set of the transliteration and R reverses it. So the two character sets expand to:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba

You'll notice that this swaps the case while doing the Atbash cypher, but we'll do the final comparison case-insensitively anyway.

+`(¶.*)(.)
$2$1

Now we reverse the second copy. Unfortunately, Retina doesn't have a convenient way for doing that yet, so we'll have to move one character from the end to the front at a time. This is done by repurposing the linefeed separator as a marker of which part hasn't been reversed yet. We match that part but capture the last character separately. That character goes in front, and the remainder is unchanged. The + tells Retina to do this repeatedly until it's no longer possible (because is at the end of the string).

i`^(.+)\1$

Finally, we check whether the two strings are the same. The i makes the pattern case-insensitive - conveniently, in .NET, this means that backreferences are also case-insensitive. You might notice that we no longer have a separator between the original input and the modified copy. We don't need one though, because they are the same length, and if the string now consists exactly of the same string twice (up to case), then those must be the original and the modified string. If you're wondering what happened to the trailing linefeed that we used as a marker, it's still there, but in many regex flavours $ also matches before the last character of the string if that character is a linefeed.

Since this stage only consists of a single line, it is taken to be a match stage, which counts the number of matches. If the input is an Atbash palindrome, we'll get exactly one match and the output is 1. If not, then this regex won't match and the output will be 0.

\$\endgroup\$
2
  • \$\begingroup\$ I guess it's better to have linefeeds be the stage separators and pilcrows be the literal than vice versa. \$\endgroup\$ Jan 7, 2016 at 16:54
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ For convenience, you can also insert linefeeds by escape sequences, \n in regex and $n in substitution, but that's wasted bytes for golfing. ;) \$\endgroup\$ Jan 7, 2016 at 16:55
5
\$\begingroup\$

GNU Sed, 105

s/.*/\l&/
h
y/abcdefghijklmnopqrstuvwxyz/zyxwvutsrqponmlkjihgfedcba/
G
:
s/\(.\)\n\1/\n/
t
/^.$/{c1
q}
c0

Outputs 1 for truthy and 0 for falsey.

I tried to do this in Retina, but couldn't figure out how to save the string before Atbash transliteration for reverse comparison with after. Perhaps there is a better way.

Sed's y transliteration command leaves a lot to be desired.

\$\endgroup\$
5
  • \$\begingroup\$ Yeah, "storing" things is still cumbersome in Retina. You'd have to duplicate the string and then transliterate and reverse only one copy. I want to add some sort of branching/forking feature in the future, but I'm not quite sure yet about the details. \$\endgroup\$ Jan 7, 2016 at 8:14
  • \$\begingroup\$ Ah, I think I see - I tried doing something similar by separating the before and after strings with a colon. I fell down with the regex at the end of the T - I was assuming it applied to each character in turn, but it if my understanding is right, it applies to the whole pattern space, which is much more useful \$\endgroup\$ Jan 7, 2016 at 16:19
  • 1
    \$\begingroup\$ The regex in T is applied to the input string. The transliteration is the only performed within the matches of that regex and everything unmatched is left unchanged. The regex defaults to [\s\S]+ so by omitting it, you're transliterating everything. \$\endgroup\$ Jan 7, 2016 at 16:21
  • \$\begingroup\$ As you wish. :) \$\endgroup\$ Jan 7, 2016 at 16:40
  • \$\begingroup\$ Since it's GNU sed, you can save a byte by trading -r flag for the backslashes in \( and \). I agree with you on the y command! \$\endgroup\$ Jan 7, 2016 at 18:54
4
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 15 chars / 30 bytes

ïþ)Ī(ᶐ,ᶐᴙ)ᴙ≔ïþ)

Try it here (Firefox only).

Explanation

ïþ)Ī(ᶐ,ᶐᴙ)ᴙ≔ïþ) // implicit: ï=input, ᶐ=A-Z
ïþ)             // ï.toUpperCase()
   Ī(ᶐ,ᶐᴙ)ᴙ     // transliterate input from A-Z to Z-A, then reverse
           ≔ïþ) // check if that's still equal to ï.toUpperCase()
                // implicit output
\$\endgroup\$
4
\$\begingroup\$

Parenthetic, 658 bytes

((()()())(()(((()))))((()()((())))))((()()())(()(((())))()()())((())()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()))((()()())(()(((())))())((()())((()(((())))()()))((()()()())((()(())(()))(()(((())))()())(()((()))))(()((())))((()((()))())((()(())(())())((()(()))(()(((())))()()())((()(()()))((())()()()()()()()()()()()()()()()()()()()()()()()()())((()(()()))((()(()())())((()((()))(()))(()(((())))()())))(()(((())))()()())))))((()(((())))())((()((()))()())(()(((())))()())))))))((()(())(()))((()()()(()))((()()()()())(()(((()))))))((()(((())))())((()()()()())(()(((())))))))

Only works for all caps without whitespace right now, using this modified version of the script so that it supports reading from stdin:

#!/usr/bin/env python
from collections import defaultdict
from itertools import izip
import copy
import operator
import os
import sys

# map from paren strings to english names
# for the predefined symbols (lambda, etc)
to_english = defaultdict(lambda:None,\
    {'()': 'lambda',
     '()()': 'define',
     '(())': 'plus',
     '(()())': 'minus',
     '()(())': 'mult',
     '(())()': 'div',
     '()()()': 'if',
     '((()))': 'empty',
     '()()()()': 'charsof',
     '()()(())': 'reverse',
     '()(())()': 'LE',
     '()(()())': 'not',
     '(()())()': 'intofchar',
     '()((()))': 'readline',
     '((()))()': 'cons',
     '(())(())': 'equal',
     '((()))(())': 'car',
     '((()))()()': 'cdr',
     '(())(())()': 'char',
     '(())()(())': 'string'})

# map from english to parenthetic
to_scheme = defaultdict(lambda:None)
for k,v in to_english.iteritems():
    to_scheme[v] = k

def Error(errorString = 'unmatched parens', debug_mode = True):
    if debug_mode:
        print "Error: " + errorString
        sys.exit()
    else:
        raise Exception('paren mismatch')

def bracketsMatch(chars):
    """Returns False if any parentheses in `chars` are not matched
    properly. Returns True otherwise.
    """
    level = 0
    for p in chars:
        if p == '(':
            level += 1
        elif p == ')':
            level -= 1
        if level < 0:
            return False    
    return level == 0

def get_exprs(chars):
    """Returns a list of character sequences such that for each sequence,
    the first and last parenthesis match.
    For example, "(())()()" would be split into ["(())", "()", "()"]
    """
    level = 0
    current = []
    for p in chars:
        if p == '(' or p == ')':
            current.append(p)
        if p == '(':
            level += 1
        elif p == ')':
            level -= 1
        if level == 0:
            yield current
            current = []

## built-in functions ##
def builtin_accumulate(init, accumulate, environment, params):
    """Helper function that handles common logic for builtin functions.
    Given an initial value, and a two-parameter function, the environment, and
    a list of params to reduce, this function will reduce [init] + params using
    the accumulate function and finally returns the resulting value.
    """
    result = init
    for param in params:
        value = interpret(param, environment)
        try: result = accumulate(result, value)
        except: Error(str(value) + ' is not the correct type')
    return result

def builtin_plus(environment, params):
    if len(params) >= 1:
        return builtin_accumulate(interpret(params[0], environment), operator.add, environment, params[1:])
    else:
        return 0.0

def builtin_minus(environment, params):
    if len(params) == 0:
        Error('subtraction requires at least 1 param')
    return builtin_accumulate(interpret(params[0], environment), operator.sub, environment, params[1:])

def builtin_mult(environment, params):
    return builtin_accumulate(1.0, operator.mul, environment, params)

def builtin_div(environment, params):
    if len(params) == 0:
        Error('division requires at least 1 param')
    return builtin_accumulate(interpret(params[0], environment), operator.div, environment, params[1:])

def builtin_LE(environment, params):
    return interpret(params[0], environment) <= interpret(params[1], environment)

def builtin_lambda(environment, params):
    bodies = [body for body in params[1:]]
    params = params[0][1]
    if len(bodies) == 0:
        Error("a function had no body")
    for kind, name in params:
        if kind != 'symbol':
            Error('lambda must have only symbols as arguments')
    def ret(old_environment, arguments):
        #print bodies
        try:
            # create new environment based on args
            environment = copy.copy(old_environment)
            for param, arg in izip(params, arguments):
                environment[param[1]] = interpret(arg, old_environment)
            # evaluate the function bodies using the new environment
            return interpret_trees(bodies, environment, False)
        except:
            Error("Error evaluating a function")
    return ret

def builtin_equal(environment, params):
    for param1, param2 in izip(params[:-1], params[1:]):
        if interpret(param1, environment) != interpret(param2, environment):
            return False
    return True

def builtin_if(environment, params):
    if len(params) != 3:
        Error("'if' takes in exactly 3 params")    
    if interpret(params[0], environment):
        return interpret(params[1], environment)
    return interpret(params[2], environment)

def builtin_not(environment, params):
    return False if interpret(params[0], environment) else True

def builtin_cons(environment, params):
    return (interpret(params[0], environment), interpret(params[1], environment))

def builtin_car(environment, params):
    result = interpret(params[0], environment)
    if not isinstance(result, tuple):
        Error("car must only be called on tuples")
    return result[0]

def builtin_cdr(environment, params):
    result = interpret(params[0], environment)
    if not isinstance(result, tuple):
        Error("cdr must only be called on tuples")
    return result[1]

def builtin_char(environment, params):
    result = interpret(params[0], environment)
    if result != int(result):
        Error("char must only be called on integers")
    return chr(int(result))

def builtin_intofchar(environment, params):
    result = interpret(params[0], environment)
    result = ord(result)
    return result

def builtin_string(environment, params):
    result = ''
    cur = interpret(params[0], environment)
    while cur != ():
        if not isinstance(cur, tuple) or not isinstance(cur[1], tuple):
            Error("string only works on linked lists")
        result += cur[0]
        cur = cur[1]
    return result

def unmakelinked(llist):
    result = ()
    while llist != ():
        if not isinstance(llist, tuple) or not isinstance(llist[1], tuple):
            Error("only works on linked lists")
        result += (llist[0],)
        llist = llist[1]
    return result

def makelinked(tup):
    result = ()
    while tup != ():
        result = (tup[-1],result)
        tup = tup[:-1]
    return result

def builtin_reverse(environment, params):
    result = interpret(params[0], environment)
    result = makelinked(unmakelinked(result)[::-1])
    return result

def builtin_charsof(environment, params):
    result = interpret(params[0], environment)
    result = makelinked(tuple(result))
    return result

def builtin_readline(environment, params):
    result = raw_input()
    return result

# define the default (top-level) scope
default_environment = \
    {to_scheme['plus']: builtin_plus,
     to_scheme['minus']: builtin_minus,
     to_scheme['mult']: builtin_mult,
     to_scheme['div']: builtin_div,
     to_scheme['lambda']: builtin_lambda,
     to_scheme['if']: builtin_if,
     to_scheme['equal']: builtin_equal,
     to_scheme['LE']: builtin_LE,
     to_scheme['not']: builtin_not,
     to_scheme['empty']: (),
     to_scheme['car']: builtin_car,
     to_scheme['cdr']: builtin_cdr,
     to_scheme['cons']: builtin_cons,
     to_scheme['char']: builtin_char,
     to_scheme['string']: builtin_string,
     to_scheme['readline']: builtin_readline,
     to_scheme['charsof']: builtin_charsof,
     to_scheme['reverse']: builtin_reverse,
     to_scheme['intofchar']: builtin_intofchar}

# parse the tokens into an AST
def parse(tokens):
    """Accepts a list of parentheses and returns a list of ASTs.
    Each AST is a pair (type, value).
    If type is 'symbol', value will be the paren sequence corresponding
    to the symbol.
    If type is 'int', value will be a float that is equal to an int.
    If type is expr, value will be a list of ASTs.
    """
    # check for errors
    if not bracketsMatch(tokens):
        Error('paren mismatch')
    # to return - a list of exprs
    exprs = []
    for expr in get_exprs(tokens):
        # check for errors
        if len(expr) < 2:
            Error('too few tokens in: ' + ''.join(expr))
        elif expr[0] != '(' or expr[-1] != ')':
            Error('expression found without () as wrapper')
        # pop off starting and ending ()s
        expr = expr[1:-1]
        # symbol
        if expr[:2] == ['(', ')'] and len(expr) > 2:
            exprs.append(('symbol', ''.join(expr[2:])))
        # integer
        elif expr[:4] == ['(', '(', ')', ')'] and len(expr) >= 4:
            exprs.append(('num', expr[4:].count('(')))
        # expr
        else:
            exprs.append(('expr', parse(expr)))
    return exprs

def interpret(tree, environment):
    """Interpret a single tree (may not be a define) and return the result"""
    kind, value = tree
    if kind == 'num':
        return float(value)
    elif kind == 'symbol':
        if value in environment:
            return environment[value]
        else:
            Error('Unresolved symbol - ' + value)
    elif kind == 'expr':
        function = interpret(value[0], environment)
        if not hasattr(function, '__call__'):
            Error('Symbol "'+value[0]+'" is not a function.')
        return function(environment, value[1:])
    else:
        Error("Unknown tree kind")

def interpret_trees(trees, environment, doprint = True):
    """Interpret a sequence of trees (may contain defines)
    and output the result.
    The trees passed in should be ASTs as returned by parse().
    If doprint is true, the post-interpretation value of each tree is printed.
    """
    environment = copy.copy(environment)
    # hoist define statements (note: trees.sort is stable)
    #trees.sort(key = lambda x: 0 if x[0] == 'expr' and x[1][0][1] == to_scheme['define'] else 1)
    ret = None
    for tree in trees:
        if tree[0] == 'expr' and tree[1][0][0] == 'symbol' and tree[1][0][1] == to_scheme['define']:
            try:
                symbol = tree[1][1]
                if symbol[0] != 'symbol':
                    Error('first argument to define must be a symbol')
                symbol = symbol[1]
                value = tree[1][2]
                environment[symbol] = interpret(value, environment)
            except:
                Error('error evaluating define statement')
        else:
            ret = interpret(tree, environment)
            if doprint:
                print ret,
    return ret

# read in the code ignoring all characters but '(' and ')' 
f = open(sys.argv[1],'r')
code = []
for line in f.readlines():
    code += [c for c in line if c in '()']

# parse and interpret the code. print 'Parenthesis Mismatch'
# if an error occured.
#try:
syntax_trees = parse(code)
interpret_trees(syntax_trees, default_environment)
#except:
#    print 'Parenthesis Mismatch'

Explanation

(
  define
  (() ()())
  input [[[[]]]]
  (() (((()))))
  exec readline
  ( (() ()((()))) )
)
(
  define
  (() ()())
  value of 'A' [[[[]]]] [][][]
  (() (((())))()()())
  65
  ((()) ()()()()()()()()()()
        ()()()()()()()()()()
        ()()()()()()()()()()
        ()()()()()()()()()()
        ()()()()()()()()()()
        ()()()()()()()()()()
        ()()()()())
)
(
  define
  (() ()())
  atbash [[[[]]]] []
  (() (((())))())
  (
    lambda
    (() ())
    (
      list [[[[]]]] [][]
      (() (((())))()())
    )
    (
      if
      (() ()()())
      (
        equal
        (() (())(()))
        list
        (() (((())))()())
        empty
        (() ((())))
      )
      then return empty
      (() ((())))
      else
      (
        cons
        (() ((()))())
        (
          char
          (() (())(())())
          (
            plus
            (() (()))
            value of 'A' 65
            (() (((())))()()())
            (
              minus
              (() (()()))
              25
              ((()) ()()()()()()()()()()
                    ()()()()()()()()()()
                    ()()()()())
              (
                minus
                (() (()()))
                (
                  intofchar
                  (() (()())())
                  (
                    car
                    (() ((()))(()))
                    list
                    (() (((())))()())
                  )
                )
                value of 'A' 65
                (() (((())))()()())
              )
            )
          )
        )
        (
          atbash
          (() (((())))())
          (
            cdr
            (() ((()))()())
            list
            (() (((())))()())
          )
        )
      )
    )
  )
)

(
  equals
  (() (())(()))
  (
    reverse
    (() ()()(()))
    (
      charsof
      (() ()()()())
      input
      (() (((()))))
    )
  )
  (
    atbash
    (() (((())))())
    (
      charsof
      (() ()()()())
      input
      (() (((()))))
    )
  )
)
\$\endgroup\$
1
  • 4
    \$\begingroup\$ Do you want your code to be the longest? :P \$\endgroup\$
    – Zorgatone
    Jan 7, 2016 at 13:27
4
\$\begingroup\$

Python 3, 90 85 bytes

s=input().upper()
print(s[::-1]==''.join(chr([o,155-o][64<o<91])for o in map(ord,s)))

We convert the input to uppercase, and then calculate the Atbashed string by subtracting all ordinals from 155 if they're in the uppercase alphabet range.

\$\endgroup\$
4
\$\begingroup\$

Kerf, 73 bytes

Kerf is a proprietary language in the same general family as APL, J and K. It's possible to write cryptic, compact oneliners and avoid the use of explicit loops:

{[s]s:_ s;n:?isnull i:s search\<a:char 97+^26;f:(/a)[i];s match/f[n]:s[n]}

However, using the spelled-out aliases for commands instead of the shorthand symbols and using meaningful identifiers makes the program much clearer, and fairly easy to follow even if you aren't familiar with Kerf:

def atbash_palindrome(str) {
  str:       tolower str
  alpha:     char range(97, 123)
  indices:   str search mapleft alpha
  encoded:   (reverse alpha)[indices]
  notfound:  which isnull indices
  return     str match reverse encoded[notfound]:str[notfound]
}

In action:

KeRF> p: {[s]s:_ s;n:?isnull i:s search\<a:char 97+^26;f:(/a)[i];s match/f[n]:s[n]};

KeRF> p mapdown ["WIZARD","Wizard","W I Z A R D","ABCXYZ","345 09%","ev","Zyba","$%%$","-AZ"]
  [1, 1, 1, 1, 0, 1, 1, 1, 0]

Kerf probably isn't going to win a ton of codegolf competitions, especially against purpose-built languages, but it might be worth tinkering with if you like the idea of APL-family languages but find the syntax too weird. (Disclaimer: I'm the author of the reference manual for Kerf.)

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

Prolog, 121 bytes

a(W):-upcase_atom(W,X),atom_codes(X,C),b(C,Z),!,reverse(Z,C).
b([A|T],[R|S]):-(A>64,A<91,R is 77-A+78;R=A),(b(T,S);S=[]).

This is called with an atom as input, e.g. a('WIZARD')..

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

JavaScript (ES6), 91

x=>(x=[...x.toLowerCase()]).every(c=>((v=parseInt(c,36))>9?(45-v).toString(36):c)==x.pop())

TEST

F=x=>(x=[...x.toLowerCase()]).every(c=>((v=parseInt(c,36))>9?(45-v).toString(36):c)==x.pop())

console.log=x=>O.textContent+=x+'\n'

;[
 ["WIZARD", true]
,["Wizard", true] // case doesn't matter
,["wIzArD", true]
,["W I Z A R D", true]
,["W IZ ARD", false] // the atbash of this is D RA ZIW, which is not a palindrome of W IZ ARD
,["ABCXYZ", true] // ZYXCBA
,["345 09%", false] // is not a palindrome
,["ev", true] // ve
,["AZGDFSSF IJHSDFIU HFIA", false]
,["Zyba", true]
,["-AZ", false] // -ZA is not a reverse of -AZ
,["Tree vvig", true] // Givv eert 
,["$%%$", true] // palindrome
,["$%ZA%$", true]
].forEach(t=>{var i=t[0],x=t[1],r=F(i);
              console.log(i+' -> '+r+(x==r?' OK':' Fail (expected:'+x+')'))})
<pre id=O></pre>

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

C, 101 97 bytes

As the question specified ASCII characters, this doesn't handle any other encodings.

f(char*s){char*p=s+strlen(s);while(*s&&!(isalpha(*--p)?*s<64||*s+*p-27&31:*s-*p))++s;return s>p;}

Explanation

int f(char*s)
{
    char *p = s + strlen(s);
    while (*s && !(isalpha(*--p) ? *s<64||*s+*p-27&31 : *s-*p))
        ++s;
    return s > p;
}

We make a pointer p that begins at the end of the string. We then loop, moving both s and p towards each other s reaches the end. This means that every pair of characters will be checked twice, but this saves a couple of bytes compared to stopping as soon as the pointers cross over.

At each iteration, we check whether *p is a letter. If so, check that *s is in the range of letters (ASCII 64 upward), and that *p and *s add up to 27 (mod 32). Non-letters over 64 will fail that test, so we don't need to check isalpha(*s).

If *p is not a letter, then we simply test whether it is equal to *s. In either case, we terminate the loop before s and p cross over.

If s and p have crossed, then every pair of letters matched correctly, so we return true; otherwise we return false.

Test program

Pass the strings to be tested as command-line arguments. This produces correct output for all the test cases. There is no supplied requirement for the empty string; my implementation returns false for that input.

#include <stdio.h>

int main(int argc, char **argv)
{
    while (*++argv)
        printf("\"%s\" => %s\n", *argv, f(*argv)?"true":"false");
    return 0;
}
\$\endgroup\$
1
  • \$\begingroup\$ you can drop f's type declaration for a K&R style prototype: f(char*s) \$\endgroup\$
    – cat
    Apr 7, 2016 at 19:08
3
\$\begingroup\$

Perl 5, 70 bytes

A subroutine:

{$"='';reverse(map/[A-Z]/?chr(155-ord):$_,(@_=split'',uc$_[0]))eq"@_"}

See it in use:

print sub{...}->("W i z a r d")
\$\endgroup\$
3
\$\begingroup\$

Vyxal, 7 bytes

⇩ḂkaḂĿ=

Try it Online!

⇩       # Lowercase input
 Ḃ      # Duplicate and reverse
     Ŀ  # Transliterate by...
  kaḂ   # lowercase alphabet and backwards lowercase alphabet
      = # is it equal to the original?
\$\endgroup\$
2
  • \$\begingroup\$ Still new in using this lang but using allows to save 2 bytes: ⇩ḂkaḂĿ= \$\endgroup\$
    – u-ndefined
    Oct 13, 2022 at 12:56
  • \$\begingroup\$ @u-ndefined Thanks! \$\endgroup\$
    – emanresu A
    Oct 13, 2022 at 21:34
2
\$\begingroup\$

MATL, 23 bytes

Uses current release.

jkt"@@2Y2m?_219+]h]tP=A

Examples

>> matl
 > jkt"@@2Y2m?_219+]h]tP=A
 > 
> Tree vvig
1

>> matl
 > jkt"@@2Y2m?_219+]h]tP=A
 > 
> W IZ ARD
0
\$\endgroup\$
2
\$\begingroup\$

CJam, 18 bytes

qeu_'[,65>_W%erW%=

Try it online

Works by converting input to uppercase, performing the translation of letters, flips the string, and checks for equality.

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

Japt, 30 27 bytes

U=Uv)w ¥Ur"[a-z]"_c +4^31 d

Try it online!

How it works

This is based largely on my Japt answer on Swap the Alphabet.

U=Uv)w ¥Ur"[a-z]"_c +4^31 d
U=Uv)      // Set U to U.toLowerCase().
w ¥        // Reverse it, and check if it is equal to:
Ur"[a-z]"  //  Take the input and replace each letter with:
 _c +4     //   Take its char code and add 4. This results in
           //   the string      "abc...xyz"
           //   becoming        "efg...|}~".
 ^31       //   XOR the result by 31. This flips its last five 5 bits.
           //   We now have     "zyx...cba".
 d         //   Convert back from a char code.
           // Implicit: output last expression
\$\endgroup\$
2
\$\begingroup\$

Ruby, 56 bytes

->s{s.upcase!;s==s.tr(x=[*?A..?Z]*'',x.reverse).reverse}

It's an anonymous function which takes a string and returns true or false. It's rather clumsy: In order to save some bytes, it uses the destructive variant of upcase (with a ! after it). upcase! unfortunately returns nil if nothing is changed (like all numeric input), so some bytes are lost trying to deal with that. Still works tho :)

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

Python 3.8 (pre-release), 73 bytes / 76 bytes

Lambda version: (73 bytes)

lambda x:(a:=[*map(ord,x.upper())])==[[c,155-c][64<c<91]for c in a][::-1]

Try it online!

Code version: (76 bytes)

a=[*map(ord,input().upper())];print(a==[[c,155-c][64<c<91]for c in a][::-1])

Try it online!

How it works:

  • .upper() Upper all the char of the input
  • a=[*map(ord,input().upper())] In both program we store in a the list containing the ascii value of each character.
  • [... for c in a] We iterate through this list
  • [c,155-c][64<c<91] If our ascii value is beetween 64 and 91 (if the char is uppercase) we "flip" it.
  • a==[...][::-1] We compare our list with the new formed list reversed

Note:

If we used Python2, we could save 1 byte on the second program because :

  • -3 bytes: map() returns a list so we don't need the [*map()] syntax anymore
  • -2 bytes (and a little adjustment): print doesn't need parenthesis
  • but +4 bytes because we have to use raw_input()instead of input
\$\endgroup\$
2
\$\begingroup\$

Fig, \$8\log_{256}(96)\approx\$ 6.585 bytes

s$Pcacza

Try it online!

s        # Are the following equal, ignoring case:
         # Implicit input
 $       # Reverse of... 
       a # Lowercased (implicit) input
  P      # Transliterated from
   ca    # Alphabet
     cz  # To reversed alphabet
\$\endgroup\$
1
\$\begingroup\$

Python, 156 112 bytes

a=map(chr,range(65,91))
s=raw_input().upper()
print ''.join([dict(zip(a,a[::-1])).get(i,i) for i in s])==s[::-1]

Basically, it makes a dictionary of the translation with uppercase letters and the input is capitalized (if everything were lowercase instead, that would add 5 bytes). Then, for each character in the capitalized input, do the translation and append to a list unless the character is not in the alphabet, in which case append the character as is. Join the entire list and compare to the reversed list.

Shout-out to @Artyer for posting almost exactly as I was going to post before me. But I need to confirm, this is my work and I did this independently.

Based on the Julia answer by Alex A. Try it here

\$\endgroup\$
1
  • \$\begingroup\$ There is an unnecessary whitespace after .get(i,i). +1. \$\endgroup\$
    – Yytsi
    Aug 9, 2016 at 20:40
1
\$\begingroup\$

Factor, 118 113 bytes

This is an anonymous function.

[ >upper dup >array [ 1string 65 90 [a,b] [ 1string ] map dup reverse zip >hashtable at ] map "" join reverse = ]

I don't know of a shorter way to generate an associative array of the alphabet :c

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

Clojure, 100 bytes

(defn x[b](let[a(.toUpperCase b)c(reverse a)](=(map #(char(if(<= 65(int %)90)(- 155(int %))%))a)c)))

It should be possible to cut it down to a single anonymous function, cutting around 10 more bytes (of declarations) but I did not find a way yet.

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

Ruby, 79 77 bytes

s=$*[0].upcase
exit(s==s.reverse.tr('A-Z','ZYXWVUTSRQPONMLKJIHGFEDCBA'))?0:1

Accepts the word to test as a command-line argument. Exits with code 0 (which is truthy to the shell) if the argument is an atbash self palindrome, or with code 1 otherwise.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Wouldn't putsing the result be shorter than exit with a ternary? \$\endgroup\$
    – cat
    Apr 7, 2016 at 19:06
  • \$\begingroup\$ FYI $* is an alias for ARGV. \$\endgroup\$
    – Jordan
    Aug 9, 2016 at 4:12
1
\$\begingroup\$

MATLAB, 61 bytes

Not the shortest solution, but still interesting

f=@(a)~any(changem(upper(a),90:-1:65,65:90)-fliplr(upper(a)))
\$\endgroup\$
1
\$\begingroup\$

Japt v2.0a0, 13 bytes

Note the unprintable (charcode 155) at the end of the second line.

u
ÔøUr\l_cn#

Try it

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

Factor + spelling, 60 bytes

[ >lower dup ALPHABET dup reverse zip substitute reverse = ]

Try it online!

Explanation:

  • >lower Convert to lowercase.

    Stack: (e.g.) "wizard"

  • dup Duplicate.

    Stack: "wizard" "wizard"

  • ALPHABET Place the lowercase alphabet on the stack.

    Stack: "wizard" "wizard" "abcdefghijklmnopqrstuvwxyz"

  • dup Duplicate.

    Stack: "wizard" "wizard" "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"

  • reverse Reverse.

    Stack: "wizard" "wizard" "abcdefghijklmnopqrstuvwxyz" "zyxwvutsrqponmlkjihgfedcba"

  • zip Make an associative array from two sequences.

    Stack: "wizard" "wizard" { { 97 122 } { 98 121 } { 99 120 } { 100 119 } { 101 118 } { 102 117 } { 103 116 } { 104 115 } { 105 114 } { 106 113 } { 107 112 } { 108 111 } { 109 110 } { 110 109 } { 111 108 } { 112 107 } { 113 106 } { 114 105 } { 115 104 } { 116 103 } { 117 102 } { 118 101 } { 119 100 } { 120 99 } { 121 98 } { 122 97 } }

  • substitute Substitute elements in a sequence that have keys in an assoc with their values.

    Stack: "wizard" "draziw"

  • reverse Reverse.

    Stack: "wizard" "wizard"

  • = Are they equal?

    Stack: t

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

05AB1E, 8 bytes

Code:

lDAAR‡RQ

Explanation:

l         # Lowercase the implicit input
 D        # Duplicate top of the stack
  AAR     # Push the lowercase alphabet (A) and the lowercase alphabet reversed (AR)
     ‡    # Transliterate a -> b
      R   # Reverse this string
       Q  # Compare with the input string

Try it online!

\$\endgroup\$

Your Answer

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

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