diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-08 18:55:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-08 18:55:04 +0000 |
| commit | 427e60c67b782623123845b80474a8cb4da0ff2f (patch) | |
| tree | ce747689d49baeb18ab86c5e22933b07257c82f8 | |
| parent | 0a59813578c2df464a5978a5c3fb4f1919e2accc (diff) | |
| parent | 71232d40036d0663b1fb1f8b84f63823075f0b68 (diff) | |
| download | perlweeklychallenge-club-427e60c67b782623123845b80474a8cb4da0ff2f.tar.gz perlweeklychallenge-club-427e60c67b782623123845b80474a8cb4da0ff2f.tar.bz2 perlweeklychallenge-club-427e60c67b782623123845b80474a8cb4da0ff2f.zip | |
Merge pull request #9374 from zapwai/branch-for-251
Week 251
| -rw-r--r-- | challenge-251/zapwai/c/ch-1.c | 54 | ||||
| -rw-r--r-- | challenge-251/zapwai/c/ch-2.c | 77 | ||||
| -rw-r--r-- | challenge-251/zapwai/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-251/zapwai/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-251/zapwai/rust/ch-1.rs | 25 | ||||
| -rw-r--r-- | challenge-251/zapwai/rust/ch-2.rs | 57 |
6 files changed, 259 insertions, 0 deletions
diff --git a/challenge-251/zapwai/c/ch-1.c b/challenge-251/zapwai/c/ch-1.c new file mode 100644 index 0000000000..81c6bc263a --- /dev/null +++ b/challenge-251/zapwai/c/ch-1.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> +void proc(int*, int); +int main() { + int arr0[] = {6, 12, 25, 1}; + int arr1[] = {10, 7, 31, 5, 2, 2}; + int arr2[] = {1,2,10}; + int L[3] = {sizeof(arr0)/sizeof(int), + sizeof(arr1)/sizeof(int), + sizeof(arr2)/sizeof(int), + }; + printf("\n"); + proc(arr0, L[0]); + printf("\t-=-=-\n"); + proc(arr1, L[1]); + printf("\t-=-=-\n"); + proc(arr2, L[2]); + printf("\n"); +} + +void proc(int* arr, int L) { + int sum = 0; + int k = 0; + if (L % 2 == 1) { + k = L/2; + sum += arr[k]; + } else { + k = L/2 - 1; + } + + for (int i = 0; i <= k; i++) { + int a; + if (L % 2 == 1) { + if (i == k) { + break; + } + a = arr[k-i-1]; + } else { + a = arr[k-i]; + } + int b = arr[k+1+i]; + char x[20]; + sprintf(x, "%d%d", a,b); + int X = atoi(x); + // printf("%d\n",X); + sum += X; + } + printf("Input: arr = {"); + for (int i = 0; i < L - 1; i++) { + printf("%d, ",arr[i]); + } + printf("%d}\n",arr[L-1]); + printf("Output: %d\n",sum); +} diff --git a/challenge-251/zapwai/c/ch-2.c b/challenge-251/zapwai/c/ch-2.c new file mode 100644 index 0000000000..f605e9724c --- /dev/null +++ b/challenge-251/zapwai/c/ch-2.c @@ -0,0 +1,77 @@ +#include <stdio.h> +void proc(int m,int n, int[m][n]); + +void ex1() { + const int M = 3; + const int N = 3; + int a[M][N] = { { 3, 7, 8}, + { 9, 11, 13}, + {15, 16, 17} }; + proc(M,N,a); +} + +void ex2() { + const int M = 3; + const int N = 4; + int a[M][N] = { { 1, 10, 4, 2}, + { 9, 3, 8, 7}, + {15, 16, 17, 12} }; + proc(M,N,a); +} + +void ex3() { + const int M = 2; + const int N = 2; + int a[M][N] = { { 7, 8}, + {1, 2} }; + proc(M,N,a); +} + +int main() { + ex1(); + ex2(); + ex3(); +} + +void proc(int M, int N, int a[M][N]) { + int mins[M]; + int maxs[N]; + + for (int i = 0; i < M; i++) { + int min = a[i][0]; + for (int j = 0; j < N; j++) { + int x = a[i][j]; + (min > x) ? min = x : 0; + } + mins[i] = min; + } + + for (int j = 0; j < N; j++) { + int max = 0; + for (int i = 0; i < M; i++) { + int x = a[i][j]; + (max < x) ? max = x : 0; + + } + maxs[j] = max; + } + + int ans = -1; + for (int i = 0; i < M; i++) { + for (int j = 0; j < N; j++) { + if (mins[i] == maxs[j]) { + ans = mins[i]; + goto over; + } + } + } + over: + printf("Input: M = \n\t"); + for (int i = 0; i < M; i++) { + for (int j = 0; j < N; j++) { + printf("%02d ", a[i][j]); + } + printf("\n\t"); + } + printf("\nOutput: %d\n", ans); +} diff --git a/challenge-251/zapwai/perl/ch-1.pl b/challenge-251/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..17b06a9cb9 --- /dev/null +++ b/challenge-251/zapwai/perl/ch-1.pl @@ -0,0 +1,16 @@ +use v5.30; +my @ints = (6, 12, 25, 1); +say "Input: \@ints = (" . join(", ", @ints) . ")"; +my $sum = 0; +do { + proc(\@ints); +} while (@ints); +say "Output: $sum"; +sub proc { + my $r = shift; + my $a = shift @$r; + my $b; + $b = pop @$r if (@$r) ; + my $x = $a.$b; + $sum += $x; +} diff --git a/challenge-251/zapwai/perl/ch-2.pl b/challenge-251/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..e7cad53007 --- /dev/null +++ b/challenge-251/zapwai/perl/ch-2.pl @@ -0,0 +1,30 @@ +use v5.30; +use List::Util qw( max min ); +my $matrix = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17], + ]; +say "Input: \$matrix = ["; +say "\t[@$_]" foreach (@$matrix); +say "\t];"; +my $h = @$matrix; +my $w = scalar @{$$matrix[0]}; +my $luck = -1; +my @min; +for my $i (0 .. $h - 1) { + push @min, min (@{$$matrix[$i]}); +} +my @max; +for my $j (0 .. $w - 1) { + my @col = map { $$matrix[$_][$j] } (0 .. $h - 1) ; + push @max, max @col; +} +loop: for my $val (@min) { + for my $val2 (@max) { + if ($val == $val2) { + $luck = $val; + last loop; + } + } +} +say "Output: $luck"; diff --git a/challenge-251/zapwai/rust/ch-1.rs b/challenge-251/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..64d08e61ee --- /dev/null +++ b/challenge-251/zapwai/rust/ch-1.rs @@ -0,0 +1,25 @@ +fn main() { + let ints = vec![6, 12, 25, 1]; + let ints2 = vec![10, 7, 31, 5, 2, 2]; + let ints3 = vec![1, 2, 10]; + proc(ints); + proc(ints2); + proc(ints3); +} + +fn proc (mut ints : Vec<i32>) { + println!("Input: ints = {:?}", ints); + let mut sum = 0; + while ints.len() > 1 + { + let a = ints.remove(0); + let b = ints.pop().unwrap(); + let x = format!("{:?}{:?}",a,b); + sum += x.parse::<i32>().unwrap(); + + } + if ints.len() == 1 { + sum += ints[0]; + } + println!("Output: {sum}"); +} diff --git a/challenge-251/zapwai/rust/ch-2.rs b/challenge-251/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..94f1cdd15a --- /dev/null +++ b/challenge-251/zapwai/rust/ch-2.rs @@ -0,0 +1,57 @@ +fn main() { + let a = [ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]; + + // let a = [ [ 1, 10, 4, 2], + // [ 9, 3, 8, 7], + // [15, 16, 17, 12] ]; + + // let a = [ [7 ,8], + // [1 ,2] ]; + + let w :usize = a[0].len(); + let h :usize = a.len(); + // proc(w, h, a); + // } + + // fn proc(w: usize, h: usize, a: [[i32;3];3]) { + let mut mins = Vec::new(); + let mut maxs = Vec::new(); + + let mut j = 0; + while j <= h-1 { + mins.push(a[j].iter().min().unwrap()); + j += 1; + } + let mut i = 0; + while i <= w-1 { + let mut max = -1; + j = 0; + while j <= h-1 { + let x = a[j][i]; + if max < x { + max = x; + } + j += 1; + } + maxs.push(max); + i += 1; + } + + let mut luck = -1; + 'outer: for i in mins { + for j in &maxs { + if i == j { + luck = *j; + break 'outer; + } + } + } + println!("Input: "); + for i in 0 .. h { + + println!("\t{:?}", a[i]); + } + println!("Output: {luck}"); +} |
