aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-30 22:00:03 +0100
committerGitHub <noreply@github.com>2024-09-30 22:00:03 +0100
commit4fc5b1d10239d98f05d83c95f22ec6becd111cd5 (patch)
treeec444747cd7c8c4cb47cd11cd248e980eff6b805
parente4cdda8a1053c2bcd7d33bbad494158355fd0b3c (diff)
parentef99aabc641bef7ebe24b809e401babda7dfc5fe (diff)
downloadperlweeklychallenge-club-4fc5b1d10239d98f05d83c95f22ec6becd111cd5.tar.gz
perlweeklychallenge-club-4fc5b1d10239d98f05d83c95f22ec6becd111cd5.tar.bz2
perlweeklychallenge-club-4fc5b1d10239d98f05d83c95f22ec6becd111cd5.zip
Merge pull request #10935 from zapwai/branch-for-289
Week 289
-rw-r--r--challenge-289/zapwai/c/ch-1.c46
-rw-r--r--challenge-289/zapwai/c/ch-2.c72
-rw-r--r--challenge-289/zapwai/javascript/ch-1.js30
-rw-r--r--challenge-289/zapwai/javascript/ch-2.js33
-rw-r--r--challenge-289/zapwai/perl/ch-1.pl33
-rw-r--r--challenge-289/zapwai/perl/ch-2.pl27
-rw-r--r--challenge-289/zapwai/python/ch-1.py25
-rw-r--r--challenge-289/zapwai/python/ch-2.py28
-rw-r--r--challenge-289/zapwai/r/ch-1.r30
-rw-r--r--challenge-289/zapwai/r/ch-2.r35
-rw-r--r--challenge-289/zapwai/rust/ch-1.rs32
-rw-r--r--challenge-289/zapwai/rust/ch-2.rs38
12 files changed, 429 insertions, 0 deletions
diff --git a/challenge-289/zapwai/c/ch-1.c b/challenge-289/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..9b5f2d7bb6
--- /dev/null
+++ b/challenge-289/zapwai/c/ch-1.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+
+int max(int ints[], int intslen) {
+ int max = ints[0];
+ for (int i = 1; i < intslen; i++)
+ if (max < ints[i])
+ max = ints[i];
+ return max;
+}
+
+void proc(int ints[], int intslen) {
+ printf( "Input: { ");
+ for (int i = 0; i < intslen; i++)
+ printf("%d ", ints[i]);
+ printf("}\n");
+ int ans = max(ints, intslen);
+ int new1[intslen];
+ int new1len = 0;
+ for (int i = 0; i < intslen; i++)
+ if (ints[i] != ans) {
+ new1[new1len] = ints[i];
+ new1len++;
+ }
+ int new2[intslen];
+ int new2len = 0;
+ if (new1len > 0) {
+ int m2 = max(new1, new1len);
+ for (int i = 0; i < new1len; i++)
+ if (new1[i] != m2) {
+ new2[new2len] = new1[i];
+ new2len++;
+ }
+ if (new2len > 0)
+ ans = max(new2, new2len);
+ }
+ printf( "Output: %d\n", ans);
+}
+
+int main() {
+ int ints[] = {5, 6, 4, 1};
+ proc(ints, sizeof(ints) / sizeof(int));
+ int ints2[] = {4, 5};
+ proc(ints2, sizeof(ints2) / sizeof(int));
+ int ints3[] = {1, 2, 2, 3};
+ proc(ints3, sizeof(ints3) / sizeof(int));
+}
diff --git a/challenge-289/zapwai/c/ch-2.c b/challenge-289/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..b952cc9891
--- /dev/null
+++ b/challenge-289/zapwai/c/ch-2.c
@@ -0,0 +1,72 @@
+#include <string.h>
+#include <stdio.h>
+#include <time.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#define NUMWORDS 1000
+#define WORDLEN 50
+
+char* jumble(char* word) {
+ if (strlen(word) < 4)
+ return word;
+ int start = word[0];
+ int end = word[strlen(word) - 1];
+ int letlen = strlen(word) - 2;
+ char let[50];
+ for (int i = 0; i < letlen; i++) {
+ let[i] = word[i+1];
+ }
+ int order[WORDLEN];
+ int orderlen = 0;
+ while (orderlen < letlen) {
+ int x = rand() % (letlen);
+ bool got = false;
+ for (int i = 0; i < orderlen; i++)
+ if (order[i] == x)
+ got = true;
+ if (!got)
+ order[orderlen++] = x;
+ }
+ int midlen = 0;
+ char middle[WORDLEN] = {};
+ for (int i = 0; i < orderlen; i++)
+ middle[midlen++] = let[order[i]];
+ char* q = malloc(strlen(word) + 1);
+ sprintf(q, "%c%s%c", start, middle, end);
+ return q;
+}
+
+void proc(char s[]) {
+ printf( "Input: %s\n", s);
+ int wordslen = 0;
+ char* words[NUMWORDS];
+ char *p = strtok(s, " ");
+
+ while(p != NULL) {
+ words[wordslen] = malloc(WORDLEN);
+ strcpy(words[wordslen++], p);
+ p = strtok(NULL, " ");
+ }
+
+ int newlistlen = 0;
+ char* newlist[NUMWORDS];
+
+ for (int i = 0; i < wordslen; i++) {
+ newlist[newlistlen] = malloc(WORDLEN);
+ strcpy(newlist[newlistlen++], jumble(words[i]));
+ free(words[i]);
+ }
+
+ printf("Output: ");
+ for (int i = 0; i < newlistlen; i++) {
+ printf("%s ", newlist[i]);
+ free(newlist[i]);
+ }
+ printf("\n");
+}
+
+int main() {
+ srand(time(NULL));
+ char s[] = "This supposed Cambridge research is unfortunately an urban legend";
+ proc(s);
+}
diff --git a/challenge-289/zapwai/javascript/ch-1.js b/challenge-289/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..2238afc3d9
--- /dev/null
+++ b/challenge-289/zapwai/javascript/ch-1.js
@@ -0,0 +1,30 @@
+let ints = [5, 6, 4, 1];
+proc(ints);
+ints = [4, 5];
+proc(ints);
+ints = [1, 2, 2, 3];
+proc(ints);
+
+function proc(ints) {
+ console.log( "Input:", ints);
+ let ans = Math.max(...ints);
+ let new1 = [];
+ for (let i = 0; i < ints.length; i++) {
+ if (ints[i] != ans) {
+ new1.push(ints[i]);
+ }
+ }
+ if (new1.length > 0) {
+ let m2 = Math.max(...new1);
+ let new2 = [];
+ for (let i = 0; i < new1.length; i++) {
+ if (new1[i] != m2) {
+ new2.push(new1[i]);
+ }
+ }
+ if (new2.length > 0) {
+ ans = Math.max(...new2);
+ }
+ }
+ console.log( "Output:", ans);
+}
diff --git a/challenge-289/zapwai/javascript/ch-2.js b/challenge-289/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..c6e50f31ca
--- /dev/null
+++ b/challenge-289/zapwai/javascript/ch-2.js
@@ -0,0 +1,33 @@
+let s = "This supposed Cambridge research is unfortunately an urban legend";
+proc(s);
+
+function jumble(word) {
+ if (word.length < 4) {
+ return word;
+ }
+ let Let = word.split("");
+ let start = Let[0];
+ let end = Let[Let.length - 1];
+ Let.pop();
+ Let.shift();
+ let order = [];
+ while (order.length < Let.length) {
+ let x = Math.floor(Math.random() * Let.length);
+ if (!order.includes(x)) {
+ order.push(x);
+ }
+ }
+ let middle = "";
+ for (let o of order) {
+ middle += Let[o];
+ }
+ let q = start + middle + end;
+ return q;
+}
+
+function proc(s) {
+ console.log( "Input:", s);
+ let words = s.split(" ");
+ let newlist = (words.map(x => jumble(x)));
+ console.log( "Output:", newlist.join(' ') );
+}
diff --git a/challenge-289/zapwai/perl/ch-1.pl b/challenge-289/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..a2b319f19d
--- /dev/null
+++ b/challenge-289/zapwai/perl/ch-1.pl
@@ -0,0 +1,33 @@
+use v5.38;
+use List::Util qw( max );
+
+my @ints = (5, 6, 4, 1);
+proc(@ints);
+@ints = (4, 5);
+proc(@ints);
+@ints = (1, 2, 2, 3);
+proc(@ints);
+
+sub proc(@ints) {
+ say "Input: @ints";
+ my $ans = max(@ints);
+ my @new1;
+ for my $i (0 .. $#ints) {
+ unless ($ints[$i] == $ans) {
+ push @new1, $ints[$i];
+ }
+ }
+ if (@new1 > 0) {
+ my $m2 = max(@new1);
+ my @new2;
+ for my $i (0 .. $#new1) {
+ unless ($new1[$i] == $m2) {
+ push @new2, $new1[$i];
+ }
+ }
+ if (@new2 > 0) {
+ $ans = max(@new2);
+ }
+ }
+ say "Output: $ans";
+}
diff --git a/challenge-289/zapwai/perl/ch-2.pl b/challenge-289/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..6838cc03eb
--- /dev/null
+++ b/challenge-289/zapwai/perl/ch-2.pl
@@ -0,0 +1,27 @@
+use v5.38;
+my $s = "This supposed Cambridge research is unfortunately an urban legend";
+proc($s);
+sub jumble($word) {
+ return $word if (length $word < 4);
+ my @let = split "", $word;
+ my $start = $let[0];
+ my $end = $let[$#let];
+ pop @let;
+ shift @let;
+ my @order;
+ while (@order < @let) {
+ my $x = int rand @let;
+ push @order, $x unless (grep {$_ == $x} (@order));
+ }
+ my $middle;
+ $middle .= $let[$_] for (@order);
+ my $q = $start . $middle . $end;
+ return $q;
+}
+
+sub proc($s) {
+ say "Input: $s";
+ my @words = split " ", $s;
+ my @new = map {jumble($_)} (@words);
+ say "Output: @new";
+}
diff --git a/challenge-289/zapwai/python/ch-1.py b/challenge-289/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..8b520005c1
--- /dev/null
+++ b/challenge-289/zapwai/python/ch-1.py
@@ -0,0 +1,25 @@
+def proc(ints) :
+ print( "Input:", ints)
+ ans = max(ints)
+ new1 = []
+ for i in range(len(ints)):
+ if ints[i] != ans:
+ new1.append(ints[i])
+ if len(new1) > 0 :
+ m2 = max(new1)
+ new2 = []
+ for i in range(len(new1)):
+ if new1[i] != m2:
+ new2.append(new1[i])
+
+ if len(new2) > 0:
+ ans = max(new2)
+ print( "Output:", ans)
+
+ints = [5, 6, 4, 1]
+proc(ints)
+ints = [4, 5]
+proc(ints)
+ints = [1, 2, 2, 3]
+proc(ints)
+
diff --git a/challenge-289/zapwai/python/ch-2.py b/challenge-289/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..f5bd6b88fc
--- /dev/null
+++ b/challenge-289/zapwai/python/ch-2.py
@@ -0,0 +1,28 @@
+import random
+
+def jumble(word) :
+ if len(word) < 4:
+ return word
+ let = list(word)
+ start = let[0]
+ end = let[len(let) - 1]
+ let = let[1:-1]
+ order = []
+ while (len(order) < len(let)) :
+ x = random.randint(0, len(let) - 1)
+ if x not in order:
+ order.append(x)
+ middle = ""
+ for i in order:
+ middle += let[i]
+ q = start + middle + end
+ return q
+
+def proc(s) :
+ print( "Input:", s)
+ words = s.split(" ")
+ new = ' '.join(map(jumble, words))
+ print( "Output:", new)
+
+s = "This supposed Cambridge research is unfortunately an urban legend"
+proc(s)
diff --git a/challenge-289/zapwai/r/ch-1.r b/challenge-289/zapwai/r/ch-1.r
new file mode 100644
index 0000000000..b06695b1a9
--- /dev/null
+++ b/challenge-289/zapwai/r/ch-1.r
@@ -0,0 +1,30 @@
+proc <-function(ints) {
+ cat( "Input:", ints, "\n" )
+ ans <- max(ints)
+ new1 <- c()
+ for (i in 1:length(ints)) {
+ if (ints[i] != ans) {
+ new1 <- append(new1, ints[i])
+ }
+ }
+ if (length(new1) > 0) {
+ m2 <- max(new1)
+ new2 <- c()
+ for (i in 1:length(new1)) {
+ if (new1[i] != m2) {
+ new2 <- append(new2, new1[i])
+ }
+ }
+ if (length(new2) > 0) {
+ ans <- max(new2)
+ }
+ }
+ cat( "Output:", ans, "\n" )
+}
+
+ints <- c(5, 6, 4, 1)
+proc(ints)
+ints <- c(4, 5)
+proc(ints)
+ints <- c(1, 2, 2, 3)
+proc(ints)
diff --git a/challenge-289/zapwai/r/ch-2.r b/challenge-289/zapwai/r/ch-2.r
new file mode 100644
index 0000000000..99a5e462c9
--- /dev/null
+++ b/challenge-289/zapwai/r/ch-2.r
@@ -0,0 +1,35 @@
+jumble <-function(word) {
+ if (nchar(word) < 4) {
+ return(word)
+ }
+ let <- strsplit(word, "")[[1]]
+ start <- let[1]
+ end <- let[length(let)]
+ let <- let[-length(let)]
+ let <- let[-1]
+ order <- c()
+ while (length(order) < length(let)) {
+ x <- sample(1:length(let), 1)
+ if (!x %in% order)
+ order <- append(order, x)
+ }
+ middle <- ""
+ for (o in order) {
+ middle <- paste(middle, let[o], sep = '')
+ }
+ q <- paste(start, middle, end, sep = '')
+ return(q)
+}
+
+proc <-function(s) {
+ cat( "Input:", s, "\n")
+ words <- strsplit(s, " ")[[1]]
+ new <- ""
+ for (o in words) {
+ new <- paste(new, jumble(o), sep = ' ')
+ }
+ cat( "Output:", new, "\n")
+}
+
+s <- "This supposed Cambridge research is unfortunately an urban legend"
+proc(s)
diff --git a/challenge-289/zapwai/rust/ch-1.rs b/challenge-289/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..e1177bee3a
--- /dev/null
+++ b/challenge-289/zapwai/rust/ch-1.rs
@@ -0,0 +1,32 @@
+fn main() {
+ let ints = vec![5, 6, 4, 1];
+ proc(ints);
+ let ints2 = vec![4, 5];
+ proc(ints2);
+ let ints3 = vec![1, 2, 2, 3];
+ proc(ints3);
+}
+
+fn proc(ints : Vec<i32>) {
+ println!("Input: {:?}", ints);
+ let mut ans = ints.iter().max().unwrap();
+ let mut new1 : Vec<i32> = Vec::new();
+ for i in 0 .. ints.len() {
+ if ints[i] != *ans {
+ new1.push(ints[i]);
+ }
+ }
+ let mut new2 : Vec<i32> = Vec::new();
+ if new1.len() > 0 {
+ let m2 = new1.iter().max().unwrap();
+ for i in 0 .. new1.len() {
+ if new1[i] != *m2 {
+ new2.push(new1[i]);
+ }
+ }
+ if new2.len() > 0 {
+ ans = new2.iter().max().unwrap();
+ }
+ }
+ println!( "Output: {ans}");
+}
diff --git a/challenge-289/zapwai/rust/ch-2.rs b/challenge-289/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..a36ce915ba
--- /dev/null
+++ b/challenge-289/zapwai/rust/ch-2.rs
@@ -0,0 +1,38 @@
+use rand::Rng;
+
+fn main() {
+ let s = "This supposed Cambridge research is unfortunately an urban legend";
+ proc(s);
+}
+
+fn jumble(word : &str) -> String {
+ let mut rng = rand::thread_rng();
+ if word.len() < 4 {
+ return word.to_string();
+ }
+ let mut Let :Vec<char> = word.chars().collect();
+ let start = Let.remove(0);
+ let end = Let.pop().unwrap();
+ let mut order : Vec<usize> = Vec::new();
+ while order.len() < Let.len() {
+ let x : usize = rng.gen_range(0 .. Let.len());
+ if !order.contains(&x) {
+ order.push(x);
+ }
+ }
+ let mut middle = String::new();
+ for o in order {
+ middle.push(Let[o]);
+ }
+ format!("{}{}{}", start, middle, end)
+}
+
+fn proc(s : &str) {
+ println!( "Input: {s}");
+ let words :Vec<&str> = s.split(" ").collect();
+ let mut ans : Vec<String> = Vec::new();
+ for w in words {
+ ans.push(jumble(w));
+ }
+ println!( "Output: {}", ans.join(" "));
+}