13
\$\begingroup\$

Input:

A string only containing the following characters:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.?! (space at the end) and two special characters (_ and |)

Output:

Mirror the text based on the position(s) of the _ and |.
_ mirrors horizontally and | mirrors vertically.

Mirroring process

  • The first _ indicates the start of the mirroring of that substring, the second _ the end.
  • If just one _ is present, you can continue on until the end of the string.
  • If more then two _ are present, the same process repeats.
  • There can only be one | in the string, and this reversed the first substring and removes the second substring.

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.?! (space at the end) will be converted to ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀𐐒ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡ (space at the end) when it is mirrored by _ (click the link for the unicode values - requires unicode v8.0+).

Example input 1: _Will I be mirrored?!
Output 1: Mıll I qǝ ɯıɹɹoɹǝp¿¡

Example input 2: Wi_ll I be mi_rrored?!
Output 2: Will I qǝ ɯırrored?!


When it is mirrored by | we simply reverse the substring from 0 to index-of-|.

Example input 1: Will I be mirror|ed?!
Output 1: Will I be mirrorrorrim eb I lliW

Both mirrors (_ and |) can be present in the string.


General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

Test cases:

_Will I be mirrored?!                          ->   Mıll I qǝ ɯıɹɹoɹǝp¿¡
Wi_ll I be mi_rrored?!                         ->   Will I qǝ ɯırrored?!
Will I be mirror|ed?!                          ->   Will I be mirrorrorrim eb I lliW
This_ is a test_ cont_aining bo|t_h mirrors.   ->   This ıs ɐ ʇǝsʇ contɐıuıuɓ qooq ɓuıuıɐtnoc ʇsǝʇ ɐ sı sihT
0_1_2_3_4_5|_6_7_8_9                           ->   0⇂2Ɛ4ϛϛ4Ɛ2⇂0
\$\endgroup\$
  • 2
    \$\begingroup\$ feature request -- Change the title to "The world is like a Mirror: smile and it'll sɯıןǝ qɐɔʞ" ;-) \$\endgroup\$ – AdmBorkBork Sep 1 '16 at 14:42
  • \$\begingroup\$ @TimmyD ᗡouǝ ;) \$\endgroup\$ – Kevin Cruijssen Sep 1 '16 at 14:48
  • \$\begingroup\$ @TimmyD In my country mirrors work like this: "smile ɘlimƨ", not "smile sɯıןǝ" :) The title should be "The world is like a Mirror: smile and it'll ɘlimƨ back" \$\endgroup\$ – tymtam Sep 2 '16 at 0:38
  • 1
    \$\begingroup\$ In the first line you should probably mention that spaces can appear in the input, too. \$\endgroup\$ – Martin Ender Sep 2 '16 at 7:18
3
\$\begingroup\$

Pyth - 174 bytes

Can prolly save with base compression or something cuz of unicode(this is only 119 chars)

u?qH\_&=hZG+G@@c2K+s+rB;1UT".?! ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀𐐒ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡ "ZxKH?}\|zs_Bhcz\|zk

Test Suite.

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

JavaScript (ES6), 308 bytes

s=>s[r='replace'](/_.*?(_|$)/g,m=>m[r](/./g,c=>'| ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡𐐒'['| abcdefghijklmnopqrstuvwxyzACDEFGHIJKLMNOPQRSTUVWXYZ0123456789.?!B'.indexOf(c)]||''))[r](/(.*)\|.*/,(m,t)=>t+[...t].reverse().join``)

Test

var solution =

s=>
  s[r='replace'](/_.*?(_|$)/g,m=>
    m[r](/./g,c=>
      '| ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡𐐒'
        ['| abcdefghijklmnopqrstuvwxyzACDEFGHIJKLMNOPQRSTUVWXYZ0123456789.?!B'
          .indexOf(c)]||''
    )
  )
  [r](/(.*)\|.*/,(m,t)=>t+[...t].reverse().join``)

var testCases = [
  '_Will I be mirrored?!',
  'Wi_ll I be mi_rrored?!',
  'Will I be mirror|ed?!',
  'This_ is a test_ cont_aining bo|t_h mirrors.',
  '0_1_2_3_4_5|_6_7_8_9'
];
tests.textContent = testCases.map((c) => c + '   =>   ' + solution(c)).join('\n');
<input type="text" oninput="result.textContent=solution(this.value)" value="This_ is a test_ cont_aining bo|t_h mirrors." /><pre id="result"></pre><pre id="tests"></pre>

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

PERL 243

242 + 1 for -p

$h=(split'\|',$_)[0];$_=$h.reverse$h if($_=~/\|/);for$b(split'_',$_){$b=~y/A-Za-z0-9.?!/ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀𐐒ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡/ if($f);$\.=$b;$f=!$f;}$_='';

