From 379206ccf0a24111b37b9c6996dc21fe576a3e6b Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 11 Mar 2024 18:17:44 -0400 Subject: 5 Years of PWC! --- challenge-260/zapwai/c/ch-1.c | 84 +++++++++++++++++++++++++++++++++ challenge-260/zapwai/c/ch-2.c | 81 +++++++++++++++++++++++++++++++ challenge-260/zapwai/javascript/ch-1.js | 21 +++++++++ challenge-260/zapwai/javascript/ch-2.js | 40 ++++++++++++++++ challenge-260/zapwai/perl/ch-1.pl | 25 ++++++++++ challenge-260/zapwai/perl/ch-2.pl | 43 +++++++++++++++++ challenge-260/zapwai/python/ch-1.py | 27 +++++++++++ challenge-260/zapwai/python/ch-2.py | 34 +++++++++++++ challenge-260/zapwai/rust/ch-1.rs | 30 ++++++++++++ challenge-260/zapwai/rust/ch-2.rs | 51 ++++++++++++++++++++ 10 files changed, 436 insertions(+) create mode 100644 challenge-260/zapwai/c/ch-1.c create mode 100644 challenge-260/zapwai/c/ch-2.c create mode 100644 challenge-260/zapwai/javascript/ch-1.js create mode 100644 challenge-260/zapwai/javascript/ch-2.js create mode 100644 challenge-260/zapwai/perl/ch-1.pl create mode 100644 challenge-260/zapwai/perl/ch-2.pl create mode 100644 challenge-260/zapwai/python/ch-1.py create mode 100644 challenge-260/zapwai/python/ch-2.py create mode 100644 challenge-260/zapwai/rust/ch-1.rs create mode 100644 challenge-260/zapwai/rust/ch-2.rs (limited to 'challenge-260') diff --git a/challenge-260/zapwai/c/ch-1.c b/challenge-260/zapwai/c/ch-1.c new file mode 100644 index 0000000000..ec3b7577b9 --- /dev/null +++ b/challenge-260/zapwai/c/ch-1.c @@ -0,0 +1,84 @@ +#include +#include +#define MAX 100 +#define BORK -111111 /* unusable key/value */ + +struct hash { + int key[MAX]; + int val[MAX]; + int length; +}; + +void init(struct hash *h) { + for (int i = 0; i < MAX; i++) { + h->key[i] = BORK; + h->val[i] = BORK; + } + h->length = 0; +} + +int get(struct hash h, int key) { + for (int i = 0; i < MAX; i++) + if (h.key[i] == key) + return h.val[i]; + return BORK; +} + +void set(struct hash *h, int key, int value) { + for (int i = 0; i < MAX; i++) + if (h->key[i] == key) + h->val[i] = value; +} + +void insert(struct hash *h, int key, int value) { + int index = h->length; + h->key[index] = key; + h->val[index] = value; + h->length++; +} + +bool contains(struct hash h, int key) { + for (int i = 0; i < MAX; i++) + if (h.key[i] == key) + return true; + return false; +} + +int has_uniq_freq(struct hash h) { + for (int i = 1; i < h.length; i++) + for (int j = 0; j < i; j++) + if (h.val[j] == h.val[i]) + return 0; + return 1; +} + +void proc(int len, int l[]) { + printf("Input: { "); + for (int i = 0; i < len; i++) + printf("%d ", l[i]); + printf("}\n"); + + struct hash f; + init(&f); + + for (int i = 0; i < len; i++) + if (contains(f, l[i])) + set(&f, l[i], get(f,l[i]) + 1); + else + insert(&f, l[i], 1); + + printf("Output %d\n", has_uniq_freq(f)); +} + +int main() { + int l1[] = {1,2,2,1,1,3}; + int l2[] = {1,2,3}; + int l3[] = {-2,0,1,-2,1,1,0,1,-2,9}; + int s1 = sizeof(l1) / sizeof(int); + int s2 = sizeof(l2) / sizeof(int); + int s3 = sizeof(l3) / sizeof(int); + proc(s1, l1); + proc(s2, l2); + proc(s3, l3); +} + diff --git a/challenge-260/zapwai/c/ch-2.c b/challenge-260/zapwai/c/ch-2.c new file mode 100644 index 0000000000..a499718f35 --- /dev/null +++ b/challenge-260/zapwai/c/ch-2.c @@ -0,0 +1,81 @@ +#include +#include +#include +#define MAX_WORD_LEN 50 + +void sortme(char** words, int wordlen) { + int cnt; + do { + cnt = 0; + for (int i = 0; i < wordlen - 1; i++) + if (strcmp(words[i+1], words[i]) < 0) { + char* tmp = words[i+1]; + words[i+1] = words[i]; + words[i] = tmp; + cnt++; + } + } while (cnt > 0); +} + +void insert_uniquely(char* word, int wordlen, char** h, int* hlen) { + for (int i = 0; i < *hlen; i++) + if (0 == strcmp(h[i], word)) + return; + h[*hlen] = malloc(MAX_WORD_LEN * sizeof(char)); + strncpy(h[*hlen], word, wordlen); + ++*hlen; +} + +int seek_word(char* word, char** sorted, int sortlen) { + for (int i = 0; i < sortlen; i++) + if (0 == strcmp(sorted[i], word)) + return i; + return -1; +} + +void swap(int i, int j, char list[]) { + char tmp = list[i]; + list[i] = list[j]; + list[j] = tmp; +} + +void L(int k, char list[], char** h, int *hlen, int wordlength) { + if (k == 1) + insert_uniquely(list, wordlength, h, hlen); + else { + L(k - 1, list, h, hlen, wordlength); + for (int i = 0; i <= k - 2; i++) { + if (k % 2 == 0) + swap(i, k-1, list); + else + swap(0, k-1, list); + L(k - 1, list, h, hlen, wordlength); + } + } +} + +void proc(char* word) { + printf("Input: %s\n", word); + char** h = malloc(1000 * sizeof(char*)); + int hlen = 0; + + char list[strlen(word)]; + for (int i = 0; i < strlen(word); i++) + list[i] = word[i]; + + L(strlen(word), list, h, &hlen, strlen(word)); + + sortme(h, hlen); + + int ind = seek_word(word, h, hlen); + printf("Output: %d\n", 1 + ind); + for (int i = 0; i < hlen; i++) + free(h[i]); + free(h); +} + +int main() { + proc("CAT"); + proc("GOGGLE"); + proc("SECRET"); +} diff --git a/challenge-260/zapwai/javascript/ch-1.js b/challenge-260/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..3e4fbc4b42 --- /dev/null +++ b/challenge-260/zapwai/javascript/ch-1.js @@ -0,0 +1,21 @@ +let l1 = [1,2,2,1,1,3]; +let l2 = [1,2,3]; +let l3 = [-2,0,1,-2,1,1,0,1,-2,9]; +for (let l of [l1, l2, l3]) + proc(l); + +function proc(l) { + console.log("Input:", l); + let f = {}; + for (let i of l) + (i in f) ? f[i]++ : f[i] = 1; + let freq = []; + let output = 1; + for (let v of Object.values(f)) { + if (freq.includes(v)) + output = 0; + else + freq.push(v); + } + console.log("Output:", output); +} diff --git a/challenge-260/zapwai/javascript/ch-2.js b/challenge-260/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..c7078f49fa --- /dev/null +++ b/challenge-260/zapwai/javascript/ch-2.js @@ -0,0 +1,40 @@ +function proc(word) { + console.log("Input:", word); + let h = {}; + L(word.length, word.split(''), h); + let sorted = Object.keys(h).sort(); + console.log("Output:", 1 + seek_word(word, sorted)); +} + +function L(k, list, h) { + if (k == 1) + h[list.join('')] = 1; + else { + L(k-1, list, h); + for (let i = 0; i < k-1; i++) { + if (k % 2 == 0) + swap(i, k-1, list); + else + swap(0, k-1, list); + L(k-1, list, h); + } + } +} + +function swap(i, j, list) { + let tmp = list[i]; + list[i] = list[j]; + list[j] = tmp; +} + +function seek_word(word, sorted) { + for (let i = 0; i < sorted.length; i++) + if (sorted[i] == word) + return i; + + return -1; +} + +proc("CAT"); +proc("GOGGLE"); +proc("SECRET"); diff --git a/challenge-260/zapwai/perl/ch-1.pl b/challenge-260/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..b4c19a9415 --- /dev/null +++ b/challenge-260/zapwai/perl/ch-1.pl @@ -0,0 +1,25 @@ +use v5.36; +my @ints = (1,2,2,1,1,3); +proc(@ints); +my @ints2 = (1,2,3); +proc(@ints2); +my @ints3 = (-2,0,1,-2,1,1,0,1,-2,9); +proc(@ints3); + +sub proc(@ints) { + say "Input: \@ints = (", join(", ", @ints).")"; + say "Output: ", has_uniq_freq(@ints); +} + +sub has_uniq_freq(@ints) { + my %f = map { $_, 0 } @ints; + $f{$_}++ foreach (@ints); + my %g = map { $_, 0 } (values %f); + $g{$_}++ foreach (values %f); + for ( values %g ) { + if ($_ > 1) { + return 0; + } + } + return 1; +} diff --git a/challenge-260/zapwai/perl/ch-2.pl b/challenge-260/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..d186c35bc3 --- /dev/null +++ b/challenge-260/zapwai/perl/ch-2.pl @@ -0,0 +1,43 @@ +use v5.36; + +sub proc($word) { + my %words; + say "Input: $word"; + my @let = split "", $word; + L(length $word, \@let, \%words); + my @sorted = sort keys %words; + say "Output: ", 1 + seek_word($word, @sorted); +} + +sub L($k, $list, $r) { + if ($k == 1) { + $$r{join("",@$list)} = 1; + } else { + L($k-1, $list, $r); + 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, $r); + } + } +} + +sub swap($i, $j, $list) { + my $hold = $$list[$i]; + $$list[$i] = $$list[$j]; + $$list[$j] = $hold; +} + +sub seek_word ($word, @sorted) { + for my $i (0 .. $#sorted) { + return $i if ($sorted[$i] eq $word); + } + return -1; +} + +proc("CAT"); +proc("GOOGLE"); +proc("SECRET"); diff --git a/challenge-260/zapwai/python/ch-1.py b/challenge-260/zapwai/python/ch-1.py new file mode 100644 index 0000000000..4de8664aea --- /dev/null +++ b/challenge-260/zapwai/python/ch-1.py @@ -0,0 +1,27 @@ +def has_uniq_freq(l): + freq = {} + for item in l: + if item in freq.keys(): + freq[item] += 1 + else: + freq[item] = 1 + gq = {} + for v in freq.values(): + if v in gq.keys(): + gq[v] += 1 + else: + gq[v] = 0 + for v in gq.values(): + if v > 1: + return 0 + return 1 + +def proc(l): + print("Input:", l); + print("Output:", has_uniq_freq(l)) + +l1 = [1,2,2,1,1,3] +l2 = [1,2,3] +l3 = [-2,0,1,-2,1,1,0,1,-2,9] +for l in [l1, l2, l3]: + proc(l) diff --git a/challenge-260/zapwai/python/ch-2.py b/challenge-260/zapwai/python/ch-2.py new file mode 100644 index 0000000000..bdfe08eca0 --- /dev/null +++ b/challenge-260/zapwai/python/ch-2.py @@ -0,0 +1,34 @@ +def seek_word(word, sorts): + for i in range(len(sorts)): + if sorts[i] == word: + return i + return -1 + +def L(k, mylist, h): + if k == 1: + h[''.join(mylist)] = 1 + else: + L(k-1, mylist, h) + for i in range(k-1): + if k % 2 == 0: + swap(i, k-1, mylist) + else: + swap(0, k-1, mylist) + L(k-1, mylist, h) + +def swap(i, j, mylist): + tmp = mylist[i] + mylist[i] = mylist[j] + mylist[j] = tmp + +def proc(word): + perms = {} + print("Input:", word) + let = list(word) + L(len(word), let, perms) + sorts = sorted(perms.keys()) + print("Output", 1 + seek_word(word, sorts)) + +proc("CAT") +proc("GOGGLE") +proc("SECRET") diff --git a/challenge-260/zapwai/rust/ch-1.rs b/challenge-260/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..7e0cf5afe2 --- /dev/null +++ b/challenge-260/zapwai/rust/ch-1.rs @@ -0,0 +1,30 @@ +use std::collections::HashMap; + +fn main() { + let l1 = [1,2,2,1,1,3]; + let l2 = [1,2,3]; + let l3 = [-2,0,1,-2,1,1,0,1,-2,9]; + let lists : Vec<&[i32]> = vec![&l1, &l2, &l3]; + for l in lists { + proc(&l); + } +} + +fn proc(l : &[i32]) { + println!("Input: {:?}", l); + let mut f = HashMap::new(); + for k in l { + let entry = f.entry(k).or_insert(0); + *entry += 1; + } + let mut freqs = vec![]; + let mut out = 1; + for v in f.values() { + if freqs.contains(v) { + out = 0; + } else { + freqs.push(*v); + } + } + println!("Output: {out}"); +} diff --git a/challenge-260/zapwai/rust/ch-2.rs b/challenge-260/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..62322619c3 --- /dev/null +++ b/challenge-260/zapwai/rust/ch-2.rs @@ -0,0 +1,51 @@ +use std::collections::HashMap; + +fn proc(word : &str) { + let mut h = HashMap::new(); + println!("Input: {}", word); + let mut letters = word.chars().collect::>(); + l(word.len(), &mut letters, &mut h); + let mut sorted :Vec = Vec::new(); + for k in h.keys() { + sorted.push(k.to_string()); + } + sorted.sort(); + println!("Output {}", 1 + seek_word(word, sorted)); +} + +fn l(k :usize, list : &mut Vec, h : &mut HashMap) { + if k == 1 { + h.insert(list.iter().collect(), 1); + } else { + l(k - 1, list, h); + for i in 0 ..= k - 2 { + if k % 2 == 0 { + swap(i, k-1, list); + } else { + swap(0, k-1, list); + } + l(k-1, list, h); + } + } +} + +fn swap(i :usize, j :usize, list :&mut Vec) { + let tmp = list[i]; + list[i] = list[j]; + list[j] = tmp; +} + +fn seek_word(word :&str, sorted : Vec) -> usize{ + for i in 0 .. sorted.len() { + if sorted[i] == word.to_string(){ + return i; + } + } + return 10000; +} + +fn main() { + proc("CAT"); + proc("GOGGLE"); + proc("SECRET"); +} -- cgit From 87dfd7b9a9b717fa6847ee0e0710e2798ea76ce6 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 12 Mar 2024 11:22:11 +1100 Subject: pwc260 solution in python --- challenge-260/pokgopun/python/ch-1.py | 56 +++++++++++++++++++++++++++++ challenge-260/pokgopun/python/ch-2.py | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 challenge-260/pokgopun/python/ch-1.py create mode 100644 challenge-260/pokgopun/python/ch-2.py (limited to 'challenge-260') diff --git a/challenge-260/pokgopun/python/ch-1.py b/challenge-260/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..734382ae7c --- /dev/null +++ b/challenge-260/pokgopun/python/ch-1.py @@ -0,0 +1,56 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-260/ +""" + +Task 1: Unique Occurrences + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to return 1 if the number of occurrences of each value + in the given array is unique or 0 otherwise. + +Example 1 + +Input: @ints = (1,2,2,1,1,3) +Output: 1 + +The number 1 occurred 3 times. +The number 2 occurred 2 times. +The number 3 occurred 1 time. + +All occurrences are unique, therefore the output is 1. + +Example 2 + +Input: @ints = (1,2,3) +Output: 0 + +Example 3 + +Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) +Output: 1 + +Task 2: Dictionary Rank +""" +### solution by pokgopun@gmail.com + +def UO(tup: tuple): + s = set(tup) + return len(s)==len(set(tup.count(e) for e in s)) + +import unittest + +class TestUO(unittest.TestCase): + def test(self): + for inpt, otpt in { + (1,2,2,1,1,3): 1, + (1,2,3): 0, + (-2,0,1,-2,1,1,0,1,-2,9): 1, + }.items(): + self.assertEqual(UO(inpt),otpt) + +unittest.main() + + diff --git a/challenge-260/pokgopun/python/ch-2.py b/challenge-260/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..6925831197 --- /dev/null +++ b/challenge-260/pokgopun/python/ch-2.py @@ -0,0 +1,68 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-260/ +""" + +Task 2: Dictionary Rank + +Submitted by: [43]Mark Anderson + __________________________________________________________________ + + You are given a word, $word. + + Write a script to compute the dictionary rank of the given word. + +Example 1 + +Input: $word = 'CAT' +Output: 3 + +All possible combinations of the letters: +CAT, CTA, ATC, TCA, ACT, TAC + +Arrange them in alphabetical order: +ACT, ATC, CAT, CTA, TAC, TCA + +CAT is the 3rd in the list. +Therefore the dictionary rank of CAT is 3. + +Example 2 + +Input: $word = 'GOOGLE' +Output: 88 + +Example 3 + +Input: $word = 'SECRET' +Output: 255 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 17th March + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from itertools import permutations + +def DC(word: str): + return tuple( + sorted( + set( + permutations(word,len(word)) + ) + ) + ).index(tuple(word)) + 1 + +import unittest + +class TestDC(unittest.TestCase): + def test(self): + for inpt, otpt in { + 'CAT': 3, + 'GOOGLE': 88, + 'SECRET': 255, + }.items(): + self.assertEqual(DC(inpt),otpt) + +unittest.main() -- cgit From 867127a74fe3c558a20cba4b1cd609f8e987e4ba Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 12 Mar 2024 13:50:16 +1100 Subject: pwc260 solution in go --- challenge-260/pokgopun/go/ch-1.go | 104 ++++++++++++++++++++++++++++++++++++++ challenge-260/pokgopun/go/ch-2.go | 98 +++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 challenge-260/pokgopun/go/ch-1.go create mode 100644 challenge-260/pokgopun/go/ch-2.go (limited to 'challenge-260') diff --git a/challenge-260/pokgopun/go/ch-1.go b/challenge-260/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..a401e1588a --- /dev/null +++ b/challenge-260/pokgopun/go/ch-1.go @@ -0,0 +1,104 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/ +/*# + +Task 1: Unique Occurrences + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to return 1 if the number of occurrences of each value + in the given array is unique or 0 otherwise. + +Example 1 + +Input: @ints = (1,2,2,1,1,3) +Output: 1 + +The number 1 occurred 3 times. +The number 2 occurred 2 times. +The number 3 occurred 1 time. + +All occurrences are unique, therefore the output is 1. + +Example 2 + +Input: @ints = (1,2,3) +Output: 0 + +Example 3 + +Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) +Output: 1 + +Task 2: Dictionary Rank +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +type myMap map[int]int + +func newMyMap() myMap { + return make(myMap) +} + +func (mm myMap) proc(is ints) { + clear(mm) + for _, v := range is { + mm[v]++ + } +} + +func (mm myMap) keys() ints { + var s ints + for k := range mm { + s = append(s, k) + } + return s +} + +func (mm myMap) values() ints { + var s ints + for _, v := range mm { + s = append(s, v) + } + return s +} + +func (mm myMap) uo(is ints) bool { + mm.proc(is) + //fmt.Println("mm = ", mm) + count_uniq_elem := len(mm.keys()) + //fmt.Println("count_uniq_elem =", count_uniq_elem) + mm.proc(mm.values()) + //fmt.Println("mm = ", mm) + count_uniq_occur := len(mm.keys()) + //fmt.Println("count_uniq_occur =", count_uniq_occur) + return count_uniq_elem == count_uniq_occur +} + +func main() { + mm := newMyMap() + for _, data := range []struct { + input ints + output bool + }{ + {ints{1, 2, 2, 1, 1, 3}, true}, + {ints{1, 2, 3}, false}, + {ints{-2, 0, 1, -2, 1, 1, 0, 1, -2, 9}, true}, + } { + //fmt.Println(data.input) + io.WriteString(os.Stdout, cmp.Diff(mm.uo(data.input), data.output)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-260/pokgopun/go/ch-2.go b/challenge-260/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..aba232c3e9 --- /dev/null +++ b/challenge-260/pokgopun/go/ch-2.go @@ -0,0 +1,98 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-260/ +/*# + +Task 2: Dictionary Rank + +Submitted by: [43]Mark Anderson + __________________________________________________________________ + + You are given a word, $word. + + Write a script to compute the dictionary rank of the given word. + +Example 1 + +Input: $word = 'CAT' +Output: 3 + +All possible combinations of the letters: +CAT, CTA, ATC, TCA, ACT, TAC + +Arrange them in alphabetical order: +ACT, ATC, CAT, CTA, TAC, TCA + +CAT is the 3rd in the list. +Therefore the dictionary rank of CAT is 3. + +Example 2 + +Input: $word = 'GOOGLE' +Output: 88 + +Example 3 + +Input: $word = 'SECRET' +Output: 255 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 17th March + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + "strings" + + "github.com/google/go-cmp/cmp" +) + +func dc(word string) int { + var res string + s := strings.Split(permute(word, "", &res), " ") + slices.Sort(s) + l := len(s) + i, j := 0, l + for j > 1 { + if s[i] == s[i+1] { + copy(s[i:], s[i+1:]) + l-- + } else { + i++ + } + j-- + } + return slices.Index(s, word) + 1 +} + +func permute(s, t string, res *string) string { + if len(s) > 0 { + for i, v := range []byte(s) { + permute(s[:i]+s[i+1:], t+string(v), res) + } + } else { + *res += " " + t + return "" + } + return (*res)[1:] +} + +func main() { + for _, data := range []struct { + input string + output int + }{ + {"CAT", 3}, + {"GOOGLE", 88}, + {"SECRET", 255}, + } { + io.WriteString(os.Stdout, cmp.Diff(dc(data.input), data.output)) // blank if ok, otherwise show the differences + } +} -- cgit From ce6d2a4181a05281b6a9f56de4830505a1505c4d Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 12 Mar 2024 09:28:03 +0000 Subject: RogerBW solutions for challenge no. 260 --- challenge-260/roger-bell-west/javascript/ch-1.js | 38 +++ challenge-260/roger-bell-west/javascript/ch-2.js | 68 ++++++ challenge-260/roger-bell-west/kotlin/ch-1.kt | 34 +++ challenge-260/roger-bell-west/kotlin/ch-2.kt | 77 ++++++ challenge-260/roger-bell-west/lua/ch-1.lua | 42 ++++ challenge-260/roger-bell-west/lua/ch-2.lua | 102 ++++++++ challenge-260/roger-bell-west/perl/ch-1.pl | 22 ++ challenge-260/roger-bell-west/perl/ch-2.pl | 29 +++ challenge-260/roger-bell-west/postscript/ch-1.ps | 91 +++++++ challenge-260/roger-bell-west/postscript/ch-2.ps | 290 +++++++++++++++++++++++ challenge-260/roger-bell-west/python/ch-1.py | 27 +++ challenge-260/roger-bell-west/python/ch-2.py | 29 +++ challenge-260/roger-bell-west/raku/ch-1.p6 | 19 ++ challenge-260/roger-bell-west/raku/ch-2.p6 | 24 ++ challenge-260/roger-bell-west/ruby/ch-1.rb | 33 +++ challenge-260/roger-bell-west/ruby/ch-2.rb | 36 +++ challenge-260/roger-bell-west/rust/ch-1.rs | 26 ++ challenge-260/roger-bell-west/rust/ch-2.rs | 33 +++ challenge-260/roger-bell-west/scala/ch-1.scala | 36 +++ challenge-260/roger-bell-west/scala/ch-2.scala | 76 ++++++ challenge-260/roger-bell-west/tests.yaml | 36 +++ 21 files changed, 1168 insertions(+) create mode 100755 challenge-260/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-260/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-260/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-260/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-260/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-260/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-260/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-260/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-260/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-260/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-260/roger-bell-west/python/ch-1.py create mode 100755 challenge-260/roger-bell-west/python/ch-2.py create mode 100755 challenge-260/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-260/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-260/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-260/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-260/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-260/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-260/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-260/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-260/roger-bell-west/tests.yaml (limited to 'challenge-260') diff --git a/challenge-260/roger-bell-west/javascript/ch-1.js b/challenge-260/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..f662f3c60d --- /dev/null +++ b/challenge-260/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,38 @@ +#! /usr/bin/node + +"use strict" + +function uniqueoccurrences(a) { + let c = new Map; + for (let n of a) { + if (c.has(n)) { + c.set(n, c.get(n) + 1); + } else { + c.set(n, 1); + } + } + if (c.size == new Set(c.values()).size) { + return 1; + } else { + return 0; + } +} + +if (uniqueoccurrences([1, 2, 2, 1, 1, 3]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (uniqueoccurrences([1, 2, 3]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (uniqueoccurrences([-2, 0, 1, -2, 1, 1, 0, 1, -2, 9]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-260/roger-bell-west/javascript/ch-2.js b/challenge-260/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..0c7fc40efd --- /dev/null +++ b/challenge-260/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,68 @@ +#! /usr/bin/node + +"use strict" + +function permute(a) { + let out = []; + let n=a.length; + let c=[]; + for (let i = 0; i < n; i++) { + c.push(0); + } + out.push([...a]); + let i=0; + while (true) { + if (i >= n) { + break; + } + if (c[i] < i) { + if (i % 2 == 0) { + [a[0],a[i]] = [a[i],a[0]]; + } else { + [a[c[i]],a[i]] = [a[i],a[c[i]]]; + } + out.push([...a]); + c[i]++; + i=0; + } else { + c[i]=0; + i++; + } + } + return out; +} + +function dictionaryrank(a) { + const c = a.split(""); + let d = new Set; + for (let o of permute(c)) { + d.add(o.join("")); + } + let dd = new Array(...d); + dd.sort(); + for (let i = 0; i < dd.length; i++) { + if (dd[i] == a) { + return i + 1; + } + } + return 0; +} + +if (dictionaryrank('CAT') == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (dictionaryrank('GOOGLE') == 88) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (dictionaryrank('SECRET') == 255) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-260/roger-bell-west/kotlin/ch-1.kt b/challenge-260/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..844714ccce --- /dev/null +++ b/challenge-260/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,34 @@ +fun uniqueoccurrences(a: List): Int { + var c = mutableMapOf().withDefault({0}) + for (n in a) { + c.set(n, c.getValue(n) + 1) + } + if (c.size == c.values.toSet().size) { + return 1 + } else { + return 0 + } +} + +fun main() { + + if (uniqueoccurrences(listOf(1, 2, 2, 1, 1, 3)) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (uniqueoccurrences(listOf(1, 2, 3)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (uniqueoccurrences(listOf(-2, 0, 1, -2, 1, 1, 0, 1, -2, 9)) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-260/roger-bell-west/kotlin/ch-2.kt b/challenge-260/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..c437a9e4fd --- /dev/null +++ b/challenge-260/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,77 @@ +fun permute(aa: List): ArrayList> { + var a = ArrayList() + for (i in aa) { + a.add(i) + } + var out = ArrayList>() + val n = a.size + var c = ArrayList(); + for (i in 0..n-1) { + c.add(0) + } + out.add(a.toList()) + var i = 0 + while (true) { + if (i >= n) { + break + } + if (c[i] < i) { + if (i % 2 == 0) { + val tmp = a[0] + a[0] = a[i] + a[i] = tmp + } else { + val tmp = a[c[i]] + a[c[i]] = a[i] + a[i] = tmp + } + out.add(a.toList()) + c[i] += 1 + i = 0 + } else { + c[i] = 0 + i += 1 + } + } + return out +} + +fun dictionaryrank(a: String): Int { + val c = a.toCharArray().toList() + var d = mutableSetOf() + for (o in permute(c)) { + d.add(o.joinToString("")) + } + var dd = ArrayList(d) + dd.sort() + var r = 0 + dd.forEachIndexed{i, s -> + if (s == a) { + r = i + 1 + } + } + return r +} + +fun main() { + + if (dictionaryrank("CAT") == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (dictionaryrank("GOOGLE") == 88) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (dictionaryrank("SECRET") == 255) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-260/roger-bell-west/lua/ch-1.lua b/challenge-260/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..2cdfb441b3 --- /dev/null +++ b/challenge-260/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,42 @@ +#! /usr/bin/lua + +function uniqueoccurrences(a) + local c = {} + for _, n in ipairs(a) do + if c[n] == nil then + c[n] = 1 + else + c[n] = c[n] + 1 + end + end + local s = {} + for _, v in pairs(c) do + if s[v] ~= nil then + return 0 + end + s[v] = true + end + return 1 +end + +if uniqueoccurrences({1, 2, 2, 1, 1, 3}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if uniqueoccurrences({1, 2, 3}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if uniqueoccurrences({-2, 0, 1, -2, 1, 1, 0, 1, -2, 9}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-260/roger-bell-west/lua/ch-2.lua b/challenge-260/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..d9407cf3ad --- /dev/null +++ b/challenge-260/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,102 @@ +#! /usr/bin/lua + +function deepcopy(src) + local dst = {} + for k, v in pairs(src) do + if type(v) == "table" then + v = deepcopy(v) + end + dst[k] = v + end + return dst +end + +function permute(a) + local out = {} + local n = #a + local c = {} + for i = 1,n do + table.insert(c, 1) + end + table.insert(out, deepcopy(a)) + local i=1 + while true do + if i > n then + break + end + if c[i] < i then + if i % 2 == 1 then + a[1],a[i] = a[i],a[1] + else + a[c[i]],a[i] = a[i],a[c[i]] + end + table.insert(out, deepcopy(a)) + c[i] = c[i]+1 + i = 1 + else + c[i] = 1 + i = i+1 + end + end + return out +end + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function join(t) + local out="" + for k,v in pairs(t) do + out = out .. v + end + return out +end + +function dictionaryrank(a) + local c = split(a) + local d = {} + for _, o in ipairs(permute(c)) do + d[join(o)] = true + end + local dd = {} + for k, _ in pairs(d) do + table.insert(dd, k) + end + table.sort(dd) + for i, s in ipairs(dd) do + if s == a then + return i + end + end + return 0 +end + +if dictionaryrank("CAT") == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if dictionaryrank("GOOGLE") == 88 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if dictionaryrank("SECRET") == 255 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-260/roger-bell-west/perl/ch-1.pl b/challenge-260/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..b644823003 --- /dev/null +++ b/challenge-260/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(uniqueoccurrences([1, 2, 2, 1, 1, 3]), 1, 'example 1'); +is(uniqueoccurrences([1, 2, 3]), 0, 'example 2'); +is(uniqueoccurrences([-2, 0, 1, -2, 1, 1, 0, 1, -2, 9]), 1, 'example 3'); + +sub uniqueoccurrences($a) { + my %c; + map {$c{$_}++} @{$a}; + my %d = map {$_ => 1} values %c; + if (scalar keys %d == scalar keys %c) { + return 1; + } else { + return 0; + } +} diff --git a/challenge-260/roger-bell-west/perl/ch-2.pl b/challenge-260/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..ca2db96207 --- /dev/null +++ b/challenge-260/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,29 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(dictionaryrank('CAT'), 3, 'example 1'); +is(dictionaryrank('GOOGLE'), 88, 'example 2'); +is(dictionaryrank('SECRET'), 255, 'example 3'); + +use Algorithm::Combinatorics qw(permutations); + +sub dictionaryrank($a) { + my @c = split '', $a; + my %d; + my $ip = permutations(\@c); + while (my $c = $ip->next) { + $d{join('', @{$c})} = 1; + } + my @dd = sort keys %d; + foreach my $i (0 .. $#dd) { + if ($dd[$i] eq $a) { + return $i + 1; + } + } + return 0; +} diff --git a/challenge-260/roger-bell-west/postscript/ch-1.ps b/challenge-260/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..602ce57d34 --- /dev/null +++ b/challenge-260/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,91 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/values { % dict -> array of dict values + [ exch + { + exch pop + } forall + ] +} bind def + +/toset { % array -> dict of (value, true) + << exch + { + true + } forall + >> +} bind def + +/dget { + 3 1 roll + 2 copy + known { + get exch pop + } { + pop pop + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + + +% end included library code + +/uniqueoccurrences { + 0 dict begin + /c 0 dict def + { + dup c exch 0 dget 1 add c 3 1 roll put + } forall + c values toset keys length c keys length eq { + 1 + } { + 0 + } ifelse + end +} bind def + +(uniqueoccurrences) test.start +[1 2 2 1 1 3] uniqueoccurrences 1 eq test +[1 2 3] uniqueoccurrences 0 eq test +[-2 0 1 -2 1 1 0 1 -2 9] uniqueoccurrences 1 eq test +test.end diff --git a/challenge-260/roger-bell-west/postscript/ch-2.ps b/challenge-260/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..d35a2a9777 --- /dev/null +++ b/challenge-260/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,290 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/permute { % [array] {proc} permute runs proc on each permutation of array + 7 dict begin + /permute.subproc exch def + /permute.a exch def + /permute.n permute.a length def + /permute.c [ permute.n { 0 } repeat ] def + permute.a permute.subproc + /permute.i 0 def + { + permute.i permute.n ge { + exit + } if + permute.c permute.i get permute.i lt { + permute.i 2 mod 0 eq { + 0 permute.i permute.swap + } { + permute.c permute.i get permute.i permute.swap + } ifelse + permute.a permute.subproc + permute.c permute.i get 1 add permute.c exch permute.i exch put + /permute.i 0 def + } { + permute.c permute.i 0 put + /permute.i permute.i 1 add def + } ifelse + } loop + end +} bind def + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + +/permute.swap { + /permute.bi exch def + /permute.ai exch def + permute.a permute.ai get + permute.a permute.bi get + permute.a exch permute.ai exch put + permute.a exch permute.bi exch put +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + end +} bind def + +/a2s { + 2 dict begin + /i exch def + i length dup string /o exch def + 1 sub 0 exch 1 exch { + dup i 3 -1 roll get o 3 1 roll put + } for + o + end +} bind def + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} bind def + +/s2a { + [ exch { } forall ] +} bind def + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 2 dict begin + /cmp exch def + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + + +% end included library code + +/dictionaryrank { + 0 dict begin + /a exch def + /l a length def + /d 0 dict def + a s2a { a2s d exch true put } permute + d keys { l string cvs } map quicksort enumerate.array { + aload pop + a deepeq { + 1 add + exit + } { + pop + } ifelse + } forall + end +} bind def + +(dictionaryrank) test.start +(CAT) dictionaryrank 3 eq test +(GOOGLE) dictionaryrank 88 eq test +(SECRET) dictionaryrank 255 eq test +test.end diff --git a/challenge-260/roger-bell-west/python/ch-1.py b/challenge-260/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..581f24f050 --- /dev/null +++ b/challenge-260/roger-bell-west/python/ch-1.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +from collections import defaultdict + +def uniqueoccurrences(a): + c = defaultdict(lambda: 0) + for v in a: + c[v] += 1 + if len(c) == len(set(c.values())): + return 1 + else: + return 0 + +import unittest + +class TestUniqueoccurrences(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(uniqueoccurrences([1, 2, 2, 1, 1, 3]), 1, 'example 1') + + def test_ex2(self): + self.assertEqual(uniqueoccurrences([1, 2, 3]), 0, 'example 2') + + def test_ex3(self): + self.assertEqual(uniqueoccurrences([-2, 0, 1, -2, 1, 1, 0, 1, -2, 9]), 1, 'example 3') + +unittest.main() diff --git a/challenge-260/roger-bell-west/python/ch-2.py b/challenge-260/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..81eb658cbd --- /dev/null +++ b/challenge-260/roger-bell-west/python/ch-2.py @@ -0,0 +1,29 @@ +#! /usr/bin/python3 + +from itertools import permutations + +def dictionaryrank(a): + d = set() + for o in permutations(a): + d.add("".join(o)) + dd = list(d) + dd.sort() + for i, s in enumerate(dd): + if s == a: + return i + 1 + return 0 + +import unittest + +class TestDictionaryrank(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(dictionaryrank("CAT"), 3, 'example 1') + + def test_ex2(self): + self.assertEqual(dictionaryrank("GOOGLE"), 88, 'example 2') + + def test_ex3(self): + self.assertEqual(dictionaryrank("SECRET"), 255, 'example 3') + +unittest.main() diff --git a/challenge-260/roger-bell-west/raku/ch-1.p6 b/challenge-260/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..42523b7149 --- /dev/null +++ b/challenge-260/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(uniqueoccurrences([1, 2, 2, 1, 1, 3]), 1, 'example 1'); +is(uniqueoccurrences([1, 2, 3]), 0, 'example 2'); +is(uniqueoccurrences([-2, 0, 1, -2, 1, 1, 0, 1, -2, 9]), 1, 'example 3'); + +sub uniqueoccurrences(@a) { + my %c; + @a.map({%c{$_}++}); + if (Set(%c.values).elems == %c.elems) { + return 1; + } else { + return 0; + } +} diff --git a/challenge-260/roger-bell-west/raku/ch-2.p6 b/challenge-260/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..57cd6222cb --- /dev/null +++ b/challenge-260/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,24 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(dictionaryrank('CAT'), 3, 'example 1'); +is(dictionaryrank('GOOGLE'), 88, 'example 2'); +is(dictionaryrank('SECRET'), 255, 'example 3'); + +sub dictionaryrank($a) { + my @c = $a.comb; + my %d = SetHash.new; + for @c.permutations -> @o { + %d{@o.join("")}++; + } + my @dd = %d.keys.sort; + for 0 .. @dd.end -> $i { + if (@dd[$i] eq $a) { + return $i + 1; + } + } + return 0; +} diff --git a/challenge-260/roger-bell-west/ruby/ch-1.rb b/challenge-260/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..687e1544db --- /dev/null +++ b/challenge-260/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,33 @@ +#! /usr/bin/ruby + +require 'set' + +def uniqueoccurrences(a) + c = Hash.new(0) + a.each do |n| + c[n] += 1 + end + if c.length == Set.new(c.values).length then + return 1 + else + return 0 + end +end + +require 'test/unit' + +class TestUniqueoccurrences < Test::Unit::TestCase + + def test_ex1 + assert_equal(1, uniqueoccurrences([1, 2, 2, 1, 1, 3])) + end + + def test_ex2 + assert_equal(0, uniqueoccurrences([1, 2, 3])) + end + + def test_ex3 + assert_equal(1, uniqueoccurrences([-2, 0, 1, -2, 1, 1, 0, 1, -2, 9])) + end + +end diff --git a/challenge-260/roger-bell-west/ruby/ch-2.rb b/challenge-260/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..e02efe0973 --- /dev/null +++ b/challenge-260/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,36 @@ +#! /usr/bin/ruby + +require 'set' + +def dictionaryrank(a) + c = a.split('') + d = Set.new + c.permutation do |o| + d.add(o.join('')) + end + dd = d.to_a.sort + dd.each_with_index do |s, i| + if s == a then + return i + 1 + end + end + return 0 +end + +require 'test/unit' + +class TestDictionaryrank < Test::Unit::TestCase + + def test_ex1 + assert_equal(3, dictionaryrank('CAT')) + end + + def test_ex2 + assert_equal(88, dictionaryrank('GOOGLE')) + end + + def test_ex3 + assert_equal(255, dictionaryrank('SECRET')) + end + +end diff --git a/challenge-260/roger-bell-west/rust/ch-1.rs b/challenge-260/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..bf662964ad --- /dev/null +++ b/challenge-260/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,26 @@ +use counter::Counter; +use std::collections::HashSet; + +#[test] +fn test_ex1() { + assert_eq!(uniqueoccurrences(vec![1, 2, 2, 1, 1, 3]), 1); +} + +#[test] +fn test_ex2() { + assert_eq!(uniqueoccurrences(vec![1, 2, 3]), 0); +} + +#[test] +fn test_ex3() { + assert_eq!(uniqueoccurrences(vec![-2, 0, 1, -2, 1, 1, 0, 1, -2, 9]), 1); +} + +fn uniqueoccurrences(a: Vec) -> u32 { + let c = a.into_iter().collect::>(); + if c.len() == c.values().collect::>().len() { + return 1; + } else { + return 0; + } +} diff --git a/challenge-260/roger-bell-west/rust/ch-2.rs b/challenge-260/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..295a3fb31c --- /dev/null +++ b/challenge-260/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,33 @@ +use itertools::Itertools; +use std::collections::HashSet; + +#[test] +fn test_ex1() { + assert_eq!(dictionaryrank("CAT"), 3); +} + +#[test] +fn test_ex2() { + assert_eq!(dictionaryrank("GOOGLE"), 88); +} + +#[test] +fn test_ex3() { + assert_eq!(dictionaryrank("SECRET"), 255); +} + +fn dictionaryrank(a: &str) -> usize { + let c = a.chars().collect::>(); + let mut d = HashSet::new(); + for o in c.iter().permutations(c.len()) { + d.insert(o.iter().copied().collect::()); + } + let mut dd = d.iter().collect::>(); + dd.sort(); + for (i, s) in dd.iter().enumerate() { + if s == &a { + return i + 1; + } + } + 0 +} diff --git a/challenge-260/roger-bell-west/scala/ch-1.scala b/challenge-260/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..50888015b4 --- /dev/null +++ b/challenge-260/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,36 @@ +import scala.collection.mutable + +object Uniqueoccurrences { + def uniqueoccurrences(a: List[Int]): Int = { + var c = mutable.Map.empty[Int, Int].withDefaultValue(0) + for (n <- a) { + c += (n -> (c(n) + 1)) + } + var r = 0 + if (c.size == c.values.toSet.size) { + r = 1 + } + r + } + def main(args: Array[String]) { + if (uniqueoccurrences(List(1, 2, 2, 1, 1, 3)) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (uniqueoccurrences(List(1, 2, 3)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (uniqueoccurrences(List(-2, 0, 1, -2, 1, 1, 0, 1, -2, 9)) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-260/roger-bell-west/scala/ch-2.scala b/challenge-260/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..9191e3a456 --- /dev/null +++ b/challenge-260/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,76 @@ +import scala.collection.mutable +import scala.collection.mutable.ListBuffer + +object Dictionaryrank { + def permute(aa: List[Char]): ListBuffer[List[Char]] = { + var a = new ListBuffer[Char] + for (i <- aa) { + a += i + } + var out = new ListBuffer[List[Char]] + val n = a.size + var c = new ListBuffer[Int] + for (i <- 0 to n-1) { + c += 0 + } + out += a.toList + var i = 0 + while (i < n) { + if (c(i) < i) { + if (i % 2 == 0) { + val tmp = a(0) + a(0) = a(i) + a(i) = tmp + } else { + val tmp = a(c(i)) + a(c(i)) = a(i) + a(i) = tmp + } + out += a.toList + c(i) += 1 + i = 0 + } else { + c(i) = 0 + i += 1 + } + } + return out + } + def dictionaryrank(a: String): Int = { + val c = a.toList + var d = mutable.Set.empty[String] + for (o <- permute(c)) { + d += o.mkString("") + } + var dd = d.to[ListBuffer] + dd = dd.sortWith(_ < _) + var r = 0 + for ((s, i) <- dd.zipWithIndex) { + if (s == a) { + r = i + 1 + } + } + r + } + def main(args: Array[String]) { + if (dictionaryrank("CAT") == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (dictionaryrank("GOOGLE") == 88) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (dictionaryrank("SECRET") == 255) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-260/roger-bell-west/tests.yaml b/challenge-260/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..299ef69ab4 --- /dev/null +++ b/challenge-260/roger-bell-west/tests.yaml @@ -0,0 +1,36 @@ +--- +ch-1: + - function: uniqueoccurrences + arguments: + - 1 + - 2 + - 2 + - 1 + - 1 + - 3 + result: 1 + - arguments: + - 1 + - 2 + - 3 + result: 0 + - arguments: + - -2 + - 0 + - 1 + - -2 + - 1 + - 1 + - 0 + - 1 + - -2 + - 9 + result: 1 +ch-2: + - function: dictionaryrank + arguments: CAT + result: 3 + - arguments: GOOGLE + result: 88 + - arguments: SECRET + result: 255 -- cgit From dbe495712e34674a941fb1c17e0cd5bca5c55ae4 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 11 Mar 2024 11:11:03 +0100 Subject: PWC 260 Task 1 Raku done Task 2 Raku done Task 1 PL/Perl done Task 2 PL/Perl done Task 1 PL/PgSQL done Task 2 PL/PgSQL done Task 1 Python done Task 2 Python done Task 1 PL/Java done Task 2 PL/Java done --- challenge-260/luca-ferrari/blog-1.txt | 1 + challenge-260/luca-ferrari/blog-10.txt | 1 + challenge-260/luca-ferrari/blog-2.txt | 1 + challenge-260/luca-ferrari/blog-3.txt | 1 + challenge-260/luca-ferrari/blog-4.txt | 1 + challenge-260/luca-ferrari/blog-5.txt | 1 + challenge-260/luca-ferrari/blog-6.txt | 1 + challenge-260/luca-ferrari/blog-7.txt | 1 + challenge-260/luca-ferrari/blog-8.txt | 1 + challenge-260/luca-ferrari/blog-9.txt | 1 + challenge-260/luca-ferrari/pljava/pom.xml | 13 +++- .../luca-ferrari/pljava/src/main/java/Task1.java | 75 ++++++++++++++++++ .../luca-ferrari/pljava/src/main/java/Task2.java | 91 ++++++++++++++++++++++ challenge-260/luca-ferrari/plperl/ch-1.plperl | 29 +++++++ challenge-260/luca-ferrari/plperl/ch-2.plperl | 32 ++++++++ challenge-260/luca-ferrari/plpgsql/ch-1.sql | 34 ++++++++ challenge-260/luca-ferrari/plpgsql/ch-2.sql | 15 ++++ challenge-260/luca-ferrari/python/ch-1.py | 33 ++++++++ challenge-260/luca-ferrari/python/ch-2.py | 31 ++++++++ challenge-260/luca-ferrari/raku/ch-1.raku | 19 +++++ challenge-260/luca-ferrari/raku/ch-2.raku | 12 +++ 21 files changed, 390 insertions(+), 4 deletions(-) create mode 100644 challenge-260/luca-ferrari/blog-1.txt create mode 100644 challenge-260/luca-ferrari/blog-10.txt create mode 100644 challenge-260/luca-ferrari/blog-2.txt create mode 100644 challenge-260/luca-ferrari/blog-3.txt create mode 100644 challenge-260/luca-ferrari/blog-4.txt create mode 100644 challenge-260/luca-ferrari/blog-5.txt create mode 100644 challenge-260/luca-ferrari/blog-6.txt create mode 100644 challenge-260/luca-ferrari/blog-7.txt create mode 100644 challenge-260/luca-ferrari/blog-8.txt create mode 100644 challenge-260/luca-ferrari/blog-9.txt create mode 100644 challenge-260/luca-ferrari/pljava/src/main/java/Task1.java create mode 100644 challenge-260/luca-ferrari/pljava/src/main/java/Task2.java create mode 100644 challenge-260/luca-ferrari/plperl/ch-1.plperl create mode 100644 challenge-260/luca-ferrari/plperl/ch-2.plperl create mode 100644 challenge-260/luca-ferrari/plpgsql/ch-1.sql create mode 100644 challenge-260/luca-ferrari/plpgsql/ch-2.sql create mode 100644 challenge-260/luca-ferrari/python/ch-1.py create mode 100644 challenge-260/luca-ferrari/python/ch-2.py create mode 100644 challenge-260/luca-ferrari/raku/ch-1.raku create mode 100644 challenge-260/luca-ferrari/raku/ch-2.raku (limited to 'challenge-260') diff --git a/challenge-260/luca-ferrari/blog-1.txt b/challenge-260/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..7510d731e5 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1 diff --git a/challenge-260/luca-ferrari/blog-10.txt b/challenge-260/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..514d3aef6b --- /dev/null +++ b/challenge-260/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge260.html#task2pljava diff --git a/challenge-260/luca-ferrari/blog-2.txt b/challenge-260/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e16337dcb3 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task2 diff --git a/challenge-260/luca-ferrari/blog-3.txt b/challenge-260/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..3d06909558 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1plperl diff --git a/challenge-260/luca-ferrari/blog-4.txt b/challenge-260/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..eda8b81e26 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task2plperl diff --git a/challenge-260/luca-ferrari/blog-5.txt b/challenge-260/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..53e0a8ea7e --- /dev/null +++ b/challenge-260/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1plpgsql diff --git a/challenge-260/luca-ferrari/blog-6.txt b/challenge-260/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..2e7c624fd6 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task2plpgsql diff --git a/challenge-260/luca-ferrari/blog-7.txt b/challenge-260/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..346f3ac1a0 --- /dev/null +++ b/challenge-260/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1python diff --git a/challenge-260/luca-ferrari/blog-8.txt b/challenge-260/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..8cb455f6be --- /dev/null +++ b/challenge-260/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task2python diff --git a/challenge-260/luca-ferrari/blog-9.txt b/challenge-260/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..7abc1e76ed --- /dev/null +++ b/challenge-260/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/03/11/PerlWeeklyChallenge260.html#task1pljava diff --git a/challenge-260/luca-ferrari/pljava/pom.xml b/challenge-260/luca-ferrari/pljava/pom.xml index 30b7a337f3..e9d5777410 100644 --- a/challenge-260/luca-ferrari/pljava/pom.xml +++ b/challenge-260/luca-ferrari/pljava/pom.xml @@ -1,3 +1,4 @@ + 4.0.0 PWC - PWC259 - 1 + + PWC260 + + + 1 + - Perl Weekly Challenge 259 - Implementation of the tasks in PL/Java for PWC 259 + Perl Weekly Challenge 260 with package PWC260 + Implementation of the tasks in PL/Java for PWC 260 US-ASCII diff --git a/challenge-260/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-260/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..5f861c3715 --- /dev/null +++ b/challenge-260/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,75 @@ + + + +package PWC260; + +/** + * PL/Java implementation for PWC 260 + * Task 1 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC260-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC260-1.jar', 'PWC260', true ); + select sqlj.set_classpath( 'public', 'PWC260' ); + + select pwc260.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC260-1.jar', 'PWC260', true ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task1 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc260", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final boolean task1_pljava( int[] nums ) throws SQLException { + logger.log( Level.INFO, "Entering pwc260.task1_pljava" ); + + List numList = new LinkedList(); + for ( int n : nums ) + numList.add( n ); + + Map> bag = new HashMap>(); + + for ( int current : numList ) { + int occurrency = Collections.frequency( numList, current ); + bag.putIfAbsent( occurrency, new LinkedList() ); + if ( ! bag.get( occurrency ).contains( current ) ) + bag.get( occurrency ).add( current ); + } + + for ( int k : bag.keySet() ) + if ( bag.get( k ).size() > 1 ) + return false; + + + return true; + + } +} diff --git a/challenge-260/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-260/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..4b4766866c --- /dev/null +++ b/challenge-260/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,91 @@ + + + +package PWC260; + +/** + * PL/Java implementation for PWC 260 + * Task 2 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC260-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC260-1.jar', 'PWC260', true ); + select sqlj.set_classpath( 'public', 'pwc260' ); + + select pwc260.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC260-1.jar', 'PWC260', true ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; +import java.math.*; + +public class Task2 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc260", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task2_pljava( String word ) throws SQLException { + logger.log( Level.INFO, "Entering pwc260.task2_pljava" ); + + List chars = new LinkedList(); + for ( String s : word.split( "" ) ) { + logger.log( Level.INFO, "CHAR " + s ); + chars.add( s ); + } + + List words = new LinkedList(); + + BigInteger limit = BigInteger.ONE; + for ( int i = 1; i <= chars.size(); i++ ) { + limit = limit.multiply( new BigInteger( "" + i ) ); + } + + logger.log( Level.INFO, "Limit is " + limit ); + while ( BigInteger.ZERO.compareTo( limit ) < 0 ) { + + + String newWord = ""; + do { + Collections.shuffle( chars ); + newWord = String.join( "", chars ); + } while ( words.contains( newWord ) ); + + words.add( newWord ); + limit = limit.subtract( BigInteger.ONE ); + } + + Collections.sort( words ); + + for ( int i = 0; i < words.size(); i++ ) + if ( words.get( i ).equals( word ) ) + return i + 1; + + + return -1; + } +} diff --git a/challenge-260/luca-ferrari/plperl/ch-1.plperl b/challenge-260/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..f0feecdf67 --- /dev/null +++ b/challenge-260/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 260 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc260; + +CREATE OR REPLACE FUNCTION +pwc260.task1_plperl( int[] ) +RETURNS boolean +AS $CODE$ + + my ( $nums ) = @_; + my $bag = {}; + + for my $current ( $nums->@* ) { + next if $bag->{ current }; # no need to reinitialize + $bag->{ $current } = scalar grep { $current == $_ } $nums->@*; + } + + for my $current ( values $bag->%* ) { + return 0 if ( scalar( grep { $current == $_ } values( $bag->%* ) ) > 1 ); + } + + return 1; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-260/luca-ferrari/plperl/ch-2.plperl b/challenge-260/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 000