aboutsummaryrefslogtreecommitdiff
path: root/challenge-126
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2021-08-22 10:22:49 -0400
committerAdam Russell <ac.russell@live.com>2021-08-22 10:22:49 -0400
commit5d8a84d12860b55926cadefcf7812d1dfe392ea6 (patch)
tree04629939ba02663ca83ac4df85a1e5ea56808d94 /challenge-126
parente36569c0953ae5b516a140d5bac859d8f9371c34 (diff)
downloadperlweeklychallenge-club-5d8a84d12860b55926cadefcf7812d1dfe392ea6.tar.gz
perlweeklychallenge-club-5d8a84d12860b55926cadefcf7812d1dfe392ea6.tar.bz2
perlweeklychallenge-club-5d8a84d12860b55926cadefcf7812d1dfe392ea6.zip
Perl and C++ solutions
Diffstat (limited to 'challenge-126')
-rw-r--r--challenge-126/adam-russell/cxx/ch-1.cxx47
-rw-r--r--challenge-126/adam-russell/cxx/ch-2.cxx85
-rw-r--r--challenge-126/adam-russell/perl/ch-1.pl37
-rw-r--r--challenge-126/adam-russell/perl/ch-2.pl59
4 files changed, 228 insertions, 0 deletions
diff --git a/challenge-126/adam-russell/cxx/ch-1.cxx b/challenge-126/adam-russell/cxx/ch-1.cxx
new file mode 100644
index 0000000000..50afb68a5a
--- /dev/null
+++ b/challenge-126/adam-russell/cxx/ch-1.cxx
@@ -0,0 +1,47 @@
+#include <iostream>
+
+/*
+ * You are given a positive integer n.
+ * Write a script to print the count of
+ * numbers from 1 to n that don't contain digit the 1.
+*/
+
+class CountNumbers{
+ private:
+ int has_1(int);
+ int count_with_1(int);
+ public:
+ int count_without_1(int);
+};
+
+int CountNumbers::has_1(int n){
+ std::string s = std::to_string(n);
+ const char* c = s.c_str();
+ while(*c != '\0'){
+ if(*c == '1')
+ return 1;
+ c++;
+ }
+ return 0;
+}
+
+int CountNumbers::count_with_1(int n){
+ int count = 0;
+ for(int i = 1; i <= n; i++)
+ count += has_1(i);
+ return count;
+}
+
+int CountNumbers::count_without_1(int n){
+ return n - count_with_1(n);
+}
+
+int main(int argc, char** argv){
+ CountNumbers cn;
+ int n;
+ n = 15;
+ std::cout << cn.count_without_1(n) << std::endl;
+ n = 25;
+ std::cout << cn.count_without_1(n) << std::endl;
+}
+
diff --git a/challenge-126/adam-russell/cxx/ch-2.cxx b/challenge-126/adam-russell/cxx/ch-2.cxx
new file mode 100644
index 0000000000..7eb15c4f3f
--- /dev/null
+++ b/challenge-126/adam-russell/cxx/ch-2.cxx
@@ -0,0 +1,85 @@
+#include <cstdlib>
+#include <iostream>
+
+/*
+ * You are given a rectangle with points
+ * marked with either x or *. Consider
+ * the x as a land mine. Write a script
+ * to print a rectangle with numbers
+ * and x as in the Minesweeper game.
+*/
+
+class MinesweeperGame{
+ private:
+ char** initialize_grid(int, int);
+ public:
+ void make_print_grid(int, int);
+};
+
+char** MinesweeperGame::initialize_grid(int m, int n){
+ char** grid = new char*[m];
+ for(int i = 0; i < m; i++){
+ grid[i] = new char[n];
+ for(int j = 0; j < n; j++){
+ grid[i][j] = '*';
+ if(rand() / double(RAND_MAX) < 1 / 3.0)
+ grid[i][j] = 'x';
+ }
+ }
+ return grid;
+}
+
+void MinesweeperGame::make_print_grid(int m, int n){
+ char** grid = initialize_grid(m, n);
+ std::cout << "Input:" << std::endl;
+ for(int i = 0; i < m; i++){
+ std::cout << "\t";
+ for(int j = 0; j < n; j++){
+ std::cout << grid[i][j] << " ";
+ }
+ std::cout << std::endl;
+ }
+ for(int i = 0; i < m; i++){
+ for(int j = 0; j < n; j++){
+ if(grid[i][j] == '*'){
+ int mine_count = 0;
+ if(i >= 1 && j >= 1 && grid[i - 1][j - 1] == 'x')
+ mine_count++;
+ if(i >= 1 && grid[i - 1][j] == 'x')
+ mine_count++;
+ if(i >= 1 && j < n - 1 && grid[i - 1][j + 1] == 'x')
+ mine_count++;
+ if(j >= 1 && grid[i][j - 1] == 'x')
+ mine_count++;
+ if(j < n - 1 && grid[i][j + 1] == 'x')
+ mine_count++;
+ if(i < m - 1 && j >= 1 && grid[i + 1][j - 1] == 'x')
+ mine_count++;
+ if(i < m - 1 && grid[i + 1][j] == 'x')
+ mine_count++;
+ if(i < m - 1 && j < n - 1 && grid[i + 1][j + 1] == 'x')
+ mine_count++;
+ char temp[1];
+ sprintf(temp, "%d", mine_count);
+ grid[i][j] = *temp;
+ }
+ }
+ }
+ std::cout << "Output:" << std::endl;
+ for(int i = 0; i < m; i++){
+ std::cout << "\t";
+ for(int j = 0; j < n; j++){
+ std::cout << grid[i][j] << " ";
+ }
+ std::cout << std::endl;
+ }
+}
+
+int main(int argc, char** argv){
+ srand(time(NULL));
+ int m = atoi(argv[1]);
+ int n = atoi(argv[2]);
+ MinesweeperGame mn;
+ mn.make_print_grid(m, n);
+}
+
diff --git a/challenge-126/adam-russell/perl/ch-1.pl b/challenge-126/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..808c6122e7
--- /dev/null
+++ b/challenge-126/adam-russell/perl/ch-1.pl
@@ -0,0 +1,37 @@
+use strict;
+use warnings;
+##
+# 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.
+##
+sub has_1{
+ my($x) = @_;
+ return 1 if $x =~ tr/1//d > 0;
+ return 0;
+}
+
+sub count_with_1{
+ my($n) = @_;
+ my $x = 1;
+ my $count = 0;
+ {
+ $count += has_1($x);
+ $x += 1;
+ redo if $x <= $n;
+ }
+ return $count;
+}
+
+sub count_without_1{
+ my($n) = @_;
+ return $n - count_with_1($n);
+}
+
+MAIN:{
+ my $N;
+ $N = 15;
+ print count_without_1($N) . "\n";
+ $N = 25;
+ print count_without_1($N) . "\n";
+}
diff --git a/challenge-126/adam-russell/perl/ch-2.pl b/challenge-126/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..1bb7d1b33b
--- /dev/null
+++ b/challenge-126/adam-russell/perl/ch-2.pl
@@ -0,0 +1,59 @@
+use strict;
+use warnings;
+##
+# You are given a rectangle with points
+# marked with either x or *. Consider
+# the x as a land mine. Write a script
+# to print a rectangle with numbers
+# and x as in the Minesweeper game.
+##
+sub initialize_grid{
+ my($m, $n) = @_;
+ my @grid;
+ for my $i (0 .. $m - 1){
+ for my $j (0 .. $n - 1){
+ $grid[$i][$j] = "*";
+ $grid[$i][$j] = "x" if rand() <= (1 / 3);
+ }
+ }
+ return @grid;
+}
+
+sub make_grid{
+ my($m, $n) = @_;
+ my @initial_grid = initialize_grid($m, $n);
+ my @grid = map {[@$_]} @initial_grid;
+ for my $i (0 .. $m - 1){
+ for my $j (0 .. $n - 1){
+ unless($grid[$i][$j] eq "x"){
+ my $mine_count = 0;
+ $mine_count++ if $i >= 1 && $j >= 1 && $grid[$i - 1][$j - 1] eq "x";
+ $mine_count++ if $i >= 1 && $grid[$i - 1][$j] eq "x";
+ $mine_count++ if $i >=1 && $j < $n - 1 && $grid[$i - 1][$j + 1] eq "x";
+ $mine_count++ if $j >= 1 && $grid[$i][$j - 1] eq "x";
+ $mine_count++ if $j < $n - 1 && $grid[$i][$j + 1] eq "x";
+ $mine_count++ if $i < $m - 1 && $j >= 1 && $grid[$i + 1][$j - 1] eq "x";
+ $mine_count++ if $i < $m - 1 && $grid[$i + 1][$j] eq "x" ;
+ $mine_count++ if $i < $m - 1 && $j < $n - 1 && $grid[$i + 1][$j + 1] eq "x";
+ $grid[$i][$j] = $mine_count;
+ }
+ }
+ }
+ return (\@initial_grid, \@grid);
+}
+
+sub print_grid{
+ my @grid = @_;
+ for my $row (@grid){
+ print "\t" . join(" ", @{$row}) . "\n"
+ }
+}
+
+MAIN:{
+ my($m, $n) = @ARGV;
+ my($initial_grid, $grid) = make_grid($m, $n);
+ print "Input:\n";
+ print_grid(@{$initial_grid});
+ print "Output:\n";
+ print_grid(@{$grid});
+}