Ungolfed:

$reverse = (split('\|', $_))[0];
$_ = $reverse . reverse($reverse) if($_=~/\|/);

for $block (split '_', $_) {
    $block =~ y/A-Za-z0-9.?!/ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀𐐒ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡/ if($flip);
    $\.=$block;
    $flip=!$flip;
}
$_='';

Example:

$ perl -p mirror.pl <<<'Will I be mirror|ed?!'
Will I be mirrorrorrim eb I lliW

I'm 99% sure this works, but most of the upsidedown characters don't display correctly on my system. I tested it by using a different range of character, e.g. .?! -> %^& instead of .?! -> ˙¿¡.

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

R, 362 362 342 339 bytes

Edit 1: I found a bug in my original (failed if there were no _ in input), fixed it, golfed some more, and I'm back at 362 where I started!

Edit 2: Golfed away 20 byes by replacing "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.?! " with paste(c(letters,LETTERS,0:9,".?! "),collapse="")

Edit 3: Removing some extra whitespace shaves off another 3 bytes.

p=strsplit
l=length
i=p(readline(),"_")[[1]]
if(l(i)-1)i[v]=chartr(paste(c(letters,LETTERS,0:9,".?! "),collapse=""),"ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀𐐒ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡ ",i[v<-seq(2,l(i),2)])
i=unlist(p(i,""))
if(l(b<-which(i=="|")-1))i[1:b+b]=rev(i[1:b])
cat(i,sep="")

Explained:

p=strsplit               # Aliases for common functions
l=length
i=p(readline(),"_")[[1]] # Read input, split by _s. Now every even-indexed substring is one that we must
                         # mirror vertically. (R indexes from 1.)
if(l(i)-1)               # If there are any _s,
  i[v]=chartr("paste(c(letters,LETTERS,0:9,".?! "),collapse=""),
  "ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀𐐒ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡ ",
  i[v<-seq(2, l(i), 2)]
)                 # Flip the characters in the vertically-mirrored substrings
i=unlist(p(i,"")) # Split each substring into a single character object
if(l(b<-which(i=="|")-1)) # If there is a |,
  i[1:b+b]=rev(i[1:b])    # insert the reversed characters at the end of the list
cat(i,sep="")             # Print output
\$\endgroup\$
1
\$\begingroup\$

Javascript (ES5), 344 340 bytes

function mirror(a){d=[];~a.indexOf("|")&&(a=a.split("|")[0]+a.split("|")[0].split("").reverse().join(""));for(var c=!1,b=0;b<a.length;++b)"_"==a[b]&&(c=!c),d[b]=c?"zʎxʍʌnʇsɹbdouɯlʞɾᴉɥɓɟǝpɔqɐZ⅄XMΛ∩⊥SᴚΌԀONW⅂⋊ſIH⅁ℲƎᗡƆᗺⱯ068ㄥ95߈Ɛᘔ⇂˙¿¡ "["zyxwvutsrqponmlkijhgfedcbaZYXWVUTSRQPNOMLKIJHGFEDCBA0987654321.?! ".indexOf(a[b])]:a[b];return d.join("")}

Ungolfed:

function mirror_u(s){return "zʎxʍʌnʇsɹbdouɯlʞɾᴉɥɓɟǝpɔqɐZ⅄XMΛ∩⊥SᴚΌԀONW⅂⋊ſIH⅁ℲƎᗡƆᗺⱯ068ㄥ95߈Ɛᘔ⇂˙¿¡ "["zyxwvutsrqponmlkijhgfedcbaZYXWVUTSRQPNOMLKIJHGFEDCBA0987654321.?! ".indexOf(s)]};

function mirror(str){var b=[];
if(~str.indexOf("|"))str=str.split("|")[0]+str.split("|")[0].split("").reverse().join("");
    var is_mirrored=false;
    for(var i=0;i<str.length;++i){
        if(str[i]=="_")is_mirrored=!is_mirrored;
        if(is_mirrored){b[i]=mirror_u(str[i])}else{b[i]=str[i]};
    }
    return b.join("");
}

EDIT: Longer working code, that is mirrored | by character 341.

