r/dailyprogrammer • u/Garth5689 • Apr 25 '18
[2018-04-25] Challenge #358 [Intermediate] Everyone's A Winner!
Description
Today's challenge comes from the website fivethirtyeight.com, which runs a weekly Riddler column. Today's dailyprogrammer challenge was the riddler on 2018-04-06.
From Matt Gold, a chance, perhaps, to redeem your busted bracket:
On Monday, Villanova won the NCAA men’s basketball national title. But I recently overheard some boisterous Butler fans calling themselves the “transitive national champions,” because Butler beat Villanova earlier in the season. Of course, other teams also beat Butler during the season and their fans could therefore make exactly the same claim.
How many transitive national champions were there this season? Or, maybe more descriptively, how many teams weren’t transitive national champions?
(All of this season’s college basketball results are here. To get you started, Villanova lost to Butler, St. John’s, Providence and Creighton this season, all of whom can claim a transitive title. But remember, teams beat those teams, too.)
Output Description
Your program should output the number of teams that can claim a "transitive" national championship. This is any team that beat the national champion, any team that beat one of those teams, any team that beat one of those teams, etc...
Challenge Input
The input is a list of all the NCAA men's basketball games from this past season via https://www.masseyratings.com/scores.php?s=298892&sub=12801&all=1
Challenge Output
1185
14
u/gandalfx Apr 25 '18
Maybe it's because I don't know much about baseball (it's that game where people hit a tennis ball with a club, right?) but I don't quite understand all the details of that input format. Specifically:
@symbol in front of the name which I have no idea what it might mean and, by extension, if it is relevant.6
u/gandalfx Apr 25 '18
Python 3 trying for some efficiency.
Prints the expected result. Interesting side note: There are a total of 1362 teams, so 87% of all teams are "transitive champions".
4
u/da_chicken Apr 25 '18 edited Apr 26 '18
PowerShell 5, because that's all I've used recently.
Only interesting thing I'm doing here really is throwing away the games where I've found a new winner since they can't be useful anymore. In theory that should make it
O( n log n )instead ofO( n^2 ), and it seems to save about 20% on time (~1.2s vs ~1.45s on my 10 year old system). You could also throw away tie games, but those are so rare as not to matter and there aren't any in this data set. To give people an idea of how much gets thrown away,$Games.Countstarts out at ~16,500. At the end, it's ~580. The outer do-while loop only runs 12 times.Here's what the collections look like as it executes:
I wanted
$Winnersto be aSystem.Collections.Generic.List<Game>instead of anSystem.Collections.ArrayList, but PowerShell doesn't correctly inherit methods when I go$Games = [System.Collections.Generic.List[Game]]::new(). You have to say$Games = New-Object -TypeName 'System.Collections.Generic.List[Game]'and that actually ends up taking longer because calling theNew-Objectcommand is more expensive. I should test this on PowerShell 6 and report it as a bug.3
u/Gprime5 Apr 25 '18 edited Apr 25 '18
Python 3
Loops through the data, if a team beat one of the winning teams, add them to the set of winning teams, when there are no more transitive teams to add, end the loop.
2
u/0rac1e Apr 26 '18 edited Apr 26 '18
Perl 6
Pretty straight-forward implementation. Make a 2D array of games
[(winner, loser), ...]. Create a loop that finds games where the winner inwinsetwas the loser (stored intrans). For each loop, make a newwinsetwhich is the set difference of the current loopstranswith all previouswinners. If alltransare inwinners, then stop. Otherwise, create set union of previouswinnersand currenttransand loop again.EDIT: Made some minor changes to improve performance, also...
Python
Here's more-or-less the exact same implementation in Python
2
u/rjsberry Apr 26 '18 edited Nov 27 '18
Rust
Circumvented parsing the input data directly by downloading the CSV posted in another Python solution.
I'm not entirely happy with the function
count_transitive_winners. Lots ofStringclones.I learned that it's possible to directly collect a sequence of
Result<_, _>asResult<Vec<_>, _>which is pretty neat.2
u/vishnumad Apr 26 '18
Kotlin
1
u/skeeto -9 8 Apr 25 '18
C using a depth first graph search. Team names are interned immediately after they're read in, and from then on are only handled as integers.
1
u/thestoicattack Apr 25 '18
C++17 along with some string interning, so we only have to use
std::string_viewin most places. Also some fun structured bindings, for both thestd::equal_rangeresult and aGamestruct.1
u/chunes 1 2 Apr 25 '18 edited Apr 25 '18
Theoretically, are the only non- transitive champions those teams that lost all their games or teams that only beat these loser teams? (And teams that only beat the teams that beat the loser teams, and so on...) It's the same exact problem but probably much smaller
1
u/g00glen00b Apr 26 '18
Java:
It works by creating a proper model from the file (
ResultandTeamResult) and then using recursion to determine who won against the winners from the previous iteration until there are no more winners to be found.1
u/zatoichi49 Apr 26 '18 edited Apr 27 '18
Method:
Create a set of tuples (winner, loser) for all games, and convert to a list. Create a set (transitive) containing the title winners. Loop through all games: if the losing team is already in the transitive set, then add the winning team (if they're not in the set already) and remove the (winner, loser) tuple from the original games list. Continue repeating the loop, stopping when no more teams are being added. Return the length of transitive to give the total transitive title holders.
Python 3:
Output:
1
u/HereBehindMyWall Apr 26 '18
Nodejs solution
1
u/nitishc Apr 27 '18
Rust
The following code involves a lot of string cloning. I still can't get the hang of how to use references properly in Rust.
1
u/Prince_John Apr 27 '18 edited Apr 27 '18
Java. No doubt there are more efficient ways to do this, but this seems to work. I overcomplicated the reading of the text file at first, then learnt some regex which helped.
I think I'm committing an error by adding Villanova to the array of winners at the beginning (although it doesn't matter in this case, because they are also transitive champions), but I couldn't figure out how to kick off the computation without something being in the array to allow the for loop to execute. Any thoughts on that would be welcome. I haven't had time to think of optimisation yet either.
1
u/[deleted] Apr 29 '18
F# uses a similar approach seen in many of the answers here. Can be run directly in
FSI.exeif "scores.csv" is in the active directory.Output
1
u/RawPugin Apr 29 '18
Shouldn't the output be 1184? My output contains Villanova to get 1185 which they shouldn't be claiming 'transitive' champion?
1
u/[deleted] Apr 30 '18
https://github.com/willkillson/Challenge-358-Intermediate-JS-
My output was 1186 to include "Villanova", and so minus 1 would give 1185, but the index is zero so 1184 total.
1
u/octolanceae Apr 30 '18
C++
Generated a map of all teams and who they lost to, then did a non-recursive count starting with Villanova. Runs in ~0.02s
output
1
u/ruincreep May 02 '18
Perl 6 (55 chars, multi-threading)
All the other solutions here seem way more complicated which makes me believe there's something wrong with mine, but the output is correct, so...
1
u/mwpfinance May 05 '18
Python 3
Self-criticism:
My first intermediate project, though!
1
u/2kofawsome Jun 30 '18
python3.6
So for whatever reason I am getting 1192 instead of 1185, but after copying and pasting the code of a few other Python 3 solutions I got 1192 for theirs as well, so somehow I messed up the input data? If anyone gets the chance can you run this code and leave a comment whether it works for you or not and gets the right answer.
1
u/zqvt Apr 25 '18 edited Apr 25 '18
Python3, reformatted the input to CSV and removed the unnecessary info https://pastebin.com/LDbXGeJn