diff options
| author | David Ferrone <zapwai@gmail.com> | 2024-03-15 11:21:15 -0400 |
|---|---|---|
| committer | David Ferrone <zapwai@gmail.com> | 2024-03-15 11:21:15 -0400 |
| commit | 60c68b3209b3c09d3cbd29b4ef33080546a43fbd (patch) | |
| tree | 48d43fa4f9c8ea2492e7ab11146b7df7bb1a1679 /challenge-005 | |
| parent | 2a68a16c1d8727b183d85c88f31ae6cec6a869b1 (diff) | |
| download | perlweeklychallenge-club-60c68b3209b3c09d3cbd29b4ef33080546a43fbd.tar.gz perlweeklychallenge-club-60c68b3209b3c09d3cbd29b4ef33080546a43fbd.tar.bz2 perlweeklychallenge-club-60c68b3209b3c09d3cbd29b4ef33080546a43fbd.zip | |
Weekly Challenge Blast from the Past
Diffstat (limited to 'challenge-005')
| -rw-r--r-- | challenge-005/zapwai/README | 1 | ||||
| -rw-r--r-- | challenge-005/zapwai/c/005.h | 70 | ||||
| -rw-r--r-- | challenge-005/zapwai/c/ch-1.c | 36 | ||||
| -rw-r--r-- | challenge-005/zapwai/c/ch-2.c | 59 | ||||
| -rw-r--r-- | challenge-005/zapwai/javascript/005.html | 21 | ||||
| -rw-r--r-- | challenge-005/zapwai/javascript/ch-1.js | 50 | ||||
| -rw-r--r-- | challenge-005/zapwai/javascript/ch-2.js | 59 | ||||
| -rw-r--r-- | challenge-005/zapwai/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-005/zapwai/perl/ch-2.pl | 41 | ||||
| -rw-r--r-- | challenge-005/zapwai/python/ch-1.py | 34 | ||||
| -rw-r--r-- | challenge-005/zapwai/python/ch-2.py | 33 | ||||
| -rw-r--r-- | challenge-005/zapwai/rust/ch-1.rs | 53 | ||||
| -rw-r--r-- | challenge-005/zapwai/rust/ch-2.rs | 60 |
13 files changed, 553 insertions, 0 deletions
diff --git a/challenge-005/zapwai/README b/challenge-005/zapwai/README new file mode 100644 index 0000000000..037b3777ef --- /dev/null +++ b/challenge-005/zapwai/README @@ -0,0 +1 @@ +Solutions by David Ferrone. diff --git a/challenge-005/zapwai/c/005.h b/challenge-005/zapwai/c/005.h new file mode 100644 index 0000000000..dacf3daa66 --- /dev/null +++ b/challenge-005/zapwai/c/005.h @@ -0,0 +1,70 @@ +#ifndef PWC005_H +#define PWC005_H +// Requires stdlib and string (I use them in main) +#define MAX_WORDS 300000 +#define MAX_WORD_LENGTH 50 + +void alphaset(char**, int, char**); +void alph(int, char*); +char* lc(char*); +void dictset(char**, int*); + +void alphaset(char** alpha, int dict_length, char** dict) { + for (int i = 0; i < dict_length; i++){ + dict[i] = lc(dict[i]); + alpha[i] = malloc(strlen(dict[i]) + 1); + strcpy(alpha[i], dict[i]); + alph(strlen(alpha[i]), alpha[i]); + } +} + +void alph(int wordlen, char* word) { + int cnt; + do { + cnt = 0; + for (int i = 0; i < wordlen - 1; i++) { + if (word[i] > word[i + 1]) { + cnt++; + char tmp = word[i]; + word[i] = word[i + 1]; + word[i + 1] = tmp; + } + } + } while(cnt > 0); +} + +char* lc(char* word) { + char* myword = strdup(word); + char* Alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + char* alph = "abcdefghijklmnopqrstuvwxyz"; + for (int i = 0; i < strlen(word); i++) { + for (int j = 0; j < strlen(Alph); j++) { + if (myword[i] == Alph[j]) { + myword[i] = alph[j]; + } + } + } + return myword; +} + +void dictset(char** dict, int* dict_length) { + FILE* fp = fopen("/usr/share/dict/words", "r"); + if (fp == NULL) { + fprintf(stderr, "Cannot open words file.\n"); + } + char buffer[MAX_WORD_LENGTH]; + int i = 0; + while (fgets(buffer, MAX_WORD_LENGTH, fp)) { + size_t length = strlen(buffer); + dict[i] = malloc(length + 1); + if (buffer[length - 1] == '\n') { + buffer[length - 1] = '\0'; + } + strncpy(dict[i], buffer + 0, length); + i++; + } + fclose(fp); + *dict_length = i; +} + +#endif diff --git a/challenge-005/zapwai/c/ch-1.c b/challenge-005/zapwai/c/ch-1.c new file mode 100644 index 0000000000..cf1e4b1a72 --- /dev/null +++ b/challenge-005/zapwai/c/ch-1.c @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "005.h" + +int main(int argc, char* argv[]) { + char* word = argv[1]; + if (argc != 2) { + fprintf(stderr, "%s: Please provide a word to list anagrams.\n", argv[0]); + return 1; + } + + char* dict[MAX_WORDS]; + int dict_length = 0; + dictset(dict, &dict_length); + + char* alpha[dict_length]; + alphaset(alpha, dict_length, dict); + + char* aword = malloc(strlen(word) + 1); + strcpy(aword, word); + alph(strlen(aword), aword); + for (int i = 0; i < dict_length; i++) { + if (strcmp(aword, alpha[i]) == 0) { + printf("%s\n", dict[i]); + } + } + + free(aword); + for (int i = 0; i < dict_length; i++) { + free(dict[i]); + free(alpha[i]); + } + return 0; +} + diff --git a/challenge-005/zapwai/c/ch-2.c b/challenge-005/zapwai/c/ch-2.c new file mode 100644 index 0000000000..d892bf3271 --- /dev/null +++ b/challenge-005/zapwai/c/ch-2.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "005.h" + +int strcompare(const void* a, const void* b) { + const char* stra = *(const char**)a; + const char* strb = *(const char**)b; + return strcmp(stra, strb); +} + +int main(int argc, char* argv[]) { + char* dict[MAX_WORDS]; + int dict_length = 0; + dictset(dict, &dict_length); + + char* alpha[dict_length]; + alphaset(alpha, dict_length, dict); + + qsort(alpha, dict_length, sizeof(char*), strcompare); + + /* Count number of anagrams */ + int flag = 0; + int maxlen = 0; + char* maxword = ""; + int cnt = 0; + for (int i = 0; i < dict_length - 1; i++) { + if (strlen(alpha[i]) < 4) { + continue; + } + if (flag == 1) { + if (strcmp(alpha[i], alpha[i+1]) != 0) { + flag = 0; + cnt++; + if (maxlen < cnt) { + maxlen = cnt; + maxword = alpha[i]; + } + cnt = 0; + } else { + cnt++; + } + } else { + if (strcmp(alpha[i], alpha[i+1]) == 0) { + flag = 1; + cnt++; + } + } + } + + printf("%d: %s\n", maxlen, maxword); + + for (int i = 0; i < dict_length; i++) { + free(dict[i]); + free(alpha[i]); + } + return 0; +} + diff --git a/challenge-005/zapwai/javascript/005.html b/challenge-005/zapwai/javascript/005.html new file mode 100644 index 0000000000..b613788d30 --- /dev/null +++ b/challenge-005/zapwai/javascript/005.html @@ -0,0 +1,21 @@ +<html> + <head> + <style> + body {background-color: lightgray;} + </style> + </head> + <body> + <br><br> + <center> + <p>Anagram Viewer!</p> + <form id="uploadForm" enctype="multipart/form-data"> + <label for="text">Enter a word:</label> + <input id="ourWord" type="text"><br> + <label for="file">Select a file:</label> + <input id="fileInput" type="file"><br> + </form> + <p id="ourParagraph">Hello World</p> + </center> + <script src="005-2.js"></script> + </body> +</html> diff --git a/challenge-005/zapwai/javascript/ch-1.js b/challenge-005/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..39a6ccd044 --- /dev/null +++ b/challenge-005/zapwai/javascript/ch-1.js @@ -0,0 +1,50 @@ +let dict; + +const fileInput = document.getElementById('fileInput'); +fileInput.addEventListener('change', handleFileUpload); + +const ourWord = document.getElementById('ourWord'); +ourWord.addEventListener('change', onWordChange); + +const ourParagraph = document.getElementById('ourParagraph'); + +function anagramTime(word) { + let sorted = dict.slice(); + sorted.forEach( (w, i) => { + sorted[i] = w.split('').sort().join(""); + }); + + let sorted_word = word.split('').sort().join(''); + + let anagrams = []; + for (let i = 0; i < dict.length; i++) { + if (sorted[i] == sorted_word) { + anagrams.push(dict[i]); + } + } + ourParagraph.innerHTML = + `Input word: ${word}<br>Anagrams: ${anagrams.join(' ')}<br>`; +} + +function onWordChange(event) { + let word = event.target.value; + anagramTime(word); +} + +function handleFileUpload(event) { + const file = event.target.files[0]; + if (!file) { + console.error('No file selected'); + return; + } + const reader = new FileReader(); + reader.onload = function(e) { + const content = e.target.result; + const words = content.split(/\s+/); + dict = words; + let word = document.getElementById('ourWord').value; + anagramTime(word); + }; + reader.readAsText(file); +} + diff --git a/challenge-005/zapwai/javascript/ch-2.js b/challenge-005/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..07ba52c4c2 --- /dev/null +++ b/challenge-005/zapwai/javascript/ch-2.js @@ -0,0 +1,59 @@ +let dict; + +const fileInput = document.getElementById('fileInput'); +fileInput.addEventListener('change', handleFileUpload); + +const ourParagraph = document.getElementById('ourParagraph'); + +function handleFileUpload(event) { + const file = event.target.files[0]; + if (!file) { + console.error('No file selected'); + return; + } + const reader = new FileReader(); + reader.onload = function(e) { + const content = e.target.result; + const words = content.split(/\s+/); + dict = words; + mostAnagrams(); + }; + reader.readAsText(file); +} + +function mostAnagrams() { + let sorted = dict.slice(); + sorted.forEach( (w, i) => { + sorted[i] = w.split('').sort().join(""); + }); + sorted.sort(); + + let maxnum = 0; + let maxword = ""; + + let flag = 0; + let cnt = 0; + for (let i = 0; i < sorted.length; i++) { + if (flag == 1) { + if (sorted[i+1] != sorted[i]) { + flag = 0; + cnt += 1; + if (maxnum < cnt) { + maxnum = cnt; + maxword = sorted[i]; + } + cnt = 0; + } + cnt += 1; + } else { + if (sorted[i + 1] == sorted[i]) { + cnt += 1; + flag = 1; + } + } + } + + ourParagraph.innerHTML = + `Max number of anagrams: ${maxnum}<br> +Characters: ${maxword}<br>`; +} diff --git a/challenge-005/zapwai/perl/ch-1.pl b/challenge-005/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..82289e6eff --- /dev/null +++ b/challenge-005/zapwai/perl/ch-1.pl @@ -0,0 +1,36 @@ +use v5.36; +my $word = $ARGV[0] || "door"; +say "Input: $word"; +my @let = split "", $word; +my %words; +sub L { + my ($k, $list) = @_; + if ($k == 1) { + $words{join("",@$list)} = 1; + } else { + L($k-1, $list); + for my $i (0 .. $k-2) { + if ($k % 2 == 0) { + swap($i, $k-1, $list); + } else { + swap(0, $k-1, $list); + } + L($k-1,$list); + } + } +} +sub swap { + my ($i, $j, $list) = @_; + my $hold = $$list[$i]; + $$list[$i] = $$list[$j]; + $$list[$j] = $hold; +} + +my $file = "/usr/share/dict/words"; +open my $fh, "<", $file; +my @file = <$fh>; +close $fh; +L(length $word, \@let); +foreach my $keyword (keys %words){ + print grep { lc($_) =~ /^$keyword$/ } @file; +} diff --git a/challenge-005/zapwai/perl/ch-2.pl b/challenge-005/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..d45fbdfe49 --- /dev/null +++ b/challenge-005/zapwai/perl/ch-2.pl @@ -0,0 +1,41 @@ +use v5.36; +my $file = "/usr/share/dict/words"; +open my $fh, "<", $file or die; +my @file = <$fh>; +close $fh; +chomp @file; +my @arr; +foreach my $word (@file) { + $word = lc $word; + my @let = split "", $word; + push @arr, join("", sort @let); +} +my @dict = sort @arr; +my ($maxlen, $maxword) = (0, ""); +my $flag = 0; +my $cnt = 0; +for my $i (0 .. $#dict) { + if (length $dict[$i] < 4) { + next; + } + if ($flag == 1) { + if (!($dict[$i+1] eq $dict[$i])) { + $flag = 0; + $cnt++; + if ($maxlen < $cnt) { + $maxlen = $cnt; + $maxword = $dict[$i]; + } + $cnt = 0; + } else { + $cnt++; + } + } else { + if ($dict[$i+1] eq $dict[$i]) { + $flag = 1; + $cnt++; + } + } +} +say "num of anagrams: $maxlen, using letters: $maxword"; +system("perl ch-1.pl $maxword"); diff --git a/challenge-005/zapwai/python/ch-1.py b/challenge-005/zapwai/python/ch-1.py new file mode 100644 index 0000000000..f9e2325e66 --- /dev/null +++ b/challenge-005/zapwai/python/ch-1.py @@ -0,0 +1,34 @@ +import sys +def a(word): + A = [] + if len(word) == 1: + A.append(word) + return A + letters = list(word) + for i in range(len(word)): + leftover = "" + for j in range(len(word)): + if i == j: + continue + else: + leftover += letters[j] + new_words = a(leftover) + for w in new_words: + new_word = letters[i] + w + A.append(new_word) + return A +args = sys.argv[1:] +for word in args: + words = set(a(word.lower())) + fn = "/usr/share/dict/words" + with open(fn, "r") as file: + lines = file.readlines() + file.close() + for i in range(len(lines)): + lines[i] = lines[i].lower().rstrip("\n") + output = [] + for w in words: + if w in lines: + output.append(w) + for out in output: + print(out) diff --git a/challenge-005/zapwai/python/ch-2.py b/challenge-005/zapwai/python/ch-2.py new file mode 100644 index 0000000000..ca1d4678bb --- /dev/null +++ b/challenge-005/zapwai/python/ch-2.py @@ -0,0 +1,33 @@ +fn = "/usr/share/dict/words" +ourlist = [] # words file but the words letters are rearranged +with open(fn,"r") as file: + lines = file.readlines() +file.close() +for i in range(len(lines)): + lines[i] = lines[i].lower().rstrip("\n") +for line in lines: + ourlist.append(''.join(sorted(line))) # letters in alphabetical order +thelist = sorted(ourlist) +maxlen = 0 +maxword = "" +flag = 0 +cnt = 0 +for i in range(len(thelist) - 1): + if len(thelist[i]) < 4: + continue + if flag == 1: + if thelist[i+1] != thelist[i]: + flag = 0 + cnt += 1 + if maxlen < cnt: + maxlen = cnt + maxword = thelist[i] + cnt = 0 + else: + cnt += 1 + if thelist[i+1] == thelist[i]: + if flag == 0: + flag = 1 + cnt = 1 + +print(maxword, maxlen) diff --git a/challenge-005/zapwai/rust/ch-1.rs b/challenge-005/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..c3c05707e3 --- /dev/null +++ b/challenge-005/zapwai/rust/ch-1.rs @@ -0,0 +1,53 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; + +fn main() -> std::io::Result<()> { + let args : Vec<_> = std::env::args().collect(); + if args.len() != 2 { + println!("Usage: {} <word>", args[0]); + println!("Output: Anagrams!"); + return Ok(()); + } + + let filename = "/usr/share/dict/words"; + let dict = read_dictionary(filename)?; + + let mut alph = dict.clone(); + alphabetize(&mut alph); + + let sorted_word = sort_chars(&args[1]); + + for i in 0 .. alph.len() { + if alph[i] == sorted_word { + println!("{}", dict[i]); + } + } + + Ok(()) +} + +fn alphabetize(list : &mut Vec<String>) { + for l in list.iter_mut() { + let mut alpha : Vec<char> = l.chars().collect(); + alpha.sort(); + *l = alpha.into_iter().collect(); + } +} + +fn read_dictionary(filename :&str) -> std::io::Result<Vec<String>> { + let file = File::open(filename)?; + let reader = BufReader::new(file); + let mut dict = Vec::new(); + for word in reader.lines() { + let word = word?.trim().to_lowercase(); + dict.push(word); + } + Ok(dict) +} + +fn sort_chars(word: &str) -> String { + let mut thing : Vec<char> = word.chars().collect(); + thing.sort(); + let sorted_word : String = thing.into_iter().collect(); + return sorted_word; +} diff --git a/challenge-005/zapwai/rust/ch-2.rs b/challenge-005/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..ff673272e8 --- /dev/null +++ b/challenge-005/zapwai/rust/ch-2.rs @@ -0,0 +1,60 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; + +fn main() -> std::io::Result<()> { + let filename = "/usr/share/dict/words"; + let dict = read_dictionary(filename)?; + + let mut alph = dict.clone(); + alphabetize(&mut alph); + alph.sort(); + + let mut flag : bool = false; + let mut cnt : u8 = 0; + let mut maxlen : u8 = 0; + let mut maxword : String = String::from(""); + + for i in 0..alph.len()-1 { + if flag { + if alph[i] != alph[i+1] { + flag = false; + cnt += 1; + if maxlen < cnt { + maxlen = cnt; + maxword = alph[i].clone(); + } + cnt = 0; + } + cnt += 1; + } else { + if alph[i] == alph[i+1] { + flag = true; + cnt += 1; + } + } + } + + println!("{maxlen}: {maxword}"); + + Ok(()) +} + +fn alphabetize(list : &mut Vec<String>) { + for l in list.iter_mut() { + let mut alpha : Vec<char> = l.chars().collect(); + alpha.sort(); + *l = alpha.into_iter().collect(); + } +} + +fn read_dictionary(filename :&str) -> std::io::Result<Vec<String>> { + let file = File::open(filename)?; + let reader = BufReader::new(file); + let mut dict = Vec::new(); + for word in reader.lines() { + let word = word?.trim().to_lowercase(); + dict.push(word); + } + + Ok(dict) +} |
