From 8df08850ad20a07dfb2fe4c52a9aca1b5325fc6f Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 15 Jul 2024 15:22:37 +0100 Subject: add solutions week 278 in python --- challenge-278/steven-wilson/python/ch-1.py | 26 ++++++++++++++++++++++++++ challenge-278/steven-wilson/python/ch-2.py | 27 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 challenge-278/steven-wilson/python/ch-1.py create mode 100644 challenge-278/steven-wilson/python/ch-2.py diff --git a/challenge-278/steven-wilson/python/ch-1.py b/challenge-278/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..8e113ff425 --- /dev/null +++ b/challenge-278/steven-wilson/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import re +from operator import itemgetter + + +def sort_string(string): + """ Given a shuffle string, return the sorted string. A string is shuffled + by appending word position to each word. + + >>> sort_string("and2 Raku3 cousins5 Perl1 are4") + 'Perl and Raku are cousins' + >>> sort_string("guest6 Python1 most4 the3 popular5 is2 language7") + 'Python is the most popular guest language' + >>> sort_string("Challenge3 The1 Weekly2") + 'The Weekly Challenge' + """ + p = re.compile('^(\w+)(\d+)$') + words_sorted = sorted((p.search(word).groups() for word in string.split(" ")), key=itemgetter(1)) + return " ".join(word[0] for word in words_sorted) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-278/steven-wilson/python/ch-2.py b/challenge-278/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..2c6f1e5423 --- /dev/null +++ b/challenge-278/steven-wilson/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + + +def reverse_word(word, char): + """ Given a word and a character, replace the substring up to and including + the character with its characters sorted alphabetically. If the character + doesn’t exist then DON'T do anything. + + >>> reverse_word("challenge", "e") + 'acehllnge' + >>> reverse_word("programming", "a") + 'agoprrmming' + >>> reverse_word("champion", "b") + 'champion' + """ + position = word.find(char) + + if position == -1: + return word + + return "".join(sorted(word[:position+1])) + word[position+1:] + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit From 3a5c11a1c8a2d1e0f94c9ae9c732740f88b4bcd2 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 15 Jul 2024 15:30:36 +0100 Subject: test character at end of word --- challenge-278/steven-wilson/python/ch-2.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/challenge-278/steven-wilson/python/ch-2.py b/challenge-278/steven-wilson/python/ch-2.py index 2c6f1e5423..8cec33da3f 100644 --- a/challenge-278/steven-wilson/python/ch-2.py +++ b/challenge-278/steven-wilson/python/ch-2.py @@ -12,6 +12,8 @@ def reverse_word(word, char): 'agoprrmming' >>> reverse_word("champion", "b") 'champion' + >>> reverse_word("champion", "n") + 'achimnop' """ position = word.find(char) -- cgit From 330a2bbf9704b672fcc237c3d44e68bb2bad4109 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 15 Jul 2024 16:04:49 +0100 Subject: sort by int value --- challenge-278/steven-wilson/python/ch-1.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/challenge-278/steven-wilson/python/ch-1.py b/challenge-278/steven-wilson/python/ch-1.py index 8e113ff425..18b5904a2a 100644 --- a/challenge-278/steven-wilson/python/ch-1.py +++ b/challenge-278/steven-wilson/python/ch-1.py @@ -16,8 +16,9 @@ def sort_string(string): 'The Weekly Challenge' """ p = re.compile('^(\w+)(\d+)$') - words_sorted = sorted((p.search(word).groups() for word in string.split(" ")), key=itemgetter(1)) - return " ".join(word[0] for word in words_sorted) + words_numbers = (p.search(word).groups() for word in string.split(" ")) + words_numbers_sorted = sorted(((w[0], int(w[1])) for w in words_numbers), key=itemgetter(1)) + return " ".join(word[0] for word in words_numbers_sorted) if __name__ == "__main__": -- cgit