diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-19 16:57:36 +1100 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-19 16:57:36 +1100 |
| commit | c1c56909fc0b2ccf5fbcc7363b3378470a73d783 (patch) | |
| tree | d460185b78d3ad9c3180b2ca79c5aff292155317 /challenge-082/lubos-kolouch/python/ch-2.py | |
| parent | c83ed6980345c9941b75df3c2884c3007befb2e3 (diff) | |
| parent | d7cc7d8fd62bb12e0129ac28c7c0bc211f560397 (diff) | |
| download | perlweeklychallenge-club-c1c56909fc0b2ccf5fbcc7363b3378470a73d783.tar.gz perlweeklychallenge-club-c1c56909fc0b2ccf5fbcc7363b3378470a73d783.tar.bz2 perlweeklychallenge-club-c1c56909fc0b2ccf5fbcc7363b3378470a73d783.zip | |
Merge remote-tracking branch 'upstream/master'
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 |
