From c5e9bf1f82ff626038731198a34bf50bf720d0cd Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 29 Apr 2024 18:04:47 +0100 Subject: add solutions week 267 in python --- challenge-267/steven-wilson/python/ch-1.py | 27 +++++++++++++++++ challenge-267/steven-wilson/python/ch-2.py | 47 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 challenge-267/steven-wilson/python/ch-1.py create mode 100644 challenge-267/steven-wilson/python/ch-2.py 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) -- cgit