51
\$\begingroup\$

Given two strings, output a third string that is not equal to either of the two inputs, but has the same length (in characters) as either of the inputs. There is guaranteed to be at least one valid output.

Test Cases

Test cases are quoted to show they are strings. Outputs are one of many possible.

input, input -> output

"test", "test" -> "tttt"
"do", "don't" -> "dnut_"
"ye s", "yes" -> "fals"
"yes", "yes" -> "noo"
"maybe", "mayue" -> "false"
"false", "false" -> "truee"
"false", "true" -> "fatr"
"1", "" -> "0"
"", "t" -> "s"
"", "abcabc" -> "testst"
"abcdefghijklmnopqrstuvwxyz", "aaaaaaaaaaaaaaaaaaaaaaaaaa" -> "zbcdefghijklmnopqrstuvwxya"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" -> "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"

Rules

  • You may chose your input domain, but it must be composed of at least printable ASCII, and your output domain must be the same as your input domain.
  • The input lengths may be the same or different.
  • The output must be valid with probability 1; that is, you may generate random strings until one is valid (and theoretically you might infinite loop), but you can't just output a random string and hope it's valid. Note that this means you output does not need to be deterministic.
  • Automatic trailing newlines allowed, but they do not count towards the length of the output.
  • Due to questions regarding Memory Errors, it must work within 60 seconds up to input lengths of 6. An answer that works for that and theoretically works for longer strings is OK, but something that Memory Errors on modern computer for input length 4 is not valid.

This is , so shortest answer in bytes wins.

\$\endgroup\$
18
  • 16
    \$\begingroup\$ This seems like another of an interesting new category of questions on this site that are easy for humans and hard for computers. Because computers aren't good at divergent thinking! It reminds me of the Spongebob episode where he's up all night trying to write an essay on what NOT to do at a stoplight. \$\endgroup\$
    – geokavel
    Aug 18 '17 at 22:47
  • 2
    \$\begingroup\$ I understand the output domain can be a subset of the input domain, yes? \$\endgroup\$
    – Luis Mendo
    Aug 18 '17 at 23:21
  • 2
    \$\begingroup\$ Nice question! I like it. \$\endgroup\$
    – isaacg
    Aug 20 '17 at 22:16
  • 2
    \$\begingroup\$ @Quelklef No, that's not different from both inputs. \$\endgroup\$ Aug 21 '17 at 2:58
  • 3
    \$\begingroup\$ I suggest "", "1" should be added as a test case, since I just realized my answer fails for this even though it works for all the provided test cases \$\endgroup\$
    – Slow loris
    Aug 22 '17 at 23:20

36 Answers 36

1
2
1
\$\begingroup\$

C# (Mono), 94 bytes

s=>t=>{var r=s!=""?s:t;while(r==s|r==t)r=r.Substring(1).Insert(0,(char)(r[0]+1)+"");return r;}

Try it online!

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

Python 3, 67 bytes

lambda a,b:[k for k in"abc"if k not in b[:1]+a[:1]][0]+(a or b)[1:]

Try it online!

Picks the first character of "a", "b", and "c" which neither string begins with - all three characters can't be the first character of one of two strings. The first character of a is then replaced with that character, unless a is empty, in which case the first character in b is replaced instead.

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

Pip, 16 bytes

T#iN#*g&iNIg++ii

Try it online!

Increments the number i until i has the same length as one of the inputs and does not equal either of the inputs, and then prints i. Takes about 30 seconds on TIO to output 100000 for length-six input.

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

Pip, 16 bytes

YMXgRXX9WyNg--yy

Try it online!

Different approach from my first submission (and much faster):

   g              List of command-line args     ["abc" "9999"]
    R             Replace
     XX           any character (regex `.`)
       9          with 9                        ["999" "9999"]
 MX               Get the max                   9999
Y                 Yank it into y variable
        W         Loop while
         yNg      y is in the cmdline args:
            --y    Decrement y
               y  Output y                      9998
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 39 bytes

a=>b=>(a[0]&1^b[0]&2^7)+(a||b).slice(1)

Try it online!

Obvious source the 3rd byte

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

Haskell, 41 bytes

n[]_=[]
n _[]=[]
n(i:c)(h:t)=succ i:n c t
\$\endgroup\$
2
  • 4
    \$\begingroup\$ The empty string isn't a valid output if one of the inputs is an empty string... \$\endgroup\$
    – betaveros
    Aug 19 '17 at 3:59
  • 3
    \$\begingroup\$ It fails if the string starts with '\1114111'. \$\endgroup\$
    – nimi
    Aug 19 '17 at 16:16
1
2

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.