Lecture Notes on 29 Jul 2013 def sum_divisors (n): sum = 0 limit = n // 2 + 1 for i in range (1, limit): if (n % i == 0): sum += i return sum def all_count (s, sub): count = 0 index = s.find(sub, 0) while (index > -1): count += 1 index = s.find (sub, index + 1) return count def word_freq (inFile, word): input = open (inFile, 'r') contents = input.read() input.close() count = contents.count (word) return count def grep (inFile, word): line_no = 0 input = open (inFile, 'r') for line in input: line_no += 1 if (line.find(word) > -1): print (line_no) input.close() def is_palindromic (s): return (s == s.reverse()) def is_anagram (s1, s2): a = list (s1) b = list (s2) a.sort() b.sort() return (a == b) def rotate (s, n): length = len(s) n = n % length return s[- n:] + s[: - n] def scalar_product (a, b): sum = 0 for i in range (len(a)): sum = sum + a[i] * b[i] return sum def sequential_search (a, x): for i in range (len(a)): if (a[i] == x): return i return -1 def binary_search (a, x): lo = 0 hi = len(a) - 1 while (lo <= hi): mid = (lo + hi) // 2 if (a[mid] < x): lo = mid + 1 elif (a[mid] > x): hi = mid - 1 else: return mid return -1