diff options
| author | rir <rirans@comcast.net> | 2024-03-16 20:07:25 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-16 20:07:25 -0400 |
| commit | ff5e8ece15a2384fbfb530710f10aa485017d4a5 (patch) | |
| tree | 6fe268d9ceb2b25a2951ea984c077450feb2efae /challenge-004 | |
| parent | 60f1003122fbada697317d943c238593f86db579 (diff) | |
| parent | 62e7fc3bb85a74125663f4fbd0a5911f6f30c81f (diff) | |
| download | perlweeklychallenge-club-ff5e8ece15a2384fbfb530710f10aa485017d4a5.tar.gz perlweeklychallenge-club-ff5e8ece15a2384fbfb530710f10aa485017d4a5.tar.bz2 perlweeklychallenge-club-ff5e8ece15a2384fbfb530710f10aa485017d4a5.zip | |
Merge branch 'manwar:master' into work
Diffstat (limited to 'challenge-004')
| -rw-r--r-- | challenge-004/zapwai/README | 1 | ||||
| -rw-r--r-- | challenge-004/zapwai/c/ch-1.c | 23 | ||||
| -rw-r--r-- | challenge-004/zapwai/c/ch-2.c | 67 | ||||
| -rw-r--r-- | challenge-004/zapwai/javascript/ch-1.js | 1 | ||||
| -rw-r--r-- | challenge-004/zapwai/javascript/ch-2.js | 39 | ||||
| -rw-r--r-- | challenge-004/zapwai/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-004/zapwai/perl/ch-2.pl | 33 | ||||
| -rw-r--r-- | challenge-004/zapwai/python/ch-1.py | 6 | ||||
| -rw-r--r-- | challenge-004/zapwai/python/ch-2.py | 34 | ||||
| -rw-r--r-- | challenge-004/zapwai/rust/ch-1.rs | 17 | ||||
| -rw-r--r-- | challenge-004/zapwai/rust/ch-2.rs | 45 |
11 files changed, 287 insertions, 0 deletions
diff --git a/challenge-004/zapwai/README b/challenge-004/zapwai/README new file mode 100644 index 0000000000..037b3777ef --- /dev/null +++ b/challenge-004/zapwai/README @@ -0,0 +1 @@ +Solutions by David Ferrone. diff --git a/challenge-004/zapwai/c/ch-1.c b/challenge-004/zapwai/c/ch-1.c new file mode 100644 index 0000000000..b70492493e --- /dev/null +++ b/challenge-004/zapwai/c/ch-1.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stdlib.h> +int main(int argc, char* argv[]) { + FILE* fp = fopen("004-1.c", "r"); + fseek(fp, 0L, SEEK_END); + int sz = ftell(fp); + fclose(fp); + char url[] = "http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html"; + char args[100]; + sprintf(args, "curl -s %s > pi.txt", url); + system(args); + FILE* fp2 = fopen("pi.txt", "r"); + fseek(fp2, 326, SEEK_SET); + char data[sz+7]; + int numread = fread(data, sizeof(char), sz+7, fp2); + fclose(fp2); + for (int i = 0; i < sz+7; i++) { + if (data[i] != '\n') { + printf("%c", data[i]); + } + } + printf("\n"); +} diff --git a/challenge-004/zapwai/c/ch-2.c b/challenge-004/zapwai/c/ch-2.c new file mode 100644 index 0000000000..b4fa93ba74 --- /dev/null +++ b/challenge-004/zapwai/c/ch-2.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +#define max_length 100 /* max length of lines */ +#define num_lines 30 /* max # of lines in file */ + +void proc(char*); +bool is_in(char, char*, bool*); + +int main() { + char* alist = "docgotrark"; + proc(alist); + return 0; +} + +void proc(char* alist) { + FILE* fh = fopen("004-file.txt", "r"); + if (fh == NULL) { printf("Error opening file\n"); } + char words[num_lines][max_length]; /* words from file */ + bool blist[strlen(alist)]; /* keeps track of letters available */ + int i = 0; + while (fgets(words[i], max_length, fh) != NULL) { + i++; + } + fclose(fh); + int N = i; /* num of words in file */ + char list[N][max_length]; /* answer list */ + printf("Input: %s\n", alist); + printf("Output: "); + int list_len = 0; /* num of words in answer list */ + for (int i = 0; i < N; i++){ /* for each word in file */ + for (int i = 0; i < strlen(alist); i++) { /* reset available letters */ + blist[i] = true; + } + int cnt = 0; /* count available letters in each word */ + for (int j = 0; j < strlen(words[i]) - 1; j++){ /* skip the \n on the end */ + if ( is_in(words[i][j], alist, blist) ) { + cnt++; + } + } + if (cnt == strlen(words[i]) - 1) { + for (int j = 0; j < strlen(words[i]) - 1; j++) { + list[list_len][j] = words[i][j]; + } + list[list_len][strlen(words[i]) - 1] = '\0'; + list_len++; + } + } + + for (int i = 0; i < list_len; i++) { + printf("%s ", list[i]); + } + printf("\n"); +} + +/* Check if c is in word, and available. (Update available letters.) */ +bool is_in(char c, char* word, bool* avail) { + for (int i = 0; i < strlen(word); i++) { + if ( (c == word[i]) && (avail[i]) ) { + avail[i] = false; + return true; + } + } + return false; +} diff --git a/challenge-004/zapwai/javascript/ch-1.js b/challenge-004/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..f9e664b9a8 --- /dev/null +++ b/challenge-004/zapwai/javascript/ch-1.js @@ -0,0 +1 @@ +console.log(Math.PI) diff --git a/challenge-004/zapwai/javascript/ch-2.js b/challenge-004/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..cdcb8528e6 --- /dev/null +++ b/challenge-004/zapwai/javascript/ch-2.js @@ -0,0 +1,39 @@ +let allow = "docgotrark"; +proc(allow); + +function proc(allow) { + let f = {} // hash containing allowed letters & counts + fill(f, allow); + let words = ["dog", "chair", "rack", "doggo"]; + let ans = []; + for (let word of words) { + let g = {}; + fill(g, word); + if (comp(f,g) == 1) { + ans.push(word); + } + } + console.log("Input:", allow); + console.log("Output:", ans); +} +// fills a hashmap to count letters +function fill(f, word) { + for (let c of word) { + f[c] = 0; + } + for (let c of word) { + f[c]++; + } +} +// compare hashes, f is allowed chars, g is the word +function comp(f,g) { + for (let k of Object.keys(g)) { + if (!(k in f)) { + return 0; + } + if (f[k] < g[k]) { + return 0; + } + } + return 1; +} diff --git a/challenge-004/zapwai/perl/ch-1.pl b/challenge-004/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..48125dd70e --- /dev/null +++ b/challenge-004/zapwai/perl/ch-1.pl @@ -0,0 +1,21 @@ +use Math::BigInt; +Math::BigInt->div_scale(2500); +my $A = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz()'; +sub f { + my $str = shift; + my @digits = split "", $str; + $_ = index $A, $_ for (@digits); + my $sum = Math::BigInt->new(0); + foreach (0 .. $#digits) { + my $b = Math::BigInt->new( length $A ); + $b -> bpow( $#digits - $_ ); + $sum -> badd( $digits[$_] * $b ); + } + $sum +} +open fh, "<", "ch-1.pl"; +my @strings = <fh>; +my $str = substr $strings[$#strings], 1; +chomp $str; +print "3.".f($str)."\n" +#SrMB89DPWO4lpkmDab5Mqw6Npy62PGjFeH2ygGQowjas2jU0xHXPj1l6zUZhWIxeCXzRuiA4vH(EVtp9byYGRpLfWJZthiXK8spgpuWBpI4tmv9Uo)aOFY4(L6s5K9YcV)IZaOF3kcIJs9q7m9Ffkaxs(W4hQl0QSavrgXw)w6YIQ)tltOz1qekb(xugL3x9yPJ4ZMgbTFUPkRmGAKURXIwD1W2R90pTCrYalIL)GEehgcw6RjigU)0HsVB6lh)7(aQCEzKXtqk3fx24mlLO61MQSwV0qKS(WLQyoygm5W(OW7XhapDcElcwHAht(KVp4VMXhwS)4jhyP12yXk38Lv13hsTs1yXtsj)rLxERAbJKkdbROhEXxlUWtO7(sfojQNFy1MBw6O(VX2eTHMlxD(bBWb4VO6NKBT9AjwoG)(exVd0((aewvz2982nX0O0GuvuxhvPx(ZazyUbo4Q6PzQnAP6sMJAH7qTVpfeZp7QuM3T8Kc2aW20fdNIWwHZaRGOp)0DzclP7qskmEwhZZ5x0cv78K4Zrh00yj02ffMUFCMMJipOgTZJPIjADDmcNw4pbjREvIMpDjGkjNNVojuABXVACjkA8tUjPpOEmd7Dj8iaxcEZ1p3)k2XAmcicZzzqpqYNWswEGTYwWvs3rMVkm diff --git a/challenge-004/zapwai/perl/ch-2.pl b/challenge-004/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..2c4a458e46 --- /dev/null +++ b/challenge-004/zapwai/perl/ch-2.pl @@ -0,0 +1,33 @@ +use v5.30.0; +my $allow = 'eiufhkljwodkg'; +open fh, "<", "words" or die 'no file?'; +my @word = <fh>; +close fh; +word: foreach my $word (@word) { + chomp $word; + my $WORD = $word; + my $allow = $allow; # local copy + my $cnt = 0; + for my $i (0 .. -1 + length $word) { + my $letter = substr $word, $i, 1; + if ($allow =~ $letter) { + $cnt++; + } + } + + if ($cnt < length $word) { + next word; + } else { + my $index = 0; + do { + my $let = substr $allow, $index, 1; + if ($word =~ /$let/) { + $allow =~ s/$let//; + $word =~ s/$let//; + } else { + $index++ + } + } while ($index < length $allow); + } + say "$WORD" unless ($word); +} diff --git a/challenge-004/zapwai/python/ch-1.py b/challenge-004/zapwai/python/ch-1.py new file mode 100644 index 0000000000..80d7d5aa1c --- /dev/null +++ b/challenge-004/zapwai/python/ch-1.py @@ -0,0 +1,6 @@ +import os +from mpmath import mp +filename = os.path.basename(__file__) +length = os.path.getsize(filename) +mp.dps = length +print(mp.pi) diff --git a/challenge-004/zapwai/python/ch-2.py b/challenge-004/zapwai/python/ch-2.py new file mode 100644 index 0000000000..cb2c28a340 --- /dev/null +++ b/challenge-004/zapwai/python/ch-2.py @@ -0,0 +1,34 @@ +def comp(f, g): + for k in g.keys(): + if (k not in f.keys()): + return 0 + elif (f[k] < g[k]): + return 0 + return 1 + + +def tally(f, word): + for c in word: + f[c] = 0 + for c in word: + f[c] += 1 + +def proc(allow): + f = {} + tally(f, allow) + fh = open("004-file.txt", "r") + words = [] + for x in fh: + y = x.rstrip('\r\n'); + words.append(y) + ans = [] + for word in words: + g = {} + tally(g, word) + if (comp(f,g)): + ans.append(word) + print("Input:", allow) + print("Output:", ans) + +allow = "docgotrark" +proc(allow) diff --git a/challenge-004/zapwai/rust/ch-1.rs b/challenge-004/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..494269972c --- /dev/null +++ b/challenge-004/zapwai/rust/ch-1.rs @@ -0,0 +1,17 @@ +use std::process::Command; +use std::io::prelude::*; +use std::fs::File; +use std::io::SeekFrom; +fn main(){ + Command::new("curl").arg("-s").arg("http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html").arg("-o").arg("pi.txt").output().expect("no curl"); + let mut file = File::open("pi.txt").expect("no pi"); + file.seek(SeekFrom::Start(326)).unwrap(); + let mut buffer = [0;590]; + file.read_exact(&mut buffer).unwrap(); + let string_buffer = String::from_utf8_lossy(&buffer); + for c in string_buffer.split("") { + if c != "\n" { + print!("{}", c); + } + } +} diff --git a/challenge-004/zapwai/rust/ch-2.rs b/challenge-004/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..c23fb6f568 --- /dev/null +++ b/challenge-004/zapwai/rust/ch-2.rs @@ -0,0 +1,45 @@ +use std::collections::HashMap; +use std::fs; + +fn main() { + let allow = "docgotrark"; + let file_path = "004-file.txt"; + let contents = fs::read_to_string(file_path) + .expect("No file?"); + let words = contents.split("\n"); +// let words = ["dog", "chair", "rack", "doggo"]; + let mut f = HashMap::new(); + fill(&mut f, allow); + println!("Input: {allow}"); + let mut ans = Vec::new(); + for word in words { + let mut g = HashMap::new(); + fill(&mut g, word); + if comp(&f, &g) { + ans.push(word); + } + } + ans.pop(); + println!("Output: {:?}",ans); +} + +fn fill(f :&mut HashMap<char, i32>, allow :&str) { + for c in allow.chars() { + f.insert(c, 0); + } + for c in allow.chars() { + f.insert(c, f.get(&c).unwrap() + 1); + } +} + +fn comp(f : &HashMap<char, i32>, g : &HashMap<char, i32>) -> bool { + for k in g.keys() { + if !f.contains_key(k) { + return false; + } + if f.get(k) < g.get(k) { + return false; + } + } + return true; +} |
