diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-27 11:58:47 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-10-27 11:58:47 +0100 |
| commit | 45b1ff9bba6da2eb6c475dd5713bc9ba96d46e88 (patch) | |
| tree | ffd03ff042ea37b6b5816ea8099997bf8bbf9e8a | |
| parent | 1cf9d11e428c2f5c674520e27d0997039837b7de (diff) | |
| download | perlweeklychallenge-club-45b1ff9bba6da2eb6c475dd5713bc9ba96d46e88.tar.gz perlweeklychallenge-club-45b1ff9bba6da2eb6c475dd5713bc9ba96d46e88.tar.bz2 perlweeklychallenge-club-45b1ff9bba6da2eb6c475dd5713bc9ba96d46e88.zip | |
Add Perl solution to challenge 126
| -rw-r--r-- | challenge-126/paulo-custodio/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-126/paulo-custodio/perl/ch-2.pl | 82 | ||||
| -rw-r--r-- | challenge-126/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-126/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-126/paulo-custodio/test.pl | 4 |
5 files changed, 140 insertions, 0 deletions
diff --git a/challenge-126/paulo-custodio/perl/ch-1.pl b/challenge-126/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..5657ca8983 --- /dev/null +++ b/challenge-126/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +# TASK #1 > Count Numbers +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to print count of numbers from 1 to $N that don’t contain digit 1. +# +# Example +# Input: $N = 15 +# Output: 8 +# +# There are 8 numbers between 1 and 15 that don't contain digit 1. +# 2, 3, 4, 5, 6, 7, 8, 9. +# +# Input: $N = 25 +# Output: 13 +# +# There are 13 numbers between 1 and 25 that don't contain digit 1. +# 2, 3, 4, 5, 6, 7, 8, 9, 20, 22, 23, 24, 25. + +use Modern::Perl; + +my $N = shift||0; +my $count = 0; +for (1..$N) { + $count++ if !/1/; +} +say $count; diff --git a/challenge-126/paulo-custodio/perl/ch-2.pl b/challenge-126/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..aa668db2f2 --- /dev/null +++ b/challenge-126/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,82 @@ +#!/usr/bin/env perl + +# TASK #2 > Minesweeper Game +# Submitted by: Cheok-Yin Fung +# You are given a rectangle with points marked with either x or *. Please +# consider the x as a land mine. +# +# Write a script to print a rectangle with numbers and x as in the Minesweeper +# game. +# +# A number in a square of the minesweeper game indicates the number of mines +# within the neighbouring squares (usually 8), also implies that there are no +# bombs on that square. +# +# Example +# Input: +# x * * * x * x x x x +# * * * * * * * * * x +# * * * * x * x * x * +# * * * x x * * * * * +# x * * * x * * * * x +# +# Output: +# x 1 0 1 x 2 x x x x +# 1 1 0 2 2 4 3 5 5 x +# 0 0 1 3 x 3 x 2 x 2 +# 1 1 1 x x 4 1 2 2 2 +# x 1 1 3 x 2 0 0 1 x + +use Modern::Perl; + +my @board = parse_board(); +compute_mines(\@board); +show_board(@board); + +sub parse_board { + my @board; + while(<>) { + my @row = split(' ', $_); + push @board, \@row; + } + return @board; +} + +sub compute_mines { + my($board) = @_; + my $height = @$board; + my $width = @{$board->[0]}; + for my $r (0 .. $height-1) { + for my $c (0 .. $width-1) { + if (!has_mine($board, $r, $c)) { + my $count = 0; + $count++ if has_mine($board, $r-1, $c-1); + $count++ if has_mine($board, $r-1, $c); + $count++ if has_mine($board, $r-1, $c+1); + $count++ if has_mine($board, $r, $c-1); + $count++ if has_mine($board, $r, $c+1); + $count++ if has_mine($board, $r+1, $c-1); + $count++ if has_mine($board, $r+1, $c); + $count++ if has_mine($board, $r+1, $c+1); + $board->[$r][$c] = $count; + } + } + } +} + +sub has_mine { + my($board, $r, $c) = @_; + my $height = @$board; + my $width = @{$board->[0]}; + return 0 if $r<0 || $r>=$height; + return 0 if $c<0 || $c>=$width; + return 1 if $board->[$r][$c] eq 'x'; + return 0; +} + +sub show_board { + my(@board) = @_; + for (@board) { + say join ' ', @$_; + } +} diff --git a/challenge-126/paulo-custodio/t/test-1.yaml b/challenge-126/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..e93ed7d5a1 --- /dev/null +++ b/challenge-126/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 15 + input: + output: 8 +- setup: + cleanup: + args: 25 + input: + output: 13 diff --git a/challenge-126/paulo-custodio/t/test-2.yaml b/challenge-126/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ae96504dbf --- /dev/null +++ b/challenge-126/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: + input: | + x * * * x * x x x x + * * * * * * * * * x + * * * * x * x * x * + * * * x x * * * * * + x * * * x * * * * x + output: | + x 1 0 1 x 2 x x x x + 1 1 0 2 2 4 3 5 5 x + 0 0 1 3 x 3 x 2 x 2 + 1 1 1 x x 4 1 2 2 2 + x 1 1 3 x 2 0 0 1 x diff --git a/challenge-126/paulo-custodio/test.pl b/challenge-126/paulo-custodio/test.pl new file mode 100644 index 0000000000..ba6c37260b --- /dev/null +++ b/challenge-126/paulo-custodio/test.pl @@ -0,0 +1,4 @@ +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; +require '../../challenge-001/paulo-custodio/test.pl'; |
