Tennis match problem

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
Hi this should be a simple one.

Let's say we have a tennis match between two players. The game is set as first to N wins. Write a function to return the winner.

Input:
String array (Players who won that round), random size of up to 10000 - [A,A,B,A,B,...]

N - Integer

Expected: A or B

Please put spoilers in your submission
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
A naive one from me first.
Solution
  • O(M), M is number of games played
  • input from arguments,
  • don't care how many different players are there too.
  • no answer if it is impossible to reach N

UPDATE: STRICTLY A WRONG SOLUTION THAT WASTED COMPUTING RESOURCES. DO NOT FOLLOW THE CODE BELOW.

Perl:
use strict;
use warnings;

# assuming number of games won by any players <= N
my($games, $N) = @ARGV;

my %winner = ();
foreach my $c (split //, $games) {
  if (++$winner{$c} == $N) {
    print $c, "\n";
    last;
  }
}

This is not a recommended solution, but will be the most hardcore I can come up with using perl. Just for fun though.
It's not meant to be nice, it is meant to just work. (^.^)
Perl:
eval{$ARGV[0]=~/(?:(.)(?{++$w{$1}==$ARGV[1]&&die("$1\n")}))+/};$@&&print"$@"
Output:
Code:
$ ./tennismatch.pl ABBA 0
$ ./tennismatch.pl ABBA 1
A
$ ./tennismatch.pl ABBA 2
B
$ ./tennismatch.pl ABBA 3
$
 
Last edited:

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
A naive one from me first.
Solution
  • O(M), M is number of games played
  • input from arguments,
  • don't care how many different players are there too.
  • no answer if it is impossible to reach N
Perl:
use strict;
use warnings;

# assuming number of games won by any players <= N
my($games, $N) = @ARGV;

my %winner = ();
foreach my $c (split //, $games) {
  if (++$winner{$c} == $N) {
    print $c, "\n";
    last;
  }
}

This is not a recommended solution, but will be the most hardcore I can come up with using perl. Just for fun though.
It's not meant to be nice, it is meant to just work. (^.^)
Perl:
eval{$ARGV[0]=~/(?:(.)(?{++$w{$1}==$ARGV[1]&&die("$1\n")}))+/};$@&&print"$@"
Output:
Code:
$ ./tennismatch.pl ABBA 0
$ ./tennismatch.pl ABBA 1
A
$ ./tennismatch.pl ABBA 2
B
$ ./tennismatch.pl ABBA 3
$
Nice, but can u make it even faster?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
I feel like this will be a major hint already, but can u think of a o(1) solution?
Sounds like a question that I didn't read properly :)
Code:
// assuming `games` is the input array of characters
games[games.length - 1] // minus the caveat of no games
 

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
Sounds like a question that I didn't read properly :)
Code:
// assuming `games` is the input array of characters
games[games.length - 1] // minus the caveat of no games
U got it. I wanna make this an interview qn to test candidates critical thinking, what do u think?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
Sure why not. I did fail it, didn't I ? :)

@diminishin
However I must indicate. It would seems the inclusion of Nis extremely misleading. Because your Nis constrained by your games array input.

Simply for a game array input of ABBAA, you can't have any values other than N=3. Games inputs like ABBA is invalid too.

Hence on 2nd thought, I probably don't think this is a good question.
:)
 
Last edited:

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
@diminishin
However I must indicate. It would seems the inclusion of Nis extremely misleading. Because your Nis constrained by your games array input.

Simply for a game array input of ABBAA, you can't have any values other than N=3. Games inputs like ABBA is invalid too.

Hence on 2nd thought, I probably don't think this is a good question.
:)
Yes this is done on purpose, N serves no purpose at all. Hmm, maybe I should find a better way to phase this qn?
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top