diff options
Diffstat (limited to 'challenge-274')
| -rw-r--r-- | challenge-274/zapwai/c/ch-1.c | 60 | ||||
| -rw-r--r-- | challenge-274/zapwai/c/ch-2.c | 32 | ||||
| -rw-r--r-- | challenge-274/zapwai/javascript/ch-1.js | 32 | ||||
| -rw-r--r-- | challenge-274/zapwai/javascript/ch-2.js | 24 | ||||
| -rw-r--r-- | challenge-274/zapwai/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-274/zapwai/perl/ch-2.pl | 36 | ||||
| -rw-r--r-- | challenge-274/zapwai/python/ch-1.py | 30 | ||||
| -rw-r--r-- | challenge-274/zapwai/python/ch-2.py | 32 | ||||
| -rw-r--r-- | challenge-274/zapwai/rust/ch-1.rs | 34 | ||||
| -rw-r--r-- | challenge-274/zapwai/rust/ch-2.rs | 27 |
10 files changed, 334 insertions, 0 deletions
diff --git a/challenge-274/zapwai/c/ch-1.c b/challenge-274/zapwai/c/ch-1.c new file mode 100644 index 0000000000..fa4193c85b --- /dev/null +++ b/challenge-274/zapwai/c/ch-1.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include <string.h> +#include <stdbool.h> +#include <stdlib.h> +#define MAX 30 + +bool isVowel(char c) { + return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' + || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); +} + +void proc(char sent[]) { + printf("Input: %s\n", sent); + char* words[MAX]; + char* token = strtok(sent, " "); + int wordslen = 0; + while (token != NULL) { + words[wordslen] = malloc(MAX); + strcpy(words[wordslen++], token); + token = strtok(NULL, " "); + } + char* output = malloc(MAX*sizeof(char)); + for (int i = 0; i < wordslen; i++) { + char first_let = words[i][0]; + char reword[MAX] = {}; + int J = strlen(words[i]); + for (int j = 1; j < J; j++) { + reword[j-1] = words[i][j]; + } + reword[J - 1] = first_let; + reword[J] = '\0'; + char A[MAX] = {}; + for (int j = 0; j <= i; j++) + strcat(A, "a"); + if (isVowel(first_let)) { + strcat(output, words[i]); + strcat(output, "ma"); + strcat(output, A); + strcat(output, " "); + } else { + strcat(output, reword); + strcat(output, "ma"); + strcat(output, A); + strcat(output, " "); + } + } + printf("Output: %s\n", output); + free(output); + for (int i = 0; i < wordslen; i++) + free(words[i]); +} + +int main() { + char sent[] = "I love Perl"; + proc(sent); + char sent2[] = "Perl and Raku are friends"; + proc(sent2); + char sent3[] = "The Weekly Challenge"; + proc(sent3); +} diff --git a/challenge-274/zapwai/c/ch-2.c b/challenge-274/zapwai/c/ch-2.c new file mode 100644 index 0000000000..99ab7481f9 --- /dev/null +++ b/challenge-274/zapwai/c/ch-2.c @@ -0,0 +1,32 @@ +#include <stdio.h> + +int delta(int t, int* r, int rlen) { + for (int i = 0; i < rlen; i++) { + if (r[i] < t) + continue; + return r[i] - t; + } + return 0; +} + +int main() { + int r1[] = {11, 23, 35, 47, 59}; + int r2[] = {5, 20, 35, 50, 65}; + int length_one = 41; + int length_two = 35; + int outlen = 0; + int out[60] = {}; + for (int t = 1; t < 59; t++) { + int delta_one = delta(t, r1, sizeof(r1) / sizeof(int)); + int delta_two = delta(t, r2, sizeof(r2) / sizeof(int)); + int time_taken_one = length_one + delta_one; + int time_taken_two = length_two + delta_two; + if ((delta_one < delta_two) && (time_taken_one > time_taken_two)) { + out[outlen] = t; + outlen++; + } + } + for (int i = 0; i < outlen; i++) + printf("%d ", out[i]); + printf("\n"); +} diff --git a/challenge-274/zapwai/javascript/ch-1.js b/challenge-274/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..6d39695393 --- /dev/null +++ b/challenge-274/zapwai/javascript/ch-1.js @@ -0,0 +1,32 @@ +let sent = "I love Perl"; +proc(sent); +sent = "Perl and Raku are friends"; +proc(sent); +sent = "The Weekly Challenge"; +proc(sent); + +function isVowel(c) { + let d = c.toLowerCase() + return (d == 'a' || d == 'e' || d == 'i' || d == 'o' || d == 'u') +} + +function proc(sent) { + console.log("Input:", sent); + let words = sent.split(" "); + for (let i = 0; i < words.length; i++) { + let A = ""; + for (let j = 0; j < i; j++) { + A += "a"; + } + let lets = words[i].split(""); + let first_let = lets[0]; + lets.shift(); + let reword = lets.join(''); + reword += first_let; + if (isVowel(first_let)) { + console.log("Output", words[i]+"ma"+A+" "); + } else { + console.log("Output", reword+"ma"+A+" "); + } + } +} diff --git a/challenge-274/zapwai/javascript/ch-2.js b/challenge-274/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..30c1c76dd2 --- /dev/null +++ b/challenge-274/zapwai/javascript/ch-2.js @@ -0,0 +1,24 @@ +let r1 = [11, 23, 35, 47, 59]; +let r2 = [5, 20, 35, 50, 65]; +let length_one = 41; +let length_two = 35; +let out = ""; +for (let t = 1; t < 59; t++) { + let delta_one = delta(t, r1); + let delta_two = delta(t, r2); + let time_taken_one = length_one + delta_one; + let time_taken_two = length_two + delta_two; + if ((delta_one <= delta_two) && (time_taken_one > time_taken_two)) { + out += t+" "; + } +} +console.log(out); + +function delta(t, ran) { + for (let r of ran) { + if (r < t) { + continue; + } + return r - t; + } +} diff --git a/challenge-274/zapwai/perl/ch-1.pl b/challenge-274/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..915833c638 --- /dev/null +++ b/challenge-274/zapwai/perl/ch-1.pl @@ -0,0 +1,27 @@ +use v5.38; +my $sent = "I love Perl"; +proc($sent); +$sent = "Perl and Raku are friends"; +proc($sent); +$sent = "The Weekly Challenge"; +proc($sent); + +sub proc($sent) { + say "Input: $sent"; + print "Output: "; + my @words = split(" ", $sent); + for my $i (0 .. $#words) { + my $A = "a" x ($i + 1); + my @lets = split("", $words[$i]); + my $first_let = $lets[0]; + shift @lets; + my $reword = join "", @lets; + $reword .= $first_let; + if ($first_let =~ /[aeiouAEIOU]/) { + print $words[$i]."ma".$A." "; + } else { + print $reword."ma".$A." "; + } + } + print "\n"; +} diff --git a/challenge-274/zapwai/perl/ch-2.pl b/challenge-274/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..91d0aad60c --- /dev/null +++ b/challenge-274/zapwai/perl/ch-2.pl @@ -0,0 +1,36 @@ +use v5.38; +my @inp = ( [12, 11, 41], [15, 5, 35] ); +my $MAX = 60; +my @R; +my @cost; +for my $l (@inp) { + my $interval = $$l[0]; + my $start = $$l[1]; + push @cost, $$l[2]; + my @L; + for my $i (0 .. $MAX/$$l[0]) { + my $x = $start + $i*$interval; + push @L, $x; + } + push @R, \@L; +} +my @r1 = @{$R[0]}; +my @r2 = @{$R[1]}; +my $length_one = $cost[0]; +my $length_two = $cost[1]; +my $out; +for my $t (1 .. 59) { + my $delta_one = delta($t, @r1); + my $delta_two = delta($t, @r2); + my $time_taken_one = $length_one + $delta_one; + my $time_taken_two = $length_two + $delta_two; + $out .= "$t, " if (($delta_one < $delta_two) && ($time_taken_one > $time_taken_two)); +} +say substr $out, 0, -2; + +sub delta($t, @r) { + foreach my $r (@r) { + next if ($r < $t); + return $r - $t; + } +} diff --git a/challenge-274/zapwai/python/ch-1.py b/challenge-274/zapwai/python/ch-1.py new file mode 100644 index 0000000000..b1dcb7a1d2 --- /dev/null +++ b/challenge-274/zapwai/python/ch-1.py @@ -0,0 +1,30 @@ +def is_vowel(c): + return c.lower() in 'aeiou' + +def proc(sent): + print("Input: ", sent) + print("Output: ", end='') + words = sent.split(" "); + for i in range(len(words)): + A = "" + for j in range(i+1): + A += "a" + lets = list(words[i]) + first_let = lets[0] + reword = "" + for k in range(1,len(lets)): + reword += lets[k] + reword += first_let + if is_vowel(first_let): + print(words[i]+"ma"+A+" ", end='') + else: + print(reword+"ma"+A+" ", end='') + print() + +sent = "I love Perl" +proc(sent) +sent = "Perl and Raku are friends" +proc(sent) +sent = "The Weekly Challenge" +proc(sent) + diff --git a/challenge-274/zapwai/python/ch-2.py b/challenge-274/zapwai/python/ch-2.py new file mode 100644 index 0000000000..cef432da13 --- /dev/null +++ b/challenge-274/zapwai/python/ch-2.py @@ -0,0 +1,32 @@ +def delta(t, ran): + for r in (ran): + if r < t: + continue + return r - t + return 0 +inp = [ [12, 11, 41], [15, 5, 35] ] +MAX = 60 +R = [] +cost = [] +for l in inp: + interval = l[0] + start = l[1] + cost.append(l[2]) + L = [] + for i in range(int(MAX/l[0])): + x = start + i*interval + L.append(x) + R.append(L) +r1 = R[0] +r2 = R[1] +length_one = cost[0] +length_two = cost[1] +out = "" +for t in range(1, MAX-1): + delta_one = delta(t, r1) + delta_two = delta(t, r2) + time_taken_one = length_one + delta_one + time_taken_two = length_two + delta_two + if (delta_one < delta_two) and (time_taken_one > time_taken_two): + out += str(t)+" " +print(out) diff --git a/challenge-274/zapwai/rust/ch-1.rs b/challenge-274/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..8fa0214662 --- /dev/null +++ b/challenge-274/zapwai/rust/ch-1.rs @@ -0,0 +1,34 @@ +fn main() { + let sent = "I love Perl"; + proc(sent); + let sent2 = "Perl and Raku are friends"; + proc(sent2); + let sent3 = "The Weekly Challenge"; + proc(sent3); +} + +fn proc(sent : &str) { + println!("Input: {sent}"); + print!("Output: "); + let words : Vec<&str> = sent.split(" ").collect(); + for i in 0 .. words.len() { + let lets : Vec<char> = words[i].chars().collect(); + let first_let = lets[0]; + let reword = &words[i][1..]; + let mut A = "".to_string(); + for _j in 0 .. i+1 { + A.push_str("a"); + } + if is_vowel(first_let) { + print!("{}ma{A} ", words[i]); + } else { + print!("{reword}{first_let}ma{A} "); + } + } + print!("\n"); +} + +fn is_vowel(c : char) -> bool { + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' + || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'; +} diff --git a/challenge-274/zapwai/rust/ch-2.rs b/challenge-274/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..251a5da2db --- /dev/null +++ b/challenge-274/zapwai/rust/ch-2.rs @@ -0,0 +1,27 @@ +fn main() { + let r1 = vec![11, 23, 35, 47, 59]; + let r2 = vec![5, 20, 35, 50, 65]; + let length_one = 41; + let length_two = 35; + let mut out : Vec<i32> = Vec::new(); + for t in 1 .. 60 { + let delta_one = delta(t, &r1); + let delta_two = delta(t, &r2); + let time_taken_one = length_one + delta_one; + let time_taken_two = length_two + delta_two; + if (delta_one < delta_two) && (time_taken_one > time_taken_two) { + out.push(t); + } + } + println!("{:?}", out); +} + +fn delta(t : i32, ran : &Vec<i32>) -> i32 { + for r in ran { + if r < &t { + continue; + } + return r - t; + } + return 0; +} |
