From e3042c4327200990cfc2d05943479f2aa713aa80 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 23 Jul 2024 16:19:02 +0100 Subject: add solutions week 279 in python --- challenge-279/steven-wilson/python/ch-2.py | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-279/steven-wilson/python/ch-2.py (limited to 'challenge-279/steven-wilson/python/ch-2.py') diff --git a/challenge-279/steven-wilson/python/ch-2.py b/challenge-279/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..be2b25d38f --- /dev/null +++ b/challenge-279/steven-wilson/python/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +from pprint import pprint + + +def split_string(string, print_strings=False): + """ Given a string, split the given string into two containing exactly same + number of vowels and return true if you can otherwise false. + >>> split_string("perl") + False + >>> split_string("book") + True + >>> split_string("good morning") + True + """ + if len(string) < 2: + return False + + position_vowels = [i for i, c in enumerate(string) if c.casefold() in 'aeiou'] + + if not len(position_vowels) % 2 == 0: + return False + + if print_strings: + split_index = int(len(position_vowels)/2) - 1 + (start, stop) = (position_vowels[split_index], position_vowels[split_index+1]) + pprint([(string[:i+1], string[i+1:]) for i in range(start, stop)]) + + return True + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit From 76161f95b561c1efaec380d74597429a28490460 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 24 Jul 2024 11:29:04 +0100 Subject: handle print w/ 0 vowels --- challenge-279/steven-wilson/python/ch-2.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'challenge-279/steven-wilson/python/ch-2.py') diff --git a/challenge-279/steven-wilson/python/ch-2.py b/challenge-279/steven-wilson/python/ch-2.py index be2b25d38f..0b9478b238 100644 --- a/challenge-279/steven-wilson/python/ch-2.py +++ b/challenge-279/steven-wilson/python/ch-2.py @@ -22,8 +22,11 @@ def split_string(string, print_strings=False): return False if print_strings: - split_index = int(len(position_vowels)/2) - 1 - (start, stop) = (position_vowels[split_index], position_vowels[split_index+1]) + if len(position_vowels) < 2: + (start, stop) = (0, len(string)-1) + else: + split_index = int(len(position_vowels)/2) - 1 + (start, stop) = (position_vowels[split_index], position_vowels[split_index+1]) pprint([(string[:i+1], string[i+1:]) for i in range(start, stop)]) return True -- cgit