function mirror(a){d=[];~a.indexOf("|")&&(a=a.split("|")[0]+a.split("|")[0].split("").reverse().join(""));for(var c=!1,b=0;b<a.length;++b)"_"==a[b]&&(c=!c),d[b]=c?"zʎxʍʌnʇsɹbdouɯlʞɾᴉɥɓɟǝpɔqɐZ⅄XMΛ∩⊥SᴚΌԀONW⅂⋊ſIH⅁ℲƎᗡƆᗺⱯ068ㄥ95߈Ɛᘔ⇂˙¿¡ "["zyxwvutsrqponmlkijhgfedcbaZYXWVUTSRQPNOMLKIJHGFEDCBA0987654321.?! ".indexOf(a[b])]:a[b];return d.join("")}//})""(nioj.d nruter;]b[a:])]b[a(fOxedni." !?.1234567890ABCDEFGHJIKLMONPQRSTUVWXYZabcdefghjiklmnopqrstuvwxyz"[" ¡¿˙⇂ᘔƐ߈59ㄥ860ⱯᗺƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Zɐqɔpǝɟɓɥᴉɾʞlɯuodbɹsʇnʌʍxʎz"?c=]b[d,)c!=c(&&]b[a=="_")b++;htgnel.a<b;0=b,1!=c rav(rof;))""(nioj.)(esrever.)""(tilps.]0[)"|"(tilps.a+]0[)"|"(tilps.a=a(&&)"|"(fOxedni.a~;][=d{)a(rorrim noitcnuf
\$\endgroup\$
  • \$\begingroup\$ The 𐐒 character takes up two UTF-16 code units, but you don't seem to be taking this into account. Does the W in the first test case come out correctly? \$\endgroup\$ – user81655 Sep 1 '16 at 15:11
  • \$\begingroup\$ The characters are all UTF-8. All test cases give the correct output. \$\endgroup\$ – Paul Schmitz Sep 1 '16 at 15:14
  • \$\begingroup\$ Oh, it seems you're using code point U+15FA for the upside-down B. The one in the challenge is code point U+10412. \$\endgroup\$ – user81655 Sep 1 '16 at 15:21
  • \$\begingroup\$ I used the characters from the linked wikipedia page, I just copied them into my editor. \$\endgroup\$ – Paul Schmitz Sep 1 '16 at 15:25
1
\$\begingroup\$

Retina, 176 bytes

\|.+
:$`
O$^`:|(?!^)\G.

:

T`w.?!`_0⇂ᘔƐ߈ϛ9ㄥ86∀:ƆᗡƎℲ⅁\HIſ⋊⅂WN\OԀΌᴚS⊥∩ΛMX⅄Zɐqɔ\pǝɟɓɥıɾʞ\lɯu\o\dbɹsʇnʌʍxʎz˙¿¡`_[^_]*_?
:
𐐒

Try it online! (The first line enables a linefeed-separated test suite.)

\$\endgroup\$
0
\$\begingroup\$

Java 7, 504 502 bytes

import java.util.*;String c(String s){Map m=new HashMap();int i=0,j;while(i<67)m.put("abcdefghijklmnopqrstuvwxyzABBCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.?! ".charAt(i),"ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀𐐒ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡ ".charAt(i++));String q=s.split("\\|")[0],z[]=(q+(s.equals(q)?"":new StringBuffer(q).reverse()+"")).split("_"),r="";for(i=-1;++i<z.length;){q="";for(char c:z[i].toCharArray())q+=m.get(c);r+=i%2<1?z[i]:q;}return r;}

Of course I'll have to answer my own challenge again. And.. it's long.. xD

Ungolfed & test cases:

Try it here.

import java.util.*;
class M{
  static String c(String s){
    Map m = new HashMap();
    int i = 0,
        j;
    while(i < 67){
      m.put("abcdefghijklmnopqrstuvwxyzABBCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.?! ".charAt(i),
          "ɐqɔpǝɟɓɥıɾʞlɯuodbɹsʇnʌʍxʎz∀𐐒ƆᗡƎℲ⅁HIſ⋊⅂WNOԀΌᴚS⊥∩ΛMX⅄Z0⇂ᘔƐ߈ϛ9ㄥ86˙¿¡ ".charAt(i++));
    }
    String q = s.split("\\|")[0],
           z[] = (q + (s.equals(q)
                        ? ""
                        : new StringBuffer(q).reverse() + "")
                 ).split("_"),
           r = "";
    for(i = -1; ++i < z.length;){
      q = "";
      for(char c : z[i].toCharArray()){
        q += m.get(c);
      }
      r += i%2 < 1
            ? z[i]
            : q;
    }
    return r;
  }

  public static void main(String[] a){
    System.out.println(c("_Will I be mirrored?!"));
    System.out.println(c("Wi_ll I be mi_rrored?!"));
    System.out.println(c("Will I be mirror|ed?!"));
    System.out.println(c("This_ is a test_ cont_aining bo|t_h mirrors."));
    System.out.println(c("0_1_2_3_4_5|_6_7_8_9"));
  }
}

Output:

Mıll I qǝ ɯıɹɹoɹǝp¿¡
Will I qǝ ɯırrored?!
Will I be mirrorrorrim eb I lliW
This ıs ɐ ʇǝsʇ contɐıuıuɓ qooq ɓuıuıɐtnoc ʇsǝʇ ɐ sı sihT
0⇂2Ɛ4ϛϛ4Ɛ2⇂0
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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