diff options
| author | Adam Russell <ac.russell@live.com> | 2021-03-13 13:14:07 -0500 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2021-03-13 13:14:07 -0500 |
| commit | 4a927b0582cd2fbd46b8b899895122e76dfa8ec4 (patch) | |
| tree | b1a63fe90be53a4121d5bd0e1ff73561e8779bc8 /challenge-103 | |
| parent | 81637069ab57f635ce60710cc1c8f3fe350eab5f (diff) | |
| download | perlweeklychallenge-club-4a927b0582cd2fbd46b8b899895122e76dfa8ec4.tar.gz perlweeklychallenge-club-4a927b0582cd2fbd46b8b899895122e76dfa8ec4.tar.bz2 perlweeklychallenge-club-4a927b0582cd2fbd46b8b899895122e76dfa8ec4.zip | |
initial commit of Perl solutions
Diffstat (limited to 'challenge-103')
| -rw-r--r-- | challenge-103/adam-russell/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-103/adam-russell/perl/ch-2.pl | 75 |
2 files changed, 96 insertions, 0 deletions
diff --git a/challenge-103/adam-russell/perl/ch-1.pl b/challenge-103/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..ea887ec3a6 --- /dev/null +++ b/challenge-103/adam-russell/perl/ch-1.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +## +# You are given a year $year. +# Write a script to determine the Chinese Zodiac for the given year $year +## +use constant ELEMENTS => {1 => q/Wood/, 2 => q/Fire/, 3 => q/Earth/, 4 => q/Metal/, 0 => q/Water/}; +use constant ANIMALS => {1 => q/Rat/, 2 => q/Ox/, 3 => q/Tiger/, 4 => q/Rabbit/, 5 => q/Dragon/, 6 => q/Snake/, 7 => q/Horse/, 8 => q/Goat/, 9 => q/Monkey/, 10 => q/Rooster/, 11 => q/Dog/, 0 => q/Pig/}; + +sub chinese_zodiac{ + my($year) = @_; + return ELEMENTS->{$year % 5} . " " . ANIMALS->{($year + 9) % 12}; +} + +MAIN:{ + my($YEAR); + $YEAR = 2017; + print chinese_zodiac($YEAR) . "\n"; + $YEAR = 1938; + print chinese_zodiac($YEAR) . "\n"; +} diff --git a/challenge-103/adam-russell/perl/ch-2.pl b/challenge-103/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..802892d875 --- /dev/null +++ b/challenge-103/adam-russell/perl/ch-2.pl @@ -0,0 +1,75 @@ +use strict; +use warnings; +## +# Write a program to output which file is currently playing. +## +sub song_times{ + my($file_name) = @_; + my %song_times; + my @song_order; + my $length = 0; + my $index = 0; + if(!$file_name){ + while(<DATA>){ + chomp; + my($time, $song) = split(/,/); + $length += $time; + $song_order[$index] = $song; + $song_times{$song} = $time; + $index++; + } + } + else{ + open(FILE, $file_name); + while(<FILE>){ + chomp; + my($time, $song) = split(/,/); + $length += $time; + $song_order[$song] = $index; + $song_times{$song} = $time; + $index++; + } + } + return [\%song_times, \@song_order, $length]; +} + +sub now_playing{ + my($start_time, $current_time, $file_name) = @_; + my($song_times, $song_order, $length_millis); + $current_time = time() if !$current_time; + ($song_times, $song_order, $length_millis) = @{song_times()} if $file_name; + ($song_times, $song_order, $length_millis) = @{song_times($file_name)} if !$file_name; + my $time_playing = $current_time - $start_time; + my $cycles = ($time_playing * 1000) / $length_millis; + my $current_cycle_millis = ($cycles - int($cycles)) * $length_millis; + my $seek_time = 0; + for my $song (@{$song_order}){ + $seek_time += $song_times->{$song}; + if($seek_time > $current_cycle_millis){ + my $position = ($song_times->{$song} - ($seek_time - $current_cycle_millis)) / 1000; + my $hours = int($position/3600); + my $minutes = int(($position % 3600) / 60); + my $seconds = int(($position % 3600) % 60); + $position = $hours . ":" . $minutes . ":" . $seconds; + return ($song, $position); + } + } +} + +MAIN:{ + my($song, $position) = now_playing(1606134123, 1614591276); + print "$song\n$position\n"; +} + + + + + +__DATA__ +1709363,"Les Miserables Episode 1: The Bishop (broadcast date: 1937-07-23)" +1723781,"Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)" +1723781,"Les Miserables Episode 3: The Trial (broadcast date: 1937-08-06)" +1678356,"Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)" +1646043,"Les Miserables Episode 5: The Grave (broadcast date: 1937-08-20)" +1714640,"Les Miserables Episode 6: The Barricade (broadcast date: 1937-08-27)" +1714640,"Les Miserables Episode 7: Conclusion (broadcast date: 1937-09-03)" |
