diff options
Diffstat (limited to 'challenge-082/lubos-kolouch/python/ch-2.py')
| -rw-r--r-- | challenge-082/lubos-kolouch/python/ch-2.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-082/lubos-kolouch/python/ch-2.py b/challenge-082/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..493bb9df02 --- /dev/null +++ b/challenge-082/lubos-kolouch/python/ch-2.py @@ -0,0 +1,47 @@ +#!/bin/env python +""" +#=============================================================================== +# +# FILE: ch-2.py +# +# USAGE: ./ch-2.py +# +# DESCRIPTION: Perl Weekly Challenge 082 +# http://www.perlweeklychallenge.org +# Task 2 - Interleave String +# +# AUTHOR: Lubos Kolouch +# CREATED: 10/17/2020 07:25:23 PM +#=============================================================================== +""" + + +def inter_leave(a: str, b: str, current: str = '', result=dict()): + """ Do the interleave """ + if not a and not b: + result[current] = 1 + return result + + if a: + result = inter_leave(a=a[1:], b=b, current=current+a[0], result=result) + + if b: + result = inter_leave(a=a, b=b[1:], current=current+b[0], result=result) + + return result + + +def can_interleave(a: str, b: str, c: str): + """ Find out if the string can be interleaved """ + + if len(a) + len(b) != len(c): + return 0 + + result = inter_leave(a=a, b=b, result=dict()) + return result.get(c, 0) + + +assert can_interleave(a='XY', b='X', c='XXY') == 1 +assert can_interleave(a='XXY', b='XXZ', c='XXXXZY') == 1 +assert can_interleave(a='YX', b='X', c='XXY') == 0 +assert can_interleave(a='XXY', b='X', c='XXY') == 0 |
