# Answers to StringList Test 1.5 # Problem #1 def StrStrip(s): while len(s) > 0: if s[0] == ' ': s = s[1:] else: break while len(s) > 0: if s[-1] == ' ': s = s[:-1] else: break return s # Problem #2 def StrSplit(s,c): answer = [] original_s = s pos = s.find(c) while pos >= 0: answer.append(s[:pos]) s = s[pos+1:] pos = s.find(c) if len(s) > 0: answer.append(s) elif original_s[-1] == c: answer.append('') return answer # Problem 3 def IsInt(s): s = StrStrip(s) if len(s) == 0: return False if s[0] == '+' or s[0] == '-': s = s[1:] if len(s) == 0: return False for c in s: if "0123456789".find(c) < 0: return False return True # Problem 4 def Mode(L): best_so_far = L[0] best_so_far_dups = 1 current = L[0] dups = 1 for i in range(1,len(L)): num = L[i] if num == current: dups += 1 if dups > best_so_far_dups: best_so_far_dups = dups best_so_far = current else: current = num dups = 1 return best_so_far