Skip to content

Instantly share code, notes, and snippets.

@managedkaos
Created July 27, 2017 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save managedkaos/855e27b180603b7801cdba837ee613c7 to your computer and use it in GitHub Desktop.
Save managedkaos/855e27b180603b7801cdba837ee613c7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# for hackerrank https://www.hackerrank.com/challenges/nested-list/problem
# read the input file
@input = <>;
# get the first line; that's the number of students
$count = shift @input;
# process the rest of the input...
foreach (@input) {
# get rid of the new line
chomp;
# if the line is a string, grab it as a name
if (/^[a-zA-Z]+$/) { $name =$_; next; }
# if the line is a number, grab is as a score
if (/^\d+\.?\d+$/) { $score = $_; }
# create a hash of scores, each one pointing to an
# array with the students that have that score
push @{ $scores{$score} }, $name;
}
# sort the scores; the lowest score will be in position 0
# second lowest score will be in position 1
@sorted_scores = sort { $a <=> $b } keys %scores;
# use the second lowest score to index into the hash of scores
# that location has the list of students with that score
# do some funky joining to print each name on a new line
print join( "\n", sort( @{ $scores{ $sorted_scores[1] } } ) ), "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment