diff options
| -rw-r--r-- | challenge-326/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-326/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-326/sgreen/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-326/sgreen/perl/ch-2.pl | 21 | ||||
| -rwxr-xr-x | challenge-326/sgreen/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-326/sgreen/python/ch-2.py | 28 | ||||
| -rwxr-xr-x | challenge-326/sgreen/python/test.py | 21 |
7 files changed, 120 insertions, 2 deletions
diff --git a/challenge-326/sgreen/README.md b/challenge-326/sgreen/README.md index 51e2c1d3e3..dd5a6d6624 100644 --- a/challenge-326/sgreen/README.md +++ b/challenge-326/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 325 +# The Weekly Challenge 326 -Blog: [Counting the discounts](https://dev.to/simongreennet/weekly-challenge-counting-the-discounts-313f) +Blog: [Blog](https://dev.to/simongreennet/weekly-challenge-3ngd) diff --git a/challenge-326/sgreen/blog.txt b/challenge-326/sgreen/blog.txt new file mode 100644 index 0000000000..ef787c02db --- /dev/null +++ b/challenge-326/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-3ngd
\ No newline at end of file diff --git a/challenge-326/sgreen/perl/ch-1.pl b/challenge-326/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..50d7b79d43 --- /dev/null +++ b/challenge-326/sgreen/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use Date::Calc 'Day_of_Year'; + +sub main ($date) { + if ($date !~ /^[0-9]{4}-[0-9][0-9]?-[0-9][0-9]?$/) { + die "Usage: $0 YYYY-MM-DD\n"; + } + + say Day_of_Year(split /-/, $date); +} + +main($ARGV[0]);
\ No newline at end of file diff --git a/challenge-326/sgreen/perl/ch-2.pl b/challenge-326/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..92d863deff --- /dev/null +++ b/challenge-326/sgreen/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@ints) { + my @result = (); + for ( my $i = 0 ; $i <= $#ints ; $i += 2 ) { + my $count = $ints[$i]; + my $value = $ints[ $i + 1 ]; + foreach my $j ( 1 .. $count ) { + push @result, $value; + } + } + + say "(" . join( ", ", @result ) . ")"; +} + +main(@ARGV); diff --git a/challenge-326/sgreen/python/ch-1.py b/challenge-326/sgreen/python/ch-1.py new file mode 100755 index 0000000000..c05a79a942 --- /dev/null +++ b/challenge-326/sgreen/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +from datetime import date +import re +import sys + + +def day_of_year(input_date: str) -> int: + """ + Calculate the day of the year from a date string in 'YYYY-MM-DD' format. + :param input_date: Date string in 'YYYY-MM-DD' format + :return: Day of the year as an integer + """ + + # Validate the input date format + if not re.match(r'^\d{4}-\d\d?-\d\d?$', input_date): + raise ValueError("Input date must be in 'YYYY-MM-DD' format") + + year, month, day = map(int, input_date.split('-')) + return int((date(year, month, day).strftime('%j'))) + + +def main(): + result = day_of_year(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-326/sgreen/python/ch-2.py b/challenge-326/sgreen/python/ch-2.py new file mode 100755 index 0000000000..153c368500 --- /dev/null +++ b/challenge-326/sgreen/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import sys + + +def decompressed_list(ints: list) -> list: + """ + Decompress a list of integers where each integer represents the number of times the next integer should be repeated. + :param ints: List of integers + :return: Decompressed list of integers + """ + result = [] + for i in range(0, len(ints), 2): + count = ints[i] + value = ints[i + 1] + result.extend([value] * count) + return result + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = decompressed_list(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-326/sgreen/python/test.py b/challenge-326/sgreen/python/test.py new file mode 100755 index 0000000000..2215ec3e13 --- /dev/null +++ b/challenge-326/sgreen/python/test.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.day_of_year('2025-02-02'), 33) + self.assertEqual(ch_1.day_of_year('2025-04-10'), 100) + self.assertEqual(ch_1.day_of_year('2025-09-07'), 250) + + def test_ch_2(self): + self.assertEqual(ch_2.decompressed_list([1, 3, 2, 4]), [3, 4, 4]) + self.assertEqual(ch_2.decompressed_list([1, 1, 2, 2]), [1, 2, 2]) + self.assertEqual(ch_2.decompressed_list([3, 1, 3, 2]), [1, 1, 1, 2, 2, 2]) + + +if __name__ == '__main__': + unittest.main() |
