diff options
| -rw-r--r-- | challenge-276/zapwai/c/ch-1.c | 26 | ||||
| -rw-r--r-- | challenge-276/zapwai/c/ch-2.c | 56 | ||||
| -rw-r--r-- | challenge-276/zapwai/javascript/ch-1.js | 20 | ||||
| -rw-r--r-- | challenge-276/zapwai/javascript/ch-2.js | 37 | ||||
| -rw-r--r-- | challenge-276/zapwai/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-276/zapwai/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-276/zapwai/python/ch-1.py | 18 | ||||
| -rw-r--r-- | challenge-276/zapwai/python/ch-2.py | 36 | ||||
| -rw-r--r-- | challenge-276/zapwai/rust/ch-1.rs | 22 | ||||
| -rw-r--r-- | challenge-276/zapwai/rust/ch-2.rs | 41 |
10 files changed, 321 insertions, 0 deletions
diff --git a/challenge-276/zapwai/c/ch-1.c b/challenge-276/zapwai/c/ch-1.c new file mode 100644 index 0000000000..83990243d7 --- /dev/null +++ b/challenge-276/zapwai/c/ch-1.c @@ -0,0 +1,26 @@ +#include <stdio.h> + +void proc(int hours[], int hourslen) { + printf("Input: { "); + for (int i = 0; i < hourslen; i++) + printf("%d ", hours[i]); + printf("}\n"); + int tally = 0; + for (int i = 0; i < hourslen - 1; i++) { + for (int j = i + 1; j < hourslen; j++) { + int sum = hours[i] + hours[j]; + if (sum % 24 == 0) + tally++; + } + } + printf("Output: %d\n", tally); +} + +int main() { + int hours[] = {12, 12, 30, 24, 24}; + proc(hours, sizeof(hours)/sizeof(int)); + int hours2[] = {72, 48, 24, 5}; + proc(hours2, sizeof(hours2)/sizeof(int)); + int hours3[] = {12, 18, 24}; + proc(hours3, sizeof(hours3)/sizeof(int)); +} diff --git a/challenge-276/zapwai/c/ch-2.c b/challenge-276/zapwai/c/ch-2.c new file mode 100644 index 0000000000..ab34090a84 --- /dev/null +++ b/challenge-276/zapwai/c/ch-2.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#define MAX_LENGTH 20 + +void proc(int ints[], int intslen) { + printf("Input: { "); + for (int i = 0; i < intslen; i++) + printf("%d ", ints[i]); + printf("} \n"); + int nums[MAX_LENGTH] = {}; + int freq[MAX_LENGTH] = {}; + for (int i = 0; i < MAX_LENGTH; i++) + freq[i] = 1; + int numslen = 0; + for (int i = 0; i < intslen; i++) { + int item = ints[i]; + int have = 0; + for (int j = 0; j < MAX_LENGTH; j++) { + if (nums[j] == item) { + have++; + } + } + if (have == 0) { + nums[numslen++] = item; + } else { + freq[item] = have + 1; + } + } + int mostfreq[MAX_LENGTH]; + + int mostfreqlen = 0; + int f = 0; + for (int i = 0; i < MAX_LENGTH; i++) { + int num = nums[i]; + if (freq[num] > f) + f = freq[num]; + } + + for (int i = 0; i < numslen; i++) { + int num = nums[i]; + if (freq[num] == f) + mostfreq[mostfreqlen++] = num; + } + printf("Output: The most frequent, { "); + + for (int i = 0; i < mostfreqlen; i++) + printf("%d ", mostfreq[i]); + printf("}, have %d occurrences.\n", f); +} + +int main() { + int ints[] = {1, 2, 2, 4, 1, 5}; + proc(ints, sizeof(ints) / sizeof(int)); + int ints2[] = {1, 2, 3, 4, 5}; + proc(ints2, sizeof(ints2) / sizeof(int)); +} + diff --git a/challenge-276/zapwai/javascript/ch-1.js b/challenge-276/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..e660b4d66b --- /dev/null +++ b/challenge-276/zapwai/javascript/ch-1.js @@ -0,0 +1,20 @@ +let hours = [12, 12, 30, 24, 24]; +proc(hours); +hours = [72, 48, 24, 5]; +proc(hours); +hours = [12, 18, 24]; +proc(hours); + +function proc(hours) { + console.log("Input:", hours); + let tally = 0; + for (let i = 0; i < hours.length - 1; i++) { + for (let j = i + 1; j < hours.length; j++) { + let sum = hours[i] + hours[j]; + if (sum % 24 == 0) { + tally++; + } + } + } + console.log("Output:", tally); +} diff --git a/challenge-276/zapwai/javascript/ch-2.js b/challenge-276/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..58bcf04627 --- /dev/null +++ b/challenge-276/zapwai/javascript/ch-2.js @@ -0,0 +1,37 @@ +let MAX_LENGTH = 20; +let ints = [1, 2, 2, 4, 1, 5]; +proc(ints); +ints = [1, 2, 3, 4, 5]; +proc(ints); + +function proc(ints) { + console.log("Input:", ints); + let nums = []; + let freq = Array(MAX_LENGTH).fill(1); + for (let item of ints) { + let have = 0; + for (let j of nums) { + if (j == item) { + have++; + } + } + if (have == 0) { + nums.push(item); + } else { + freq[item] = have + 1; + } + } + let mostfreq = []; + let f = 0; + for (let num of nums) { + if (freq[num] > f) { + f = freq[num]; + } + } + for (let num of nums) { + if (freq[num] == f) { + mostfreq.push(num); + } + } + console.log("The most frequent is", mostfreq, "with", f, "occurrences."); +} diff --git a/challenge-276/zapwai/perl/ch-1.pl b/challenge-276/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..9a170b7da8 --- /dev/null +++ b/challenge-276/zapwai/perl/ch-1.pl @@ -0,0 +1,21 @@ +use v5.38; +my @hours = (12, 12, 30, 24, 24); +proc(@hours); +@hours = (72, 48, 24, 5); +proc(@hours); +@hours = (12, 18, 24); +proc(@hours); + +sub proc(@hours) { + say "Input: @hours"; + my $tally = 0; + for my $i (0 .. $#hours - 1) { + for my $j ($i + 1 .. $#hours) { + my $sum = $hours[$i] + $hours[$j]; + if ($sum % 24 == 0) { + $tally++; + } + } + } + say "Output: $tally"; +} diff --git a/challenge-276/zapwai/perl/ch-2.pl b/challenge-276/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..cf80969b1a --- /dev/null +++ b/challenge-276/zapwai/perl/ch-2.pl @@ -0,0 +1,44 @@ +use v5.38; +my $MAX_LENGTH = 20; +my @ints = (1, 2, 2, 4, 1, 5); +proc(@ints); +@ints = (1, 2, 3, 4, 5); +proc(@ints); + +sub proc(@ints) { + say "Input: @ints"; + my @nums; + my @freq = (1) x $MAX_LENGTH; + for my $item (@ints) { + my $have = 0; + for (@nums) { + if ($_ == $item) { + $have++; + } + } + if ($have == 0) { + push @nums, $item; + } else { + $freq[$item] = $have + 1; + } + } + say "Output:"; + foreach my $i (0 .. $#freq) { + if ($freq[$i] != 1) { + say "$i -> $freq[$i]"; + } + } + my @mostfreq; + my $f = 0; + foreach my $num (@nums) { + if ($freq[$num] > $f) { + $f = $freq[$num]; + } + } + foreach my $num (@nums) { + if ($freq[$num] == $f) { + push @mostfreq, $num; + } + } + say "The most frequent is {@mostfreq} with $f occurrences."; +} diff --git a/challenge-276/zapwai/python/ch-1.py b/challenge-276/zapwai/python/ch-1.py new file mode 100644 index 0000000000..6b6a82a4fb --- /dev/null +++ b/challenge-276/zapwai/python/ch-1.py @@ -0,0 +1,18 @@ +def proc(hours): + print("Input:", hours) + tally = 0 + for i in range(len(hours) - 1): + for j in range(i + 1, len(hours)): + mysum = hours[i] + hours[j] + if mysum % 24 == 0: + tally += 1 + print("Output:", tally) + + +hours = [12, 12, 30, 24, 24] +proc(hours) +hours = [72, 48, 24, 5] +proc(hours) +hours = [12, 18, 24] +proc(hours) + diff --git a/challenge-276/zapwai/python/ch-2.py b/challenge-276/zapwai/python/ch-2.py new file mode 100644 index 0000000000..983fba6636 --- /dev/null +++ b/challenge-276/zapwai/python/ch-2.py @@ -0,0 +1,36 @@ +def proc(ints): + print("Input:", ints) + nums = [] + freq = [1] * MAX_LENGTH + for item in ints: + have = 0 + for j in nums: + if j == item: + have += 1 + + if have == 0: + nums.append(item) + else: + freq[item] = have + 1 + print("Output:") + for i in range(len(freq)): + if freq[i] != 1: + print(i, "->", freq[i]) + mostfreq = [] + f = 0 + for num in nums: + if freq[num] > f: + f = freq[num] + for num in nums: + if freq[num] == f: + mostfreq.append(num) + + print("The most frequent is", mostfreq, "with", f, "occurrences.") + + +MAX_LENGTH = 20 +ints = [1, 2, 2, 4, 1, 5] +proc(ints) +ints = [1, 2, 3, 4, 5] +proc(ints) + diff --git a/challenge-276/zapwai/rust/ch-1.rs b/challenge-276/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..7c21720d0c --- /dev/null +++ b/challenge-276/zapwai/rust/ch-1.rs @@ -0,0 +1,22 @@ +fn main() { + let hours = vec![12, 12, 30, 24, 24]; + proc(hours); + let hours2 = vec![72, 48, 24, 5]; + proc(hours2); + let hours3 = vec![12, 18, 24]; + proc(hours3); +} + +fn proc(hours : Vec<i32>) { + println!( "Input: {:?}", hours); + let mut tally = 0; + for i in 0 .. hours.len() - 1 { + for j in i + 1 .. hours.len() { + let sum = hours[i] + hours[j]; + if sum % 24 == 0 { + tally += 1; + } + } + } + println!( "Output: {tally}" ); +} diff --git a/challenge-276/zapwai/rust/ch-2.rs b/challenge-276/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..96c2b46e4c --- /dev/null +++ b/challenge-276/zapwai/rust/ch-2.rs @@ -0,0 +1,41 @@ +fn main() { + let ints = vec![1, 2, 2, 4, 1, 5]; + proc(ints); + let ints2 = vec![1, 2, 3, 4, 5]; + proc(ints2); +} + +fn proc(ints : Vec<i32>) { + println!("Input: {:?}", ints); + let mut nums : Vec<i32> = Vec::new(); + let mut freq : Vec<i32> = Vec::new(); + for _i in 0 .. 20 { + freq.push(1); + } + for item in ints { + let mut have = 0; + for j in &nums { + if *j == item { + have += 1; + } + } + if have == 0 { + nums.push(item); + } else { + freq[item as usize] = have + 1; + } + } + let mut mostfreq :Vec<i32> = Vec::new(); + let mut f = 0; + for num in &nums { + if freq[*num as usize] > f { + f = freq[*num as usize]; + } + } + for num in &nums { + if freq[*num as usize] == f { + mostfreq.push(*num); + } + } + println!("Output: The most frequent is {:?} with {f} occurrences.", mostfreq); +} |
