diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-09-25 21:49:24 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-09-25 21:49:24 +0200 |
| commit | f55f26ce46c572302f49ce768e7c6bf4cd8d5fdb (patch) | |
| tree | b15449bf1ac0d0d48f2f026acfa4f1cffaf51253 | |
| parent | 24b9a96053d234b811652a92f85cc4020786ee74 (diff) | |
| download | perlweeklychallenge-club-f55f26ce46c572302f49ce768e7c6bf4cd8d5fdb.tar.gz perlweeklychallenge-club-f55f26ce46c572302f49ce768e7c6bf4cd8d5fdb.tar.bz2 perlweeklychallenge-club-f55f26ce46c572302f49ce768e7c6bf4cd8d5fdb.zip | |
feat(challenge-236/lubos-kolouch/perl,python,blog/): Challenge 236 LK Perl Python Blog
| -rw-r--r-- | challenge-236/lubos-kolouch/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-236/lubos-kolouch/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-236/lubos-kolouch/perl/ch-2.pl | 59 | ||||
| -rw-r--r-- | challenge-236/lubos-kolouch/python/ch-1.py | 43 | ||||
| -rw-r--r-- | challenge-236/lubos-kolouch/python/ch-2.py | 40 | ||||
| -rw-r--r-- | challenge-236/lubos-kolouch/raku/ch-1.raku | 31 | ||||
| -rw-r--r-- | challenge-236/lubos-kolouch/raku/ch-2.raku | 34 |
7 files changed, 248 insertions, 0 deletions
diff --git a/challenge-236/lubos-kolouch/blog.txt b/challenge-236/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..ba518f1802 --- /dev/null +++ b/challenge-236/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-09-25_Weekly_challenge_236 diff --git a/challenge-236/lubos-kolouch/perl/ch-1.pl b/challenge-236/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..1aba5facd3 --- /dev/null +++ b/challenge-236/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,40 @@ +use strict; +use warnings; + +sub can_sell_with_exact_change { + my @bills = @_; + my $five = 0; + my $ten = 0; + + for my $bill (@bills) { + if ( $bill == 5 ) { + $five++; + } + elsif ( $bill == 10 ) { + return 0 if $five == 0; + $five--; + $ten++; + } + else { + if ( $ten > 0 && $five > 0 ) { + $ten--; + $five--; + } + elsif ( $five >= 3 ) { + $five -= 3; + } + else { + return 0; + } + } + } + + return 1; +} + +# Test Cases +print can_sell_with_exact_change( 5, 5, 5, 10, 20 ) + . "\n"; # Should return 1 (True) +print can_sell_with_exact_change( 5, 5, 10, 10, 20 ) + . "\n"; # Should return 0 (False) +print can_sell_with_exact_change( 5, 5, 5, 20 ) . "\n"; # Should return 1 (True) diff --git a/challenge-236/lubos-kolouch/perl/ch-2.pl b/challenge-236/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..9c7fe5779d --- /dev/null +++ b/challenge-236/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,59 @@ +use strict; +use warnings; + +sub find_loops { + my @arr = @_; + my %visited; + my $loop_count = 0; + + for my $i ( 0 .. $#arr ) { + + # Skip if this index has been visited + next if exists $visited{$i}; + + # Initialize a new loop + $loop_count++; + my $current_index = $i; + + # Find members of this loop + while ( !exists $visited{$current_index} ) { + $visited{$current_index} = 1; + $current_index = $arr[$current_index]; + } + } + + return $loop_count; +} + +# Test cases +my @test_cases = ( + [ + [ + 4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, + 10 + ], + 3 + ], + [ + [ + 0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, + 19 + ], + 6 + ], + [ + [ + 9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, + 17 + ], + 1 + ] +); + +for my $i ( 0 .. $#test_cases ) { + my ( $arr, $expected ) = @{ $test_cases[$i] }; + my $result = find_loops(@$arr); + print "Test case ", $i + 1, ": ", + $result == $expected ? "Passed" : "Failed", + " (Got: $result, Expected: $expected)\n"; +} diff --git a/challenge-236/lubos-kolouch/python/ch-1.py b/challenge-236/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..6d2066b264 --- /dev/null +++ b/challenge-236/lubos-kolouch/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def can_sell_with_exact_change(bills: List[int]) -> bool: + """ + Determines if it is possible to sell juice to each customer with exact change. + + Args: + - bills (List[int]): List of bills from customers. + + Returns: + - bool: True if it is possible to give exact change to each customer, False otherwise. + """ + five, ten = 0, 0 # Initialize counters for $5 and $10 bills + + # Loop through each bill in the list + for bill in bills: + if bill == 5: + five += 1 # No change needed, just collect the $5 bill + elif bill == 10: + if five == 0: # Need a $5 bill to give change + return False + five -= 1 # Give a $5 bill as change + ten += 1 # Collect the $10 bill + else: # bill is $20 + if ten > 0 and five > 0: # First option to give change + ten -= 1 + five -= 1 + elif five >= 3: # Second option to give change + five -= 3 + else: + return False # Can't give change + + return True # All customers received exact change + + +# Test cases +print(can_sell_with_exact_change([5, 5, 5, 10, 20])) # Should return True +print(can_sell_with_exact_change([5, 5, 10, 10, 20])) # Should return False +print(can_sell_with_exact_change([5, 5, 5, 20])) # Should return True diff --git a/challenge-236/lubos-kolouch/python/ch-2.py b/challenge-236/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..ac3c40d8f8 --- /dev/null +++ b/challenge-236/lubos-kolouch/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Python implementation for "Array Loops" problem +from typing import List + + +def find_loops(arr: List[int]) -> int: + visited = set() + loop_count = 0 + + for i in range(len(arr)): + # Skip if this index has been visited + if i in visited: + continue + + # Initialize a new loop + loop_count += 1 + current_index = i + + # Find members of this loop + while current_index not in visited: + visited.add(current_index) + current_index = arr[current_index] + + return loop_count + + +# Test cases +test_cases = [ + ([4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10], 3), + ([0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19], 6), + ([9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17], 1), +] + +for i, (arr, expected) in enumerate(test_cases): + result = find_loops(arr) + print( + f"Test case {i+1}: {'Passed' if result == expected else 'Failed'} (Got: {result}, Expected: {expected})" + ) diff --git a/challenge-236/lubos-kolouch/raku/ch-1.raku b/challenge-236/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..fee87d1c9f --- /dev/null +++ b/challenge-236/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,31 @@ +sub can-sell-with-exact-change(@bills) { + my $five = 0; + my $ten = 0; + + for @bills -> $bill { + if $bill == 5 { + $five++; + } elsif $bill == 10 { + return False if $five == 0; + $five--; + $ten++; + } else { + if $ten > 0 && $five > 0 { + $ten--; + $five--; + } elsif $five >= 3 { + $five -= 3; + } else { + return False; + } + } + } + + return True; +} + +# Test Cases +say can-sell-with-exact-change([5, 5, 5, 10, 20]); # Should return True +say can-sell-with-exact-change([5, 5, 10, 10, 20]); # Should return False +say can-sell-with-exact-change([5, 5, 5, 20]); # Should return True + diff --git a/challenge-236/lubos-kolouch/raku/ch-2.raku b/challenge-236/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..bca37330d9 --- /dev/null +++ b/challenge-236/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,34 @@ +sub find-loops(@arr) { + my %visited; + my $loop-count = 0; + + for 0..^@arr.elems -> $i { + # Skip if this index has been visited + next if %visited{$i}:exists; + + # Initialize a new loop + $loop-count++; + my $current-index = $i; + + # Find members of this loop + while !(%visited{$current-index}:exists) { + %visited{$current-index} = True; + $current-index = @arr[$current-index]; + } + } + + return $loop-count; +} + +# Test cases +my @test-cases = ( + [[4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10], 3], + [[0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19], 6], + [[9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17], 1] +); + +for @test-cases.kv -> $i, [$arr, $expected] { + my $result = find-loops(@$arr); + say "Test case ", $i + 1, ": ", $result == $expected ?? "Passed" !! "Failed", " (Got: $result, Expected: $expected)"; +} + |
