diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-07-12 12:36:04 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-07-12 12:36:04 +1000 |
| commit | 29cb8af4fcd8e3de5009ec0c770ee69198d4b69e (patch) | |
| tree | 78d1bee457f93e0a3cb129203675d8d95e6f072b /challenge-068 | |
| parent | 52f5259d4f06d99e48eaf1acb4b7642c8f4512f6 (diff) | |
| download | perlweeklychallenge-club-29cb8af4fcd8e3de5009ec0c770ee69198d4b69e.tar.gz perlweeklychallenge-club-29cb8af4fcd8e3de5009ec0c770ee69198d4b69e.tar.bz2 perlweeklychallenge-club-29cb8af4fcd8e3de5009ec0c770ee69198d4b69e.zip | |
add POD for usage and description
Diffstat (limited to 'challenge-068')
| -rw-r--r-- | challenge-068/jeongoon/perl/ch-1.pl | 66 | ||||
| -rw-r--r-- | challenge-068/jeongoon/perl/ch-2.pl | 119 |
2 files changed, 144 insertions, 41 deletions
diff --git a/challenge-068/jeongoon/perl/ch-1.pl b/challenge-068/jeongoon/perl/ch-1.pl index b93260b23e..b2bd4a15a4 100644 --- a/challenge-068/jeongoon/perl/ch-1.pl +++ b/challenge-068/jeongoon/perl/ch-1.pl @@ -1,10 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Getopt::Long; - -sub usage () { - print basename($0).$/; -} +use Getopt::Long qw(:config no_ignore_case); +use Pod::Usage; sub read_row_ { my @buffer = @{$_[0]}; @@ -20,6 +17,12 @@ sub get_row_ { sub gen_rows_of_string ($$) { my ( $R, $C ) = @_; + + # note: as the name of subroutine say + # I only use the "the list in string form" on this challenge. + # e.g) '[0, 0, 1]', '[1, 1, 0] ... ' + + # reduce the occurence of zero on the same matrix my $max_chance = int( $R / 2 ); my @result; @@ -29,6 +32,9 @@ sub gen_rows_of_string ($$) { join( ", ", map { if ( $max_chance > 0 and $_ < 2 ) { + # [ 0, 1 ] --> 0 + # [ others ] --> 1 + # only when max_chance left --$max_chance; 0; } @@ -48,16 +54,17 @@ my $use_random = 1; my $read_from_stdin = 0; my $R = 3; my $C = 3; +my $help = 0; +my $man = 0; GetOptions( "round=i" => \$round, - "stdin" => \$read_from_stdin, - "rows=i" => \$R, - "columns=i" => \$C ) or usage(); + "stdin" => sub { $read_from_stdin = 1, $use_random = 0 }, + "rows|R=i" => \$R, + "columns|C=i" => \$C, + "help" => \$help, + ) or pod2usage(2); - -if ( $read_from_stdin ) { - $use_random = 0; -} +pod2usage( -exitval => 0, -verbose => 2 ) if $help; unless ( $use_random and $R > 2 and $C > 2 ) { die "Invalid Rows($R) or Columns($C): both should be greater than 2"; @@ -72,7 +79,6 @@ if ( $use_random ) { } -# initial value { my ( @row_whole_zero_flag, $row_some_zero_str ); @@ -80,7 +86,7 @@ if ( $use_random ) { read_row_( \@row_raw ) unless $use_random; ( $row_some_zero_str = $row_raw[0] ) =~ s/0/1/g; - while ( defined( my $r_raw = get_row_( \@row_raw) ) ) { + while ( defined( my $r_raw = get_row_( \@row_raw ) ) ) { my $new_row_some_zero_str = $row_some_zero_str & $r_raw; push @row_whole_zero_flag, !!( index( $r_raw, "0" ) > 0 ); $row_some_zero_str = $new_row_some_zero_str; @@ -96,3 +102,35 @@ if ( $use_random ) { print $/; redo if ( --$round ) > 0; } + +__END__ + +=head1 NAME + +CHALLENGE 68 Task #1 - Zero Matrix + +=head1 SYNOPSIS + +perl ch-1.pl [--round=N] [--stdin] [--rows=N] [--columns=N] + + Options: + --round: the number of execution of task 1 [ default: 1 ] + --stdin: read data from the stdin, + not from automatically genrated matrix + --rows: number of rows of matrix [ > 2; default: 3 ] + --columns: number of columns of matrix [ > 2; default: 3 ] + +=head1 DESCRIPTION + + Task: + You are given a matrix of size M x N having only 0s and 1s. + Write a script to set the entire row and column to 0 if an element is 0. + + My Solution: + We can classify all the rows into only two types. + 1. the row we found at least one 'zero' -> all elements become 'zero' + or + 2. the row we couldn't find any 'zero' but all of this kind of row + must have the same list of numbers. + +=cut diff --git a/challenge-068/jeongoon/perl/ch-2.pl b/challenge-068/jeongoon/perl/ch-2.pl index 3a4afdca7b..7dbeb55c06 100644 --- a/challenge-068/jeongoon/perl/ch-2.pl +++ b/challenge-068/jeongoon/perl/ch-2.pl @@ -2,12 +2,20 @@ use utf8; use open ':std', ':encoding(UTF-8)'; use strict; use warnings; +use Getopt::Long; +use Pod::Usage; BEGIN { $::debugging = 0; - if ( grep { /^--debug$/ } @ARGV ) { - $::debugging = 1; - } + $::reading_data_from_stdin = 0; + my $help = 0; + + GetOptions( "debug" => \$::debugging, + "stdin" => \$::reading_data_from_stdin, + "help" => \$help, + ) or pod2usage(2); + + pod2usage( -exitval => 0, -verbose => 2 ) if $help; our $dprint = sub( @ ) { ++$|; @@ -57,14 +65,14 @@ sub count { $count; } -sub last_itr { - my $itr = $_[0]->itr; - return undef unless defined $itr; - while ( defined $itr->next_itr ) { - $itr = $itr->next_itr; - } - return $itr; -} +#sub last_itr { # not used in this case +# my $itr = $_[0]->itr; +# return undef unless defined $itr; +# while ( defined $itr->next_itr ) { +# $itr = $itr->next_itr; +# } +# return $itr; +#} package linked_list::iterator; use Scalar::Util qw(blessed refaddr weaken isweak); @@ -144,25 +152,38 @@ sub print_all_values ($) { # instruction is not clear about how to give the list # so I copied from the website and modfied. my $example_str = "1 → 2 → 3 → 4 → 5 → 6 → 7"; -print STDERR "Default: $example_str\n", - "please input a linked list like above or press <enter> to use default values..\n", - "Input: "; -my $input = <STDIN>; -chomp $input; +my $input = ""; + +if ( $::reading_data_from_stdin ) { + print STDERR "Default: $example_str\n". + "please input a linked list like above". + " or press <enter> to use default values..\n". + "Input: "; + $input = <STDIN>; + chomp $input; +} +elsif ( $input eq "" or not $::reading_data_from_stdin ) { + print STDERR "Using list: $example_str\n"; + $input = $example_str; +} -$input ||= $example_str; +# finding separator my @words = split /\b/, $input; -my %words_count; +my %words_score; for my $w ( @words ) { $w =~ s/\s+/ /g; # remove excess spaces ... - ++$words_count{$w}; + if ( index( $w, '->' ) > 0 or index( $w, '→' ) > 0 ) { + $words_score{$w} += 5; + } else { + ++$words_score{$w}; + } } -my $max_count = 0; -for my $k ( keys %words_count ) { - if ( $words_count{$k} > $max_count ) { - $max_count = $words_count{$k}; +my $max_score = 0; +for my $k ( keys %words_score ) { + if ( $words_score{$k} > $max_score ) { + $max_score = $words_score{$k}; $sep_str = $k; } } @@ -188,8 +209,6 @@ my $count = $L->count; my $pair_left_num = 1; my $pair_left_pos = 0; -my $pair_right_num = $count - $pair_left_num + 1; -my $pair_right_pos = $pair_left_pos + 1; my $l_itr = $L->itr; @@ -209,8 +228,6 @@ for ( my $round = int( $count / 2 ); $round > 0; --$round ) { ++$pair_left_num; $pair_left_pos += 2; - --$pair_right_num; - $pair_right_pos = ( $pair_left_pos + 1 ); } undef $l_itr; @@ -218,3 +235,51 @@ $itr = $L->itr; print STDERR "Output:\n"; print_all_values( $itr ); + + +__END__ +=encoding utf8 +=head1 NAME + +CHALLENGE 68 Task #2 - Reorder List + +=head1 SYNOPSIS + +perl ch-2.pl [--debug] [--stdin] + + Options: + --stdin: read data from the stdin, + not from internal example + --rows: number of rows of matrix [ > 2; default: 3 ] + --columns: number of columns of matrix [ > 2; default: 3 ] + +=head1 DESCRIPTION + + Task: + + You are given a singly linked list $L as below: + + L0 → L1 → … → Ln-1 → Ln + + Write a script to reorder list as below: + + L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → ... + + My Solution: + I simply follow the rule in this case because I assume that the starting + point of this challenge when we are "only" given a real linked list. + + Steps are also straight foward. + + 1. I made poor single-linked list class and save the data in the list + 2. with 'left' iterators I follow L0, L1 ... + 3. with 'right' iterators I follow Ln, Ln-1 ... + + Pros: + I could save memory a little bit not by making another array + Cons: + If the list is too long, it could be slow + Comment: + I think we can impove it by using circular linked list. + +=cut |
