diff options
| -rw-r--r-- | challenge-278/zapwai/c/ch-1.c | 51 | ||||
| -rw-r--r-- | challenge-278/zapwai/c/ch-2.c | 55 | ||||
| -rw-r--r-- | challenge-278/zapwai/javascript/ch-1.js | 34 | ||||
| -rw-r--r-- | challenge-278/zapwai/javascript/ch-2.js | 24 | ||||
| -rw-r--r-- | challenge-278/zapwai/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-278/zapwai/perl/ch-2.pl | 25 | ||||
| -rw-r--r-- | challenge-278/zapwai/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-278/zapwai/python/ch-2.py | 24 | ||||
| -rw-r--r-- | challenge-278/zapwai/rust/ch-1.rs | 36 | ||||
| -rw-r--r-- | challenge-278/zapwai/rust/ch-2.rs | 32 |
10 files changed, 348 insertions, 0 deletions
diff --git a/challenge-278/zapwai/c/ch-1.c b/challenge-278/zapwai/c/ch-1.c new file mode 100644 index 0000000000..9ffdda16f0 --- /dev/null +++ b/challenge-278/zapwai/c/ch-1.c @@ -0,0 +1,51 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#define MAX 50 + +void proc(char mystr[]) { + printf("Input: %s\n", mystr); + char *words[MAX]; + int keys[MAX] = {}; + int sentencelen = 0; + char *token = strtok(mystr, " "); + while (token != NULL) { + int len = strlen(token); + keys[sentencelen] = token[len - 1] - '0'; + words[sentencelen] = malloc(len * sizeof(char)); + strncpy(words[sentencelen], token, len - 1); + sentencelen++; + token = strtok(NULL, " "); + } + int cnt = 1; + while (cnt > 0) { + cnt = 0; + for (int i = 0; i < sentencelen - 1; i++) { + if (keys[i] > keys[i + 1]) { + int keynum = keys[i]; + keys[i] = keys[i + 1]; + keys[i + 1] = keynum; + char *word = words[i]; + words[i] = words[i + 1]; + words[i + 1] = word; + cnt++; + } + } + } + printf("Output: "); + for (int i = 0; i < sentencelen; i++) { + printf("%s ", words[i]); + free(words[i]); + } + printf("\n"); +} + +int main() { + char mystr[] = "and2 Raku3 cousins5 Perl1 are4"; + proc(mystr); + char mystr2[] = "guest6 Python1 most4 the3 popular5 is2 language7"; + proc(mystr2); + char mystr3[] = "Challenge3 The1 Weekly2"; + proc(mystr3); +} + diff --git a/challenge-278/zapwai/c/ch-2.c b/challenge-278/zapwai/c/ch-2.c new file mode 100644 index 0000000000..0113453b07 --- /dev/null +++ b/challenge-278/zapwai/c/ch-2.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <string.h> + +void arrange(char word[], int wordlen) { + char newword[100] = {}; + for (int i = 0; i < wordlen; i++) + newword[i] = word[i]; + int cnt = 1; + while (cnt > 0) { + cnt = 0; + for (int i = 0; i < wordlen - 1; i++) + if (newword[i] > newword[i+1]) { + cnt++; + char let = newword[i]; + newword[i] = newword[i+1]; + newword[i+1] = let; + } + } + for (int i = 0; i < wordlen; i++) + printf("%c", newword[i]); +} + +void proc(char mystr[], int mystrlen, char mychar) { + printf( "Input: mystr = %s, char = %c\n", mystr, mychar); + char *e; + e = strchr(mystr, mychar); + int ind = (int)(e - mystr); + if (e == NULL) { + printf( "Output: %s\n", mystr); + } else { + printf("Output: "); + char begin[100] = {}; + for (int i = 0; i < ind + 1; i++) + begin[i] = mystr[i]; + arrange(begin, sizeof(begin) / sizeof(char)); + for (int i = ind + 1; i < mystrlen; i++) + printf("%c", mystr[i]); + printf("\n"); + } +} + +int main() { + char mystr[] = "challenge"; + int mystrlen = sizeof(mystr) / sizeof(char); + char mychar = 'e'; + proc(mystr, mystrlen, mychar); + char mystr2[] = "programming"; + int mystrlen2 = sizeof(mystr2) / sizeof(char); + char mychar2 = 'a'; + proc(mystr2, mystrlen2, mychar2); + char mystr3[] = "champion"; + int mystrlen3 = sizeof(mystr3) / sizeof(char); + char mychar3 = 'b'; + proc(mystr3, mystrlen3, mychar3); +} diff --git a/challenge-278/zapwai/javascript/ch-1.js b/challenge-278/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..a399c8ee21 --- /dev/null +++ b/challenge-278/zapwai/javascript/ch-1.js @@ -0,0 +1,34 @@ +let str = "and2 Raku3 cousins5 Perl1 are4"; +proc(str); +str = "guest6 Python1 most4 the3 popular5 is2 language7"; +proc(str); +str = "Challenge3 The1 Weekly2"; +proc(str); + +function proc(str) { + console.log("Input:", str); + let words = []; + let keys = []; + for (let word of str.split(" ")) { + let key = word.substr(-1); + let w = word.substr(0, word.length - 1); + words.push(w); + keys.push(key); + } + let cnt = 1; + while (cnt > 0) { + cnt = 0; + for (let i = 0; i < words.length - 1; i++) { + if (keys[i] > keys[i + 1]) { + let keynum = keys[i]; + keys[i] = keys[i + 1]; + keys[i + 1] = keynum; + let word = words[i]; + words[i] = words[i + 1]; + words[i + 1] = word; + cnt++; + } + } + } + console.log( "Output:",words); +} diff --git a/challenge-278/zapwai/javascript/ch-2.js b/challenge-278/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..1753075d96 --- /dev/null +++ b/challenge-278/zapwai/javascript/ch-2.js @@ -0,0 +1,24 @@ +let str = "challenge"; +let mychar = "e"; +proc(str, mychar); +str = "programming"; +mychar = "a"; +proc(str, mychar); +str = "champion"; +mychar = "b"; +proc(str, mychar); +function proc(str, mychar) { + console.log("Input: str =", str, "mychar =", mychar); + let ind = str.indexOf(mychar); + if (ind == -1) { + console.log("Output:",str); + } else { + let begin = str.substr(0, ind + 1); + let endy = str.substr(ind + 1); + console.log(arrange(begin)+endy); + } +} +function arrange(word) { + let arr = word.split(""); + return arr.sort().join(separator=''); +} diff --git a/challenge-278/zapwai/perl/ch-1.pl b/challenge-278/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..54f34145b4 --- /dev/null +++ b/challenge-278/zapwai/perl/ch-1.pl @@ -0,0 +1,35 @@ +use v5.38; +my $str = "and2 Raku3 cousins5 Perl1 are4"; +proc($str); +$str = "guest6 Python1 most4 the3 popular5 is2 language7"; +proc($str); +$str = "Challenge3 The1 Weekly2"; +proc($str); + +sub proc($str) { + say "Input: $str"; + my @words; + my @keys; + for my $word (split " ", $str) { + my $key = substr($word, -1); + my $w = substr($word, 0, -1); + push @words, $w; + push @keys, $key; + } + my $cnt = 1; + while ($cnt > 0) { + $cnt = 0; + for my $i (0 .. $#words - 1) { + if ($keys[$i] > $keys[$i + 1]) { + my $keynum = $keys[$i]; + $keys[$i] = $keys[$i + 1]; + $keys[$i + 1] = $keynum; + my $word = $words[$i]; + $words[$i] = $words[$i + 1]; + $words[$i + 1] = $word; + $cnt++; + } + } + } + say "Output: @words"; +} diff --git a/challenge-278/zapwai/perl/ch-2.pl b/challenge-278/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..846a64740e --- /dev/null +++ b/challenge-278/zapwai/perl/ch-2.pl @@ -0,0 +1,25 @@ +use v5.38; +my $str = "challenge"; +my $char = "e"; +proc($str, $char); +$str = "programming"; +$char = "a"; +proc($str, $char); +$str = "champion"; +$char = "b"; +proc($str, $char); +sub proc($str, $char) { + say "Input: \$str = $str, \$char = $char"; + my $ind = index($str, $char); + if ($ind == -1) { + say "Output: $str"; + } else { + my $begin = substr($str, 0, $ind + 1); + my $end = substr($str, $ind + 1); + say arrange($begin) , $end; + } +} +sub arrange($word) { + my @arr = split "", $word; + return sort @arr; +} diff --git a/challenge-278/zapwai/python/ch-1.py b/challenge-278/zapwai/python/ch-1.py new file mode 100644 index 0000000000..d3baa2924b --- /dev/null +++ b/challenge-278/zapwai/python/ch-1.py @@ -0,0 +1,32 @@ +def proc(mystr): + print("Input:", mystr); + words = [] + keys = [] + for word in mystr.split(" "): + key = word[len(word)-1 : len(word)] + w = word[0:-1] + words.append(w) + keys.append(key) + cnt = 1 + while cnt > 0: + cnt = 0 + for i in range(len(words) - 1): + if (keys[i] > keys[i + 1]): + keynum = keys[i] + keys[i] = keys[i + 1] + keys[i + 1] = keynum + word = words[i] + words[i] = words[i + 1] + words[i + 1] = word + cnt += 1 + + print("Output:", words) + + +mystr = "and2 Raku3 cousins5 Perl1 are4" +proc(mystr) +mystr = "guest6 Python1 most4 the3 popular5 is2 language7" +proc(mystr) +mystr = "Challenge3 The1 Weekly2" +proc(mystr) + diff --git a/challenge-278/zapwai/python/ch-2.py b/challenge-278/zapwai/python/ch-2.py new file mode 100644 index 0000000000..c268a03861 --- /dev/null +++ b/challenge-278/zapwai/python/ch-2.py @@ -0,0 +1,24 @@ +def arrange(word): + arr = list(word) + mysort = sorted(arr) + return "".join(mysort) + +def proc(mystr, char): + print("Input: mystr =", mystr, "char =", char) + ind = mystr.find(char) + if (ind == -1): + print("Output:", mystr) + else: + begin = mystr[0 : ind + 1] + end = mystr[ind + 1 : len(mystr)] + print(arrange(begin)+end) + +mystr = "challenge" +char = "e" +proc(mystr, char) +mystr = "programming" +char = "a" +proc(mystr, char) +mystr = "champion" +char = "b" +proc(mystr, char) diff --git a/challenge-278/zapwai/rust/ch-1.rs b/challenge-278/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..242b0df897 --- /dev/null +++ b/challenge-278/zapwai/rust/ch-1.rs @@ -0,0 +1,36 @@ +fn main() { + let mystr = "and2 Raku3 cousins5 Perl1 are4"; + proc(mystr); + let mystr2 = "guest6 Python1 most4 the3 popular5 is2 language7"; + proc(mystr2); + let mystr3 = "Challenge3 The1 Weekly2"; + proc(mystr3); +} + +fn proc(mystr : &str) { + println!("Input: {mystr}"); + let mut words : Vec<&str> = Vec::new(); + let mut keys : Vec<i32> = Vec::new(); + for word in mystr.split(' ').collect::<Vec<&str>>() { + let key = &word[word.len() - 1 .. word.len()]; + let w = &word[0 .. word.len() - 1]; + words.push(w); + keys.push(key.parse::<i32>().unwrap()); + } + let mut cnt = 1; + while cnt > 0 { + cnt = 0; + for i in 0 .. words.len() - 1 { + if keys[i] > keys[i + 1] { + let keynum = keys[i]; + keys[i] = keys[i + 1]; + keys[i + 1] = keynum; + let word = words[i]; + words[i] = words[i + 1]; + words[i + 1] = word; + cnt += 1; + } + } + } + println!("Output: {:?}", words); +} diff --git a/challenge-278/zapwai/rust/ch-2.rs b/challenge-278/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..c3ce6056ca --- /dev/null +++ b/challenge-278/zapwai/rust/ch-2.rs @@ -0,0 +1,32 @@ +fn main() { + let mystr = "challenge"; + let mychar = 'e'; + proc(mystr, mychar); + let mystr2 = "programming"; + let mychar2 = 'a'; + proc(mystr2, mychar2); + let mystr3 = "champion"; + let mychar3 = 'b'; + proc(mystr3, mychar3); +} +fn proc(mystr : &str, mychar : char) { + println!( "Input: mystr = {mystr}, mychar = {mychar}") ; + let ind = mystr.chars().position(|c| c == mychar); + if ind != None { + let indy = ind.unwrap(); + let begin = &mystr[0 .. indy + 1]; + let end = &mystr[indy + 1 .. ]; + print!("Output: "); + arrange(begin); + println!("{}", end); + } else { + println!( "Output: {mystr}"); + } +} +fn arrange(word : &str) { + let mut arr : Vec<char> = word.chars().collect(); + arr.sort(); + for c in arr { + print!("{c}"); + } +} |
