diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-08-10 20:00:56 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-08-10 20:00:56 +0200 |
| commit | 407bdf405e1cc65708dc231c0a236d79c327e8d3 (patch) | |
| tree | e178f86a700ad09d55b0b71d64282066e6e1dd08 /challenge-200/lubos-kolouch | |
| parent | 2cbddb9ecfe878d6e3b10d472e6647e6229ad1ce (diff) | |
| download | perlweeklychallenge-club-407bdf405e1cc65708dc231c0a236d79c327e8d3.tar.gz perlweeklychallenge-club-407bdf405e1cc65708dc231c0a236d79c327e8d3.tar.bz2 perlweeklychallenge-club-407bdf405e1cc65708dc231c0a236d79c327e8d3.zip | |
feat(challenge-200/lubos-kolouch/): Challenge 200 LK Perl Python Blog
Diffstat (limited to 'challenge-200/lubos-kolouch')
| -rw-r--r-- | challenge-200/lubos-kolouch/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-200/lubos-kolouch/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-200/lubos-kolouch/perl/ch-2.pl | 26 | ||||
| -rw-r--r-- | challenge-200/lubos-kolouch/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-200/lubos-kolouch/python/ch-2.py | 24 |
5 files changed, 121 insertions, 0 deletions
diff --git a/challenge-200/lubos-kolouch/blog.txt b/challenge-200/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..2ec6b7973b --- /dev/null +++ b/challenge-200/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-01-16_Weekly_challenge_200 diff --git a/challenge-200/lubos-kolouch/perl/ch-1.pl b/challenge-200/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..aec66b646d --- /dev/null +++ b/challenge-200/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,34 @@ +use strict; +use warnings; +use Test::More; +use Data::Dumper; + +sub arithmetic_slices { + my @array = @_; + return () if @array < 3; + + my @slices; + for my $i ( 0 .. $#array - 2 ) { + if ( $array[ $i + 1 ] - $array[$i] == $array[ $i + 2 ] - $array[ $i + 1 ] ) { + my $diff = $array[ $i + 1 ] - $array[$i]; + my @slice = @array[ $i, $i + 1, $i + 2 ]; + push @slices, [@slice]; + for my $j ( $i + 3 .. $#array ) { + if ( $array[$j] - $array[ $j - 1 ] == $diff ) { + push @slice, $array[$j]; + push @slices, [@slice]; + } + else { + last; + } + } + } + } + return @slices; +} + +# Test cases +my @test_case_1 = arithmetic_slices( 1, 2, 3, 4 ); +is_deeply( \@test_case_1, [ [ 1, 2, 3 ], [ 1, 2, 3, 4 ], [ 2, 3, 4 ] ], 'Test Case 1' ); + +done_testing(); diff --git a/challenge-200/lubos-kolouch/perl/ch-2.pl b/challenge-200/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..9a7a1aafc3 --- /dev/null +++ b/challenge-200/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,26 @@ +use strict; +use warnings; + +use strict; +use warnings; + +my @truth = qw<abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg>; + +sub draw_seven_segment { + my $number = shift; + my @lines = ("") x 5; + + for my $digit ( split //, $number ) { + my $segments = $truth[$digit]; + $lines[0] .= ( $segments =~ /a/ ? "-" x 7 : " " x 7 ) . " "; + $lines[1] .= ( $segments =~ /f/ ? "|" : " " ) . ( " " x 6 ) . ( $segments =~ /b/ ? "|" : " " ) . " "; + $lines[2] .= ( $segments =~ /g/ ? "-" x 7 : " " x 7 ) . " "; + $lines[3] .= ( $segments =~ /e/ ? "|" : " " ) . ( " " x 6 ) . ( $segments =~ /c/ ? "|" : " " ) . " "; + $lines[4] .= ( $segments =~ /d/ ? "-" x 7 : " " x 7 ) . " "; + } + + return join "\n", @lines; +} + +my $number = 200; +print draw_seven_segment($number), "\n"; diff --git a/challenge-200/lubos-kolouch/python/ch-1.py b/challenge-200/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..a451ddaeee --- /dev/null +++ b/challenge-200/lubos-kolouch/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest + + +def arithmetic_slices(lst): + if len(lst) < 3: + return [] + + slices = [] + for i in range(len(lst) - 2): + if lst[i + 1] - lst[i] == lst[i + 2] - lst[i + 1]: + diff = lst[i + 1] - lst[i] + slice = [lst[i], lst[i + 1], lst[i + 2]] + slices.append(slice[:]) + for j in range(i + 3, len(lst)): + if lst[j] - lst[j - 1] == diff: + slice.append(lst[j]) + slices.append(slice[:]) + else: + break + + return slices + + +class TestArithmeticSlices(unittest.TestCase): + + def test_cases(self): + self.assertEqual(arithmetic_slices([1, 2, 3, 4]), + [[1, 2, 3], [1, 2, 3, 4], [2, 3, 4]], 'Test Case 1') + self.assertEqual(arithmetic_slices([2]), [], 'Test Case 2') + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-200/lubos-kolouch/python/ch-2.py b/challenge-200/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..fc111377ec --- /dev/null +++ b/challenge-200/lubos-kolouch/python/ch-2.py @@ -0,0 +1,24 @@ +truth = [ + 'abcdef', 'bc', 'abdeg', 'abcdg', 'bcfg', 'acdfg', 'acdefg', 'abc', + 'abcdefg', 'abcfg' +] + + +def draw_seven_segment(number): + lines = [""] * 5 + + for digit in str(number): + segments = truth[int(digit)] + lines[0] += ("-" * 7 if 'a' in segments else " " * 7) + " " + lines[1] += ("|" if 'f' in segments else + " ") + (" " * 6) + ("|" if 'b' in segments else " ") + " " + lines[2] += ("-" * 7 if 'g' in segments else " " * 7) + " " + lines[3] += ("|" if 'e' in segments else + " ") + (" " * 6) + ("|" if 'c' in segments else " ") + " " + lines[4] += ("-" * 7 if 'd' in segments else " " * 7) + " " + + return '\n'.join(lines) + + +number = 200 +print(draw_seven_segment(number)) |
