diff options
34 files changed, 391 insertions, 33 deletions
diff --git a/day1/DebugginDiagram.png b/day1/DebugginDiagram.png Binary files differnew file mode 100644 index 0000000..530d1a0 --- /dev/null +++ b/day1/DebugginDiagram.png diff --git a/day1/Session-1.snm b/day1/Session-1.snm deleted file mode 100644 index e69de29..0000000 --- a/day1/Session-1.snm +++ /dev/null diff --git a/day1/Session-1.vrb b/day1/Session-1.vrb deleted file mode 100644 index 2fb75f0..0000000 --- a/day1/Session-1.vrb +++ /dev/null @@ -1,14 +0,0 @@ -\frametitle {Basic looping} - \begin{lstlisting} -# Fibonacci series: -# the sum of two elements -# defines the next -a, b = 0, 1 -while b < 10: - print b, - a, b = b, a + b - -\end{lstlisting} -\typ{1 1 2 3 5 8}\\ -\alert{Recall it is easy to write infinite loops with \kwrd{while}} - \inctime{20} diff --git a/day1/Session-2.snm b/day1/Session-2.snm deleted file mode 100644 index e69de29..0000000 --- a/day1/Session-2.snm +++ /dev/null diff --git a/day1/Session-2.vrb b/day1/Session-2.vrb deleted file mode 100644 index 0b55248..0000000 --- a/day1/Session-2.vrb +++ /dev/null @@ -1,12 +0,0 @@ -\frametitle {\typ{for}: the list companion} - \begin{lstlisting} -In [89]: for p, ch in enumerate( a ): - ....: print p, ch - ....: - ....: -0 a -1 b -2 c - \end{lstlisting} -Try: \typ{print enumerate(a)} -\inctime{10} diff --git a/day1/Session-3.snm b/day1/Session-3.snm deleted file mode 100644 index e69de29..0000000 --- a/day1/Session-3.snm +++ /dev/null diff --git a/day1/Session-4.snm b/day1/Session-4.snm deleted file mode 100644 index e69de29..0000000 --- a/day1/Session-4.snm +++ /dev/null diff --git a/day1/Session-4.tex b/day1/Session-4.tex index 2ac886d..4dc586b 100755 --- a/day1/Session-4.tex +++ b/day1/Session-4.tex @@ -39,6 +39,8 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries} +\usepackage{pgf} + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Macros \setbeamercolor{emphbar}{bg=blue!20, fg=black} @@ -339,10 +341,10 @@ or modulo by zero \begin{frame}[fragile] \frametitle{Debugging effectively} \begin{itemize} - \item \kwrd{print} based strategy - \item Process: Hypothesis, test, refine, rinse-repeat + \item \kwrd{print} based strategy + \item Process: \end{itemize} -\inctime{15} +\pgfimage[interpolate=true,width=5cm,height=5cm]{DebugginDiagram.png} \end{frame} \begin{frame}[fragile] @@ -350,7 +352,27 @@ or modulo by zero \begin{itemize} \item Using \typ{\%debug} and \typ{\%pdb} in IPython \end{itemize} -\inctime{15} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Debugging in IPython} +\small +\begin{lstlisting} +In [1]: import mymodule +In [2]: mymodule.test() +--------------------------------------------- +NameError Traceback (most recent call last) +/media/python/iitb/workshops/day1/<ipython console> in <module>() +/media/python/iitb/workshops/day1/mymodule.py in test() + 1 def test(): +----> 2 print spam +NameError: global name 'spam' is not defined +In [3]: %debug +> /media/python/iitb/workshops/day1/mymodule.py(2)test() + 0 print spam +ipdb> +\end{lstlisting} +\inctime{15} \end{frame} \begin{frame}[fragile] @@ -366,7 +388,7 @@ or modulo by zero \item Advances Functions: default arguments, keyword arguments \item Functional Programming, list comprehensions \item Errors and Exceptions in Python - \item Debugging: How to use pdb, \%pdb and \%debug in IPython + \item Debugging: \%pdb and \%debug in IPython \end{itemize} \end{frame} \end{document} diff --git a/day1/Session-4.vrb b/day1/Session-4.vrb deleted file mode 100644 index 5edf650..0000000 --- a/day1/Session-4.vrb +++ /dev/null @@ -1,2 +0,0 @@ -\frametitle {Debugging: Exercise} -\inctime{10} diff --git a/day1/exercise/aliquot.py b/day1/exercise/aliquot.py new file mode 100644 index 0000000..f07cd77 --- /dev/null +++ b/day1/exercise/aliquot.py @@ -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) diff --git a/day1/exercise/amicable.py b/day1/exercise/amicable.py new file mode 100644 index 0000000..0986e33 --- /dev/null +++ b/day1/exercise/amicable.py @@ -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. diff --git a/day1/exercise/amicable_debug.py b/day1/exercise/amicable_debug.py new file mode 100644 index 0000000..9369219 --- /dev/null +++ b/day1/exercise/amicable_debug.py @@ -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. diff --git a/day1/exercise/arm.py b/day1/exercise/arm.py new file mode 100644 index 0000000..1234267 --- /dev/null +++ b/day1/exercise/arm.py @@ -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 diff --git a/day1/exercise/bishop.py b/day1/exercise/bishop.py new file mode 100644 index 0000000..ccb27d9 --- /dev/null +++ b/day1/exercise/bishop.py @@ -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 diff --git a/day1/exercise/collatz.py b/day1/exercise/collatz.py new file mode 100644 index 0000000..2b980d7 --- /dev/null +++ b/day1/exercise/collatz.py @@ -0,0 +1,7 @@ +a = -1 +while a > 1: + print a + if a % 2: + a = a * 3 + 1 + else: + a /= 2 diff --git a/day1/exercise/datestring.py b/day1/exercise/datestring.py new file mode 100644 index 0000000..d990904 --- /dev/null +++ b/day1/exercise/datestring.py @@ -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) diff --git a/day1/exercise/duplicate_marks.py b/day1/exercise/duplicate_marks.py new file mode 100644 index 0000000..ce4cbca --- /dev/null +++ b/day1/exercise/duplicate_marks.py @@ -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)) diff --git a/day1/exercise/even_perfect_4.py b/day1/exercise/even_perfect_4.py new file mode 100644 index 0000000..7910acf --- /dev/null +++ b/day1/exercise/even_perfect_4.py @@ -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 diff --git a/day1/exercise/find_pow_2.py b/day1/exercise/find_pow_2.py new file mode 100644 index 0000000..937ab41 --- /dev/null +++ b/day1/exercise/find_pow_2.py @@ -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])) diff --git a/day1/exercise/funcs2.py b/day1/exercise/funcs2.py new file mode 100644 index 0000000..319f795 --- /dev/null +++ b/day1/exercise/funcs2.py @@ -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() diff --git a/day1/exercise/gcd.py b/day1/exercise/gcd.py new file mode 100644 index 0000000..41db13d --- /dev/null +++ b/day1/exercise/gcd.py @@ -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) + diff --git a/day1/exercise/gcd_another.py b/day1/exercise/gcd_another.py new file mode 100644 index 0000000..fe9e6a6 --- /dev/null +++ b/day1/exercise/gcd_another.py @@ -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) + diff --git a/day1/exercise/kwfreq.py b/day1/exercise/kwfreq.py new file mode 100644 index 0000000..32a8765 --- /dev/null +++ b/day1/exercise/kwfreq.py @@ -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 + diff --git a/day1/exercise/linspace.py b/day1/exercise/linspace.py new file mode 100644 index 0000000..404ac4c --- /dev/null +++ b/day1/exercise/linspace.py @@ -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) diff --git a/day1/exercise/markstats.py b/day1/exercise/markstats.py new file mode 100644 index 0000000..6697c17 --- /dev/null +++ b/day1/exercise/markstats.py @@ -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) + diff --git a/day1/exercise/missing_num.py b/day1/exercise/missing_num.py new file mode 100644 index 0000000..6f19805 --- /dev/null +++ b/day1/exercise/missing_num.py @@ -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 diff --git a/day1/exercise/pyramid1.py b/day1/exercise/pyramid1.py new file mode 100644 index 0000000..ba24141 --- /dev/null +++ b/day1/exercise/pyramid1.py @@ -0,0 +1,5 @@ +n = input('How many lines? ') +for i in range(1, n + 1): + for j in range(i): + print i, + print diff --git a/day1/exercise/pytriads.py b/day1/exercise/pytriads.py new file mode 100644 index 0000000..916c000 --- /dev/null +++ b/day1/exercise/pytriads.py @@ -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 + diff --git a/day1/exercise/readmarks.py b/day1/exercise/readmarks.py new file mode 100644 index 0000000..6a90fde --- /dev/null +++ b/day1/exercise/readmarks.py @@ -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()) diff --git a/day1/exercise/roots.py b/day1/exercise/roots.py new file mode 100644 index 0000000..c6bf43e --- /dev/null +++ b/day1/exercise/roots.py @@ -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() diff --git a/day1/exercise/round_float.py b/day1/exercise/round_float.py new file mode 100644 index 0000000..7c1b745 --- /dev/null +++ b/day1/exercise/round_float.py @@ -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 diff --git a/day1/exercise/strrange.py b/day1/exercise/strrange.py new file mode 100644 index 0000000..ee63f75 --- /dev/null +++ b/day1/exercise/strrange.py @@ -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 diff --git a/day1/exercise/word_frequencies.py b/day1/exercise/word_frequencies.py new file mode 100644 index 0000000..deb9802 --- /dev/null +++ b/day1/exercise/word_frequencies.py @@ -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 diff --git a/quiz.tex b/quiz.tex new file mode 100644 index 0000000..c65df67 --- /dev/null +++ b/quiz.tex @@ -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} |