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