46 Python Problems

From http://www.ling.gu.se/~lager/python_exercises.html

Files Code and Result

poblem19.py

problem1.py

problem2.py

problem3.py

problem4.py

problem5.py

problem6.py

problem7.py

problem8.py

problem9.py

problem10.py

problem11.py

problem12.py

problem13.py

problem14.py

problem15.py

problem16.py

problem17.py

problem18.py

problem20.py

problem21.py

problem22.py

problem23.py

problem24.py

problem25.py

problem26.py

problem27.py

problem28.py

problem29.py

problem30.py

problem31.py

problem32.py

problem33.py

problem34.py

problem36.py

problem37.py

problem38.py

problem42.py

problem43.py

problem44.py

problem45.py

problem46.py

problem44.py

def bracketed(num):
            import random
            a ='['
            b = ']'
            c = int(num)
            lst =[]
            sett = set()
            sett.add(a)
            sett .add(b)
            # randomize the brackets
            for x in range(1,c + 1):
                    lst.append(random.sample(sett,2))
            # convert to string for display purposes.
            strng = ''
            for item in lst:
                    for items in item:
                       strng += items
            print(strng)
            # iterate in blocks to check True/False
            for i in range(0, len(strng), 2):
                    slice = strng[i:i+2]
                    if slice[0] == a and slice[1] == b:
                            continue
                    else:
                            return  False
            return  True
n = 5
#n = input('Enter amount of brackets: ' )
ans = bracketed(n)
print('Result: ', ans)

Result

][][[][][]
Result:  False