diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-03-15 04:24:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-15 04:24:22 +0000 |
| commit | 4b80d90679283b4176933171e44ba2efaec34cfb (patch) | |
| tree | 6ed6cf6ce40a789e7a53c68c01f41cb528d888d5 /challenge-103 | |
| parent | b5c9376af56b4981c28010cbc46213597faa0a75 (diff) | |
| parent | 0d2d9c79c0f1cc62f5513d6587a21803f360c216 (diff) | |
| download | perlweeklychallenge-club-4b80d90679283b4176933171e44ba2efaec34cfb.tar.gz perlweeklychallenge-club-4b80d90679283b4176933171e44ba2efaec34cfb.tar.bz2 perlweeklychallenge-club-4b80d90679283b4176933171e44ba2efaec34cfb.zip | |
Merge pull request #3721 from adamcrussell/challenge-103
Challenge 103
Diffstat (limited to 'challenge-103')
| -rw-r--r-- | challenge-103/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-103/adam-russell/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-103/adam-russell/perl/ch-2.pl | 75 |
3 files changed, 97 insertions, 0 deletions
diff --git a/challenge-103/adam-russell/blog.txt b/challenge-103/adam-russell/blog.txt new file mode 100644 index 0000000000..5924dd93b8 --- /dev/null +++ b/challenge-103/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/03/14 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..42c6f6d38a --- /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 = sprintf("%02d", $hours) . ":" . sprintf("%02d", $minutes) . ":" . sprintf("%02d", $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)" |
