diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-18 19:59:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-18 19:59:32 +0100 |
| commit | 85073f31dae3decce780d1fd66cdcb79c82c7f65 (patch) | |
| tree | 318d3bc02836f4cde215f7f15e9ec875c74a1490 /challenge-064/paulo-custodio/python/ch-2.py | |
| parent | bfe139d559d498bcf1ae48cc08520bfc99ee9b85 (diff) | |
| parent | 51c035e04c5ae5f9f02555206f57eb675a737617 (diff) | |
| download | perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.gz perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.bz2 perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.zip | |
Merge pull request #10860 from pauloscustodio/master
Add Python solutions
Diffstat (limited to 'challenge-064/paulo-custodio/python/ch-2.py')
| -rw-r--r-- | challenge-064/paulo-custodio/python/ch-2.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-064/paulo-custodio/python/ch-2.py b/challenge-064/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..12034bea57 --- /dev/null +++ b/challenge-064/paulo-custodio/python/ch-2.py @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +# Challenge 064 +# +# TASK #2 > Word Break +# Submitted by: Mohammad S Anwar +# +# You are given a string $S and an array of words @W. +# +# Write a script to find out if $S can be split into sequence of one or more +# words as in the given @W. +# +# Print the all the words if found otherwise print 0. +# +# Example 1: +# Input: +# +# $S = "perlweeklychallenge" +# @W = ("weekly", "challenge", "perl") +# +# Output: +# +# "perl", "weekly", "challenge" +# Example 2: +# Input: +# +# $S = "perlandraku" +# @W = ("python", "ruby", "haskell") +# +# Output: +# +# 0 as none matching word found. + +import itertools +import unittest + +def word_break(S, *W): + k = len(W) + for words in itertools.permutations(W): + if ''.join(words) == S: + return ' '.join(W) + return "0" + +class TestWordBreak(unittest.TestCase): + def test_word_break(self): + self.assertEqual(word_break("perlweeklychallenge", "weekly", "challenge", "perl"), + "weekly challenge perl") + self.assertEqual(word_break("perlandraku", "python", "ruby", "haskell"), + "0") + +if __name__ == '__main__': + unittest.main() |
