Code Golf Asked by Stephen on September 5, 2020
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 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"
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 code-golf, so shortest answer in bytes wins.
Correct answer by Adnan on September 5, 2020
∋l~lℕṫ.&≡ⁿ
There may be a better way to do this, but I've been fighting Brachylog strings for a while now and this solution works.
∋ For some element of the input list
l get its length
~l That is also the length of
ℕ some positive integer
ṫ Converting that integer to a string
. is the output
& Also:
ⁿ It is not true, for any X in the input list
≡ that the output equals X
Outputs strings like "100"
. If "100"
is already in the input list, goes to "101"
, etc.
Answered by DLosc on September 5, 2020
Answered by l4m2 on September 5, 2020
{(1 x.max.comb...*∉$_).tail}
Anonymous codeblock that takes input as a list of two strings, and returns the first number from 1111...
with a non-empty amount of 1s that isn't in the input.
{ } # Anonymous code block
1 x.max.comb # String multiply 1 by the size of the non-empty string
... # Create a sequence increasing by 1
*∉$_ # Until the number is not in the input
( ).tail # And take the last number
Answered by Jo King on September 5, 2020
Full program, takes input as a 2-element nested list.
⊃¨⎕A∘~¨,⌿↑⎕
Answered by voidhawk on September 5, 2020
YMXgRXX9WyNg--yy
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
Answered by DLosc on September 5, 2020
T#iN#*g&iNIg++ii
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.
Answered by DLosc on September 5, 2020
Replaces first char of the first (non-empty) input string with 1,2,3 until no match is found to either input. Try it Online!
-9, -12, -9, -8 bytes all thanks to GammaFunction
x="${1:-$2}"
for s in {1..3}"${x:1}"
{ [[ $s = @($1|$2) ]]||break;}
echo "$s"
(quite an improvement over the original...)
Answered by roblogic on September 5, 2020
lambda a,b:[k for k in"abc"if k not in b[:1]+a[:1]][0]+(a or b)[1:]
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.
Answered by Sara J on September 5, 2020
-4 bytes by using builtin array argv
, -10 bytes by using prefix removal and RC_EXPAND_PARAM
, -1 byte by inlining the brace expansion.
<<<${${${:-{1..3}${^@#?}}:|argv}[1]}
First, this was an awesome challenge, I went through a ton of ideas before landing on this one.
<<<${${${:-{1..3}${^@#?}}:|argv}[1]}
${:- } # empty fallback
${ @#?} # remove first character from each parameter
${^@ } # enable brace expansion (set -P)
{1..3}${^@#?} # expand to 1foo 2foo 3foo 1bar 2bar 3bar
${ :|argv} # Set difference with 'argv'
${ [1]} # The first found element
<<< # print to stdout
@
and *
are not identifiers, so ${ :|@}
and ${ :|*}
don't work, hence the use of ${ :|argv}
This method will work up to 93 inputs and find a 94th which is unique. Simply replace the
{1..3}
with the maximum possible range{~..!}
.
for ((;$#i<${#${1:-$2}}||$@[(I)$i];i++)):
<<<$i
Completely new method courtesy of JoKing's Perl 6 submission, but doesn't work on large strings (n>20) due to integer size restrictions. $@[(I)$i]
is reverse array lookup to largest index, it will output zero (falsy in arithmetic expansion) if $i is not found in the command line parameters.
Answered by GammaFunction on September 5, 2020
s=>t=>{var r=s!=""?s:t;while(r==s|r==t)r=r.Substring(1).Insert(0,(char)(r[0]+1)+"");return r;}
Answered by TheLethalCoder on September 5, 2020
-27 bytes thanks to @Nevay's magical touch! :)
a->b->{b=b.length<1?a:b;if(a.length<1||(b[0]^=2)==a[0])b[0]^=1;return b;}
Input domain = Printable ASCII + codepoint 127.
Answered by Olivier Grégoire on September 5, 2020
The function needs the provided strings to be mutable (i.e. either arrays or dynamically allocated).
f(a,b)char*a,*b;{a=*a?a:b;*a=*a>70?33:99;*a+=*a==*b;puts(a);}
Works for the standard ASCII range
Explanation:
a=*a?a:b // If a is empty, point to b instead
*a=*a>70?33:99 // Choose a different value for the 1st character of a,
// while giving enough space to increment it without
// going back to its previous value
*a+=*a==*b // Increment the 1st character of a if the arbitrary
// chosen value is equal to the value of the 1st
// character of b
puts(a) // Outputs a
Answered by scottinet on September 5, 2020
sub{$_="a"x length $_[0]||$_[1];$_++while $_ eq$_[0]||$_ eq$_[1];$_}
Explanation:
Starting from "a"s was to avoid incrementing to point where Perl lengthens the string; with only two strings to avoid being same as, it couldn't overflow.
Execute with:
perl -e '$s = ' -E 'sub{$_="a"x length $_[0]||$_[1];$_++while $_ eq$_[0]||$_ eq$_[1];$_}' -E ';say $s->("z", "true")'
Answered by Ed. on September 5, 2020
hC-LG.T
1 byte thanks to Jakube
We use .T
, length preserving transpose, rather than C
, truncating transpose, so that it works on inputs where one string is empty.
Given two strings as a tuple, we transpose them (.T
), then map the resulting pair of characters or single character by subtracting the character(s) from the lowerase alphabet with -LG
, then transpose the resulting list of strings of unused characters with C
, then return the first such string with h
. This consists of the first letter alphabetically that is not in either string, for each position.
Answered by isaacg on September 5, 2020
@Giuseppe saved 9 bytes, @user2390246 saved 13 bytes
function(x,y)sub("^.",letters[!letters%in%substr(c(x,y),1,1)][1],x)
# define function
f <- function(x,y)sub("^.",letters[!letters%in%substr(c(x,y),1,1)][1],x)
# test cases
f("test","test")
[1] "aest"
f("do","don't")
[1] "ao"
f("ye s","yes")
[1] "ae s"
f("maybe","mayue")
[1] "aaybe"
f("false","false")
[1] "aalse"
f("false","true")
[1] "aalse"
f("1","")
[1] "a"
f("art","bug")
[1] "crt"
Answered by Slow loris on September 5, 2020
a=>b=>(a[0]+b[0]|0?'a':9-(a[0]^b[0]))+(a||b).substr(1)
Answered by Fermyon on September 5, 2020
sub{$_=$_[0];$_=$_[1]||$_ if/^(xz*)?$/;s/[^z]/z/||s/./y/;$_ eq$_[1]&&s/./x/;$_}
Takes input as two separate arguments and returns the third string.
The subroutine attempts to produce a string very similar to the first string but with the first non-z
character replaced with a z
. Then it deals with corner cases by replacing the first character with y
or x
, as needed, if it finds that one of the inputs was in fact a sequence of all z
's.
Answered by Silvio Mayolo on September 5, 2020
->a,b{([?a,?b,?c].map{|e|e*[a,b].max.size}-[a,b])[0]}
Basically generates the strings a...a
, b...b
, and c...c
and selects the first not in the input.
Answered by Conor O'Brien on September 5, 2020
x!y=[s|s<-(<$max x y)<$>"abc",s/=x,s/=y]!!0
Takes the max (lexicographically later) string, which we know is nonempty; replaces all characters with one of "a", "b", and "c" using <$
; and returns the first that is neither of the inputs. I think this is similar to Neil's Charcoal answer and/or geokavel's CJam answer.
(I've lurked for a while but this is my first time answering on this site; hi!)
Answered by betaveros on September 5, 2020
+>+[-,[>>,]<<[<<]>]>>[>]<<<[<]>[<+>>[-<+<+>>][>]<[->+<]<[-]>>>++<[<]>[.>>]>[>>]]>[+[>]+[<]>[.>>]]
Run code online (note that "dynamic memory" must be selected in the bottom-right)
Awesome challenge! I thought it would be trivial but it ended up being really difficult. I keep coming back to it because I feel like there should be some elegant 20-or-so-byte BF solution. At this point, I'm pretty happy I (seemingly) got it to work at all in BF.
Input is taken as str1
+