From cdef6ae41439d99cbc884343dc0e3ff35e892eaa Mon Sep 17 00:00:00 2001 From: pme Date: Sat, 26 Jul 2025 10:52:45 +0200 Subject: challenge-332 --- challenge-332/peter-meszaros/perl/ch-1.pl | 55 ++++++++++++++++++++++++++ challenge-332/peter-meszaros/perl/ch-2.pl | 65 +++++++++++++++++++++++++++++++ challenge-332/peter-meszaros/tcl/ch-1.tcl | 51 ++++++++++++++++++++++++ challenge-332/peter-meszaros/tcl/ch-2.tcl | 64 ++++++++++++++++++++++++++++++ 4 files changed, 235 insertions(+) create mode 100755 challenge-332/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-332/peter-meszaros/perl/ch-2.pl create mode 100755 challenge-332/peter-meszaros/tcl/ch-1.tcl create mode 100755 challenge-332/peter-meszaros/tcl/ch-2.tcl diff --git a/challenge-332/peter-meszaros/perl/ch-1.pl b/challenge-332/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..4dfcad7a82 --- /dev/null +++ b/challenge-332/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Binary Date + +Submitted by: Mohammad Sajid Anwar + +You are given a date in the format YYYY-MM-DD. + +Write a script to convert it into binary date. + +=head2 Example 1 + + Input: $date = "2025-07-26" + Output: "11111101001-111-11010" + +=head2 Example 2 + + Input: $date = "2000-02-02" + Output: "11111010000-10-10" + +=head2 Example 3 + + Input: $date = "2024-12-31" + Output: "11111101000-1100-11111" + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ["2025-07-26", "11111101001-111-11010", "Example 1"], + ["2000-02-02", "11111010000-10-10", "Example 2"], + ["2024-12-31", "11111101000-1100-11111", "Example 3"], +]; + +sub binary_date +{ + my $date = shift; + + my @bindate; + for my $i (split /-/, $date) { + push @bindate, sprintf("%b", $i); + } + return join '-', @bindate; +} + +for (@$cases) { + is(binary_date($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-332/peter-meszaros/perl/ch-2.pl b/challenge-332/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..826450fabd --- /dev/null +++ b/challenge-332/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Odd Letters + +Submitted by: Mohammad Sajid Anwar + +You are given a string. + +Write a script to find out if each letter in the given string appeared odd +number of times. + +=head2 Example 1 + + Input: $str = "weekly" + Output: false + + w: 1 time + e: 2 times + k: 1 time + l: 1 time + y: 1 time + + The letter 'e' appeared 2 times i.e. even. + +=head2 Example 2 + + Input: $str = "perl" + Output: true + +=head2 Example 3 + + Input: $source = "challenge" + Output: false + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ["weekly", 0, "Example 1"], + ["perl", 1, "Example 2"], + ["challenge", 0, "Example 3"], +]; + +sub odd_letters +{ + my $str = shift; + + my %count; + $count{$_}++ for split //, $str; + for my $letter (keys %count) { + return 0 unless $count{$letter} % 2; + } + return 1; +} + +for (@$cases) { + is(odd_letters($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-332/peter-meszaros/tcl/ch-1.tcl b/challenge-332/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..618db2b98e --- /dev/null +++ b/challenge-332/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,51 @@ +#!/usr/bin/env tclsh +# +# Task 1: Binary Date +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given a date in the format YYYY-MM-DD. +# +# Write a script to convert it into binary date. +# +# Example 1 +# +# Input: $date = "2025-07-26" +# Output: "11111101001-111-11010" +# +# Example 2 +# +# Input: $date = "2000-02-02" +# Output: "11111010000-10-10" +# +# Example 3 +# +# Input: $date = "2024-12-31" +# Output: "11111101000-1100-11111" +# + +package require tcltest + +set cases { + {"2025-07-26" "11111101001-111-11010" "Example 1"} + {"2000-02-02" "11111010000-10-10" "Example 2"} + {"2024-12-31" "11111101000-1100-11111" "Example 3"} +} + +proc binary_date {date} { + set bindate {} + foreach part [split $date "-"] { + lappend bindate [format "%b" $part] + } + return [join $bindate "-"] +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + binary_date [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-332/peter-meszaros/tcl/ch-2.tcl b/challenge-332/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..75efcc80ed --- /dev/null +++ b/challenge-332/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,64 @@ +#!/usr/bin/env tclsh +# +# Task 2: Odd Letters +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string. +# +# Write a script to find out if each letter in the given string appeared odd +# number of times. +# +# Example 1 +# +# Input: $str = "weekly" +# Output: false +# +# w: 1 time +# e: 2 times +# k: 1 time +# l: 1 time +# y: 1 time +# +# The letter 'e' appeared 2 times i.e. even. +# +# Example 2 +# +# Input: $str = "perl" +# Output: true +# +# Example 3 +# +# Input: $source = "challenge" +# Output: false +# + +package require tcltest + +set cases { + {"weekly" 0 "Example 1"} + {"perl" 1 "Example 2"} + {"challenge" 0 "Example 3"} +} + +proc odd_letters {str} { + foreach l [split $str ""] { + dict incr counts $l + } + foreach letter [dict keys $counts] { + if {[dict get $counts $letter] % 2 == 0} { + return 0 + } + } + return 1 +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + odd_letters [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + -- cgit