Ihre Aufgabe ist einfach . Bestimmen Sie, ob eine Zeichenfolge , die die andere (nicht - Adresse, der Wert) entspricht , ohne die Verwendung von Gleichheitsoperator (wie ==
, ===
, oder .equal()
) oder Ungleichheit ( !=
, !==
) etwas ähnliches für andere Sprachen. Das heißt überall! Sie dürfen diese Operatoren an keiner Stelle im Code verwenden. Sie können jedoch Umschalter verwenden, !exp
da Sie die nicht direkt vergleichen exp != with something else
.
Außerdem dürfen Sie keine Funktionen wie strcmp , strcasecmp usw. verwenden.
Wie für Vergleichsoperatoren ( >=
, <=
, >
, <
), werden sie auch nicht zulässig . Mir ist klar, dass einige Antworten dies beinhalten, aber ich würde gerne mehr Antworten sehen, die den Gleichheitsoperator nicht einschränken.
Ein Beispiel mit PHP wird gezeigt:
<?php
$a = 'string';
$b = 'string';
$tmp = array_unique(array($a, $b));
return -count($tmp) + 2;
Geben Sie einfach true oder false zurück (oder etwas, das in der Sprache als true oder false wie 0 oder 1 ausgewertet wird), um anzugeben, ob die Zeichenfolgen übereinstimmen. Die Zeichenfolgen sollten im obigen Beispiel fest codiert sein. Die Saiten sollten im Golf nicht mitgezählt werden. Wenn Sie also die Variable vorher deklarieren, zählen Sie die Deklaration nicht mit.
Antworten:
Python
49 45 18 22 1514(+ 3, wenn Stringvariablen berücksichtigt werden)
Die Zeichenfolge sollte bei zwei Vorkommen vona
und einem Vorkommenb
von in Anführungszeichen eingeschlossenen Zeichenfolgen fest codiert sein .a
undb
sollte auf die Zeichenfolgen vorinitialisiert werden.Python-Shell, 9
(+ 3, wenn Stringvariablen berücksichtigt werden)
Ausgabe in der Shell
Erläuterung
Erstellt ein Diktat (Hash-Tabelle) mit dem Schlüssel der ersten und zweiten Zeichenfolge. Wenn der zweite String derselbe ist, wird der Wert von first durch den von second ersetzt. Schließlich drucken wir den Wert des ersten Schlüssels.
BEARBEITEN: OP erlaubt 0/1 anstelle von False / True sowie die Verwendung von vorinitialisierten Variablen.
quelle
a
andb
are not to be included, the strings should be hard-coded there, that's why I added + 2*len(str1) + len(str2) + 6 (')Python (
1711):(Checks if b is contained in a and a is contained in b, if that wasn't clear from the code.)
Alternative python: (
87)derived from Tom Verelst's Go solution:
Bonus: this works for any type.
EDIT:
Wait a second, just read that you can also directly program in the strings, and don't have to count quotes... (or at least, that what golfscript does). So... Python on par with golfscript? Oh my!
Alternative alternative Python (
54):(thanks Claudiu)
original:
Alternative Alternative Alternative Bendy-ruly Python (2):
Nothing was said about comparison keywords (This is not a serious submission, just something that occurred to me...)
quelle
b in a in b
. The and a is not necessary...in
and[a]
. i.e.b in[a]
should work.JavaScript,
1110Strings have to be stored in a and b.
Edit: thanks Danny for pointing out,
|
is enough instead of||
quelle
|
?Ruby, 11
Checks if each string is contained within the other.
quelle
!(a<b||b<a)
would be the same...Python - 11 (without the strings)
quelle
a<=b<=a
which is only 7 characters. Although I don't know whether the comparison<=
would be considered an "inequality". From the question it appears that any comparison that is not an equality check is okay, which would allow<=
.GolfScript (5 chars)
Fairly straightforward port of the PHP reference implementation. Leaves
0
(=false) on the stack if the strings are the same, or1
(=true) if they're different.quelle
1
if the string are the same, and2
if they're different.'string1''string1'].&,1&
worksJavascript (45 bytes):
Here is another solution in Javascript.
The space is important.
c
should betrue
.quelle
!a.replace(b,'')
is counted. So character count should be 16. Actually, some people even count it 14, since you can specify the string directly.C++,
635856quelle
auto
instead ofconst char*
?coreutils: uniq -d
Just enter your two strings as the standard input of a pipe and
uniq -d | grep -q .
will print nothing but will have a return value of success or error. If you want to print the boolean, just replace withuniq -d | grep -c .
How many characters? I let you count;
uniq -d|grep -q .
with no extra spaces has 17 characters in it, but since the whole job is performed by uniq, I would say this solution is a 0-character one in...uniq
own language!Actually,
uniq -d
will print one line if the two strings are identical, and nothing if the are different.quelle
Strings have to be stored in a and b. Will not work if either is
null
.C#, 53
C#, 28
quelle
PHP - 49 characters
quelle
!strlen(str_replace($a,'',$b));
It should return 1 if two strings are equal?$a == 'foo'
and$b = 'foofoo'
:)!strlen(preg_replace("/{$a}/", '', $b, 1));
and is 45 characters?APL (
89)Update: the old one does not work for strings of different lengths.
↑⍺⍵
: make a matrix with⍺
on the first line and⍵
on the second line, filling blanks with spaces.∊⌿
: For each column, see if the upper row contains the lower row (as in the old version).∧/
: Take the logicaland
of all the values.Old one:
⍺∊¨⍵
: for each combination of elements in⍺
and⍵
, see if the element from⍺
contains the element from⍵
. Since in a string these will all be single characters, and a string contains itself, this is basically comparing each pair of characters.∧/
: take the logical and of all the values (if all the characters match, the strings are equal)quelle
Python - 12
This solution uses sets. Subtracting equal sets will result in an empty set, which has a boolean value of False. Negating that will result in a True value for a and b being equal strings.
Edit: Thanks to Peter Taylor for pointing out the unnecessary whitespace.
quelle
a="s", b="ss"
?"s"!="ss"
, it will outputFalse
. Case sensitivity is also preserved. It even works fora="", b="s"
. The code does not convert strings to sets, but creates sets containing the strings.{}
isn't the same asset()
. You can save 1 char by removing the whitespace.{a}
is equivalent toset([a])
.not {a}-{b}
?C — 62
Tested. Call as
e(str1, str2)
Come to think of it, if you don't count the
char*p,char*q
, which seems only fair, it's only 49 bytes :)quelle
np
andnq
. One loop will do, because if you reach the end of one string before the other they will have a different value.*p&*q
may stop the loop too early (e.g.'0'&'A'==0
)*p=='0'
&*q=='A'
, we want the loop to stop early, as we know the strings are not equal.Haskell -- 9
Note that this, just like many entries here, is just an expression. This is not a Haskell program.
quelle
Java -
162147 charactersThe idea is to compare the difference of each byte, same bytes will have difference 0. The program will throw
java.lang.ArrayIndexOutOfBoundsException
for when bytes are different (try to access a negative index) or when strings are of different length. It will catch the exception and return 0 (strings not equal), or return 1 otherwise (strings equal).Compressed:
Normal:
quelle
PHP
This script may not have any utility, but atleast this provides a way to compare strings.
PHP
Aother one:
quelle
Prolog 7
This makes use of the pattern matching feature in Prolog to unify the 2 arguments to the predicate, which effectively tests for equals equivalence when there is no unbound variable.
Sample usage:
Technically speaking, the behavior of this solution is that of unification operator
=/2
, rather than that of==/2
, which checks for term equivalence. The difference shows when unbound variables are involved. In this solution, when unbound variable is supplied, the predicate will returntrue
when unification is successful. In comparison,==/2
will compare order of term without unification.quelle
PHP, 21
This one is doing the job using variable indirection.
Or, if you don't need it to be bool
EDIT : I forgot to handle the case where you try to compare two empty strings, so the code now is
which is 21 chars.
quelle
CPython: 6
The use of
is
is obviously pretty suspect, but since the task specifically calls out that we're to determine value equality rather than reference equality, andis
only compares object identity, I feel like it may not fall under the list of banned operators.Of course there's also some question as to whether this is even valid; it works on all of my systems, but it's implementation-specific and probably won't always work if the strings aren't defined by hand in the interactive interpreter.
quelle
Mathematica / Wolfram Language, 15 bytes
Pretty self explanatory, sets each string as a set, then checks the length of the union of the two sets. If the strings are the same, returns 1, otherwise returns 0. If I'm allowed to return '2' for "different" and '1' for "same", subtract two bytes.
quelle
C 342 golfed
Note: Visual Studio complains if you don't use their safe methods eg gets_s. CodeBlocks with mingw compiles without warnings.
C 655 not golfed
Code creates weighted sum of chars for each string. If the difference is zero then they are equal, including 2 empty strings:
quelle
Python
It's long and it's not beautiful, but this is my first entry!
quelle
PHP, 68 Bytes
I assume you're prohibited to use any comparison operators. So
<
or>
are included.The idea is to use bitwise XOR. In different languages this operator has different syntax - I'll show an example for PHP. There it's available with
^
. Unfortunately, its behavior with strings isn't as good as it could be, so you'll need to check string length before. That is because in PHP, xor will strip the longer string down to the length of the shorter string.Next thing is to work with strings properly, because a single
xor
will not produce a result, available for further operations in PHP. That's whyunpack()
was used. So, the code would be:It's longer than option with
<
/>
but it won't use them. Also, the important thing is about PHP type juggling (so empty array will be cast tofalse
). Or maybe there's a simpler way to check if an array contain non-zero members (Edit: while I've been typing this, there's a good catch withtrim()
in another answer, so we can get rid of array operations)But I believe there are languages, where we can do just
a ^ b
- literally, getting the result. If it's0
(treated from all resulted bytes) - then our strings are equal. It's very easy and even more simple than<
or>
stuff.quelle
grep 14 characters
Of course, I only count the grep code; the two strings are on two consecutive lines in the input (either a pipe or a file or even an interactive session).
quelle
Matlab: 12 chars (after the strings are in variables)
The code including assignments would be:
quelle
The very crazy way
Just for the fun, but many ways for making it fail if one thinks about it. More over, don't forget the strings will be EXECUTED by the shell.
A good counter-example is comparing "string" as first string and "rm -Rf /" as a second string; just check as root and see: it will say "true" though both strings obviously aren't the same.
quelle
JavaScript [18 bytes]
OR
This will return
true
ifa == b
andfalse
ifa =/= b
. The logic behind is creating an object with a value ofa
as a property and returning1
orundefined
in case if a property ofb
value exists or doesn't exist in that object.quelle
!!
is not necessary1
orundefined
(or object/undefined
for the second case).18-2 = 16 bytes
.JavaScript [15 bytes]
This will return
true
ifa == b
andfalse
ifa =/= b
. The script is looking for the value ofb
in the array that holds a single element of value ofa
.quelle
C -
8683Obvioulsy not the shortest, but this doesn't work with string variables and instead takes the strings as input from the console. Also, I sort of like the recursive main, even if it's obviously not the shortest version. But certainly the least advisable.
quelle
char** v
can be written aschar**v
. There are some exceptions (like42 / *pointer
), but in most cases spaces can be safely removed near special characters.