aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-16 20:22:45 +0100
committerGitHub <noreply@github.com>2024-09-16 20:22:45 +0100
commit6ba96d45227883a88bec4e91870d341e097ca8d1 (patch)
tree77e38d414aaea777fa5f89cdd1c3837c8f759ad6
parent7211ba649cbe1119acfa16d7fa942f70e3b2759d (diff)
parent3e3e9530a265cb3f549a3556bb78ffdcb1023650 (diff)
downloadperlweeklychallenge-club-6ba96d45227883a88bec4e91870d341e097ca8d1.tar.gz
perlweeklychallenge-club-6ba96d45227883a88bec4e91870d341e097ca8d1.tar.bz2
perlweeklychallenge-club-6ba96d45227883a88bec4e91870d341e097ca8d1.zip
Merge pull request #10852 from zapwai/branch-for-287
Week 287
-rw-r--r--challenge-287/zapwai/c/ch-1.c87
-rw-r--r--challenge-287/zapwai/c/ch-2.c94
-rw-r--r--challenge-287/zapwai/javascript/ch-1.js63
-rw-r--r--challenge-287/zapwai/javascript/ch-2.js39
-rw-r--r--challenge-287/zapwai/perl/ch-1.pl64
-rw-r--r--challenge-287/zapwai/perl/ch-2.pl37
-rw-r--r--challenge-287/zapwai/python/ch-1.py49
-rw-r--r--challenge-287/zapwai/python/ch-2.py35
-rw-r--r--challenge-287/zapwai/r/ch-1.r61
-rw-r--r--challenge-287/zapwai/r/ch-2.r35
-rw-r--r--challenge-287/zapwai/rust/ch-1.rs86
-rw-r--r--challenge-287/zapwai/rust/ch-2.rs43
12 files changed, 693 insertions, 0 deletions
diff --git a/challenge-287/zapwai/c/ch-1.c b/challenge-287/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..8e4805fa98
--- /dev/null
+++ b/challenge-287/zapwai/c/ch-1.c
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdbool.h>
+
+bool has_lower(char* str) {
+ for (int i = 0; i < strlen(str); i++)
+ if (islower(str[i]))
+ return true;
+ return false;
+}
+bool has_upper(char* str) {
+ for (int i = 0; i < strlen(str); i++)
+ if (isupper(str[i]))
+ return true;
+ return false;
+}
+bool has_digit(char* str) {
+ for (int i = 0; i < strlen(str); i++)
+ if (isdigit(str[i]))
+ return true;
+ return false;
+}
+void proc(char* str) {
+ printf( "Input: %s\n", str);
+ int len = strlen(str);
+ int len_diff = 0;
+ if (len < 6) {
+ len_diff = 6 - len;
+ } else if (len > 20) {
+ len_diff = len - 20;
+ }
+
+ char* l = str;
+ int lengths[50] = {};
+ int j = 0;
+ int hits = 0;
+ for (int i = 0; i < len - 1; i++) {
+ if (l[i] == l[i+1]) {
+ hits++;
+ } else {
+ if (hits > 1) {
+ lengths[j] = 1 + hits;
+ j++;
+ }
+ hits = 0;
+ }
+ }
+ if (hits > 1) {
+ lengths[j] = 1 + hits;
+ j++;
+ }
+ int steps = 0;
+ for (int i = 0; i < j; i++) {
+ int l = lengths[i];
+ steps += l / 3;
+ }
+
+ int lflag = 1;
+ int uflag = 1;
+ int dflag = 1;
+ if (has_lower(str)) lflag = 0;
+ if (has_upper(str)) uflag = 0;
+ if (has_digit(str)) dflag = 0;
+ int tally = lflag + uflag + dflag;
+
+ int out_val = len_diff + steps;
+ if (out_val < tally) {
+ out_val += tally - out_val;
+ }
+
+ printf( "Output: %d\n", out_val);
+}
+
+int main() {
+ char* str = "a";
+ proc(str);
+ str = "aB2";
+ proc(str);
+ str = "PaaSW0rd";
+ proc(str);
+ str = "turbbbbot";
+ proc(str);
+ str = "111";
+ proc(str);
+
+}
diff --git a/challenge-287/zapwai/c/ch-2.c b/challenge-287/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..677db18089
--- /dev/null
+++ b/challenge-287/zapwai/c/ch-2.c
@@ -0,0 +1,94 @@
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdbool.h>
+
+bool is_number(char* str) {
+ for (int i = 0; i < strlen(str); i++)
+ if (!isdigit(str[i]))
+ return false;
+ return true;
+}
+
+bool is_dEd(char* str) {
+ // ^\d+e|E\d+
+ char chunk[10][50];
+ int j = 0;
+ char *q;
+ char *p = strchr(str, 'e');
+ if (p != NULL) {
+ q = strtok(str, "e");
+ while (q != NULL) {
+ strcpy(chunk[j++], q);
+ q = strtok(NULL, "e");
+ }
+ if (j != 2)
+ return false;
+ if (is_number(chunk[0]) && is_number(chunk[1]))
+ return true;
+ } else {
+ p = strchr(str, 'E');
+ if (p != NULL) {
+ q = strtok(str, "E");
+ while (q != NULL) {
+ strcpy(chunk[j++], q);
+ q = strtok(NULL, "E");
+ }
+ if (j != 2)
+ return false;
+ if (is_number(chunk[0]) && is_number(chunk[1]))
+ return true;
+ } else {
+ return false;
+ }
+ }
+ return false;
+}
+
+
+void proc(char str[]) {
+ printf("Input: %s\n", str);
+ char* output = "False";
+ char piece[100][100];
+ int plen = 0;
+ char* p = strtok(str, ".");
+ while (p != NULL) {
+ strcpy(piece[plen++], p);
+ p = strtok(NULL, ".");
+ }
+
+ if (plen == 1) {
+ if (is_number(str)) {
+ output = "True";
+ } else if (is_dEd(str)) {
+ output = "True";
+ }
+ } else if (plen == 2) {
+ if (is_number(piece[0]) && is_number(piece[1])) {
+ output = "True";
+ } else if (is_number(piece[0]) && is_dEd(piece[1])) {
+ output = "True";
+ }
+ }
+ printf("Output: %s\n", output);
+}
+
+int main() {
+ char str[] = "1";
+ proc(str);
+ char str2[] = "56e10";
+ proc(str2);
+ char str3[] = "2E32";
+ proc(str3);
+ char str4[] = "a";
+ proc(str4);
+ char str5[] = "1.2";
+ proc(str5);
+ char str6[] = "1.2.6";
+ proc(str6);
+ char str7[] = "3.142e10";
+ proc(str7);
+ char str8[] = "3.142e42B";
+ proc(str8);
+}
+
diff --git a/challenge-287/zapwai/javascript/ch-1.js b/challenge-287/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..716ad43bb9
--- /dev/null
+++ b/challenge-287/zapwai/javascript/ch-1.js
@@ -0,0 +1,63 @@
+let str = "a";
+proc(str);
+str = "aB2";
+proc(str);
+str = "PaaSW0rd";
+proc(str);
+str = "turbbbbot";
+proc(str);
+str = "111";
+proc(str);
+
+function proc(str) {
+ console.log( "Input:", str);
+ let len = str.length;
+ let len_diff = 0;
+ if (len < 6) {
+ len_diff = 6 - len;
+ } else if (len > 20) {
+ len_diff = len - 20;
+ }
+ let l = str.split("");
+ let lengths = [];
+ let hits = 0;
+ for (let i = 0; i < l.length - 1; i++) {
+ if (l[i] == l[i+1]) {
+ hits++;
+ } else {
+ if (hits > 1) {
+ lengths.push(1+hits);
+ }
+ hits = 0;
+ }
+ }
+ if (hits > 1) {
+ lengths.push(1+hits);
+ }
+ let steps = 0;
+ for (let l of lengths) {
+ steps += Math.floor(l/3);
+ }
+ let lflag = 1;
+ let uflag = 1;
+ let dflag = 1;
+ let lreg = new RegExp('[a-z]');
+ let ureg = new RegExp('[A-Z]');
+ let dreg = new RegExp('\d');
+ if (lreg.test(str)) {
+ lflag = 0;
+ }
+ if (ureg.test(str)) {
+ uflag = 0;
+ }
+ if (dreg.test(str)) {
+ dflag = 0;
+ }
+ let tally = lflag + uflag + dflag;
+ let out_val = len_diff + steps;
+ if (out_val < tally) {
+ out_val += tally - out_val;
+ }
+ console.log( "Output: ", out_val);
+}
+
diff --git a/challenge-287/zapwai/javascript/ch-2.js b/challenge-287/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..23e068bc82
--- /dev/null
+++ b/challenge-287/zapwai/javascript/ch-2.js
@@ -0,0 +1,39 @@
+let str = "1";
+proc(str);
+str = "56e10";
+proc(str);
+str = "2E32";
+proc(str);
+str = "a";
+proc(str);
+str = "1.2";
+proc(str);
+str = "1.2.6";
+proc(str);
+str = "3.142e10";
+proc(str);
+str = "3.142e42B";
+proc(str);
+
+function proc(str) {
+ console.log( "Input:", str);
+ let output = "False";
+ let p = str.split(".");
+ let dre = /^\d+$/
+ let dere = /^\d+e\d+$|^\d+E\d+$/;
+ let deere = /^\d+e\d+$|^\d+E\d+$/;
+ if (p.length == 1) {
+ if (dre.test(str)) {
+ output = "True";
+ } else if (dere.test(str)) {
+ output = "True";
+ }
+ } else if (p.length == 2) {
+ if (dre.test(p[0]) && dre.test(p[1])) {
+ output = "True";
+ } else if (dre.test(p[0]) && deere.test(p[1])) {
+ output = "True";
+ }
+ }
+ console.log("Output:", output);
+}
diff --git a/challenge-287/zapwai/perl/ch-1.pl b/challenge-287/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..32e66cf963
--- /dev/null
+++ b/challenge-287/zapwai/perl/ch-1.pl
@@ -0,0 +1,64 @@
+use v5.38;
+my $str = "a";
+proc($str);
+$str = "aB2";
+proc($str);
+$str = "PaaSW0rd";
+proc($str);
+$str = "turbbbbot";
+proc($str);
+$str = "111";
+proc($str);
+
+sub proc($str) {
+ say "Input: $str";
+ my $len = length $str;
+ my $len_diff = 0;
+ if ($len < 6) {
+ $len_diff = 6 - $len;
+ } elsif ($len > 20) {
+ $len_diff = $len - 20;
+ }
+ # len_diff is the number of changes needed because of length requirement
+
+ # Check for repeated letters (3 or more)
+ my @l = split "", $str;
+ my @len;
+ my $hits = 0;
+ for my $i (0 .. $#l - 1) {
+ if ($l[$i] eq $l[$i+1]) {
+ $hits++;
+ } else {
+ if ($hits > 1) {
+ push @len, ++$hits;
+ }
+ $hits = 0;
+ }
+ }
+ if ($hits > 1) {
+ push @len, ++$hits;
+ }
+ my $steps = 0;
+ for my $l (@len) {
+ $steps += int($l/3);
+ }
+ # steps is the number of changes needed to remove repeats
+
+ my $lflag = 1;
+ my $uflag = 1;
+ my $dflag = 1;
+ $lflag = 0 if ($str =~ /[a-z]/);
+ $uflag = 0 if ($str =~ /[A-Z]/);
+ $dflag = 0 if ($str =~ /\d/);
+ my $tally = $lflag + $uflag + $dflag;
+ # tally is the number of missing constraints (lower/upper/digit)
+
+ #say "\t\t",$len_diff, $steps, $tally;
+ my $out_val = $len_diff + $steps;
+ if ($out_val < $tally) {
+ $out_val += $tally - $out_val;
+ }
+ # Tally is only needed if tally > len_diff + steps.
+ say "Output: ", $out_val;
+}
+
diff --git a/challenge-287/zapwai/perl/ch-2.pl b/challenge-287/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..8936508322
--- /dev/null
+++ b/challenge-287/zapwai/perl/ch-2.pl
@@ -0,0 +1,37 @@
+use v5.38;
+my $str = "1";
+proc($str);
+$str = "56e10";
+proc($str);
+$str = "2E32";
+proc($str);
+$str = "a";
+proc($str);
+$str = "1.2";
+proc($str);
+$str = "1.2.6";
+proc($str);
+$str = "3.142e10";
+proc($str);
+$str = "3.142e42B";
+proc($str);
+
+sub proc($str) {
+ say "Input: $str";
+ my $output = "False";
+ my @p = split /\./, $str;
+ if (@p == 1) {
+ if ($str =~ /^\d+$/) {
+ $output = "True";
+ } elsif ($str =~ /^\d+e|E\d+$/) {
+ $output = "True";
+ }
+ } elsif (@p == 2) {
+ if ($p[0] =~ /^\d+$/ && $p[1] =~ /^\d+$/) {
+ $output = "True";
+ } elsif ($p[0] =~ /^\d+$/ && $p[1] =~ /^\d+e\d+$|E\d+$/) {
+ $output = "True";
+ }
+ }
+ say "Output: $output\n";
+}
diff --git a/challenge-287/zapwai/python/ch-1.py b/challenge-287/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..d3764062d6
--- /dev/null
+++ b/challenge-287/zapwai/python/ch-1.py
@@ -0,0 +1,49 @@
+import re
+def proc(mystr) :
+ print( "Input:", mystr)
+ mylen = len( mystr )
+ len_diff = 0
+ if mylen < 6:
+ len_diff = 6 - mylen
+ elif mylen > 20:
+ len_diff = mylen - 20
+ l = list(mystr)
+ lengths = []
+ hits = 0
+ for i in range(mylen - 1):
+ if l[i] == l[i+1]:
+ hits += 1
+ else :
+ if hits > 1 :
+ lengths.append(1+hits)
+ hits = 0
+ if hits > 1 :
+ lengths.append(1+hits)
+ steps = 0
+ for l in lengths :
+ steps += int(l/3)
+ lflag = 1
+ uflag = 1
+ dflag = 1
+ if re.search('[a-z]',mystr):
+ lflag = 0
+ if re.search('[A-Z]',mystr):
+ uflag = 0
+ if re.search('\d',mystr):
+ dflag = 0
+ tally = lflag + uflag + dflag
+ out_val = len_diff + steps
+ if out_val < tally :
+ out_val += tally - out_val
+ print( "Output: ", out_val)
+
+mystr = "a"
+proc(mystr)
+mystr = "aB2"
+proc(mystr)
+mystr = "PaaSW0rd"
+proc(mystr)
+mystr = "turbbbbot"
+proc(mystr)
+mystr = "111"
+proc(mystr)
diff --git a/challenge-287/zapwai/python/ch-2.py b/challenge-287/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..867df4f639
--- /dev/null
+++ b/challenge-287/zapwai/python/ch-2.py
@@ -0,0 +1,35 @@
+import re
+def proc(mystr) :
+ print("Input:", mystr)
+ output = "False"
+ p = mystr.split(".")
+ if len(p) == 1 :
+ if re.search('^\d+$', mystr):
+ output = "True"
+ elif re.search('^\d+e\d+$|^\d+E\d+$', mystr):
+ output = "True"
+ elif len(p) == 2:
+ if re.search('^\d+$', p[0]) and re.search('^\d+$', p[1]):
+ output = "True"
+ elif re.search('^\d+$', p[0]) and re.search('^\d+e\d+$|^\d+E\d+$', p[1]):
+ output = "True"
+
+ print("Output:", output)
+
+mystr = "1"
+proc(mystr)
+mystr = "56e10"
+proc(mystr)
+mystr = "2E32"
+proc(mystr)
+mystr = "a"
+proc(mystr)
+mystr = "1.2"
+proc(mystr)
+mystr = "1.2.6"
+proc(mystr)
+mystr = "3.142e10"
+proc(mystr)
+mystr = "3.142e42B"
+proc(mystr)
+
diff --git a/challenge-287/zapwai/r/ch-1.r b/challenge-287/zapwai/r/ch-1.r
new file mode 100644
index 0000000000..ea756f6fb6
--- /dev/null
+++ b/challenge-287/zapwai/r/ch-1.r
@@ -0,0 +1,61 @@
+proc<-function(str) {
+ cat("Input:", str,"\n")
+ len <- nchar(str)
+ len_diff <- 0
+ if (len < 6) {
+ len_diff <- 6 - len
+ } else if (len > 20) {
+ len_diff <- len - 20
+ }
+ l <- strsplit(str, "")[[1]]
+ lengths = c()
+ hits <- 0
+ if (len > 1) {
+ for (i in 1:(len-1)) {
+ if (l[i] == l[i+1]) {
+ hits <- hits + 1
+ } else {
+ if (hits > 1) {
+ lengths <- append(lengths, 1+hits)
+ }
+ hits <- 0
+ }
+ }
+ if (hits > 1) {
+ lengths <- append(lengths, 1+hits)
+ }
+ }
+ steps <- 0
+ for (l in lengths) {
+ steps <- steps + floor(l/3)
+ }
+ lflag <- 1
+ uflag <- 1
+ dflag <- 1
+ if (grepl('[a-z]', str)) {
+ lflag <- 0
+ }
+ if (grepl('[A-Z]', str)) {
+ uflag <- 0
+ }
+ if (grepl('\\d', str)) {
+ dflag <- 0
+ }
+ tally <- lflag + uflag + dflag
+ out_val <- len_diff + steps
+ if (out_val < tally) {
+ out_val <- out_val + tally - out_val
+ }
+ cat( "Output: ", out_val, "\n")
+}
+
+str <- "a"
+proc(str)
+str <- "aB2"
+proc(str)
+str <- "PaaSW0rd"
+proc(str)
+str <- "turbbbbotttt"
+proc(str)
+str <- "111"
+proc(str)
diff --git a/challenge-287/zapwai/r/ch-2.r b/challenge-287/zapwai/r/ch-2.r
new file mode 100644
index 0000000000..53001b373a
--- /dev/null
+++ b/challenge-287/zapwai/r/ch-2.r
@@ -0,0 +1,35 @@
+proc <-function(str) {
+ cat("Input:", str, "\n")
+ output <- "False"
+ p <- strsplit(str, "\\.")[[1]]
+ if (length(p) == 1) {
+ if (grepl('^\\d+$',str)) {
+ output <- "True"
+ } else if (grepl('^\\d+e\\d+$|\\d+E\\d+$', str)) {
+ output <- "True"
+ }
+ } else if (length(p) == 2) {
+ if (grepl('^\\d+$', p[1]) && grepl('^\\d+$', p[2])) {
+ output <- "True"
+ } else if (grepl('^\\d+$', p[1]) && grepl('^\\d+e\\d+$|\\d+E\\d+$', p[2])) {
+ output <- "True"
+ }
+ }
+ cat("Output:", output, "\n")
+}
+str <- "1"
+proc(str)
+str <- "56e10"
+proc(str)
+str <- "2E32"
+proc(str)
+str <- "a"
+proc(str)
+str <- "1.2"
+proc(str)
+str <- "1.2.6"
+proc(str)
+str <- "3.142e10"
+proc(str)
+str <- "3.142e42B"
+proc(str)
diff --git a/challenge-287/zapwai/rust/ch-1.rs b/challenge-287/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..3d9c9a3c25
--- /dev/null
+++ b/challenge-287/zapwai/rust/ch-1.rs
@@ -0,0 +1,86 @@
+fn has_lower(mystr : &str) -> bool {
+ for c in mystr.chars() {
+ if c.is_lowercase() {
+ return true;
+ }
+ }
+ return false;
+}
+
+fn has_upper(mystr : &str) -> bool {
+ for c in mystr.chars() {
+ if c.is_uppercase() {
+ return true;
+ }
+ }
+ return false;
+}
+
+fn has_digit(mystr : &str) -> bool {
+ for c in mystr.chars() {
+ if c.is_digit(10) {
+ return true;
+ }
+ }
+ return false;
+}
+
+fn proc(mystr : &str) {
+ println!( "Input: {mystr}");
+ let len = mystr.len();
+ let mut len_diff = 0;
+ if len < 6 {
+ len_diff = 6 - len;
+ } else if len > 20 {
+ len_diff = len - 20;
+ }
+
+ let l : Vec<&str> = mystr.split("").collect();
+ let mut lengths : Vec<i32> = Vec::new();
+ let mut hits = 0;
+ for i in 0 .. mystr.len() {
+ if l[i] == l[i+1] {
+ hits += 1;
+ } else {
+ if hits > 1 {
+ lengths.push(1+hits);
+ }
+ hits = 0;
+ }
+ }
+ if hits > 1 {
+ lengths.push(1+hits);
+ }
+ let mut steps = 0;
+ for l in lengths {
+ steps += l/3;
+ }
+
+ let mut lflag = 1;
+ let mut uflag = 1;
+ let mut dflag = 1;
+ if has_lower(mystr) {lflag = 0;}
+ if has_upper(mystr) {uflag = 0;}
+ if has_digit(mystr) {dflag = 0;}
+ let tally = lflag + uflag + dflag;
+
+ let mut out_val = len_diff as i32 + steps;
+ if out_val < tally {
+ out_val += tally - out_val;
+ }
+ println!( "Output: {}", out_val);
+}
+
+fn main() {
+ let mut mystr = "a";
+ proc(mystr);
+ mystr = "aB2";
+ proc(mystr);
+ mystr = "PaaSW0rd";
+ proc(mystr);
+ mystr = "turbbbbot";
+ proc(mystr);
+ mystr = "111";
+ proc(mystr);
+
+}
diff --git a/challenge-287/zapwai/rust/ch-2.rs b/challenge-287/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..d2c895dbfa
--- /dev/null
+++ b/challenge-287/zapwai/rust/ch-2.rs
@@ -0,0 +1,43 @@
+use regex::Regex;
+
+fn main() {
+ let mut mystr = "1";
+ proc(mystr);
+ mystr = "56e10";
+ proc(mystr);
+ mystr = "2E32";
+ proc(mystr);
+ mystr = "a";
+ proc(mystr);
+ mystr = "1.2";
+ proc(mystr);
+ mystr = "1.2.6";
+ proc(mystr);
+ mystr = "3.142e10";
+ proc(mystr);
+ mystr = "3.142e42B";
+ proc(mystr);
+}
+
+fn proc(mystr : &str) {
+ println!( "Input: {mystr}");
+ let mut output = "False";
+ let p :Vec<&str> = mystr.split(".").collect();
+ let dre = Regex::new(r"^\d+$").unwrap();
+ let dere = Regex::new(r"^\d+e|E\d+$").unwrap();
+ let deere = Regex::new(r"^\d+e\d+$|E\d+$").unwrap();
+ if p.len() == 1 {
+ if dre.is_match(mystr) {
+ output = "True";
+ } else if dere.is_match(mystr) {
+ output = "True";
+ }
+ } else if p.len() == 2 {
+ if dre.is_match(p[0]) && dre.is_match(p[1]) {
+ output = "True";
+ } else if dre.is_match(p[0]) && deere.is_match(p[1]) {
+ output = "True";
+ }
+ }
+ println!( "Output: {output}");
+}