From a56426175505a28f0cf131f3bb668cfd87d16dff Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 11 Jan 2020 09:46:54 +0000 Subject: - Added solutions by Burkhard Nickels. --- challenge-042/burkhard-nickels/python/ch-1.py | 19 ++++++++++++++ challenge-042/burkhard-nickels/python/ch-2.py | 38 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100755 challenge-042/burkhard-nickels/python/ch-1.py create mode 100755 challenge-042/burkhard-nickels/python/ch-2.py (limited to 'challenge-042/burkhard-nickels/python') diff --git a/challenge-042/burkhard-nickels/python/ch-1.py b/challenge-042/burkhard-nickels/python/ch-1.py new file mode 100755 index 0000000000..40388def78 --- /dev/null +++ b/challenge-042/burkhard-nickels/python/ch-1.py @@ -0,0 +1,19 @@ +#!/usr/bin/python + +import array as arr + +print "ch-1.py (Version 1.0) PWC #42 Task #1: Octal numbers." + +for i in range(0,50): + print "Decimal: ", i, " - Octal: ", oct(i) + + +def print_string(n): + buf = "Decimal: %d, Octal: %o" % (n, n) + return buf + +a = list(range(0,50)) +x = map(print_string, a) +# print(list(x)) +for e in x: print e + diff --git a/challenge-042/burkhard-nickels/python/ch-2.py b/challenge-042/burkhard-nickels/python/ch-2.py new file mode 100755 index 0000000000..a52447f036 --- /dev/null +++ b/challenge-042/burkhard-nickels/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/python + +import array as arr +import re +import random + +print "ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + +def create_brackets(nr): + s = "" + for i in range(1,nr): + br = random.randint(0,1) + if br: s += ")" + else : s += "(" + return s + +def balanced_brackets(brs): + brs, found = re.subn('\(\)','',brs); + ok = 0 + if found: + ok = balanced_brackets(brs) + return ok + else: + if re.search('\(|\)',brs): return 0 + else: return 1 + +loops = 0 +while True: + loops = loops + 1 + nr = random.randint(2,10) + brs = create_brackets(nr) + ok = balanced_brackets(brs) + rs = "NOT OK" + if ok: rs = "OK" + print("(%2d) %10s = %s" % (loops, brs, rs)) + if ok: break + if loops > 20: break + -- cgit