diff options
| author | Ali <adeadmarshal@gmail.com> | 2025-11-17 19:21:57 +0330 |
|---|---|---|
| committer | Ali <adeadmarshal@gmail.com> | 2025-11-17 19:21:57 +0330 |
| commit | cd1ed827514a111e3a9191ba92f198a5819fd355 (patch) | |
| tree | 2f015b28366f5fe8815bb9d5026a305d5f02f135 | |
| parent | 51f93bc0962522ed3bc5152f8266cc780c83f190 (diff) | |
| download | perlweeklychallenge-club-cd1ed827514a111e3a9191ba92f198a5819fd355.tar.gz perlweeklychallenge-club-cd1ed827514a111e3a9191ba92f198a5819fd355.tar.bz2 perlweeklychallenge-club-cd1ed827514a111e3a9191ba92f198a5819fd355.zip | |
TWC348
19 files changed, 434 insertions, 0 deletions
diff --git a/challenge-348/deadmarshal/blog.txt b/challenge-348/deadmarshal/blog.txt new file mode 100644 index 0000000000..2daa8afd72 --- /dev/null +++ b/challenge-348/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2025/11/twc348.html diff --git a/challenge-348/deadmarshal/erlang/ch1.erl b/challenge-348/deadmarshal/erlang/ch1.erl new file mode 100644 index 0000000000..c6a6462324 --- /dev/null +++ b/challenge-348/deadmarshal/erlang/ch1.erl @@ -0,0 +1,29 @@ +-module(ch1). +-export([string_alike/1,main/0]). + +-spec string_alike(string()) -> boolean(). +string_alike(S) -> + Vowels = sets:from_list("aeiouAEIOU"), + H = string:length(S) div 2, + {First,Second} = lists:split(H,S), + C1 = count_vowels(First,Vowels), + C2 = count_vowels(Second,Vowels), + C1 =:= C2 andalso C1 =/= 0. + +-spec count_vowels(string(),sets:set(char())) -> non_neg_integer(). +count_vowels(S,Vowels) -> + lists:foldl(fun(C,Acc) -> + case sets:is_element(C,Vowels) of + true -> Acc + 1; + false -> Acc + end + end,0,S). + +main() -> + io:format("~p~n~p~n~p~n~p~n~p~n", + [string_alike("textbook"), + string_alike("book"), + string_alike("AbCdEfGh"), + string_alike("rhythmmyth"), + string_alike("UmpireeAudio")]). + diff --git a/challenge-348/deadmarshal/erlang/ch2.erl b/challenge-348/deadmarshal/erlang/ch2.erl new file mode 100644 index 0000000000..67822df40e --- /dev/null +++ b/challenge-348/deadmarshal/erlang/ch2.erl @@ -0,0 +1,28 @@ +-module(ch2). +-export([convert_time/2,main/0]). + +-spec to_min(string()) -> integer(). +to_min(S) -> + [HStr,MStr] = string:split(S,":",all), + H = list_to_integer(HStr), + M = list_to_integer(MStr), + H * 60 + M. + +-spec convert_time(string(),string()) -> integer(). +convert_time(Source,Target) -> + A = to_min(Source), + B = to_min(Target), + D = (B - A + 1440) rem 1440, + {Res,_} = lists:foldl(fun(Step,{Acc,Rem}) -> + {Acc + (Rem div Step), Rem rem Step} + end,{0,D},[60,15,5,1]), + Res. + +main() -> + io:format("~p~n~p~n~p~n~p~n~p~n", + [convert_time("02:30","02:45"), + convert_time("11:55","12:15"), + convert_time("09:00","13:00"), + convert_time("23:45","00:30"), + convert_time("14:20","15:25")]). + diff --git a/challenge-348/deadmarshal/gleam/ch1/README.md b/challenge-348/deadmarshal/gleam/ch1/README.md new file mode 100644 index 0000000000..a6237ef8ed --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch1/README.md @@ -0,0 +1,24 @@ +# ch1 + +[](https://hex.pm/packages/ch1) +[](https://hexdocs.pm/ch1/) + +```sh +gleam add ch1@1 +``` +```gleam +import ch1 + +pub fn main() -> Nil { + // TODO: An example of the project in use +} +``` + +Further documentation can be found at <https://hexdocs.pm/ch1>. + +## Development + +```sh +gleam run # Run the project +gleam test # Run the tests +``` diff --git a/challenge-348/deadmarshal/gleam/ch1/gleam.toml b/challenge-348/deadmarshal/gleam/ch1/gleam.toml new file mode 100644 index 0000000000..90e2fbfc62 --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch1/gleam.toml @@ -0,0 +1,19 @@ +name = "ch1" +version = "1.0.0" + +# Fill out these fields if you intend to generate HTML documentation or publish +# your project to the Hex package manager. +# +# description = "" +# licences = ["Apache-2.0"] +# repository = { type = "github", user = "", repo = "" } +# links = [{ title = "Website", href = "" }] +# +# For a full reference of all the available options, you can have a look at +# https://gleam.run/writing-gleam/gleam-toml/. + +[dependencies] +gleam_stdlib = ">= 0.44.0 and < 2.0.0" + +[dev-dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/challenge-348/deadmarshal/gleam/ch1/manifest.toml b/challenge-348/deadmarshal/gleam/ch1/manifest.toml new file mode 100644 index 0000000000..61ab519d2d --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch1/manifest.toml @@ -0,0 +1,11 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" }, + { name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" }, +] + +[requirements] +gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } diff --git a/challenge-348/deadmarshal/gleam/ch1/src/ch1.gleam b/challenge-348/deadmarshal/gleam/ch1/src/ch1.gleam new file mode 100644 index 0000000000..deae02ea6c --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch1/src/ch1.gleam @@ -0,0 +1,31 @@ +import gleam/bool +import gleam/io +import gleam/list +import gleam/set +import gleam/string + +fn count_vowels(s: String) -> Int { + let vowels = set.from_list("aeiouAEIOU" |> string.to_graphemes) + list.fold(s |> string.to_graphemes, 0, fn(acc, c) { + case set.contains(vowels, c) { + True -> acc + 1 + False -> acc + } + }) +} + +fn string_alike(s: String) -> Bool { + let n = string.length(s) + let half = n / 2 + let #(first, second) = #(string.slice(s, 0, half), string.slice(s, half, n)) + let c1 = count_vowels(first) + let c2 = count_vowels(second) + c1 == c2 && c1 != 0 +} + +pub fn main() -> Nil { + list.each( + ["textbook", "book", "AbCdEfGh", "rhythmmyth", "UmpireeAudio"], + fn(s) { s |> string_alike |> bool.to_string |> io.println }, + ) +} diff --git a/challenge-348/deadmarshal/gleam/ch1/test/ch1_test.gleam b/challenge-348/deadmarshal/gleam/ch1/test/ch1_test.gleam new file mode 100644 index 0000000000..fba3c8872b --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch1/test/ch1_test.gleam @@ -0,0 +1,13 @@ +import gleeunit + +pub fn main() -> Nil { + gleeunit.main() +} + +// gleeunit test functions end in `_test` +pub fn hello_world_test() { + let name = "Joe" + let greeting = "Hello, " <> name <> "!" + + assert greeting == "Hello, Joe!" +} diff --git a/challenge-348/deadmarshal/gleam/ch2/README.md b/challenge-348/deadmarshal/gleam/ch2/README.md new file mode 100644 index 0000000000..bf772e3022 --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch2/README.md @@ -0,0 +1,24 @@ +# ch2 + +[](https://hex.pm/packages/ch2) +[](https://hexdocs.pm/ch2/) + +```sh +gleam add ch2@1 +``` +```gleam +import ch2 + +pub fn main() -> Nil { + // TODO: An example of the project in use +} +``` + +Further documentation can be found at <https://hexdocs.pm/ch2>. + +## Development + +```sh +gleam run # Run the project +gleam test # Run the tests +``` diff --git a/challenge-348/deadmarshal/gleam/ch2/gleam.toml b/challenge-348/deadmarshal/gleam/ch2/gleam.toml new file mode 100644 index 0000000000..001a64da6a --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch2/gleam.toml @@ -0,0 +1,19 @@ +name = "ch2" +version = "1.0.0" + +# Fill out these fields if you intend to generate HTML documentation or publish +# your project to the Hex package manager. +# +# description = "" +# licences = ["Apache-2.0"] +# repository = { type = "github", user = "", repo = "" } +# links = [{ title = "Website", href = "" }] +# +# For a full reference of all the available options, you can have a look at +# https://gleam.run/writing-gleam/gleam-toml/. + +[dependencies] +gleam_stdlib = ">= 0.44.0 and < 2.0.0" + +[dev-dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/challenge-348/deadmarshal/gleam/ch2/manifest.toml b/challenge-348/deadmarshal/gleam/ch2/manifest.toml new file mode 100644 index 0000000000..61ab519d2d --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch2/manifest.toml @@ -0,0 +1,11 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" }, + { name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" }, +] + +[requirements] +gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } diff --git a/challenge-348/deadmarshal/gleam/ch2/src/ch2.gleam b/challenge-348/deadmarshal/gleam/ch2/src/ch2.gleam new file mode 100644 index 0000000000..71bfad8e81 --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch2/src/ch2.gleam @@ -0,0 +1,41 @@ +import gleam/int +import gleam/io +import gleam/list +import gleam/string + +fn to_min(s: String) -> Int { + let assert [hstr, mstr] = string.split(s, ":") + let assert Ok(h) = int.parse(hstr) + let assert Ok(m) = int.parse(mstr) + h * 60 + m +} + +fn convert_time(source: String, target: String) -> Int { + let a = to_min(source) + let b = to_min(target) + let d = { b - a + 1440 } % 1440 + let #(res, _) = + list.fold([60, 15, 5, 1], #(0, d), fn(step, e) { + let #(total, remainder) = step + #(total + { remainder / e }, remainder % e) + }) + res +} + +pub fn main() -> Nil { + list.each( + [ + ["02:30", "02:45"], + ["11:55", "12:15"], + ["09:00", "13:00"], + ["23:45", "00:30"], + ["14:20", "15:25"], + ], + fn(ss) { + let assert [f, s] = ss + convert_time(f, s) + |> int.to_string + |> io.println + }, + ) +} diff --git a/challenge-348/deadmarshal/gleam/ch2/test/ch2_test.gleam b/challenge-348/deadmarshal/gleam/ch2/test/ch2_test.gleam new file mode 100644 index 0000000000..fba3c8872b --- /dev/null +++ b/challenge-348/deadmarshal/gleam/ch2/test/ch2_test.gleam @@ -0,0 +1,13 @@ +import gleeunit + +pub fn main() -> Nil { + gleeunit.main() +} + +// gleeunit test functions end in `_test` +pub fn hello_world_test() { + let name = "Joe" + let greeting = "Hello, " <> name <> "!" + + assert greeting == "Hello, Joe!" +} diff --git a/challenge-348/deadmarshal/go/ch1.go b/challenge-348/deadmarshal/go/ch1.go new file mode 100644 index 0000000000..855c973ad5 --- /dev/null +++ b/challenge-348/deadmarshal/go/ch1.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "strings" +) + +func countVowels(s string) (c int) { + for _, ch := range strings.ToLower(s) { + switch ch { + case 'a', 'e', 'i', 'o', 'u': + c++ + } + } + return +} + +func stringAlike(s string) bool { + mid := len(s) / 2 + a, b := countVowels(s[:mid]), countVowels(s[mid:]) + return a == b && a != 0 +} + +func main() { + fmt.Println(stringAlike("textbook")) + fmt.Println(stringAlike("book")) + fmt.Println(stringAlike("AbCdEfGh")) + fmt.Println(stringAlike("rhythmmyth")) + fmt.Println(stringAlike("UmpireeAudio")) +} diff --git a/challenge-348/deadmarshal/go/ch2.go b/challenge-348/deadmarshal/go/ch2.go new file mode 100644 index 0000000000..e21fb47625 --- /dev/null +++ b/challenge-348/deadmarshal/go/ch2.go @@ -0,0 +1,40 @@ +package main + +import ( + "errors" + "fmt" + "strconv" + "strings" +) + +func toMin(s string) (int, error) { + parts := strings.Split(s, ":") + h, err1 := strconv.Atoi(parts[0]) + m, err2 := strconv.Atoi(parts[1]) + if je := errors.Join(err1, err2); je != nil { + return -1, je + } + return h*60 + m, nil +} + +func convertTime(source string, target string) int { + a, err1 := toMin(source) + b, err2 := toMin(target) + if errors.Join(err1, err2) != nil { + panic("Wrong format!") + } + res, d := 0, (b-a+1440)%1440 + for _, i := range []int{60, 15, 5, 1} { + res += d / i + d %= i + } + return res +} + +func main() { + fmt.Println(convertTime("02:30", "02:45")) + fmt.Println(convertTime("11:55", "12:15")) + fmt.Println(convertTime("09:00", "13:00")) + fmt.Println(convertTime("23:45", "00:30")) + fmt.Println(convertTime("14:20", "15:25")) +} diff --git a/challenge-348/deadmarshal/java/Ch1.java b/challenge-348/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..8a27ba54a4 --- /dev/null +++ b/challenge-348/deadmarshal/java/Ch1.java @@ -0,0 +1,29 @@ +import java.util.Set; + +public class Ch1 { + public static void main(String[] args) { + System.out.println(string_alike("textbook")); + System.out.println(string_alike("book")); + System.out.println(string_alike("AbCdEfGh")); + System.out.println(string_alike("rhythmmyth")); + System.out.println(string_alike("UmpireeAudio")); + } + + private static boolean string_alike(String s) { + int mid = s.length() / 2; + int a = count_vowels(s.substring(0,mid)); + int b = count_vowels(s.substring(mid)); + return a == b && a != 0; + } + + private static int count_vowels(String s) { + int count = 0; + for(char c : s.toLowerCase().toCharArray()) { + switch(c) { + case 'a','e','i','o','u' -> count++; + } + } + return count; + } +} + diff --git a/challenge-348/deadmarshal/java/Ch2.java b/challenge-348/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..9af728fde0 --- /dev/null +++ b/challenge-348/deadmarshal/java/Ch2.java @@ -0,0 +1,29 @@ +import java.util.Arrays; + +public class Ch2 { + public static void main(String[] args) { + System.out.println(convert_time("02:30","02:45")); + System.out.println(convert_time("11:55","12:15")); + System.out.println(convert_time("09:00","13:00")); + System.out.println(convert_time("23:45","00:30")); + System.out.println(convert_time("14:20","15:25")); + } + + private static int convert_time(String source,String target) { + int a = to_min(source),b = to_min(target); + int res = 0, d = (b - a + 1440) % 1440; + for(int i : Arrays.asList(60,15,5,1)) { + res += d / i; + d %= i; + } + return res; + } + + private static int to_min(String s) { + String[] parts = s.split(":"); + int a = Integer.parseInt(parts[0]); + int b = Integer.parseInt(parts[1]); + return a * 60 + b; + } +} + diff --git a/challenge-348/deadmarshal/perl/ch-1.pl b/challenge-348/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..1a9feb904d --- /dev/null +++ b/challenge-348/deadmarshal/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub string_alike{ + my $h = length($_[0]) / 2; + my $a = (substr $_[0],0,$h) =~ tr/aeiouAEIOU//; + my $b = (substr $_[0],$h) =~ tr/aeiouAEIOU//; + $a == $b && $a != 0 +} + +printf "%d\n",string_alike('textbook'); +printf "%d\n",string_alike('book'); +printf "%d\n",string_alike('AbCdEfGh'); +printf "%d\n",string_alike('rhythmmyth'); +printf "%d\n",string_alike('UmpireeAudio') + diff --git a/challenge-348/deadmarshal/perl/ch-2.pl b/challenge-348/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..e5e33e687f --- /dev/null +++ b/challenge-348/deadmarshal/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub to_min{ + my @parts = split ':',$_[0]; + $parts[0] * 60 + $parts[1] +} + +sub convert_time{ + my $diff = (to_min($_[1]) - to_min($_[0]) + 1440) % 1440; + my $res = 0; + foreach my $i(qw(60 15 5 1)) { + $res += $diff / $i; + $diff %= $i + } + $res +} + +printf "%d\n",convert_time('02:30','02:45'); +printf "%d\n",convert_time('11:55','12:15'); +printf "%d\n",convert_time('09:00','13:00'); +printf "%d\n",convert_time('23:45','00:30'); +printf "%d\n",convert_time('14:20','15:25') + |
