aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-04 15:40:30 +0000
committerGitHub <noreply@github.com>2024-01-04 15:40:30 +0000
commit4966798fdb2608c7f8082a4577ad829a6c155458 (patch)
tree855abfa70c1af9fc8795cc467bfc624debd49123
parent8394ffe8bf18f7861b6c40373ce31c074125c487 (diff)
parentd9939e7b1ea45317c2d5c83cbc6ac9456a7065fd (diff)
downloadperlweeklychallenge-club-4966798fdb2608c7f8082a4577ad829a6c155458.tar.gz
perlweeklychallenge-club-4966798fdb2608c7f8082a4577ad829a6c155458.tar.bz2
perlweeklychallenge-club-4966798fdb2608c7f8082a4577ad829a6c155458.zip
Merge pull request #9345 from zapwai/branch-for-250
C for week 250
-rw-r--r--challenge-250/zapwai/c/ch-1.c27
-rw-r--r--challenge-250/zapwai/c/ch-2.c44
-rw-r--r--challenge-250/zapwai/perl/ch-1.pl2
-rw-r--r--challenge-250/zapwai/rust/ch-1.rs18
-rw-r--r--challenge-250/zapwai/rust/ch-2.rs36
5 files changed, 126 insertions, 1 deletions
diff --git a/challenge-250/zapwai/c/ch-1.c b/challenge-250/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..9467561fec
--- /dev/null
+++ b/challenge-250/zapwai/c/ch-1.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+void proc (int *ints, int len)
+{
+ int ans = -1;
+ for (int i=0; i < len - 1; i++) {
+ if (i%10 == ints[i]) {
+ ans = i;
+ break;
+ }
+ }
+ printf("Input: ints = (");
+ for (int i=0; i < len - 2; i++) {
+ printf("%d, ",ints[i]);
+ }
+ printf("%d)\n",ints[len - 1]);
+ printf("Output: %d\n",ans);
+
+}
+
+int main () {
+ int ints[] = {0,1,2};
+ int ints2[] = {4,3,2,1};
+ int len = sizeof(ints) / sizeof(int);
+ int len2 = sizeof(ints2) / sizeof(int);
+ proc(&ints, len);
+ proc(&ints2, len2);
+} \ No newline at end of file
diff --git a/challenge-250/zapwai/c/ch-2.c b/challenge-250/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..979337fb80
--- /dev/null
+++ b/challenge-250/zapwai/c/ch-2.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+void proc(char* A[], int L) {
+ int max = 0;
+
+ printf("Input: (");
+ for (int i = 0; i < L - 1; i++) {
+ printf("%s, ", A[i]);
+ }
+ printf("%s)",A[L-1]);
+
+ for (int i = 0; i < L; i++) {
+ int j = 0;
+ int num_cnt = 0;
+ int len = 0;
+ while (A[i][j]) {
+ if ( (A[i][j] >= '0') && (A[i][j] <= '9') ) {
+ num_cnt++;
+ }
+ len++;
+ j++;
+ }
+ int x = -1;
+ if (num_cnt == len) {
+ x = atoi(A[i]);
+ }
+ else {
+ x = len;
+ }
+ if (max < x) {
+ max = x;
+ }
+ }
+
+ printf("\nOutput: %d\n\n", max);
+}
+int main() {
+ const char* B[] = {"001", "1", "000", "0001"};
+ int L2 = sizeof(B)/sizeof(char*);
+ const char* A[] = {"perl", "2", "000", "python", "r4ku"};
+ int L = sizeof(A)/sizeof(char*);
+ proc(A, L);
+ proc(B, L2);
+} \ No newline at end of file
diff --git a/challenge-250/zapwai/perl/ch-1.pl b/challenge-250/zapwai/perl/ch-1.pl
index 8a77df08de..a9c8705de4 100644
--- a/challenge-250/zapwai/perl/ch-1.pl
+++ b/challenge-250/zapwai/perl/ch-1.pl
@@ -4,7 +4,7 @@ my @ints = (0, 1, 2);
#my @ints = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
my $index = -1;
for my $i (0 .. $#ints) {
- if ($i == $ints[$i]) {
+ if ($i % 10 == $ints[$i]) {
$index = $i ;
last;
}
diff --git a/challenge-250/zapwai/rust/ch-1.rs b/challenge-250/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..5aaa43811e
--- /dev/null
+++ b/challenge-250/zapwai/rust/ch-1.rs
@@ -0,0 +1,18 @@
+fn main() {
+ let v = vec![0,1,2];
+ let v2 = vec![4,3,2,1];
+ proc(v);
+ proc(v2);
+}
+
+fn proc(v :Vec<usize>) {
+ println!("Input: ints = {:?}", v);
+ let mut max :i32 = -1;
+ for i in 0 .. v.len() {
+ if i%10==v[i] {
+ max = i as i32;
+ break;
+ }
+ }
+ println!("Output: {max}");
+}
diff --git a/challenge-250/zapwai/rust/ch-2.rs b/challenge-250/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..834662da3b
--- /dev/null
+++ b/challenge-250/zapwai/rust/ch-2.rs
@@ -0,0 +1,36 @@
+fn main() {
+ let t = vec!["perl", "2", "000", "python", "r4ku"];
+ let t2 = vec!["001", "1", "000", "0001"];
+ proc(t);
+ proc(t2);
+}
+
+fn proc(t :Vec<&str>) {
+ let mut max :i32 = 0;
+ for i in t.iter() {
+ let c = val(i);
+ if max < c {
+ max = c;
+ }
+ }
+ println!("Input: str = {:?}",t);
+ println!("Output: {max}");
+}
+
+fn val(i :&str) -> i32 {
+ return if is_num(i) {
+ i.parse::<i32>().unwrap()
+ } else {
+ i.len() as i32
+ };
+}
+
+fn is_num (i :&str) -> bool {
+ let mut cnt = 0;
+ for c in i.chars() {
+ if c.is_digit(10) {
+ cnt += 1;
+ }
+ }
+ return if cnt == i.len() {true} else {false};
+}