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

problem42.py

def check(line,n, title_lst):
            '''(list,index,list) -> bool
            checking for non-sentence-ending
            characters
            '''
            if line[n + 2].islower() and line[n + 2].isalpha():
                    return False
            elif line[n+1] == ' ' and line[n + 2] == line.isupper()\
                                                    and line[n + 2] == line.isalnum():
                    return True
            elif line[n+1].isalpha() or line[n+1].isnumeric():
                    return False
            elif line[n+1] == ' ' and (line[n-2:n] in title_lst\
                                                    or line[n-3:n] in title_lst):
                    return False
            elif  not ' ' in line[n-1:n+2]:
                    return False
            elif '.' in line[n+1] or ',' in line[n+1]:
                    return False
            else:
                    return True

def sentence_splitter():
            '''(file) -> str
            open and return a split text file
            '''
            line2 = ''
            line_starter = 0
            splitter = 0
            indexes = []
            title_lst = ['Mr', 'Mrs', 'Dr', 'Jr']
            f = open('sentence.txt','r' )
            for line in f:
                    chk_len = len(line)
                    # collect indexes of '.!?'
                    indexes = [n for (n,e) in enumerate(line) if e == '.']
                    indexes2 = [n for (n,e) in enumerate(line) if e == '?']
                    indexes3 = [n for (n,e) in enumerate(line) if e == '!']
                    indexes = indexes + indexes2 + indexes3
                    indexes = sorted(indexes)
                    print(indexes)

                    # select indexes one by one
                    for x in range(0, len(indexes)):
                            splitter = indexes[x]
                            if splitter == len(line)-1:
                                    line2 += line[line_starter + 1:splitter + 1].lstrip()
                                    print(line2)
                                    exit()
                            # Sort splitter index, return lowest index
                            truth = check(line,splitter,title_lst)

                            if truth:
                                    if line_starter == 0:
                                            line2 += line[line_starter:splitter + 1].strip() + '\n'
                                    else:
                                            line2 += line[line_starter + 1:splitter + 1].strip() + '\n'
                                    line_starter = splitter

                                    # end of sentence check
                                    len_line = len(line) - 1
                                    if splitter == len_line:
                                            line2 += line[line_starter+1:splitter + 1].strip() + '\n'
                                            print(line2)
                                            exit()
                            else:
                                    if line_starter == 0: # start of sentence
                                            line2 += line[line_starter:splitter + 1].lstrip()
                                            line_starter = splitter
                                    else:
                                            line2 += line[line_starter + 1:splitter + 1].lstrip()
                                            # start of slice
                                            line_starter = splitter


ans = sentence_splitter()
print(ans)

Result

[2, 26, 36, 57, 59, 81, 94, 109, 127, 157, 158, 159, 189, 200]
Mr.Smith bought cheapsite.com for 1.5 million dollars, i.e.he paid a lot for it.
Did he mind?
Adam Jones Jr.thinks he didn't.
In any case, this isn't true...
Well, with a probability of .9 it isn't.