diff options
| author | Kivanc Yazan <kyzn@cpan.org> | 2020-01-05 19:50:45 -0800 |
|---|---|---|
| committer | Kivanc Yazan <kyzn@cpan.org> | 2020-01-05 19:50:50 -0800 |
| commit | 02cf68f699fd591b6102e92c1885a112997fbb47 (patch) | |
| tree | 7881b71f7f0354edd0c547caa3a1abdeb5d9161a /challenge-042/kivanc-yazan/python3/ch-2.py | |
| parent | 895359996318bf38dd21c0461557e13cc13e4562 (diff) | |
| download | perlweeklychallenge-club-02cf68f699fd591b6102e92c1885a112997fbb47.tar.gz perlweeklychallenge-club-02cf68f699fd591b6102e92c1885a112997fbb47.tar.bz2 perlweeklychallenge-club-02cf68f699fd591b6102e92c1885a112997fbb47.zip | |
Kivanc Solutions for w042
Diffstat (limited to 'challenge-042/kivanc-yazan/python3/ch-2.py')
| -rwxr-xr-x | challenge-042/kivanc-yazan/python3/ch-2.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/challenge-042/kivanc-yazan/python3/ch-2.py b/challenge-042/kivanc-yazan/python3/ch-2.py new file mode 100755 index 0000000000..8dd3c0169f --- /dev/null +++ b/challenge-042/kivanc-yazan/python3/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +import random + +# Write a script to generate a string with random number of ( and ) brackets. +# Then make the script validate the string if it has balanced brackets. + +# Let's make sure we have equal number of ( and ) to increase chances +count = random.randint(1,10) # Could be as low as 1, as high as 10 +chars = [ '(', ')' ] * count +random.shuffle(chars) +print(''.join(chars)) + +# Walk through to validate +current_open = 0 +for char in chars: + if char == '(': + current_open += 1 + elif current_open == 0: + print('Not Valid') + exit() + else: + current_open -= 1 +print('Valid') |
