diff options
| author | Ali <adeadmarshal@gmail.com> | 2025-06-16 11:32:09 +0330 |
|---|---|---|
| committer | Ali <adeadmarshal@gmail.com> | 2025-06-16 11:32:09 +0330 |
| commit | cdc47b7166ceaa3b26d50060a0aaa73181a65ede (patch) | |
| tree | 95bbb04ecd28804768a35bc3dcc4a5fb9ee3d367 | |
| parent | 26cfae99bb0a2fdf9710bcc51e8abc8d7ed627f6 (diff) | |
| download | perlweeklychallenge-club-cdc47b7166ceaa3b26d50060a0aaa73181a65ede.tar.gz perlweeklychallenge-club-cdc47b7166ceaa3b26d50060a0aaa73181a65ede.tar.bz2 perlweeklychallenge-club-cdc47b7166ceaa3b26d50060a0aaa73181a65ede.zip | |
TWC326
| -rw-r--r-- | challenge-326/deadmarshal/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/erlang/ch1.erl | 10 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/erlang/ch2.erl | 17 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/go/ch1.go | 21 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/go/ch2.go | 19 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/java/Ch1.java | 13 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/java/Ch2.java | 21 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/modula-3/Ch1/src/Ch1.m3 | 38 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/modula-3/Ch1/src/m3makefile | 5 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/modula-3/Ch2/src/Ch2.m3 | 33 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/modula-3/Ch2/src/m3makefile | 4 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/perl/ch-1.pl | 13 | ||||
| -rw-r--r-- | challenge-326/deadmarshal/perl/ch-2.pl | 19 |
13 files changed, 214 insertions, 0 deletions
diff --git a/challenge-326/deadmarshal/blog.txt b/challenge-326/deadmarshal/blog.txt new file mode 100644 index 0000000000..e3d9959ba6 --- /dev/null +++ b/challenge-326/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2025/06/twc326.html diff --git a/challenge-326/deadmarshal/erlang/ch1.erl b/challenge-326/deadmarshal/erlang/ch1.erl new file mode 100644 index 0000000000..78d0b924af --- /dev/null +++ b/challenge-326/deadmarshal/erlang/ch1.erl @@ -0,0 +1,10 @@ +-module(ch1). +-export([day_of_the_year/1]). + +-spec day_of_the_year(string()) -> non_neg_integer(). +day_of_the_year(DateStr) -> + {ok,[Year,Month,Day],_} = io_lib:fread("~d-~d-~d",DateStr), + Date = {Year,Month,Day}, + calendar:date_to_gregorian_days(Date) - + calendar:date_to_gregorian_days(Year,1,1) + 1. + diff --git a/challenge-326/deadmarshal/erlang/ch2.erl b/challenge-326/deadmarshal/erlang/ch2.erl new file mode 100644 index 0000000000..ece1fb03bb --- /dev/null +++ b/challenge-326/deadmarshal/erlang/ch2.erl @@ -0,0 +1,17 @@ +-module(ch2). +-export([decompressed_list/1]). + +-spec pairs(L,L) -> R when + L :: [T], + R :: [T], + T :: integer(). +pairs([],Acc) -> lists:reverse(Acc); +pairs([A,B|T],Acc) -> pairs(T,[{A,B}|Acc]). + +-spec decompressed_list(L) -> R when + L :: [T], + R :: [T], + T :: integer(). +decompressed_list(L) -> + lists:flatmap(fun({A,B}) -> lists:duplicate(A,B) end,pairs(L,[])). + diff --git a/challenge-326/deadmarshal/go/ch1.go b/challenge-326/deadmarshal/go/ch1.go new file mode 100644 index 0000000000..36eb64f761 --- /dev/null +++ b/challenge-326/deadmarshal/go/ch1.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "log" + "time" +) + +func dayOfTheYear(str string) int { + t, err := time.Parse(time.DateOnly, str) + if err != nil { + log.Fatal("Wrong date string") + } + return t.YearDay() +} + +func main() { + fmt.Println(dayOfTheYear("2025-02-02")) + fmt.Println(dayOfTheYear("2025-04-10")) + fmt.Println(dayOfTheYear("2025-09-07")) +} diff --git a/challenge-326/deadmarshal/go/ch2.go b/challenge-326/deadmarshal/go/ch2.go new file mode 100644 index 0000000000..c80834c5fb --- /dev/null +++ b/challenge-326/deadmarshal/go/ch2.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func decompressedList(arr []int) []int { + res := []int{} + for i := 0; i < len(arr); i += 2 { + for j := 1; j <= arr[i]; j++ { + res = append(res, arr[i+1]) + } + } + return res +} + +func main() { + fmt.Println(decompressedList([]int{1, 3, 2, 4})) + fmt.Println(decompressedList([]int{1, 1, 2, 2})) + fmt.Println(decompressedList([]int{3, 1, 3, 2})) +} diff --git a/challenge-326/deadmarshal/java/Ch1.java b/challenge-326/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..5d8c082edd --- /dev/null +++ b/challenge-326/deadmarshal/java/Ch1.java @@ -0,0 +1,13 @@ +import java.time.LocalDate; + +public class Ch1 { + public static void main(String[] args) { + System.out.println(day_of_the_year("2025-02-02")); + System.out.println(day_of_the_year("2025-04-10")); + System.out.println(day_of_the_year("2025-09-07")); + } + + private static int day_of_the_year(String date) { + return LocalDate.parse(date).getDayOfYear(); + } +}
\ No newline at end of file diff --git a/challenge-326/deadmarshal/java/Ch2.java b/challenge-326/deadmarshal/java/Ch2.java new file mode 100644 index 0000000000..67ae25bcc0 --- /dev/null +++ b/challenge-326/deadmarshal/java/Ch2.java @@ -0,0 +1,21 @@ +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Ch2 { + public static void main(String[] args) { + System.out.println(decompressed_list(new int[]{1, 3, 2, 4})); + System.out.println(decompressed_list(new int[]{1, 1, 2, 2})); + System.out.println(decompressed_list(new int[]{3, 1, 3, 2})); + } + + private static List<Integer> decompressed_list(int[] arr) { + return IntStream.range(0, arr.length / 2) + .mapToObj(i -> Arrays.stream(new int[arr[2 * i]]) + .map(val -> arr[2 * i + 1]) + .boxed()) + .flatMap(s -> s) + .collect(Collectors.toList()); + } +} diff --git a/challenge-326/deadmarshal/modula-3/Ch1/src/Ch1.m3 b/challenge-326/deadmarshal/modula-3/Ch1/src/Ch1.m3 new file mode 100644 index 0000000000..d19d99bf77 --- /dev/null +++ b/challenge-326/deadmarshal/modula-3/Ch1/src/Ch1.m3 @@ -0,0 +1,38 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT SIO,Scan,Text,Lex,FloatMode; +FROM StableError IMPORT Halt; + +PROCEDURE IsLeapYear(Y:CARDINAL):BOOLEAN = + BEGIN + RETURN Y MOD 4 = 0 AND Y MOD 100 # 0 OR Y MOD 400 = 0 + END IsLeapYear; + +PROCEDURE DayOfTheYear(READONLY Date:TEXT):CARDINAL + RAISES{FloatMode.Trap,Lex.Error} = + CONST + Days = ARRAY[0..1],[0..12] OF CARDINAL{ + ARRAY[0..12] OF CARDINAL{0,0,31,59,90,120,151,181,212,243,273,304,334}, + ARRAY[0..12] OF CARDINAL{0,0,31,60,91,121,152,182,213,244,274,305,335}}; + VAR + Month,Day,Year:CARDINAL; + BEGIN + Year := Scan.Int(Text.Sub(Date,0,4)); + Month := Scan.Int(Text.Sub(Date,5,2)); + Day := Scan.Int(Text.Sub(Date,8,2)); + RETURN Days[ORD(IsLeapYear(Year)),Month] + Day; + END DayOfTheYear; + +BEGIN + TRY + SIO.PutInt(DayOfTheYear("2025-02-02")); SIO.Nl(); + SIO.PutInt(DayOfTheYear("2025-04-10")); SIO.Nl(); + SIO.PutInt(DayOfTheYear("2025-09-07")); SIO.Nl() + EXCEPT + FloatMode.Trap => Halt("FloatMode.Trap") + | Lex.Error => Halt("Failed lexing text") + ELSE + Halt("Some other exception!") + END; +END Ch1. + diff --git a/challenge-326/deadmarshal/modula-3/Ch1/src/m3makefile b/challenge-326/deadmarshal/modula-3/Ch1/src/m3makefile new file mode 100644 index 0000000000..4182be12b1 --- /dev/null +++ b/challenge-326/deadmarshal/modula-3/Ch1/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("libsio") +import("stable") +implementation("Ch1") +program("Ch1") diff --git a/challenge-326/deadmarshal/modula-3/Ch2/src/Ch2.m3 b/challenge-326/deadmarshal/modula-3/Ch2/src/Ch2.m3 new file mode 100644 index 0000000000..c9209c3d4c --- /dev/null +++ b/challenge-326/deadmarshal/modula-3/Ch2/src/Ch2.m3 @@ -0,0 +1,33 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT SIO,IntSeq; + +VAR + A1 := ARRAY[0..3] OF CARDINAL{1,3,2,4}; + A2 := ARRAY[0..3] OF CARDINAL{1,1,2,2}; + A3 := ARRAY[0..3] OF CARDINAL{3,1,3,2}; + +PROCEDURE DecompressedList(VAR A:ARRAY OF CARDINAL):IntSeq.T = + VAR Res := NEW(IntSeq.T).init(NUMBER(A)); + BEGIN + FOR I := FIRST(A) TO LAST(A) BY 2 DO + FOR J := 1 TO A[I] DO Res.addhi(A[I+1]) END; + END; + RETURN Res + END DecompressedList; + +PROCEDURE PrintSeq(READONLY S:IntSeq.T) = + BEGIN + FOR I := 0 TO S.size()-1 DO + SIO.PutInt(S.get(I)); + SIO.PutChar(' ') + END; + SIO.Nl() + END PrintSeq; + +BEGIN + PrintSeq(DecompressedList(A1)); + PrintSeq(DecompressedList(A2)); + PrintSeq(DecompressedList(A3)) +END Ch2. + diff --git a/challenge-326/deadmarshal/modula-3/Ch2/src/m3makefile b/challenge-326/deadmarshal/modula-3/Ch2/src/m3makefile new file mode 100644 index 0000000000..78802242fe --- /dev/null +++ b/challenge-326/deadmarshal/modula-3/Ch2/src/m3makefile @@ -0,0 +1,4 @@ +import("libm3") +import("libsio") +implementation("Ch2") +program("Ch2") diff --git a/challenge-326/deadmarshal/perl/ch-1.pl b/challenge-326/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..4e87963284 --- /dev/null +++ b/challenge-326/deadmarshal/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Time::Piece; + +sub day_of_the_year{ + Time::Piece->strptime($_[0],'%Y-%m-%d')->yday + 1 +} + +printf "%d\n",day_of_the_year('2025-02-02'); +printf "%d\n",day_of_the_year('2025-04-10'); +printf "%d\n",day_of_the_year('2025-09-07'); + diff --git a/challenge-326/deadmarshal/perl/ch-2.pl b/challenge-326/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..404746a0fc --- /dev/null +++ b/challenge-326/deadmarshal/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Data::Show; + +sub decompressed_list{ + my ($arr) = @_; + my ($i,@res) = (0); + while($i < $#{$arr}){ + push @res, ($arr->[$i+1]) x $arr->[$i]; + $i += 2 + } + @res +} + +print show decompressed_list([1,3,2,4]); +print show decompressed_list([1,1,2,2]); +print show decompressed_list([3,1,3,2]); + |
