aboutsummaryrefslogtreecommitdiff
path: root/challenge-042
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-26 13:11:43 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-03-26 13:11:43 +0200
commit2a6bb0ae68aad6f29c807dc4f0423768cc845d9a (patch)
tree443f0decd33a748576a4e759ac2ea121319ebcc6 /challenge-042
parente654b9a8e0b8cb51c472c474d80c9b71affb892a (diff)
downloadperlweeklychallenge-club-2a6bb0ae68aad6f29c807dc4f0423768cc845d9a.tar.gz
perlweeklychallenge-club-2a6bb0ae68aad6f29c807dc4f0423768cc845d9a.tar.bz2
perlweeklychallenge-club-2a6bb0ae68aad6f29c807dc4f0423768cc845d9a.zip
Challenges 035 038 042 LK Perl Python
Diffstat (limited to 'challenge-042')
-rw-r--r--challenge-042/lubos-kolouch/perl/ch-1.pl19
-rw-r--r--challenge-042/lubos-kolouch/perl/ch-2.pl47
-rw-r--r--challenge-042/lubos-kolouch/python/ch-1.py35
-rw-r--r--challenge-042/lubos-kolouch/python/ch-2.py66
4 files changed, 167 insertions, 0 deletions
diff --git a/challenge-042/lubos-kolouch/perl/ch-1.pl b/challenge-042/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..49c8bc9d49
--- /dev/null
+++ b/challenge-042/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+use Test::More;
+
+# Convert a decimal number to octal
+sub decimal_to_octal {
+ my $decimal = shift;
+ return sprintf( "%o", $decimal );
+}
+
+# Test the decimal to octal conversion function
+is( decimal_to_octal(0), '0', '0 is converted to 0' );
+is( decimal_to_octal(1), '1', '1 is converted to 1' );
+is( decimal_to_octal(2), '2', '2 is converted to 2' );
+is( decimal_to_octal(7), '7', '7 is converted to 7' );
+is( decimal_to_octal(8), '10', '8 is converted to 10' );
+is( decimal_to_octal(50), '62', '50 is converted to 62' );
+
+done_testing();
diff --git a/challenge-042/lubos-kolouch/perl/ch-2.pl b/challenge-042/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..391570f485
--- /dev/null
+++ b/challenge-042/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,47 @@
+use strict;
+use warnings;
+use List::Util qw(shuffle);
+use Test::More tests => 5;
+
+# Generate a random string of brackets
+sub generate_bracket_string {
+ my $length = shift;
+ my @brackets = ( '(', ')' );
+ my @shuffled_brackets = shuffle(@brackets);
+ return join( '', @shuffled_brackets[ 0 .. $length - 1 ] );
+}
+
+# Check if a string has balanced brackets
+sub is_balanced {
+ my $bracket_string = shift;
+ my @stack = ();
+ for my $bracket ( split( //, $bracket_string ) ) {
+ if ( $bracket eq '(' ) {
+ push( @stack, $bracket );
+ }
+ elsif ( $bracket eq ')' ) {
+ if ( @stack == 0 ) {
+ return 0;
+ }
+ pop(@stack);
+ }
+ }
+ return @stack == 0;
+}
+
+# Test the is_balanced function
+ok( is_balanced(''), 'Empty string is balanced' );
+ok( is_balanced('()'), 'Simple brackets are balanced' );
+ok( is_balanced('(())'), 'Nested brackets are balanced' );
+ok( !is_balanced('(('), 'Unbalanced brackets are not balanced' );
+ok( !is_balanced('())'), 'Unbalanced brackets are not balanced' );
+
+# Generate a random string of brackets and check if it has balanced brackets
+my $bracket_string = generate_bracket_string(10);
+print "Bracket string: $bracket_string\n";
+if ( is_balanced($bracket_string) ) {
+ print "The bracket string is balanced\n";
+}
+else {
+ print "The bracket string is not balanced\n";
+}
diff --git a/challenge-042/lubos-kolouch/python/ch-1.py b/challenge-042/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..d1cb5c5c46
--- /dev/null
+++ b/challenge-042/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import unittest
+
+
+def decimal_to_octal(decimal: int) -> str:
+ """Converts a decimal number to an octal string.
+
+ Args:
+ decimal: The decimal number to convert.
+
+ Returns:
+ The octal string representation of the decimal number.
+ """
+ return oct(decimal)[2:]
+
+
+class TestDecimalToOctal(unittest.TestCase):
+ def test_zero(self) -> None:
+ self.assertEqual(decimal_to_octal(0), "0")
+
+ def test_single_digit(self) -> None:
+ self.assertEqual(decimal_to_octal(5), "5")
+
+ def test_multiple_digits(self) -> None:
+ self.assertEqual(decimal_to_octal(50), "62")
+
+
+if __name__ == "__main__":
+ unittest.main()
+
+for i in range(0, 51):
+ octal = decimal_to_octal(i)
+ print(f"Decimal {i} = Octal {octal}")
diff --git a/challenge-042/lubos-kolouch/python/ch-2.py b/challenge-042/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..9fd40978cb
--- /dev/null
+++ b/challenge-042/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import unittest
+import random
+
+
+def generate_bracket_string(length: int) -> str:
+ """Generates a random string of ( and ) brackets of the specified length.
+
+ Args:
+ length: The length of the bracket string to generate.
+
+ Returns:
+ The generated bracket string.
+ """
+ brackets = ["(", ")"]
+ return "".join(random.choices(brackets, k=length))
+
+
+def is_balanced(bracket_string: str) -> bool:
+ """Checks if a string has balanced brackets.
+
+ Args:
+ bracket_string: The string to check.
+
+ Returns:
+ True if the string has balanced brackets, False otherwise.
+ """
+ stack = []
+ for bracket in bracket_string:
+ if bracket == "(":
+ stack.append(bracket)
+ elif bracket == ")":
+ if len(stack) == 0:
+ return False
+ stack.pop()
+ return len(stack) == 0
+
+
+class TestIsBalanced(unittest.TestCase):
+ def test_empty_string(self) -> None:
+ self.assertTrue(is_balanced(""))
+
+ def test_single_opening_bracket(self) -> None:
+ self.assertFalse(is_balanced("("))
+
+ def test_single_closing_bracket(self) -> None:
+ self.assertFalse(is_balanced(")"))
+
+ def test_balanced_string(self) -> None:
+ self.assertTrue(is_balanced("(())()"))
+
+ def test_unbalanced_string(self) -> None:
+ self.assertFalse(is_balanced("(()))"))
+
+
+if __name__ == "__main__":
+ unittest.main()
+
+bracket_string = generate_bracket_string(10)
+print(f"Bracket string: {bracket_string}")
+if is_balanced(bracket_string):
+ print("The bracket string is balanced")
+else:
+ print("The bracket string is not balanced")