diff options
| author | Zapwai <zapwai@gmail.com> | 2024-01-22 11:55:43 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-22 11:55:43 -0500 |
| commit | 0ade75326d4c642b8230a166c233dccb1415d0c2 (patch) | |
| tree | e05de9daa63a0b0f543b0d8a6b1a00eb12f3ee42 | |
| parent | 9d7dc816f7775abfee7d90f0a2a969611902be54 (diff) | |
| download | perlweeklychallenge-club-0ade75326d4c642b8230a166c233dccb1415d0c2.tar.gz perlweeklychallenge-club-0ade75326d4c642b8230a166c233dccb1415d0c2.tar.bz2 perlweeklychallenge-club-0ade75326d4c642b8230a166c233dccb1415d0c2.zip | |
Week 253
| -rw-r--r-- | challenge-253/zapwai/c/ch-1.c | 26 | ||||
| -rw-r--r-- | challenge-253/zapwai/c/ch-2.c | 74 | ||||
| -rw-r--r-- | challenge-253/zapwai/perl/ch-1.pl | 10 | ||||
| -rw-r--r-- | challenge-253/zapwai/perl/ch-2.pl | 50 | ||||
| -rw-r--r-- | challenge-253/zapwai/rust/ch-1.rs | 21 | ||||
| -rw-r--r-- | challenge-253/zapwai/rust/ch-2.rs | 56 |
6 files changed, 237 insertions, 0 deletions
diff --git a/challenge-253/zapwai/c/ch-1.c b/challenge-253/zapwai/c/ch-1.c new file mode 100644 index 0000000000..60aae278e1 --- /dev/null +++ b/challenge-253/zapwai/c/ch-1.c @@ -0,0 +1,26 @@ +#include <stdio.h> +void proc (int L, int len, char s[L][len], char sep) { + printf("Input: s = "); + for (int i = 0; i < L; i++) { + printf("%s ", s[i]); + } + printf("\nOutput: "); + for (int i = 0; i < L; i++) { + for (int j = 0; j < len; j++) { + (s[i][j] == sep) ? printf(" ") : printf("%c", s[i][j]); + } + printf(" "); + } + printf("\n"); +} +int main () { + const int len = 30; + int L = 3; + char s[][len] = {"one.two.three", "four.five", "six"}; + char sep = '.'; + proc(L, len, s, sep); + int L2 = 2; + char s2[][len] = {"$perl$$", "$$raku$"}; + char sep2 = '$'; + proc(L2, len, s2, sep2); +}
\ No newline at end of file diff --git a/challenge-253/zapwai/c/ch-2.c b/challenge-253/zapwai/c/ch-2.c new file mode 100644 index 0000000000..bab65946c5 --- /dev/null +++ b/challenge-253/zapwai/c/ch-2.c @@ -0,0 +1,74 @@ +#include <stdio.h> +/* return 1 if rowi < rowj, 0 otherwise*/ +int rowcmp(int i, int j, int *r) { + if (r[i] < r[j]) { + return 1; + } else if (r[i] > r[j]) { + return 0; + } else { + return (i < j) ? 1 : 0; + } +} + +void proc(int M, int N, int m[M][N]) { + printf("Input: \n m = \n"); + for (int i = 0; i < M; i++) { + for (int j = 0; j < N; j++) { + printf("%d", m[i][j]); + } + printf("\n"); + } + int r[M]; /* find cnt of 1 in each row */ + for (int i = 0; i < M; i++) { + int cnt = 0; + for (int j = 0; j < N; j++) { + (m[i][j] == 1) ? cnt++ : 0; + } + r[i] = cnt; + } + /* sort using the row method in rowcmp */ + int ans[M]; + for (int i = 0; i < M; i++) { + ans[i] = i; + } + int cnt = 0; + do { + cnt = 0; + for (int i = 0; i < M - 1; i++) { + if (rowcmp(ans[i], ans[i+1], r) == 0) { + int x = ans[i]; + ans[i] = ans[i+1]; + ans[i+1] = x; + cnt++; + } + } + } while (cnt > 0); + printf("\nOutput: ("); + for (int i = 0; i < M - 1; i++) { + printf("%d, ", ans[i]); + } + printf("%d)\n", ans[M-1]); +} + +int main() { + const int M = 5; + const int N = 5; + int m[M][N] = { + {1, 1, 0, 0, 0}, + {1, 1, 1, 1, 0}, + {1, 0, 0, 0, 0}, + {1, 1, 0, 0, 0}, + {1, 1, 1, 1, 1} + }; + proc(M, N, m); + printf("\n"); + const int M2 = 4; + const int N2 = 4; + int m2[M][N] = { + {1, 0, 0, 0}, + {1, 1, 1, 0}, + {1, 0, 0, 0}, + {1, 0, 0, 0}, + }; + proc(M2, N2, m2); +}
\ No newline at end of file diff --git a/challenge-253/zapwai/perl/ch-1.pl b/challenge-253/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..d4ed67cb54 --- /dev/null +++ b/challenge-253/zapwai/perl/ch-1.pl @@ -0,0 +1,10 @@ +use v5.30; +my @words = ("one.two.three","four.five","six"); +my $separator = '.'; +#my @words = ('$perl$$', '$$raku$'); +#my $separator = '$'; +my $word = join(" ", @words); +my $s = "\\$separator"; +$word =~ s/$s/ /g; +my @word = split " ", $word; +say "@word";
\ No newline at end of file diff --git a/challenge-253/zapwai/perl/ch-2.pl b/challenge-253/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..ef7417bdd7 --- /dev/null +++ b/challenge-253/zapwai/perl/ch-2.pl @@ -0,0 +1,50 @@ +use v5.30; + +my @m = ( + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] +); + +# @m = ( +# [1, 0, 0, 0], +# [1, 1, 1, 1], +# [1, 0, 0, 0], +# [1, 0, 0, 0] +# ); + +my %freq; +foreach my $row (0 .. $#m) { + for my $val (@{$m[$row]}) { + $freq{$row}++ if ($val == 1); + } +} + +my @ind = (0 .. -1 + scalar keys %freq); + +my $cnt = 0; +do { + $cnt = 0; + for my $i (0 .. $#ind - 1) { + if (is_weaker($ind[$i],$ind[$i + 1])) { + $cnt++; + my $ind = $ind[$i]; + $ind[$i] = $ind[$i + 1]; + $ind[$i + 1] = $ind; + } + } +} while ($cnt); +say join(", ",reverse @ind); + +sub is_weaker { # return 1 if row i is less than row j + my ($i, $j) = @_; + if ($freq{$i} < $freq{$j}) { + return 1; + } elsif ($freq{$i} > $freq{$j}) { + return 0; + } else { + return ($i < $j) ? 1 : 0; + } +}
\ No newline at end of file diff --git a/challenge-253/zapwai/rust/ch-1.rs b/challenge-253/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..512a5519d7 --- /dev/null +++ b/challenge-253/zapwai/rust/ch-1.rs @@ -0,0 +1,21 @@ +fn main () { + // let words = ["$perl$$", "$$raku$"]; + // let sep = '$'; + + let words = ["one.two.three","four.five","six"]; + let sep = '.'; + + println!("Input: sep = '{sep}' words = {:?}", words); + print!("Output: "); + for w in words { + for c in w.chars() { + if c == sep { + print!(" "); + } else { + print!("{c}"); + } + } + print!(" "); + } + println!(""); +}
\ No newline at end of file diff --git a/challenge-253/zapwai/rust/ch-2.rs b/challenge-253/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..1166f0c0b6 --- /dev/null +++ b/challenge-253/zapwai/rust/ch-2.rs @@ -0,0 +1,56 @@ +fn main () { + // let m = [ + // [1, 0, 0, 0], + // [1, 1, 1, 1], + // [1, 0, 0, 0], + // [1, 0, 0, 0] + // ]; + + let m = [ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ]; + let mut r : Vec<i32> = Vec::new(); + println!("Input: \n m = "); + for i in 0 .. m.len() { + r.push(0); + println!(" {:?}", m[i]); + for d in m[i] { + if d == 1 { + r[i] += 1; + } + } + } + let mut ans : Vec<usize> = Vec::new(); + for i in 0 .. m.len() { + ans.push(i); + } + // sort ans array + let mut cnt = 1; + while cnt > 0 { + cnt = 0; + for i in 0 .. m.len()-1 { + if !rowcmp(ans[i],ans[i+1], &r) { + let x = ans[i]; + ans[i] = ans[i+1]; + ans[i+1] = x; + cnt += 1; + } + } + } + println!("Output: {:?}", ans); +} + +// return true if rowi < rowj, else false +fn rowcmp( i: usize, j :usize, r :&Vec<i32>) -> bool { + if r[i] < r[j] { + return true; + } else if r[i] > r[j] { + return false; + } else { + return if i < j {true} else {false}; + } +}
\ No newline at end of file |
