def pig_latin(w): """Return the Pig Latin equivalent of English word w.""" if starts_with_a_vowel(w): return w + 'ay' return pig_latin(w[1:] + w[0]) def starts_with_a_vowel(w): """Return whether w begins with a vowel.""" return w[0].lower() in 'aeiou' def fact_iter(n): """Return n! for n > 0.""" total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): """Return n! for n > 0.""" if n == 1: return 1 return n * fact(n-1) def reverse(s): """Return the reverse of string s. >>> reverse('sub') 'bus' """ if s == '': return s return reverse(s[1:]) + s[0] def reverse_iter(s): """Return the reverse of string s. >>> reverse_iter('sub') 'bus' """ r, i = '', 0 while i < len(s): r, i = s[i] + r, i + 1 return r def reverse2(s): """Return the reverse of string s. >>> reverse2('sub') 'bus' """ def reverse_s(r, i): if not i < len(s): return r return reverse_s(s[i] + r, i + 1) return reverse_s('', 0) def approx_eq(x, y, tolerance=1e-12): return abs(x-y) <= tolerance def sqrt(x): """Compute the square root of x by mutually recursive Newton's method. >>> sqrt(9) 3.0 >>> sqrt(2) 1.414213562373095 """ def guess_sqrt(guess): if approx_eq(guess*guess, x): return guess return guess_again(guess) def guess_again(guess): return guess_sqrt(0.5 * (guess + x/guess)) return guess_sqrt(1)