diff options
| -rw-r--r-- | challenge-325/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-325/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-325/sgreen/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-325/sgreen/perl/ch-2.pl | 24 | ||||
| -rwxr-xr-x | challenge-325/sgreen/python/ch-1.py | 34 | ||||
| -rwxr-xr-x | challenge-325/sgreen/python/ch-2.py | 32 | ||||
| -rwxr-xr-x | challenge-325/sgreen/python/test.py | 21 |
7 files changed, 140 insertions, 2 deletions
diff --git a/challenge-325/sgreen/README.md b/challenge-325/sgreen/README.md index 1044b4c6b8..51e2c1d3e3 100644 --- a/challenge-325/sgreen/README.md +++ b/challenge-325/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 324 +# The Weekly Challenge 325 -Blog: [The total array](https://dev.to/simongreennet/weekly-challenge-the-total-array-2h8k) +Blog: [Counting the discounts](https://dev.to/simongreennet/weekly-challenge-counting-the-discounts-313f) diff --git a/challenge-325/sgreen/blog.txt b/challenge-325/sgreen/blog.txt new file mode 100644 index 0000000000..50a94f8223 --- /dev/null +++ b/challenge-325/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-counting-the-discounts-313f
\ No newline at end of file diff --git a/challenge-325/sgreen/perl/ch-1.pl b/challenge-325/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..6601fb61c4 --- /dev/null +++ b/challenge-325/sgreen/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@ints) { + my $max_count = 0; + my $current_count = 0; + foreach my $num (@ints) { + if ( $num == 1 ) { + $current_count++; + if ( $current_count > $max_count ) { + $max_count = $current_count; + } + } + else { + $current_count = 0; + } + } + + say $max_count; +} + +main(@ARGV); diff --git a/challenge-325/sgreen/perl/ch-2.pl b/challenge-325/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..c5d2e63a74 --- /dev/null +++ b/challenge-325/sgreen/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@prices) { + my @solution = (); + foreach my $i ( 0 .. $#prices ) { + my $discount = 0; + foreach my $j ( $i + 1 .. $#prices ) { + if ( $prices[$j] <= $prices[$i] ) { + $discount = $prices[$j]; + last; + } + } + push @solution, $prices[$i] - $discount; + } + + say '(' . join( ', ', @solution ) . ')'; +} + +main(@ARGV); diff --git a/challenge-325/sgreen/python/ch-1.py b/challenge-325/sgreen/python/ch-1.py new file mode 100755 index 0000000000..899fdf8dbf --- /dev/null +++ b/challenge-325/sgreen/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import sys + + +def consecutive_ones(ints: list) -> int: + """ + Function to find the maximum number of consecutive 1s in a binary array. + :param ints: List of integers (0s and 1s) + :return: Maximum count of consecutive 1s + """ + max_count = 0 + current_count = 0 + + for num in ints: + if num == 1: + current_count += 1 + if current_count > max_count: + max_count = current_count + else: + current_count = 0 + + return max_count + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = consecutive_ones(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-325/sgreen/python/ch-2.py b/challenge-325/sgreen/python/ch-2.py new file mode 100755 index 0000000000..5110b21fa8 --- /dev/null +++ b/challenge-325/sgreen/python/ch-2.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +import sys + + +def final_price(prices: list) -> list: + """ + Function to calculate the final price of items after applying discounts. + :param prices: List of integers representing item prices + :return: List of final prices after discounts + """ + solution = [] + for i in range(len(prices)): + discount = 0 + for j in range(i + 1, len(prices)): + if prices[j] <= prices[i]: + discount = prices[j] + break + solution.append(prices[i] - discount) + + return solution + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = final_price(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-325/sgreen/python/test.py b/challenge-325/sgreen/python/test.py new file mode 100755 index 0000000000..e08bbd66e3 --- /dev/null +++ b/challenge-325/sgreen/python/test.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.consecutive_ones([0, 1, 1, 0, 1, 1, 1]), 3) + self.assertEqual(ch_1.consecutive_ones([0, 0, 0, 0]), 0) + self.assertEqual(ch_1.consecutive_ones([1, 0, 1, 0, 1, 1]), 2) + + def test_ch_2(self): + self.assertEqual(ch_2.final_price([8, 4, 6, 2, 3]), [4, 2, 4, 2, 3]) + self.assertEqual(ch_2.final_price([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5]) + self.assertEqual(ch_2.final_price([7, 1, 1, 5]), [6, 0, 1, 5]) + + +if __name__ == '__main__': + unittest.main() |
