Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

This question already has an answer here:

Compare if three string variables with the same content, return true if they are or false if they are not without using conditional operators and bitwise operators.

e.g. in C# without && || ?:

return  (string1==string2)==(string1==string3)==true;

PS: initial idea came from these question

share|improve this question

marked as duplicate by Peter Taylor, Quincunx, Tony H., Geobits, Howard Mar 18 at 18:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Popularity contests shouldn't get the code-challenge tag. I've removed that tag. –  ProgramFOX Mar 18 at 17:33
    
How about just... if(string1==string2)return string2==string3;return false –  mniip Mar 18 at 17:33
    
Many thanks @ProgramFOX, I'm new to PCG –  Bolu Mar 18 at 17:34
2  
Also, I think it's a good idea to ban bitwise operators. true & true is actually the same as true && true. –  ProgramFOX Mar 18 at 17:36
3  
Trivial variant on Determine if Strings are equal –  Peter Taylor Mar 18 at 18:05

2 Answers 2

Perl

The function CompareStrings takes any number of strings and returns the strings true or false:

  • true is returned if there are one or more strings and all strings are equal.
  • false is returned if there is no string or at least two different strings.
sub CompareStrings (@) {
    my %hash = map { $_ => 1 } @_;
    return ('false', 'true')[not scalar(keys %hash) - 1];
}

The strings are put as keys in a hash. If there is at least one string and all strings are equal, then the hash has one key exactly. Otherwise there are zero or two or more keys. After 1 is subtracted the operator not maps the cases to the empty string and 1 that are used to select the result string (the empty string becomes 0).

sub CompareStrings (@) {
    my %hash = map { $_ => 1 } @_;
    return ('false', 'true')[not scalar(keys %hash) - 1];
}

Complete script with test cases:

#!/usr/bin/env perl
use strict;
$^W=1;

sub CompareStrings (@) {
    # several variants to initialize the hash, whose keys are the strings:
    # my %hash; @hash{@_} = (1) x @_;
    # my %hash; $hash{$_} = 1 for @_;
    my %hash = map { $_ => 1 } @_;
    return ('false', 'true')[not scalar(keys %hash) - 1];
}

# Testing

$\ = "\n";
print CompareStrings qw[];                  # false
print CompareStrings qw[foobar];            # true
print CompareStrings qw[hello hello];       # true
print CompareStrings qw[Hello World];       # false
print CompareStrings qw[abc abc abc];       # true 
print CompareStrings qw[abc abc def];       # false
print CompareStrings qw[abc def ghi];       # false
print CompareStrings qw[a a a a a a a a];   # true
print CompareStrings qw]a a a a oops a a];  # false

__END__
share|improve this answer

GolfScript

1$=:a;=a+2="true""false"if

This program assumes three strings on the stack.

Test online

How it works:

  1. 1$ copies the second element on the stack (the second string) to the top of the stack.
  2. =:a; returns 1 if the two top elements (the second and the third string) are equal; otherwise, 0. Then, it stores this result in the variable a and it pops it from the stack. Now only the first and the second string remain on the stack.
  3. = checks whether the first and the second string are equal.
  4. a+ puts the value of a on the stack and calculates the sum of a and the result of the previous comparison.
  5. 2="true""false"if puts "true" on the stack if the sum of the comparison results is equal to 2: in this case, both comparisons returned true. If the sum is 1 or 0 then it puts "false" on the stack. This value is now the only one on the stack, and at the and of a program, GolfScript always outputs the values on the stack.

[Edit]

Thanks to Peter Taylor for pointing out that variables are not necessary:

1$=@@=+2="true""false"if
share|improve this answer
    
You don't need a variable. @@ will push the top item on the stack down two. (And since idiomatic GolfScript is golfed, I prefer my answer to the question which I claim this one duplicates. Just change ( to a suitable comparison). –  Peter Taylor Mar 18 at 18:26
    
@PeterTaylor: Thank you, I have updated my answer. –  ProgramFOX Mar 18 at 18:30

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