in reply to Perl Solution to Spotify Programming Puzzle

#! perl -slw use strict; my $n = <>; my %counts; ++$counts{ $_ } for split ' ', do{ local $/, <> }; my @sorted = sort{ $counts{$b} <=> $counts{$a} }keys %counts; my( $i, $min, $total ) = (0) x 3; ++$min while ( $total += $counts{ $sorted[ $i++ ] } ) < $n; print $min+1; print for @sorted[ 0 .. $min ];

Tests:

C:\test>SpotifyComp.pl 4 1009 2000 1009 2001 1002 2002 1003 2002 ^Z 2 2002 1009 C:\test>SpotifyComp.pl 2 1009 2011 1017 2011 ^Z 1 2011

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Perl Solution to Spotify Programming Puzzle
by repellent (Priest) on Aug 25, 2011 at 05:34 UTC
    Actual:
    > SpotifyComp.pl 4 1009 2000 1008 2001 1002 2002 1003 2002 ^D 3 2002 1002 1009

    Expected:
    3 2002 1008 or 2001 1009 or 2000
Re^2: Perl Solution to Spotify Programming Puzzle
by ::rml:: (Sexton) on Aug 25, 2011 at 23:50 UTC

    Cool. It's also a lot faster than my version.

    [rml@box current]$ time perl BrowserUK.pl sample-input.txt 2 2002 1009 real 0m0.006s user 0m0.002s sys 0m0.004s [rml@box current]$ time perl rml.pl sample-input.txt 2 1009 2002 real 0m0.028s user 0m0.023s sys 0m0.003s
    -- C-x C-c
      Cool. It's also a lot faster than my version.

      Yes. But unfortunately, fatally flawed.

      I should never try to assess the inherent complexity of a task when approaching my personal shutdown time :) Nor take example solutions as sufficient tests.

      That said, the fix to the approach doesn't seem to be too complicated, though I haven't coded it yet, so that thought might come back to bite me. Maybe I'll get to it once I cleared the non-optional tasks of my day.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.