Added quiz tex file and all exercise problems Madhu worked out.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Thu, 08 Oct 2009 18:59:47 +0530
changeset 64 333092b68926
parent 63 f5eac04a00fe
child 65 a525e580bc43
Added quiz tex file and all exercise problems Madhu worked out.
day1/exercise/aliquot.py
day1/exercise/amicable.py
day1/exercise/amicable_debug.py
day1/exercise/arm.py
day1/exercise/bishop.py
day1/exercise/collatz.py
day1/exercise/datestring.py
day1/exercise/duplicate_marks.py
day1/exercise/even_perfect_4.py
day1/exercise/find_pow_2.py
day1/exercise/funcs2.py
day1/exercise/gcd.py
day1/exercise/gcd_another.py
day1/exercise/kwfreq.py
day1/exercise/linspace.py
day1/exercise/markstats.py
day1/exercise/missing_num.py
day1/exercise/pyramid1.py
day1/exercise/pytriads.py
day1/exercise/readmarks.py
day1/exercise/roots.py
day1/exercise/round_float.py
day1/exercise/strrange.py
day1/exercise/word_frequencies.py
quiz.tex
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/aliquot.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,8 @@
+def aliquot(n):
+    sum = 0
+    for i in range(1, (n/2)+1):
+        if n % i == 0:
+            sum += i
+    return sum
+
+print aliquot(14)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/amicable.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,18 @@
+import math
+
+def aliquot(n):
+    sum = 0
+    for i in range(1, int(math.sqrt(n))+1):
+        if n % i == 0:
+            sum += i + n/i
+    return sum
+
+amicable = []
+for n in range(10000, 100000):
+    m = aliquot(n)
+    if aliquot(m) == n:
+        amicable.append((m, n))
+
+print amicable
+
+# please please please profile this.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/amicable_debug.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,18 @@
+import math
+
+def aliquot(n):
+    sum = 0
+    for i in range(1, math.sqrt(n)+1):
+        if n % i == 0:
+            sum += i + n/i
+    return sum
+
+amicable = []
+for n in range(10000, 100000):
+    m = aliquot(n)
+    if aliquot(m) == n:
+        amicable.append((m, n))
+
+print amicable
+
+# please please please profile this.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/arm.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,10 @@
+cubes = []
+for i in range(10):
+    cubes.append(i ** 3)
+
+for i in range(100, 1000):
+    a = i % 10
+    b = (i / 10) % 10
+    c = (i / 100) % 10
+    if i == cubes[a] + cubes[b] + cubes[c]:
+        print "Armstrong Number: ", i
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/bishop.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,7 @@
+r, c = 5, 4
+for i in range(1, 9):
+    for j in range(1, 9):
+        a = r - i
+        b = c - j
+        if a and b and a == b or a == -b:
+            print i, j
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/collatz.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,7 @@
+a = -1
+while a > 1:
+    print a
+    if a % 2:
+        a = a * 3 + 1
+    else:
+        a /= 2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/datestring.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,23 @@
+def get_date_from_str(date_str):
+    month2mm = {
+        'January': 1,
+        'February': 2,
+        'March': 3,
+        'April': 4,
+        'May': 5,
+        'June': 6,
+        'July': 7,
+        'August': 8,
+        'September': 9,
+        'October': 10,
+        'November': 11,
+        'December': 12,
+        }
+
+    dd, month, yyyy = date_str.split()
+
+    mm = month2mm[month]
+    return int(yyyy), int(dd.strip(',')), mm
+
+date_str = raw_input('Enter a date string? ')
+print get_date_from_str(date_str)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/duplicate_marks.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,13 @@
+students = {
+    'Madhu': 12,
+    'Shantanu':45,
+    'Puneeth': 54,
+    'Vattam': 35,
+    'KD': 50,
+    }
+
+all_marks = students.values()
+unique_marks = set(all_marks)
+
+print "Number of Duplicate marks: ", len(all_marks) - len(unique_marks)
+print "Duplicate marks: ", set(all_marks - list(unique_marks))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/even_perfect_4.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,17 @@
+def is_perfect_square(n):
+    i = 1
+    while i * i < n:
+        i += 1
+    return i * i == n
+
+def all_digits_even(n):
+    if n < 0: n = -n
+    while n > 0:
+        if n % 2 == 1:
+            return False
+        n /= 10
+    return True
+
+for i in range(2222, 8888):
+    if all_digits_even(i) and is_perfect_square(i):
+        print i
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/find_pow_2.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,29 @@
+def is_pow_2(n):
+    bin_count = 0
+    while n > 0:
+        if n % 2 == 1:
+            bin_count += 1
+        if bin_count > 1:
+            return False
+        n /= 2
+
+    return bin_count == 1
+
+def collatz_pow_2(n):
+    if n == 1: return 4
+    if n == 2: return 4
+    collatz_pow_2 = []
+    while n > 2:
+        print n, 
+        if is_pow_2(n):
+            collatz_pow_2.append(n)
+
+        if n % 2:
+            n = n * 3 - 1
+        else:
+            n /= 2 
+
+    return max(collatz_pow_2)
+
+import sys
+collatz_pow_2(int(sys.argv[1]))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/funcs2.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,28 @@
+import math
+
+def linspace(a, b, N):
+    lns = []
+    step = (float(b) - float(a)) / float(N - 1)
+    print step
+    for i in range(N):
+        lns.append(a + i*step)
+
+    return lns
+
+def sin_func():
+    x = linspace(0, 5, 11)
+    sin_list = []
+    for i in x:
+        sin_list.append(math.sin(i))
+
+    print sin_list
+
+def sinsin_func():
+    x = linspace(0, 5, 11)
+    sin_list = []
+    for i in x:
+        sin_list.append(math.sin(i) + math.sin(10*i))
+
+    print sin_list
+
+sinsin_func()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/gcd.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,8 @@
+def gcd(a, b):
+  if a % b == 0:
+    return b
+  return gcd(b, a%b)
+
+print gcd(5, 40)
+print gcd(11, 60)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/gcd_another.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,13 @@
+def gcd(a, b):
+  if a - b == 0:
+    return b
+  if a > b:
+    return gcd(b, a-b)
+  else:
+    return gcd(b, b-a)
+
+def lcm(a, b):
+    return (a * b) / gcd(a, b)
+
+print lcm(21, 14)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/kwfreq.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,14 @@
+import keyword
+f = open('/home/madhu/pyprogs/pytriads.py')
+
+freq = {}
+for line in f:
+    words = line.split()
+    for word in words:
+        key = word.strip(',.!;?()[]: ')
+        if keyword.iskeyword(key):
+            value = freq.get(key, 1)
+            freq[key] = value + 1
+
+print freq
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/linspace.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,10 @@
+def linspace(a, b, N):
+    lns = []
+    step = (float(b) - float(a)) / float(N - 1)
+    print step
+    for i in range(N):
+        lns.append(a + i*step)
+
+    return lns
+
+print linspace(0, 5, 11)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/markstats.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,22 @@
+import math
+
+f = open('/home/madhu/Desktop/marks.dat')
+    
+subj_marks = [[]] * 5
+names = []
+for line in f:
+    fields = line.split(';')
+    names.append(fields[2])
+    for i in range(5):
+        subj_marks[i].append(int(fields[i+3]))
+
+for i in range(5):
+    avg_marks = float(sum(subj_marks[i])) / len(subj_marks[i])
+    student = names[subj_marks[i].index(max(subj_marks[i]))]
+    sigma = 0
+    for j in subj_marks[i]:
+        sigma += (j - avg_marks) ** 2
+
+    std_dev = math.sqrt(sigma)
+    print "Average marks for subject: %f is Standard Deviation is %f, Student with Highest Marks is %s" % (avg_marks, std_dev, student)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/missing_num.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,16 @@
+str_range = '4-7, 9, 12, 15'
+
+ranges = str_range.split(',')
+
+lst = []
+for r in ranges:
+    vals = r.split('-')
+    if len(vals) == 2:
+       lst.extend(range(int(vals[0]), int(vals[1]) + 1))
+    else:
+       lst.append(int(vals[0]))
+
+set_range = set(lst)
+all_elems = set(range(min(lst), max(lst)))
+
+print all_elems - set_range
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/pyramid1.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,5 @@
+n = input('How many lines? ')
+for i in range(1, n + 1):
+    for j in range(i):
+        print i,
+    print
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/pytriads.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,18 @@
+def is_perfect_square(n):
+    i = 1
+    while i * i < n:
+        i += 1
+    return i * i == n, i
+
+def gcd(a, b):
+    if a % b == 0:
+        return b
+    else:
+        return gcd(b, a%b)
+
+for a in range(3, 100):
+    for b in range(a+1, 100):
+        ips, c = is_perfect_square((a * a) + (b * b))
+        if ips and gcd(a, b) == 1:
+            print a, b, c
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/readmarks.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,5 @@
+f = open('/home/madhu/Desktop/marks.dat')
+
+for line in f:
+    fields = line.split(';')
+    print "Name: %s, Total Marks: %s" % (fields[2].strip(), fields[8].strip())
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/roots.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,28 @@
+import math
+
+def linspace(a, b, N):
+    lns = []
+    step = (float(b) - float(a)) / float(N - 1)
+    print step
+    for i in range(N):
+        lns.append(a + i*step)
+
+    return lns
+
+def sinsin_func():
+    x = linspace(0, 5, 11)
+    sin_list = []
+    for i in x:
+        sin_list.append(math.sin(i) + math.sin(10*i))
+
+    return sin_list
+
+def find_root_range():
+    sin_list = sinsin_func()
+    for i, sins in enumerate(sin_list):
+        if (sin_list[i] > 0 and sin_list[i+1] < 0) or (sin_list[i] > 0 and sin_list[i+1] < 0):
+            print "Roots lie between: %f and %f" % (sin_list[i], sin_list[i+1])
+        if sin_list[i] == 0:
+            print "%f is the root" % sin_list[i]
+
+find_root_range()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/round_float.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,12 @@
+# round using int
+n = 17.3 # any float
+int (n + .5)
+
+# round it off to first decimal place
+round(amount * 10) / 10.0
+
+# round it off to nearest 5 paise
+round(amount * 20) / 20.0
+
+# exchange two variables
+a, b = b, a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/strrange.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,13 @@
+str_ranges = "1, 3-7, 12, 15, 18-21"
+
+ranges = str_ranges.split(',')
+
+lst = []
+for r in ranges:
+    vals = r.split('-')
+    if len(vals) == 2:
+       lst.extend(range(int(vals[0]), int(vals[1]) + 1))
+    else:
+       lst.append(int(vals[0]))
+
+print lst
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/word_frequencies.py	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,11 @@
+f = open('/home/madhu/pyprogs/pytriads.py')
+
+freq = {}
+for line in f:
+    words = line.split()
+    for word in words:
+        key = word.strip(',.!;?\'" ')
+        value = freq.get(key, 1)
+        freq[key] = value + 1
+
+print freq
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quiz.tex	Thu Oct 08 18:59:47 2009 +0530
@@ -0,0 +1,11 @@
+\documentclass[a4paper,10pt]{book}
+
+
+\begin{document}
+Which version of Python were you using? 
+List some key differences between IPython and Vanilla Python
+What is the biggest integer number that can be represented by Python?
+What is the result of 17.0 / 2?
+What does '*' * 40 produce?
+ 
+\end{document}