diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-05 19:09:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-05 19:09:52 +0000 |
| commit | 3f5d5e30307e368e8ce61541cad4b940ea91b2b1 (patch) | |
| tree | 739f7f7e74afc0ed59ecf733427adecac1937633 | |
| parent | 49fc732634fff3eb699b27aa218bbfbe7177d90b (diff) | |
| parent | c16e9c1fc0b5b52bab73a19d41ed2c08a2827b26 (diff) | |
| download | perlweeklychallenge-club-3f5d5e30307e368e8ce61541cad4b940ea91b2b1.tar.gz perlweeklychallenge-club-3f5d5e30307e368e8ce61541cad4b940ea91b2b1.tar.bz2 perlweeklychallenge-club-3f5d5e30307e368e8ce61541cad4b940ea91b2b1.zip | |
Merge pull request #9527 from zapwai/branch-for-255
Week 255
| -rw-r--r-- | challenge-255/zapwai/c/ch-1.c | 37 | ||||
| -rw-r--r-- | challenge-255/zapwai/c/ch-2.c | 85 | ||||
| -rw-r--r-- | challenge-255/zapwai/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-255/zapwai/perl/ch-2.pl | 35 | ||||
| -rw-r--r-- | challenge-255/zapwai/rust/ch-1.rs | 40 | ||||
| -rw-r--r-- | challenge-255/zapwai/rust/ch-2.rs | 42 |
6 files changed, 275 insertions, 0 deletions
diff --git a/challenge-255/zapwai/c/ch-1.c b/challenge-255/zapwai/c/ch-1.c new file mode 100644 index 0000000000..474a4d5098 --- /dev/null +++ b/challenge-255/zapwai/c/ch-1.c @@ -0,0 +1,37 @@ +#include <stdio.h> +void proc(char*, char*, int, int); +int main() { + + char s[] = "Perl"; + char t[] = "Preel"; + proc(s, t, sizeof(s)/sizeof(char), sizeof(t)/sizeof(char)); + + char s2[] = "Weekly"; + char t2[] = "Weeakly"; + proc(s2, t2, sizeof(s2)/sizeof(char), sizeof(t2)/sizeof(char)); + + char s3[] = "Box"; + char t3[] = "Boxy"; + proc(s3, t3, sizeof(s3)/sizeof(char), sizeof(t3)/sizeof(char)); + +} + +void proc(char *s, char *t, int slen, int tlen) { + int fs[123] = {0}; /* entries 65 to 122 contain freqs of chars */ + int ft[123] = {0}; + + for (int i = 0; i < slen; i++) { + fs[s[i]]++; + } + for (int i = 0; i < tlen; i++) { + ft[t[i]]++; + } + + int ansnum; + for (int i = 0; i < 123; i++) { + if ( (ft[i] == 1) && (fs[i] == 0) ) { ansnum = i; break; } + else if ( ft[i] > fs[i] ) { ansnum = i; break; } + } + + printf("Input: s: %s t: %s\nOutput: %c\n", s, t, ansnum); +}
\ No newline at end of file diff --git a/challenge-255/zapwai/c/ch-2.c b/challenge-255/zapwai/c/ch-2.c new file mode 100644 index 0000000000..7a49b28886 --- /dev/null +++ b/challenge-255/zapwai/c/ch-2.c @@ -0,0 +1,85 @@ +#include <stdio.h> +const int M = 20; /* upper bound on number of words in the sentence */ +void proc(char *p, char *wor, int plen, int worlen) { + printf("Input: "); + printf("s: %s ", p); + printf("\n\tw: %s\n", wor); + /* w is a list of strings, were splitting on ' ' */ + char w[M][20]; + int wlen[M] = {0}; /* length of words in list */ + int skip[M] = {0}; /* entries to skip bc they match wor */ + int freq[M] = {0}; /* a cumulative count of each word */ + + int entry = 0; + int j = 0; + /* copy words into w, skip punctuation */ + for (int i = 0; i < plen; i++) { + if (p[i] == ' ') { + wlen[entry] = j; + entry++; + j = 0; + continue; + } else if ( (p[i] < 65) || (p[i] > 122) ) { + continue; + } else { + w[entry][j] = p[i]; + j++; + } + } + wlen[entry] = j; /* entry is now the number of words - 1 */ + /* find which words to skip bc they match wor */ + for (int i = 0; i <= entry; i++) { + int cnt = 0; + for (int k = 0; k < wlen[i]; k++) { + if (wor[k] == w[i][k]) { + cnt++; + } + } + if (cnt == worlen - 1) { + skip[i] = 1; + } + } + /* compute frequency of words , 0 for banned word */ + for (int i = 0; i <= entry; i++) { + if (skip[i] == 1) { continue; } + else { freq[i]++; } + + for (int lvl = 0; lvl < i; lvl++) { + int cnt = 0; + for (int k = 0; k < wlen[lvl]; k++) { + if (w[lvl][k] != w[i][k]) { goto nex;} + else {cnt++;} + } + if (cnt == wlen[i]) { + freq[i]++; + } + nex: + } + } + int max = 0; + int index = 0; + for (int i = 0; i <= entry; i++) { + if (max < freq[i]) { + max = freq[i]; + index = i; + } + } + printf("Output: "); + for (int k = 0; k < wlen[index]; k++) { + printf("%c", w[index][k]); + } + printf("\n\n"); +} + +int main() { + char p[] = "Joe hit a ball, the hit ball flew far after it was hit."; + char w[] = "hit"; + int plen = sizeof(p) / sizeof(char); + int wlen = sizeof(w) / sizeof(char); + char p2[] = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge."; + char w2[] = "the"; + int plen2 = sizeof(p2) / sizeof(char); + int wlen2 = sizeof(w2) / sizeof(char); + proc(p, w, plen, wlen); + proc(p2, w2, plen2, wlen2); +}
\ No newline at end of file diff --git a/challenge-255/zapwai/perl/ch-1.pl b/challenge-255/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..d46220b66b --- /dev/null +++ b/challenge-255/zapwai/perl/ch-1.pl @@ -0,0 +1,36 @@ +use v5.30; + +my $s0 = "Perl"; +my $t0 = "Preel"; + +my $s1 = "Weekly"; +my $t1 = "Weeakly"; + +my $s2 = "Box"; +my $t2 = "Boxy"; +my @L = ($s0, $t0, $s1, $t1, $s2, $t2); + +do { + my ($s, $t) = (shift @L, shift @L); + proc($s,$t); +} until (!@L); + +sub proc() { + my ($s, $t) = @_; + say "Input: \$s = $s, \$t = $t"; + my @S = split "", $s; + my @T = split "", $t; + my %Sfreq; + my %Tfreq; + for (@S) { + $Sfreq{$_}++; + } + for (@T) { + $Tfreq{$_}++; + } + for my $key (keys %Tfreq) { + if ((!$Sfreq{$key}) or ($Sfreq{$key} < $Tfreq{$key})) { + say "Output: $key"; + } + } +}
\ No newline at end of file diff --git a/challenge-255/zapwai/perl/ch-2.pl b/challenge-255/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..951aa28e9c --- /dev/null +++ b/challenge-255/zapwai/perl/ch-2.pl @@ -0,0 +1,35 @@ +use v5.30; + +my $p0 = "Joe hit a ball, the hit ball flew far after it was hit."; +my $w0 = "hit"; + +my $p1 = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge."; +my $w1 = "the"; + +my @L = ($p0, $w0, $p1, $w1); + +do { + my ($p, $w) = (shift @L, shift @L); + proc($p, $w); +} until (!@L); + +sub proc(){ + my ($p, $w) = @_; + say "Input: \$p = $p\n\t\$w = $w"; + $p =~ s/\.//g; + $p =~ s/,//g; + my @words = split " ", $p; + my %f; + foreach my $word (@words) { + next if ($word =~ /$w/); + $f{$word}++; + } + my ($ans, $max) = ("", 0); + foreach my $word (keys %f) { + if ($max < $f{$word}) { + $max = $f{$word}; + $ans = $word; + } + } + say "Output: $ans\n"; +}
\ No newline at end of file diff --git a/challenge-255/zapwai/rust/ch-1.rs b/challenge-255/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..1cefb4d7b6 --- /dev/null +++ b/challenge-255/zapwai/rust/ch-1.rs @@ -0,0 +1,40 @@ +use std::collections::HashMap; +fn main() { + let s = "Perl"; + let t = "Preel"; + proc(s,t); + + let s = "Weekly"; + let t = "Weeakly"; + proc(s,t); + + let s = "Box"; + let t = "Boxy"; + proc(s,t); +} + +fn proc(s : &str, t : &str) { + let mut fs : HashMap<char, i32> = HashMap::new(); + let mut ft : HashMap<char, i32> = HashMap::new(); + for c in s.chars() { + if !fs.contains_key(&c) { + fs.insert(c,1); + } else { + fs.insert(c, fs.get(&c).unwrap() + 1); + } + } + for c in t.chars() { + if !ft.contains_key(&c) { + ft.insert(c,1); + } else { + ft.insert(c, ft.get(&c).unwrap() + 1); + } + } + println!("Input: s = {s}, t = {t}"); + for k in ft.keys() { + if !fs.contains_key(&k) || fs.get(&k).unwrap() < ft.get(&k).unwrap() { + println!("Output: {k}"); + } + } + +}
\ No newline at end of file diff --git a/challenge-255/zapwai/rust/ch-2.rs b/challenge-255/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..e9a95a624c --- /dev/null +++ b/challenge-255/zapwai/rust/ch-2.rs @@ -0,0 +1,42 @@ +use std::collections::HashMap; +fn main() { + let p = "Joe hit a ball, the hit ball flew far after it was hit."; + let w = "hit"; + proc(p,w); + + let p = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge."; + let w = "the"; + proc(p,w); +} + +fn proc(p: &str, w: &str) { + let mut pt : String = String::from(""); + for c in p.chars() { + if c.is_alphabetic() || c.is_whitespace() { + let s = String::from(c); + pt.push_str(&s); + } + } + let words = pt.split(" "); + let mut f : HashMap<&str, i32> = HashMap::new(); + for word in words { + if word != w { + if !f.contains_key(word) { + f.insert(word, 1); + } else { + f.insert(word, f.get(&word).unwrap() + 1); + } + } + } + // println!("{:?}",f); + let mut max = 0; + let mut ans = String::new(); + for (k, v) in f { + if max < v { + max = v; + ans = k.to_string(); + } + } + println!("Input: w = {w}, p = {p}"); + println!("Output: {ans}"); +}
\ No newline at end of file |
