aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-30 23:22:57 +0100
committerGitHub <noreply@github.com>2024-04-30 23:22:57 +0100
commit6fc4529e43de2004a1f17bb5f27774abc6a4425b (patch)
treea8fe5638a192365d43d2e1f6c18607197fb56a21
parent361820eafe6d760eaed0092e9973925bef10c665 (diff)
parentc5e9bf1f82ff626038731198a34bf50bf720d0cd (diff)
downloadperlweeklychallenge-club-6fc4529e43de2004a1f17bb5f27774abc6a4425b.tar.gz
perlweeklychallenge-club-6fc4529e43de2004a1f17bb5f27774abc6a4425b.tar.bz2
perlweeklychallenge-club-6fc4529e43de2004a1f17bb5f27774abc6a4425b.zip
Merge pull request #10011 from oWnOIzRi/week267
add solutions week 267 in python
-rw-r--r--challenge-267/steven-wilson/python/ch-1.py27
-rw-r--r--challenge-267/steven-wilson/python/ch-2.py47
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-267/steven-wilson/python/ch-1.py b/challenge-267/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..13dd7863b0
--- /dev/null
+++ b/challenge-267/steven-wilson/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+from math import prod
+
+
+def product_sign(*integers):
+ ''' Given an array, find the sign of product of all integers in the given
+ array. The sign is 1 if the product is positive, -1 if the product is
+ negative and 0 if product is zero.
+ >>> product_sign(-1, -2, -3, -4, 3, 2, 1)
+ 1
+ >>> product_sign(1, 2, 0, -2, -1)
+ 0
+ >>> product_sign(-1, -1, 1, -1, 2)
+ -1
+ '''
+ if not integers or not all(isinstance(x, int) for x in integers):
+ raise TypeError('All paramaters should be of type int')
+
+ product = prod(integers)
+ return 1 if product > 0 else -1 if product < 0 else 0
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)
diff --git a/challenge-267/steven-wilson/python/ch-2.py b/challenge-267/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..05be9768be
--- /dev/null
+++ b/challenge-267/steven-wilson/python/ch-2.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+from string import ascii_lowercase
+LINE_WIDTH = 100
+
+
+def line_counts(string, *widths):
+ ''' Given a string and a 26-items array containing the width of each
+ character from a to z, find out the number of lines and the width of the
+ last line needed to display the given string, assuming you can only fit 100
+ width units on a line.
+ >>> line_counts("abcdefghijklmnopqrstuvwxyz",10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10)
+ (3, 60)
+ >>> line_counts("bbbcccdddaaa",4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10)
+ (2, 4)
+ '''
+ if not all(x in ascii_lowercase for x in string):
+ raise TypeError('string should be all ascii lowercase')
+
+ if not all(isinstance(x, int) for x in widths):
+ raise TypeError('All widths should be of type int')
+
+ if not len(widths) == 26:
+ raise TypeError("Not the corrent amount of arguments, there should be 26 widths")
+
+ widths_count = dict(zip(ascii_lowercase, widths))
+ lines = []
+ current_line = []
+ current_width = 0
+
+ for character in string:
+ character_width = widths_count[character]
+ if current_width + character_width > LINE_WIDTH:
+ lines.append(''.join(current_line))
+ current_line = []
+ current_width = 0
+ current_line.append(character)
+ current_width += character_width
+ lines.append(''.join(current_line))
+
+ return (len(lines), current_width)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)