diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-10-01 10:57:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-01 10:57:52 +0100 |
| commit | 28abfae6c8d25978fc5d8dd80ea27dfd8206f6ae (patch) | |
| tree | 2f38e392224a42fdc488b358f0425247ffef6106 | |
| parent | 73088061a0d535bdc70789d046d8ff87b74ef07a (diff) | |
| parent | c35b1f6ff698fb20a298662649a0498fda9af985 (diff) | |
| download | perlweeklychallenge-club-28abfae6c8d25978fc5d8dd80ea27dfd8206f6ae.tar.gz perlweeklychallenge-club-28abfae6c8d25978fc5d8dd80ea27dfd8206f6ae.tar.bz2 perlweeklychallenge-club-28abfae6c8d25978fc5d8dd80ea27dfd8206f6ae.zip | |
Merge pull request #693 from Prajithp/master
solutions for perl weekly challenge 028
| -rw-r--r-- | challenge-028/prajith-p/ch-1.pl | 10 | ||||
| -rw-r--r-- | challenge-028/prajith-p/ch-2.pl | 261 |
2 files changed, 271 insertions, 0 deletions
diff --git a/challenge-028/prajith-p/ch-1.pl b/challenge-028/prajith-p/ch-1.pl new file mode 100644 index 0000000000..93442ec106 --- /dev/null +++ b/challenge-028/prajith-p/ch-1.pl @@ -0,0 +1,10 @@ +#!/usr/bin/perl + +my $file = $ARGV[0]; + +my $r = -r $file ? ( + -B $file ? + "The file content is binary" + : "The file content is ascii" ) + : "Could not open file: $file"; +print $r; diff --git a/challenge-028/prajith-p/ch-2.pl b/challenge-028/prajith-p/ch-2.pl new file mode 100644 index 0000000000..9cf0c23023 --- /dev/null +++ b/challenge-028/prajith-p/ch-2.pl @@ -0,0 +1,261 @@ +#!/usr/bin/perl + +use feature qw<say>; +use strict; +use Tie::IxHash; + +#inspired by https://github.com/cauefcr/py-ascii-digital-clock + +our $buf = Clock::Buffer->new(); +our $big_chars = {}; + +package main { + + sub get_big_chars { + local $/; + my $content = <DATA>; + my @dig_in = split( '\n', $content ); + my $curr = 0; + my $j = 0; + + foreach my $i (@dig_in) { + if ( substr( $i, 0, 1 ) eq '-' ) { + $curr = substr $i, 1; + $j = 0; + $big_chars->{$curr} = Clock::LargeNumber->new($curr); + } + else { + if ( $j != 0 ) { $big_chars->{$curr}->add_str( $j - 1, $i ) } + } + $j += 1; + } + } + + sub new_separator { + for my $i ( keys %{ $buf->lines } ) { + my $line = $buf->lines->{$i} . " "; + $buf->set_line( $i, $line ); + } + } + + sub load_to_buf { + my $time_str = shift; + my @c = split( "", $time_str ); + + for my $char (@c) { + $big_chars->{$char}->add_to_buffer; + new_separator(); + } + } + + sub get_time_string { + my ( $S, $M, $H ) = localtime(time); + return $H . ":" . "$M" . ":" . "$S"; + } + + &get_big_chars(); + while (1) { + sleep(1); + system("clear"); + my $now = get_time_string(); + load_to_buf($now); + $buf->print_lines(); + $buf->clean(); + } + +} + +package Clock::Buffer { + + sub new { + my $class = shift; + + my %lines; + my $t = tie %lines, 'Tie::IxHash'; + %lines = ( + 0 => "", + 1 => "", + 2 => "", + 3 => "", + 4 => "", + 5 => "", + 6 => "", + 7 => "", + 8 => "" + ); + + my $self = { lines => \%lines }; + return bless $self, $class; + } + + sub print_lines { + my $self = shift; + for my $line ( keys %{ $self->{'lines'} } ) { + say $self->{'lines'}->{$line}; + } + } + + sub clean { + my $self = shift; + for my $line ( keys %{ $self->{'lines'} } ) { + $self->{'lines'}->{$line} = ""; + } + } + + sub set_line { + my ( $self, $k, $v ) = @_; + $self->{'lines'}->{$k} = $v; + } + + sub lines { return $_[0]->{'lines'}; } +}; + +package Clock::LargeNumber { + + sub new { + my ( $class, $n ) = @_; + my %lines; + my $t = tie %lines, 'Tie::IxHash'; + %lines = ( + 0 => "", + 1 => "", + 2 => "", + 3 => "", + 4 => "", + 5 => "", + 6 => "", + 7 => "", + 8 => "" + ); + + my $self = { + lines => \%lines, + n => $n + }; + return bless $self, $class; + } + + sub add_str { + my ( $self, $key, $stg ) = @_; + $self->{'lines'}->{$key} = $stg; + } + + sub add_to_buffer { + my $self = shift; + for my $n ( 0 .. 8 ) { + my $lines = $buf->lines->{$n} . '' . $self->{lines}->{$n}; + $buf->set_line( $n, $lines ); + } + } +}; +__END__ +-0 + ###### +### ### +## ## +## ## +## ## +## ## +## ## +### ### + ###### +-1 + ## + ## + ## + ## + ## + ## + ## + ## + ## +-2 + ###### +## ## + ## + ## +######## +## +## +## ## +######## +-3 + ###### +## ## + ## + ## + ####### + ## + ## +## ## + ###### +-4 +## ## +## ## +## ## +## ## + ####### + ## + ## + ## + ## +-5 +######## +## # +## +## +######## + ## + ## +## ## + ###### +-6 + ###### +## ## +## +## +####### +## ## +## ## +## ## + ###### +-7 +######## +# ## + ## + ## + ###### + ## + ## + ## + ## +-8 + ###### +## ## +# # +## ## + ###### +## ## +# # +## ## + ###### +-9 + ###### +## ## +# # +## ## + ####### + ## + ## +## ## + ###### +-: + + ## + ## + ## + + ## + ## + ## + |
