From a4b5b10a163d4d2cc6c709f685bb302d202a9ad9 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 16 Jan 2021 10:38:40 +0100 Subject: Solutions Challenge 095 Perl LK --- challenge-095/lubos-kolouch/perl/ch-1.pl | 34 ++++++++++++++ challenge-095/lubos-kolouch/perl/ch-2.pl | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 challenge-095/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-095/lubos-kolouch/perl/ch-2.pl diff --git a/challenge-095/lubos-kolouch/perl/ch-1.pl b/challenge-095/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..f1f24ec458 --- /dev/null +++ b/challenge-095/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch_1.pl +# +# USAGE: ./ch_1.pl +# +# DESCRIPTION: Perl Weekly Challenge 095 +# Task1 +# Palindrome Number +# +# AUTHOR: Lubos Kolouch +# CREATED: 01/16/2021 10:12:20 AM +#=============================================================================== + +use strict; +use warnings; +use 5.010; + +sub is_palindrome { + my $what = shift; + + # int to cover cases like 00010 + return 1 if int($what) eq reverse int($what); + return 0; +} + +use Test::More; + +is(is_palindrome(1221), 1, 'Test 1221'); +is(is_palindrome(-101), 0, 'Test -101'); +is(is_palindrome('00001221'), 1, 'Test 00001221'); + +done_testing; diff --git a/challenge-095/lubos-kolouch/perl/ch-2.pl b/challenge-095/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..3913d94354 --- /dev/null +++ b/challenge-095/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch_2.pl +# +# USAGE: ./ch_2.pl +# +# DESCRIPTION: Perl Weekly Challenge #095 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-095/ +# Demo Stack +# +# AUTHOR: Lubos Kolouch +# CREATED: 01/13/2021 02:39:16 PM +#=============================================================================== + +use strict; +use warnings; + +{ +package Stack; +use Moose; +use List::Util qw/min/; +use Data::Dumper; + + has 'values' => (is => 'rw', isa=>'ArrayRef', default=>sub{ [] }); + + sub push { + my $self = shift; + my $what = shift; + + unshift @{$self->values}, $what; + } + + sub pop { + my $self = shift; + my $what = shift; + + return shift @{$self->values} if @{$self->values}; + return; + } + + + sub top { + my $self = shift; + + return ${$self->values}[0] if @{$self->values}; + return; + } + + sub stack_min { + my $self = shift; + + return min(@{$self->values}); + } +} + +no Moose; +use Test::More; + +my $stack = Stack->new; +$stack->push(2); +is($stack->top, 2, '2 on top'); + +$stack->push(-1); +is($stack->top, -1, '-1 on top'); + +$stack->push(0); +is($stack->top, 0, '0 on top'); + +is($stack->pop, 0, 'Remove 0'); +is($stack->top, -1, '-1 on top'); + +$stack->push(0); +is($stack->top, 0, '0 on top'); + +is($stack->stack_min, -1, '-1 is min'); +done_testing(); -- cgit From abca1675f01662e3888446898e0a327f46f27afa Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 16 Jan 2021 10:45:56 +0100 Subject: Add test to CH1 perl --- challenge-095/lubos-kolouch/perl/ch-1.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge-095/lubos-kolouch/perl/ch-1.pl b/challenge-095/lubos-kolouch/perl/ch-1.pl index f1f24ec458..7f5894e34c 100644 --- a/challenge-095/lubos-kolouch/perl/ch-1.pl +++ b/challenge-095/lubos-kolouch/perl/ch-1.pl @@ -29,6 +29,7 @@ use Test::More; is(is_palindrome(1221), 1, 'Test 1221'); is(is_palindrome(-101), 0, 'Test -101'); +is(is_palindrome(90), 0, 'Test 90'); is(is_palindrome('00001221'), 1, 'Test 00001221'); done_testing; -- cgit From 4b33fafb8879536097affca8624f1a901793fea6 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 16 Jan 2021 10:56:15 +0100 Subject: Python solutions --- challenge-095/lubos-kolouch/python/ch-1.py | 30 +++++++++++++ challenge-095/lubos-kolouch/python/ch-2.py | 70 ++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 challenge-095/lubos-kolouch/python/ch-1.py create mode 100644 challenge-095/lubos-kolouch/python/ch-2.py diff --git a/challenge-095/lubos-kolouch/python/ch-1.py b/challenge-095/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..f6795dc1b2 --- /dev/null +++ b/challenge-095/lubos-kolouch/python/ch-1.py @@ -0,0 +1,30 @@ +#!env python +""" +# =============================================================================== +# +# FILE: ch_2.py +# +# USAGE: ./ch_2.py +# +# DESCRIPTION: Perl Weekly Challenge #095 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-095/ +# Palindrome Number +# +# AUTHOR: Lubos Kolouch +# CREATED: 01/13/2021 02:39:16 PM +#=============================================================================== +""" + + +def is_palindrome(what: int): + """ test the palindrome """ + + # the 0+ is to avoid situations like 0000123 + return str(what+0) == str(what+0)[::-1] + + +def test_numbers(): + """ Tests """ + assert is_palindrome(1221) == 1 + assert is_palindrome(-101) == 0 + assert is_palindrome(90) == 0 diff --git a/challenge-095/lubos-kolouch/python/ch-2.py b/challenge-095/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..8be6bda569 --- /dev/null +++ b/challenge-095/lubos-kolouch/python/ch-2.py @@ -0,0 +1,70 @@ +#!env python +""" +# =============================================================================== +# +# FILE: ch_2.py +# +# USAGE: ./ch_2.py +# +# DESCRIPTION: Perl Weekly Challenge #095 +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-095/ +# Demo Stack +# +# AUTHOR: Lubos Kolouch +# CREATED: 01/13/2021 02:39:16 PM +#=============================================================================== +""" + + +class Stack: + """ Class to the stack """ + + def __init__(self): + self.stack = [] + + def push(self, value: int): + """ Demostrate push """ + self.stack.insert(0, value) + + @property + def pop(self): + """ Demonstrate pop """ + + try: + return self.stack.pop(0) + except IndexError: + return None + + @property + def top(self): + """ Demonstrate top """ + + return self.stack[0] + + @property + def stack_min(self): + """ Demostrate min """ + + return min(self.stack) + + +def test_cases(): + """ Test cases """ + + stack = Stack() + assert stack.pop == None + + stack.push(2) + assert stack.top == 2 + + stack.push(-1) + assert stack.top == -1 + + stack.push(0) + assert stack.top == 0 + + assert stack.pop == 0 + assert stack.top == -1 + + stack.push(0) + assert stack.stack_min == -1 -- cgit