From f5508397240ade08a77224d91ca07a4e7676990b Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 3 Feb 2025 14:44:27 -0500 Subject: Week 307 --- challenge-307/zapwai/perl/ch-1.pl | 17 +++++++++++++++++ challenge-307/zapwai/perl/ch-2.pl | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 challenge-307/zapwai/perl/ch-1.pl create mode 100644 challenge-307/zapwai/perl/ch-2.pl diff --git a/challenge-307/zapwai/perl/ch-1.pl b/challenge-307/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..2ff1dc4bf4 --- /dev/null +++ b/challenge-307/zapwai/perl/ch-1.pl @@ -0,0 +1,17 @@ +use v5.38; +sub proc(@ints) { + say "Input: \@ints = @ints"; + my @int = sort {$a <=> $b} @ints; + my @ind; + + for my $i (0 .. $#ints) { + push @ind, $i if ($ints[$i] != $int[$i]); + } + say "Output: @ind"; +} +my @ints = (5,2,4,3,1); +proc(@ints); +@ints = (1,2,1,1,3); +proc(@ints); +@ints = (3,1,3,2,3); +proc(@ints); diff --git a/challenge-307/zapwai/perl/ch-2.pl b/challenge-307/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..ac8b4f396f --- /dev/null +++ b/challenge-307/zapwai/perl/ch-2.pl @@ -0,0 +1,35 @@ +use v5.38; +# return true if given words are anagrams +sub is_anagram($w1, $w2) { + my @l1 = sort split "", $w1; + my @l2 = sort split "", $w2; + return 0 if (@l1 != @l2); + for my $i (0 .. $#l1) { + return 0 if ($l1[$i] ne $l2[$i]); + } + return 1; +} +sub proc(@words) { + say "Input: \@words = @words"; + my $n; + do { + $n = 0; + my @ind; + for my $i (0 .. $#words - 1) { + if (is_anagram($words[$i], $words[$i+1])) { + $n++; + push @ind, $i; + + } + } + for (reverse @ind) { + splice @words, $_, 1; + } + } while ($n); + say "Output: ". scalar @words . " (@words)"; + +} +my @words = ("acca", "dog", "god", "perl", "repl"); +proc(@words); +@words = ("abba", "baba", "aabb", "ab", "ab"); +proc(@words); -- cgit From 9df025ad92caa9c4e654c7dbd02a09914e161d47 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 3 Feb 2025 21:26:10 +0100 Subject: Add solution 307 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-307/jeanluc2020/blog-1.txt | 1 + challenge-307/jeanluc2020/blog-2.txt | 1 + challenge-307/jeanluc2020/perl/ch-1.pl | 66 ++++++++++++++++++++++++++ challenge-307/jeanluc2020/perl/ch-2.pl | 86 ++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 challenge-307/jeanluc2020/blog-1.txt create mode 100644 challenge-307/jeanluc2020/blog-2.txt create mode 100755 challenge-307/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-307/jeanluc2020/perl/ch-2.pl diff --git a/challenge-307/jeanluc2020/blog-1.txt b/challenge-307/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..9132caa1d0 --- /dev/null +++ b/challenge-307/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-307-1.html diff --git a/challenge-307/jeanluc2020/blog-2.txt b/challenge-307/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..bc0d1dce3d --- /dev/null +++ b/challenge-307/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-307-2.html diff --git a/challenge-307/jeanluc2020/perl/ch-1.pl b/challenge-307/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..73bb81a019 --- /dev/null +++ b/challenge-307/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-307/#TASK1 +# +# Task 1: Check Order +# =================== +# +# You are given an array of integers, @ints. +# +# Write a script to re-arrange the given array in an increasing order and +# return the indices where it differs from the original array. +# +## Example 1 +## +## Input: @ints = (5, 2, 4, 3, 1) +## Output: (0, 2, 3, 4) +## +## Before: (5, 2, 4, 3, 1) +## After : (1, 2, 3, 4, 5) +## +## Difference at indices: (0, 2, 3, 4) +# +## Example 2 +## +## Input: @ints = (1, 2, 1, 1, 3) +## Output: (1, 3) +## +## Before: (1, 2, 1, 1, 3) +## After : (1, 1, 1, 2, 3) +## +## Difference at indices: (1, 3) +# +## Example 3 +## +## Input: @ints = (3, 1, 3, 2, 3) +## Output: (0, 1, 3) +## +## Before: (3, 1, 3, 2, 3) +## After : (1, 2, 3, 3, 3) +## +## Difference at indices: (0, 1, 3) +# +############################################################ +## +## discussion +## +############################################################ +# +# Create the sorted array, then comapre the two arrays by index. +# Remember all positions where the two arrays differ. + +use v5.36; + +check_order(5, 2, 4, 3, 1); +check_order(1, 2, 1, 1, 3); +check_order(3, 1, 3, 2, 3); + +sub check_order { + my @ints = @_; + say "Input: (" . join(", ", @ints) . ")"; + my @sorted = sort {$a<=>$b} @ints; + my @result = (); + foreach my $i (0..$#ints) { + push @result, $i if $ints[$i] != $sorted[$i]; + } + say "Output: (" . join(", ", @result) . ")"; +} diff --git a/challenge-307/jeanluc2020/perl/ch-2.pl b/challenge-307/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ec9e8500ca --- /dev/null +++ b/challenge-307/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,86 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-307/#TASK2 +# +# Find Anagrams +# ============= +# +# You are given a list of words, @words. +# +# Write a script to find any two consecutive words and if they are anagrams, +# drop the first word and keep the second. You continue this until there is no +# more anagrams in the given list and return the count of final list. +# +## Example 1 +## +## Input: @words = ("acca", "dog", "god", "perl", "repl") +## Output: 3 +## +## Step 1: "dog" and "god" are anagrams, so dropping "dog" and keeping "god" +## => ("acca", "god", "perl", "repl") +## Step 2: "perl" and "repl" are anagrams, so dropping "perl" and keeping "repl" +## => ("acca", "god", "repl") +# +## Example 2 +## +## Input: @words = ("abba", "baba", "aabb", "ab", "ab") +## Output: 2 +## +## Step 1: "abba" and "baba" are anagrams, so dropping "abba" and keeping "baba" +## => ("baba", "aabb", "ab", "ab") +## Step 2: "baba" and "aabb" are anagrams, so dropping "baba" and keeping "aabb" +## => ("aabb", "ab", "ab") +## Step 3: "ab" and "ab" are anagrams, so dropping "ab" and keeping "ab" +## => ("aabb", "ab") +# +############################################################ +## +## discussion +## +############################################################ +# +# As long as the length of the array keeps changing, check if two consecutive +# words are anagrams. If so, remove the first word and continue. Once the +# length of the array no longer changes, we can output the length. + +use v5.36; + +find_anagrams("acca", "dog", "god", "perl", "repl"); +find_anagrams("abba", "baba", "aabb", "ab", "ab"); + +sub find_anagrams (@words) { + say "Input: (" . join(", ", @words) . ")"; + my $len = scalar(@words); + my $oldlen = 1 + $len; + while($oldlen != $len) { + my @tmp = (); + foreach my $i (0..$#words) { + if(is_anagram($words[$i], $words[$i+1])) { + push @tmp, @words[$i+1..$#words]; + last; + } else { + push @tmp, $words[$i]; + } + } + @words = @tmp; + $oldlen = $len; + $len = scalar(@words); + } + say "Output: $len"; +} + +sub is_anagram($word1, $word2) { + my $w1; + my $w2; + return 0 unless defined $word2; + return 0 if length($word1) != length($word2); + foreach my $c (split//,$word1) { + $w1->{$c}++; + } + foreach my $c (split//,$word2) { + $w2->{$c}++; + } + foreach my $c (keys %$w1) { + return 0 if $w1->{$c} != $w2->{$c}; + } + return 1; +} -- cgit From 13f3c6d4575662cfb48f057d0b71968406da7ad5 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 3 Feb 2025 21:24:21 +0000 Subject: add solution week 307 task 1 in python --- challenge-307/steven-wilson/python/ch_01.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 challenge-307/steven-wilson/python/ch_01.py diff --git a/challenge-307/steven-wilson/python/ch_01.py b/challenge-307/steven-wilson/python/ch_01.py new file mode 100644 index 0000000000..555435eb77 --- /dev/null +++ b/challenge-307/steven-wilson/python/ch_01.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + + +def check_order(integers): + """ Given an array of integers, re-arrange the given array in an increasing + order and return the indices where it differs from the original array. + + >>> check_order((5, 2, 4, 3, 1)) + (0, 2, 3, 4) + >>> check_order((1, 2, 1, 1, 3)) + (1, 3) + >>> check_order((3, 1, 3, 2, 3)) + (0, 1, 3) + """ + return tuple(n for n, i in enumerate(sorted(integers)) if i != integers[n]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit From 9e5deef559ffa5bb3cab2a89475de0c0a370c59b Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 3 Feb 2025 21:36:21 +0000 Subject: change file name --- challenge-307/steven-wilson/python/ch-01.py | 21 +++++++++++++++++++++ challenge-307/steven-wilson/python/ch_01.py | 21 --------------------- 2 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 challenge-307/steven-wilson/python/ch-01.py delete mode 100644 challenge-307/steven-wilson/python/ch_01.py diff --git a/challenge-307/steven-wilson/python/ch-01.py b/challenge-307/steven-wilson/python/ch-01.py new file mode 100644 index 0000000000..555435eb77 --- /dev/null +++ b/challenge-307/steven-wilson/python/ch-01.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + + +def check_order(integers): + """ Given an array of integers, re-arrange the given array in an increasing + order and return the indices where it differs from the original array. + + >>> check_order((5, 2, 4, 3, 1)) + (0, 2, 3, 4) + >>> check_order((1, 2, 1, 1, 3)) + (1, 3) + >>> check_order((3, 1, 3, 2, 3)) + (0, 1, 3) + """ + return tuple(n for n, i in enumerate(sorted(integers)) if i != integers[n]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-307/steven-wilson/python/ch_01.py b/challenge-307/steven-wilson/python/ch_01.py deleted file mode 100644 index 555435eb77..0000000000 --- a/challenge-307/steven-wilson/python/ch_01.py +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env python3 - - -def check_order(integers): - """ Given an array of integers, re-arrange the given array in an increasing - order and return the indices where it differs from the original array. - - >>> check_order((5, 2, 4, 3, 1)) - (0, 2, 3, 4) - >>> check_order((1, 2, 1, 1, 3)) - (1, 3) - >>> check_order((3, 1, 3, 2, 3)) - (0, 1, 3) - """ - return tuple(n for n, i in enumerate(sorted(integers)) if i != integers[n]) - - -if __name__ == "__main__": - import doctest - - doctest.testmod(verbose=True) -- cgit From 4be972678a3d2e97cc72b496d0c21424555efe1e Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 4 Feb 2025 10:50:37 +0000 Subject: RogerBW solutions for challenge no. 307 --- challenge-307/roger-bell-west/crystal/ch-1.cr | 25 +++ challenge-307/roger-bell-west/crystal/ch-2.cr | 22 ++ challenge-307/roger-bell-west/javascript/ch-1.js | 63 ++++++ challenge-307/roger-bell-west/javascript/ch-2.js | 32 +++ challenge-307/roger-bell-west/kotlin/ch-1.kt | 33 +++ challenge-307/roger-bell-west/kotlin/ch-2.kt | 30 +++ challenge-307/roger-bell-west/lua/ch-1.lua | 64 ++++++ challenge-307/roger-bell-west/lua/ch-2.lua | 51 +++++ challenge-307/roger-bell-west/perl/ch-1.pl | 22 ++ challenge-307/roger-bell-west/perl/ch-2.pl | 24 +++ challenge-307/roger-bell-west/postscript/ch-1.ps | 254 +++++++++++++++++++++++ challenge-307/roger-bell-west/postscript/ch-2.ps | 246 ++++++++++++++++++++++ challenge-307/roger-bell-west/python/ch-1.py | 25 +++ challenge-307/roger-bell-west/python/ch-2.py | 21 ++ challenge-307/roger-bell-west/raku/ch-1.p6 | 20 ++ challenge-307/roger-bell-west/raku/ch-2.p6 | 22 ++ challenge-307/roger-bell-west/ruby/ch-1.rb | 30 +++ challenge-307/roger-bell-west/ruby/ch-2.rb | 26 +++ challenge-307/roger-bell-west/rust/ch-1.rs | 27 +++ challenge-307/roger-bell-west/rust/ch-2.rs | 28 +++ challenge-307/roger-bell-west/scala/ch-1.scala | 35 ++++ challenge-307/roger-bell-west/scala/ch-2.scala | 32 +++ challenge-307/roger-bell-west/tests.json | 28 +++ 23 files changed, 1160 insertions(+) create mode 100755 challenge-307/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-307/roger-bell-west/crystal/ch-2.cr create mode 100755 challenge-307/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-307/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-307/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-307/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-307/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-307/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-307/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-307/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-307/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-307/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-307/roger-bell-west/python/ch-1.py create mode 100755 challenge-307/roger-bell-west/python/ch-2.py create mode 100755 challenge-307/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-307/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-307/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-307/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-307/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-307/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-307/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-307/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-307/roger-bell-west/tests.json diff --git a/challenge-307/roger-bell-west/crystal/ch-1.cr b/challenge-307/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..b1553bf54a --- /dev/null +++ b/challenge-307/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,25 @@ +#! /usr/bin/crystal + +def checkorder(a) + b = a.sort + out = Array(Int32).new + a.each_with_index do |c, i| + if b[i] != c + out.push(i) + end + end + out +end + +require "spec" +describe "checkorder" do + it "test_ex1" do + checkorder([5, 2, 4, 3, 1]).should eq [0, 2, 3, 4] + end + it "test_ex2" do + checkorder([1, 2, 1, 1, 3]).should eq [1, 3] + end + it "test_ex3" do + checkorder([3, 1, 3, 2, 3]).should eq [0, 1, 3] + end +end diff --git a/challenge-307/roger-bell-west/crystal/ch-2.cr b/challenge-307/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..baa71ac565 --- /dev/null +++ b/challenge-307/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,22 @@ +#! /usr/bin/crystal + +def findanagrams(a) + b = a.map{|x| x.chars.sort.join} + out = 1 + b.each_cons(2) do |s| + if s[0] != s[1] + out += 1 + end + end + out +end + +require "spec" +describe "findanagrams" do + it "test_ex1" do + findanagrams(["acca", "dog", "god", "perl", "repl"]).should eq 3 + end + it "test_ex2" do + findanagrams(["abba", "baba", "aabb", "ab", "ab"]).should eq 2 + end +end diff --git a/challenge-307/roger-bell-west/javascript/ch-1.js b/challenge-307/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..67fb61adee --- /dev/null +++ b/challenge-307/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,63 @@ +#! /usr/bin/node + +"use strict" + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +function checkorder(a) { + let b = [...a]; + b.sort(function(a,b) { + return a-b; + }); + let out = []; + for (let i = 0; i < a.length; i++) { + if (a[i] != b[i]) { + out.push(i); + } + } + return out; +} + +if (deepEqual(checkorder([5, 2, 4, 3, 1]), [0, 2, 3, 4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(checkorder([1, 2, 1, 1, 3]), [1, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(checkorder([3, 1, 3, 2, 3]), [0, 1, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-307/roger-bell-west/javascript/ch-2.js b/challenge-307/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..e3afbdbd96 --- /dev/null +++ b/challenge-307/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,32 @@ +#! /usr/bin/node + +"use strict" + +function findanagrams(a) { + let b = []; + for (let s of a) { + let c = s.split(""); + c.sort(); + b.push(c.join("")); + } + let out = 1; + for (let i = 1; i < b.length; i++) { + if (b[i - 1] != b[i]) { + out += 1; + } + } + return out; +} + +if (findanagrams(['acca', 'dog', 'god', 'perl', 'repl']) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (findanagrams(['abba', 'baba', 'aabb', 'ab', 'ab']) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-307/roger-bell-west/kotlin/ch-1.kt b/challenge-307/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..7a378aa251 --- /dev/null +++ b/challenge-307/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,33 @@ +fun checkorder(a: List): List { + var b = a.sorted() + var out = ArrayList() + a.forEachIndexed{i, c -> + if (b[i] != c) { + out.add(i) + } + } + return out.toList() +} + +fun main() { + + if (checkorder(listOf(5, 2, 4, 3, 1)) == listOf(0, 2, 3, 4)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (checkorder(listOf(1, 2, 1, 1, 3)) == listOf(1, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (checkorder(listOf(3, 1, 3, 2, 3)) == listOf(0, 1, 3)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-307/roger-bell-west/kotlin/ch-2.kt b/challenge-307/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..97178d031b --- /dev/null +++ b/challenge-307/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,30 @@ +fun findanagrams(a: List): Int { + var b = ArrayList() + for (s in a) { + b.add(s.toList().sorted().joinToString("")) + } + var out = 1 + for (s in b.windowed(size = 2)) { + if (s[0] != s[1]) { + out += 1 + } + } + return out +} + +fun main() { + + if (findanagrams(listOf("acca", "dog", "god", "perl", "repl")) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (findanagrams(listOf("abba", "baba", "aabb", "ab", "ab")) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-307/roger-bell-west/lua/ch-1.lua b/challenge-307/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..69a8f1f295 --- /dev/null +++ b/challenge-307/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,64 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +-- modified by Roger +function recursive_compare(t1,t2) + -- Use usual comparison first. + if t1==t2 then return true end + -- We only support non-default behavior for tables + if (type(t1)~="table") then return false end + -- They better have the same metatables + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + -- Build list of all keys + local kk = {} + for k1, _ in pairs(t1) do + kk[k1] = true + end + for k2, _ in pairs(t2) do + kk[k2] = true + end + -- Check each key that exists in at least one table + for _, k in ipairs(kk) do + if (not recursive_compare(t1[k], t2[k])) then + return false + end + end + return true +end + +function checkorder(a) + local b = a + table.sort(b, function (i, j) return i < j end) + local out = {} + for i = 1, #a do + if a[i] ~= b[i] then + table.insert(out, i) + end + end + return out +end + +if recursive_compare(checkorder({5, 2, 4, 3, 1}), {0, 2, 3, 4}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(checkorder({1, 2, 1, 1, 3}), {1, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(checkorder({3, 1, 3, 2, 3}), {0, 1, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-307/roger-bell-west/lua/ch-2.lua b/challenge-307/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..5e0f75eb2d --- /dev/null +++ b/challenge-307/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,51 @@ +#! /usr/bin/lua + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function join(t) + local out="" + for i, v in ipairs(t) do + out = out .. v + end + return out +end + +function findanagrams(a) + local b = {} + for _, s in ipairs(a) do + c = split(s) + table.sort(c) + table.insert(b, join(c)) + end + local out = 1 + for i = 2, #a do + if b[i - 1] ~= b[i] then + out = out + 1 + end + end + return out +end + +if findanagrams({"acca", "dog", "god", "perl", "repl"}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if findanagrams({"abba", "baba", "aabb", "ab", "ab"}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-307/roger-bell-west/perl/ch-1.pl b/challenge-307/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..7859357ffe --- /dev/null +++ b/challenge-307/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_deeply(checkorder([5, 2, 4, 3, 1]), [0, 2, 3, 4], 'example 1'); +is_deeply(checkorder([1, 2, 1, 1, 3]), [1, 3], 'example 2'); +is_deeply(checkorder([3, 1, 3, 2, 3]), [0, 1, 3], 'example 3'); + +sub checkorder($a) { + my @b = sort {$::a <=> $::b} @{$a}; + my @out; + foreach my $i (0 .. $#b) { + if ($a->[$i] != $b[$i]) { + push @out, $i; + } + } + \@out; +} diff --git a/challenge-307/roger-bell-west/perl/ch-2.pl b/challenge-307/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..219c8d6653 --- /dev/null +++ b/challenge-307/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(findanagrams(['acca', 'dog', 'god', 'perl', 'repl']), 3, 'example 1'); +is(findanagrams(['abba', 'baba', 'aabb', 'ab', 'ab']), 2, 'example 2'); + +sub findanagrams($a) { + my @b; + foreach my $s (@{$a}) { + push @b, join("", sort split("", $s)); + } + my $out = 1; + foreach my $i (1 .. $#b) { + if ($b[$i - 1] ne $b[$i]) { + $out++; + } + } + $out; +} diff --git a/challenge-307/roger-bell-west/postscript/ch-1.ps b/challenge-307/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..152e34793f --- /dev/null +++ b/challenge-307/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,254 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/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 + +/deepcopy { + 2 dict begin + /a exch def + a type /dicttype eq { + << + a keys { + /k exch def + k + a k get deepcopy + } forall + >> + } { + a type /arraytype eq { + [ + a { + deepcopy + } forall + ] + } { + a type /stringtype eq { + a dup length string cvs + } { + a + } ifelse + } ifelse + } ifelse + 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 + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} 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 + +/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 + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} 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 + +/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 + +/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 + + +% end included library code + +/checkorder { + 0 dict begin + /a exch def + /b a deepcopy quicksort def + [ + a enumerate.array { + aload pop + /c exch def + /i exch def + b i get c ne { + i + } if + } forall + ] + end +} bind def + +(checkorder) test.start +[5 2 4 3 1] checkorder [0 2 3 4] deepeq test +[1 2 1 1 3] checkorder [1 3] deepeq test +[3 1 3 2 3] checkorder [0 1 3] deepeq test +test.end diff --git a/challenge-307/roger-bell-west/postscript/ch-2.ps b/challenge-307/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..e15ddd1a86 --- /dev/null +++ b/challenge-307/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,246 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/rotor { + 5 dict begin + /delta exch def + /size exch def + dup length /len exch def + /ar exch def + /ix 0 def + [ + { + ix size add len gt { + exit + } if + ar ix size getinterval + /ix ix size delta add add def + } loop + ] + end +} bind def + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} 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 + +/s2a { + [ exch { } forall ] +} 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 + +/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 + +/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 + +/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 + +/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.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 + +/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 + + +% end included library code + +/findanagrams { + 1 exch + { + s2a quicksort a2s + } map + 2 -1 rotor + { + aload pop deepeq not { + 1 add + } if + } forall +} bind def + +(findanagrams) test.start +[(acca) (dog) (god) (perl) (repl)] findanagrams 3 eq test +[(abba) (baba) (aabb) (ab) (ab)] findanagrams 2 eq test +test.end diff --git a/challenge-307/roger-bell-west/python/ch-1.py b/challenge-307/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..82416a2927 --- /dev/null +++ b/challenge-307/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def checkorder(a): + b = a.copy() + b.sort() + out = [] + for i, c in enumerate(a): + if b[i] != c: + out.append(i) + return out + +import unittest + +class TestCheckorder(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(checkorder([5, 2, 4, 3, 1]), [0, 2, 3, 4], 'example 1') + + def test_ex2(self): + self.assertEqual(checkorder([1, 2, 1, 1, 3]), [1, 3], 'example 2') + + def test_ex3(self): + self.assertEqual(checkorder([3, 1, 3, 2, 3]), [0, 1, 3], 'example 3') + +unittest.main() diff --git a/challenge-307/roger-bell-west/python/ch-2.py b/challenge-307/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..155ecdbd29 --- /dev/null +++ b/challenge-307/roger-bell-west/python/ch-2.py @@ -0,0 +1,21 @@ +#! /usr/bin/python3 + +def findanagrams(a): + b = ["".join(sorted(s)) for s in a] + out = 1 + for i in range(1, len(b)): + if b[i - 1] != b[i]: + out += 1 + return out + +import unittest + +class TestFindanagrams(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(findanagrams(["acca", "dog", "god", "perl", "repl"]), 3, 'example 1') + + def test_ex2(self): + self.assertEqual(findanagrams(["abba", "baba", "aabb", "ab", "ab"]), 2, 'example 2') + +unittest.main() diff --git a/challenge-307/roger-bell-west/raku/ch-1.p6 b/challenge-307/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..abd01dc5ec --- /dev/null +++ b/challenge-307/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(checkorder([5, 2, 4, 3, 1]), [0, 2, 3, 4], 'example 1'); +is-deeply(checkorder([1, 2, 1, 1, 3]), [1, 3], 'example 2'); +is-deeply(checkorder([3, 1, 3, 2, 3]), [0, 1, 3], 'example 3'); + +sub checkorder(@a) { + my @b = @a.sort({$^a <=> $^b}); + my @out; + for @b.kv -> $i, $c { + if (@a[$i] != $c) { + @out.push($i); + } + } + @out; +} diff --git a/challenge-307/roger-bell-west/raku/ch-2.p6 b/challenge-307/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..513a3c30e1 --- /dev/null +++ b/challenge-307/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,22 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(findanagrams(['acca', 'dog', 'god', 'perl', 'repl']), 3, 'example 1'); +is(findanagrams(['abba', 'baba', 'aabb', 'ab', 'ab']), 2, 'example 2'); + +sub findanagrams(@a) { + my @b; + for @a -> $s { + @b.push($s.comb.sort.join("")); + } + my $out = 1; + for 1 .. @b.end -> $i { + if (@b[$i - 1] ne @b[$i]) { + $out++; + } + } + $out; +} diff --git a/challenge-307/roger-bell-west/ruby/ch-1.rb b/challenge-307/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..fb3239a02f --- /dev/null +++ b/challenge-307/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,30 @@ +#! /usr/bin/ruby + +def checkorder(a) + b = a.sort + out = [] + a.each_with_index do |c, i| + if b[i] != c + out.push(i) + end + end + out +end + +require 'test/unit' + +class TestCheckorder < Test::Unit::TestCase + + def test_ex1 + assert_equal([0, 2, 3, 4], checkorder([5, 2, 4, 3, 1])) + end + + def test_ex2 + assert_equal([1, 3], checkorder([1, 2, 1, 1, 3])) + end + + def test_ex3 + assert_equal([0, 1, 3], checkorder([3, 1, 3, 2, 3])) + end + +end diff --git a/challenge-307/roger-bell-west/ruby/ch-2.rb b/challenge-307/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..072d727a78 --- /dev/null +++ b/challenge-307/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,26 @@ +#! /usr/bin/ruby + +def findanagrams(a) + b = a.map{|x| x.chars.sort.join} + out = 1 + b.each_cons(2) do |s| + if s[0] != s[1] + out += 1 + end + end + out +end + +require 'test/unit' + +class TestFindanagrams < Test::Unit::TestCase + + def test_ex1 + assert_equal(3, findanagrams(['acca', 'dog', 'god', 'perl', 'repl'])) + end + + def test_ex2 + assert_equal(2, findanagrams(['abba', 'baba', 'aabb', 'ab', 'ab'])) + end + +end diff --git a/challenge-307/roger-bell-west/rust/ch-1.rs b/challenge-307/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..93b991f31c --- /dev/null +++ b/challenge-307/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,27 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(checkorder(vec![5, 2, 4, 3, 1]), vec![0, 2, 3, 4]); +} + +#[test] +fn test_ex2() { + assert_eq!(checkorder(vec![1, 2, 1, 1, 3]), vec![1, 3]); +} + +#[test] +fn test_ex3() { + assert_eq!(checkorder(vec![3, 1, 3, 2, 3]), vec![0, 1, 3]); +} + +fn checkorder(a: Vec) -> Vec { + let mut b = a.clone(); + b.sort(); + a.into_iter() + .enumerate() + .filter(|(i, c)| b[*i] != *c) + .map(|(i, _c)| i) + .collect::>() +} diff --git a/challenge-307/roger-bell-west/rust/ch-2.rs b/challenge-307/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..9eb6bab6fb --- /dev/null +++ b/challenge-307/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,28 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(findanagrams(vec!["acca", "dog", "god", "perl", "repl"]), 3); +} + +#[test] +fn test_ex2() { + assert_eq!(findanagrams(vec!["abba", "baba", "aabb", "ab", "ab"]), 2); +} + +fn findanagrams(a: Vec<&str>) -> usize { + let mut b: Vec = Vec::new(); + for s in a { + let mut c = s.chars().collect::>(); + c.sort(); + b.push(c.iter().cloned().collect::()); + } + let mut out = 1; + for s in b.windows(2) { + if s[0] != s[1] { + out += 1; + } + } + out +} diff --git a/challenge-307/roger-bell-west/scala/ch-1.scala b/challenge-307/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..141f986621 --- /dev/null +++ b/challenge-307/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,35 @@ +import scala.collection.mutable.ListBuffer + +object Checkorder { + def checkorder(a: List[Int]): List[Int] = { + var b = a.sortWith(_ < _) + var out = new ListBuffer[Int] + for ((c, i) <- a.zipWithIndex) { + if (b(i) != c) { + out += i + } + } + out.toList + } + def main(args: Array[String]) { + if (checkorder(List(5, 2, 4, 3, 1)) == List(0, 2, 3, 4)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (checkorder(List(1, 2, 1, 1, 3)) == List(1, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (checkorder(List(3, 1, 3, 2, 3)) == List(0, 1, 3)) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-307/roger-bell-west/scala/ch-2.scala b/challenge-307/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..877a5dc46b --- /dev/null +++ b/challenge-307/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,32 @@ +import scala.collection.mutable.ListBuffer + +object Findanagrams { + def findanagrams(a: List[String]): Int = { + var b = new ListBuffer[String] + for (s <- a) { + b += s.toList.sortWith(_ < _).toString + } + var out = 1 + for (s <- b.sliding(2)) { + if (s(0) != s(1)) { + out += 1 + } + } + out + } + def main(args: Array[String]) { + if (findanagrams(List("acca", "dog", "god", "perl", "repl")) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (findanagrams(List("abba", "baba", "aabb", "ab", "ab")) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-307/roger-bell-west/tests.json b/challenge-307/roger-bell-west/tests.json new file mode 100644 index 0000000000..1cf1570c11 --- /dev/null +++ b/challenge-307/roger-bell-west/tests.json @@ -0,0 +1,28 @@ +{ + "ch-1" : [ + { + "function" : "checkorder", + "arguments" : [ 5, 2, 4, 3, 1 ], + "result" : [ 0, 2, 3, 4 ] + }, + { + "arguments" : [ 1, 2, 1, 1, 3 ], + "result" : [ 1, 3 ] + }, + { + "arguments" : [ 3, 1, 3, 2, 3 ], + "result" : [ 0, 1, 3 ] + } + ], + "ch-2" : [ + { + "function" : "findanagrams", + "arguments" : [ "acca", "dog", "god", "perl", "repl" ], + "result" : 3 + }, + { + "arguments" : [ "abba", "baba", "aabb", "ab", "ab" ], + "result" : 2 + } + ] +} -- cgit From 15344124d3e067484077c69389975e9345a267d8 Mon Sep 17 00:00:00 2001 From: Andreas Mahnke Date: Tue, 4 Feb 2025 16:57:45 +0100 Subject: Challenge 307 --- challenge-307/mahnkong/perl/ch-1.pl | 18 ++++++++++++++++++ challenge-307/mahnkong/perl/ch-2.pl | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 challenge-307/mahnkong/perl/ch-1.pl create mode 100644 challenge-307/mahnkong/perl/ch-2.pl diff --git a/challenge-307/mahnkong/perl/ch-1.pl b/challenge-307/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..3db4d28ea8 --- /dev/null +++ b/challenge-307/mahnkong/perl/ch-1.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@ints) { + my @sorted = sort { $a <=> $b } @ints; + my $diffing_indices = []; + + for (my $i = 0; $i < scalar(@ints); $i++) { + push @$diffing_indices, $i if ($ints[$i] != $sorted[$i]); + } + return $diffing_indices; +} + +is_deeply(run(5, 2, 4, 3, 1), [0, 2, 3, 4], "Example 1"); +is_deeply(run(1, 2, 1, 1, 3), [1, 3], "Example 2"); +is_deeply(run(3, 1, 3, 2, 3), [0, 1, 3], "Example 3"); diff --git a/challenge-307/mahnkong/perl/ch-2.pl b/challenge-307/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..34733a264c --- /dev/null +++ b/challenge-307/mahnkong/perl/ch-2.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run(@words) { + @words = reverse(@words); + while (1) { + my $max_index = $#words; + for (my $i = $max_index; $i > 0; $i--) { + if (join('', sort(split(//, $words[$i]))) eq join('', sort(split(//, $words[$i-1])))) { + splice(@words, $i, 1); + } + } + last if $max_index == $#words; + } + return scalar(@words); +} + +is(run("acca", "dog", "god", "perl", "repl"), 3, "Example 1"); +is(run("abba", "baba", "aabb", "ab", "ab"), 2, "Example 2"); -- cgit From c04cf6be6621e9be28c104d6fe295b71b0699fb8 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Tue, 4 Feb 2025 20:32:22 +0330 Subject: TWC307 --- challenge-307/deadmarshal/blog.txt | 1 + challenge-307/deadmarshal/java/Ch1.java | 21 +++++ challenge-307/deadmarshal/java/Ch2.java | 24 ++++++ challenge-307/deadmarshal/modula-3/Ch1/src/Ch1.m3 | 38 +++++++++ .../deadmarshal/modula-3/Ch1/src/m3makefile | 4 + challenge-307/deadmarshal/modula-3/Ch2/src/Ch2.m3 | 32 ++++++++ .../deadmarshal/modula-3/Ch2/src/m3makefile | 5 ++ challenge-307/deadmarshal/perl/ch-1.pl | 14 ++++ challenge-307/deadmarshal/perl/ch-2.pl | 15 ++++ challenge-307/deadmarshal/zig/ch1/build.zig | 91 ++++++++++++++++++++++ challenge-307/deadmarshal/zig/ch1/build.zig.zon | 72 +++++++++++++++++ challenge-307/deadmarshal/zig/ch1/src/main.zig | 60 ++++++++++++++ challenge-307/deadmarshal/zig/ch1/src/root.zig | 10 +++ challenge-307/deadmarshal/zig/ch2/build.zig | 91 ++++++++++++++++++++++ challenge-307/deadmarshal/zig/ch2/build.zig.zon | 72 +++++++++++++++++ challenge-307/deadmarshal/zig/ch2/src/main.zig | 46 +++++++++++ challenge-307/deadmarshal/zig/ch2/src/root.zig | 10 +++ 17 files changed, 606 insertions(+) create mode 100644 challenge-307/deadmarshal/blog.txt create mode 100644 challenge-307/deadmarshal/java/Ch1.java create mode 100644 challenge-307/deadmarshal/java/Ch2.java create mode 100644 challenge-307/deadmarshal/modula-3/Ch1/src/Ch1.m3 create mode 100644 challenge-307/deadmarshal/modula-3/Ch1/src/m3makefile create mode 100644 challenge-307/deadmarshal/modula-3/Ch2/src/Ch2.m3 create mode 100644 challenge-307/deadmarshal/modula-3/Ch2/src/m3makefile create mode 100644 challenge-307/deadmarshal/perl/ch-1.pl create mode 100644 challenge-307/deadmarshal/perl/ch-2.pl create mode 100644 challenge-307/deadmarshal/zig/ch1/build.zig create mode 100644 challenge-307/deadmarshal/zig/ch1/build.zig.zon create mode 100644 challenge-307/deadmarshal/zig/ch1/src/main.zig create mode 100644 challenge-307/deadmarshal/zig/ch1/src/root.zig create mode 100644 challenge-307/deadmarshal/zig/ch2/build.zig create mode 100644 challenge-307/deadmarshal/zig/ch2/build.zig.zon create mode 100644 challenge-307/deadmarshal/zig/ch2/src/main.zig create mode 100644 challenge-307/deadmarshal/zig/ch2/src/root.zig diff --git a/challenge-307/deadmarshal/blog.txt b/challenge-307/deadmarshal/blog.txt new file mode 100644 index 0000000000..68a0d71f92 --- /dev/null +++ b/challenge-307/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2025/02/twc307.html diff --git a/challenge-307/deadmarshal/java/Ch1.java b/challenge-307/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..091b91b204 --- /dev/null +++ b/challenge-307/deadmarshal/java/Ch1.java @@ -0,0 +1,21 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Ch1 { + public static void main(String[] args) { + System.out.println(check_order(new int[]{5, 2, 4, 3, 1})); + System.out.println(check_order(new int[]{1, 2, 1, 1, 3})); + System.out.println(check_order(new int[]{3, 1, 3, 2, 3})); + } + + private static List check_order(int[] arr) { + List ret = new ArrayList<>(); + int[] sorted = Arrays.copyOf(arr, arr.length); + Arrays.sort(sorted); + for (int i = 0; i < sorted.length; ++i) { + if (sorted[i] != arr[i]) ret.add(i); + } + return ret; + } +} diff --git a/challenge-307/deadmarshal/java/Ch2.java b/challenge-307/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..a4ef4c615d --- /dev/null +++ b/challenge-307/deadmarshal/java/Ch2.java @@ -0,0 +1,24 @@ +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Ch2 { + public static void main(String[] args) { + System.out.println( + find_anagrams(new String[]{"acca", "dog", "god", "perl", "repl"})); + System.out.println( + find_anagrams(new String[]{"abba", "baba", "aabb", "ab", "ab"})); + } + + private static int find_anagrams(String[] arr) { + int sum = 1; + for (int i = 0; i < arr.length; ++i) { + arr[i] = Stream.of(arr[i].split("")) + .sorted() + .collect(Collectors.joining()); + } + for (int i = 1; i < arr.length; ++i) + if (!Objects.equals(arr[i - 1], arr[i])) sum++; + return sum; + } +} diff --git a/challenge-307/deadmarshal/modula-3/Ch1/src/Ch1.m3 b/challenge-307/deadmarshal/modula-3/Ch1/src/Ch1.m3 new file mode 100644 index 0000000000..ff9f34a8d6 --- /dev/null +++ b/challenge-307/deadmarshal/modula-3/Ch1/src/Ch1.m3 @@ -0,0 +1,38 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT SIO,IntArraySort,IntSeq; + +PROCEDURE CheckOrder(VAR A:ARRAY OF INTEGER):IntSeq.T = + VAR + Sorted := NEW(REF ARRAY OF INTEGER,NUMBER(A)); + S := NEW(IntSeq.T).init(NUMBER(A)); + BEGIN + SUBARRAY(Sorted^,FIRST(Sorted^),NUMBER(Sorted^)) := + SUBARRAY(A,FIRST(A),NUMBER(A)); + IntArraySort.Sort(Sorted^); + FOR I := FIRST(A) TO LAST(A) DO + IF A[I] # Sorted[I] THEN S.addhi(I); END + END; + RETURN S + END CheckOrder; + +PROCEDURE PrintSequence(READONLY S:IntSeq.T) = + BEGIN + FOR I := 0 TO S.size()-1 DO + SIO.PutInt(S.get(I)); + SIO.PutChar(' ') + END; + SIO.Nl() + END PrintSequence; + +VAR + A1 := ARRAY[0..4] OF INTEGER{5,2,4,3,1}; + A2 := ARRAY[0..4] OF INTEGER{1,2,1,1,3}; + A3 := ARRAY[0..4] OF INTEGER{3,1,3,2,3}; + +BEGIN + PrintSequence(CheckOrder(A1)); + PrintSequence(CheckOrder(A2)); + PrintSequence(CheckOrder(A3)) +END Ch1. + diff --git a/challenge-307/deadmarshal/modula-3/Ch1/src/m3makefile b/challenge-307/deadmarshal/modula-3/Ch1/src/m3makefile new file mode 100644 index 0000000000..643b33d043 --- /dev/null +++ b/challenge-307/deadmarshal/modula-3/Ch1/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +import("libsio") +implementation("Ch1") +program("Ch1") diff --git a/challenge-307/deadmarshal/modula-3/Ch2/src/Ch2.m3 b/challenge-307/deadmarshal/modula-3/Ch2/src/Ch2.m3 new file mode 100644 index 0000000000..8ad853d524 --- /dev/null +++ b/challenge-307/deadmarshal/modula-3/Ch2/src/Ch2.m3 @@ -0,0 +1,32 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT SIO,Text,CharArraySort; + +PROCEDURE SortText(VAR Str:TEXT):TEXT = + VAR + A := NEW(REF ARRAY OF CHAR,Text.Length(Str)); + BEGIN + Text.SetChars(A^,Str); + CharArraySort.Sort(A^); + RETURN Text.FromChars(A^) + END SortText; + +PROCEDURE FindAnagrams(VAR A:ARRAY OF TEXT):CARDINAL = + VAR Sum:CARDINAL := 1; + BEGIN + FOR I := FIRST(A) TO LAST(A) DO A[I] := SortText(A[I]) END; + FOR I := 1 TO LAST(A) DO + IF NOT Text.Equal(A[I-1],A[I]) THEN INC(Sum) END + END; + RETURN Sum + END FindAnagrams; + +VAR + A1 := ARRAY[0..4] OF TEXT{"acca","dog","god","perl","repl"}; + A2 := ARRAY[0..4] OF TEXT{"abba","baba","aabb","ab","ab"}; + +BEGIN + SIO.PutInt(FindAnagrams(A1)); SIO.Nl(); + SIO.PutInt(FindAnagrams(A2)); SIO.Nl() +END Ch2. + diff --git a/challenge-307/deadmarshal/modula-3/Ch2/src/m3makefile b/challenge-307/deadmarshal/modula-3/Ch2/src/m3makefile new file mode 100644 index 0000000000..b71c63d9b0 --- /dev/null +++ b/challenge-307/deadmarshal/modula-3/Ch2/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("libsio") +array_sort("Char","Char") +implementation("Ch2") +program("Ch2") diff --git a/challenge-307/deadmarshal/perl/ch-1.pl b/challenge-307/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..9a3170b3e9 --- /dev/null +++ b/challenge-307/deadmarshal/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Data::Show; + +sub check_order{ + my @sorted = sort{$a <=> $b} @{$_[0]}; + map{$sorted[$_] != $_[0]->[$_] ? $_ : ()} 0..$#sorted +} + +print show check_order([5,2,4,3,1]); +print show check_order([1,2,1,1,3]); +print show check_order([3,1,3,2,3]); + diff --git a/challenge-307/deadmarshal/perl/ch-2.pl b/challenge-307/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..eac358a96a --- /dev/null +++ b/challenge-307/deadmarshal/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub find_anagrams{ + my ($arr) = @_; + my @sorted = map {join'',sort split ''} @$arr; + my $sum = 1; + map{$sum++ if $sorted[$_-1] ne $sorted[$_]} 1..$#sorted; + $sum +} + +printf "%d\n",find_anagrams(['acca','dog','god','perl','repl']); +printf "%d\n",find_anagrams(['abba','baba','aabb','ab','ab']); + diff --git a/challenge-307/deadmarshal/zig/ch1/build.zig b/challenge-307/deadmarshal/zig/ch1/build.zig new file mode 100644 index 0000000000..3a545f1f98 --- /dev/null +++ b/challenge-307/deadmarshal/zig/ch1/build.zig @@ -0,0 +1,91 @@ +const std = @import("std"); + +// Although this function looks imperative, note that its job is to +// declaratively construct a build graph that will be executed by an external +// runner. +pub fn build(b: *std.Build) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); + + const lib = b.addStaticLibrary(.{ + .name = "ch1", + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = b.path("src/root.zig"), + .target = target, + .optimize = optimize, + }); + + // This declares intent for the library to be installed into the standard + // location when the user invokes the "install" step (the default step when + // running `zig build`). + b.installArtifact(lib); + + const exe = b.addExecutable(.{ + .name = "ch1", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); + + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // By making the run step depend on the install step, it will be run from the + // installation directory rather than directly from within the cache directory. + // This is not necessary, however, if the application depends on other installed + // files, this ensures they will be present and in the expected location. + run_cmd.step.dependOn(b.getInstallStep()); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + // Creates a step for unit testing. This only builds the test executable + // but does not run it. + const lib_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/root.zig"), + .target = target, + .optimize = optimize, + }); + + const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); + + const exe_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + + const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + + // Similar to creating the run step earlier, this exposes a `test` step to + // the `zig build --help` menu, providing a way for the user to request + // running the unit tests. + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_lib_unit_tests.step); + test_step.dependOn(&run_exe_unit_tests.step); +} diff --git a/challenge-307/deadmarshal/zig/ch1/build.zig.zon b/challenge-307/deadmarshal/zig/ch1/build.zig.zon new file mode 100644 index 0000000000..a98b632533 --- /dev/null +++ b/challenge-307/deadmarshal/zig/ch1/build.zig.zon @@ -0,0 +1,72 @@ +.{ + // This is the default name used by packages depending on this one. For + // example, when a user runs `zig fetch --save `, this field is used + // as the key in the `dependencies` table. Although the user can choose a + // different name, most users will stick with this provided value. + // + // It is redundant to include "zig" in this name because it is already + // within the Zig package namespace. + .name = "ch1", + + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + + // This field is optional. + // This is currently advisory only; Zig does not yet do anything + // with this value. + //.minimum_zig_version = "0.11.0", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. Only files listed here will remain on disk + // when using the zig package manager. As a rule of thumb, one should list + // files required for compilation plus any license(s). + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + // For example... + //"LICENSE", + //"README.md", + }, +} diff --git a/challenge-307/deadmarshal/zig/ch1/src/main.zig b/challenge-307/deadmarshal/zig/ch1/src/main.zig new file mode 100644 index 0000000000..4f72fd5771 --- /dev/null +++ b/challenge-307/deadmarshal/zig/ch1/src/main.zig @@ -0,0 +1,60 @@ +const std = @import("std"); + +fn checkOrder( + comptime T: type, + allocator: std.mem.Allocator, + arr: []T, +) !std.ArrayList(usize) { + var ret = std.ArrayList(usize).init(allocator); + errdefer ret.deinit(); + const sorted = try allocator.alloc(u8, arr.len); + defer allocator.free(sorted); + + std.mem.copyForwards(T, sorted, arr); + std.mem.sort(T, sorted, {}, std.sort.asc(T)); + + for (sorted, 0..) |_, i| { + if (sorted[i] != arr[i]) { + try ret.append(i); + } + } + + return ret; +} + +fn printArrayList( + comptime T: type, + al: std.ArrayList(T), +) void { + for (al.items) |e| { + std.debug.print("{} ", .{e}); + } + std.debug.print("{s}", .{"\n"}); +} + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer { + std.debug.assert(gpa.detectLeaks() == false); + std.debug.assert(gpa.deinit() == .ok); + } + const allocator = gpa.allocator(); + + var arr1 = [_]u8{ 5, 2, 4, 3, 1 }; + var arr2 = [_]u8{ 1, 2, 1, 1, 3 }; + var arr3 = [_]u8{ 3, 1, 3, 2, 3 }; + + const al1 = try checkOrder(u8, allocator, &arr1); + const al2 = try checkOrder(u8, allocator, &arr2); + const al3 = try checkOrder(u8, allocator, &arr3); + + defer { + al1.deinit(); + al2.deinit(); + al3.deinit(); + } + + printArrayList(usize, al1); + printArrayList(usize, al2); + printArrayList(usize, al3); +} diff --git a/challenge-307/deadmarshal/zig/ch1/src/root.zig b/challenge-307/deadmarshal/zig/ch1/src/root.zig new file mode 100644 index 0000000000..ecfeade1a3 --- /dev/null +++ b/challenge-307/deadmarshal/zig/ch1/src/root.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const testing = std.testing; + +export fn add(a: i32, b: i32) i32 { + return a + b; +} + +test "basic add functionality" { + try testing.expect(add(3, 7) == 10); +} diff --git a/challenge-307/deadmarshal/zig/ch2/build.zig b/challenge-307/deadmarshal/zig/ch2/build.zig new file mode 100644 index 0000000000..9a44c64cdc --- /dev/null +++ b/challenge-307/deadmarshal/zig/ch2/build.zig @@ -0,0 +1,91 @@ +const std = @import("std"); + +// Alth