Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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