diff options
author | Santosh G. Vattam | 2010-03-11 18:01:23 +0530 |
---|---|---|
committer | Santosh G. Vattam | 2010-03-11 18:01:23 +0530 |
commit | 9638afaff8985d5528cb38842c13cc71cd6cab83 (patch) | |
tree | 671a499f31425f35ea158136da94f7a5272749d8 | |
parent | fb965a5047dca69c518543440c2a6fd5647555c4 (diff) | |
download | workshops-more-scipy-9638afaff8985d5528cb38842c13cc71cd6cab83.tar.gz workshops-more-scipy-9638afaff8985d5528cb38842c13cc71cd6cab83.tar.bz2 workshops-more-scipy-9638afaff8985d5528cb38842c13cc71cd6cab83.zip |
Updated after Day 2 at GRDCS
-rw-r--r-- | day1/exercise/aliquot.py | 6 | ||||
-rw-r--r-- | day1/exercise/amicable.py | 9 | ||||
-rw-r--r-- | day1/exercise/amicable_debug.py | 14 | ||||
-rw-r--r-- | day1/exercise/even_perfect_4a.py | 2 | ||||
-rw-r--r-- | day1/exercise/kwfreq.py | 3 | ||||
-rw-r--r-- | day1/exercise/pytriads.py | 4 | ||||
-rw-r--r-- | day1/exercise/word_frequencies.py | 8 | ||||
-rwxr-xr-x | day2/day2quiz.tex | 4 | ||||
-rw-r--r-- | day2/session1.tex | 2 | ||||
-rw-r--r-- | day2/session2.tex | 37 | ||||
-rw-r--r-- | day2/session3.tex | 1 |
11 files changed, 43 insertions, 47 deletions
diff --git a/day1/exercise/aliquot.py b/day1/exercise/aliquot.py index 25518f0..7d5676c 100644 --- a/day1/exercise/aliquot.py +++ b/day1/exercise/aliquot.py @@ -1,5 +1,7 @@ - def aliquot(n): + """returns the aliquot of a number which + is defined as the sum of all the proper + divisors of a number""" sum = 1 i = 2 @@ -7,7 +9,7 @@ def aliquot(n): if n % i == 0: sum += i + (n / i) i += 1 - if n % i == 0: sum += i + if i*i == n: sum += i return sum n = int(raw_input('Enter a number? ')) diff --git a/day1/exercise/amicable.py b/day1/exercise/amicable.py index c9aea71..6e5b2e1 100644 --- a/day1/exercise/amicable.py +++ b/day1/exercise/amicable.py @@ -1,15 +1,8 @@ -def is_perfect_square(n): - i = 1 - while i * i < n: - i += 1 - return i * i == n, i - def aliquot(n): sum = 1 i = 2 - is_ps, root = is_perfect_square(n) - while i < root: + while i * i < n: if n % i == 0: sum += i + (n / i) i += 1 diff --git a/day1/exercise/amicable_debug.py b/day1/exercise/amicable_debug.py index 9369219..6f42d86 100644 --- a/day1/exercise/amicable_debug.py +++ b/day1/exercise/amicable_debug.py @@ -1,16 +1,20 @@ import math def aliquot(n): - sum = 0 - for i in range(1, math.sqrt(n)+1): + sum = 1 + i = 2 + + while i * i < n: if n % i == 0: - sum += i + n/i + sum += i + (n / i) + i += 1 + if i*i == n: sum += i return sum amicable = [] -for n in range(10000, 100000): +for n in range(1000, 10000): m = aliquot(n) - if aliquot(m) == n: + if m > n and aliquot(m) == n: amicable.append((m, n)) print amicable diff --git a/day1/exercise/even_perfect_4a.py b/day1/exercise/even_perfect_4a.py index ccadc1c..d11c44d 100644 --- a/day1/exercise/even_perfect_4a.py +++ b/day1/exercise/even_perfect_4a.py @@ -11,4 +11,4 @@ while i <= 94: square = i * i if all_digits_even(square): print square - i += 1 + i += 2 diff --git a/day1/exercise/kwfreq.py b/day1/exercise/kwfreq.py index e905e26..1414c4b 100644 --- a/day1/exercise/kwfreq.py +++ b/day1/exercise/kwfreq.py @@ -1,6 +1,3 @@ -import keyword -f = open('amicable.py') - freq = {} for line in f: words = line.split() diff --git a/day1/exercise/pytriads.py b/day1/exercise/pytriads.py index bea543d..caf9137 100644 --- a/day1/exercise/pytriads.py +++ b/day1/exercise/pytriads.py @@ -10,8 +10,8 @@ def gcd(a, b): else: return gcd(b, a%b) -for a in range(3, 501): - for b in range( a+1, 501, 2): +for a in range(3, 101): + for b in range( a+1, 101, 2): if gcd( a, b ) == 1: is_ps, c = is_perfect_square((a * a) + (b * b)) if is_ps: print a, b, c diff --git a/day1/exercise/word_frequencies.py b/day1/exercise/word_frequencies.py index deb9802..1f7351c 100644 --- a/day1/exercise/word_frequencies.py +++ b/day1/exercise/word_frequencies.py @@ -1,11 +1,13 @@ -f = open('/home/madhu/pyprogs/pytriads.py') +f = open('/home/vattam/Desktop/circulate/word-freq/holmes.txt') freq = {} for line in f: words = line.split() for word in words: key = word.strip(',.!;?\'" ') - value = freq.get(key, 1) - freq[key] = value + 1 + if key in freq: + freq[key] += 1 + else: + freq[key] = 1 print freq diff --git a/day2/day2quiz.tex b/day2/day2quiz.tex index 5bdb267..f7d3ea0 100755 --- a/day2/day2quiz.tex +++ b/day2/day2quiz.tex @@ -57,6 +57,7 @@ On the top right hand corner please write down the following: \item Name: \item University/College/Company: \item Student/Teacher/Professional: + \item Field of interest/study: \end{itemize} \end{frame} @@ -162,7 +163,7 @@ In []: a.extend([5, 6]) In []: a = (1, 2, 3) In []: a[1] = 10 \end{lstlisting} - What is the result? + What is the output? \end{frame} \begin{frame}[fragile] @@ -183,4 +184,3 @@ func(1) \end{frame} \end{document} - diff --git a/day2/session1.tex b/day2/session1.tex index 3ed70c3..702017e 100644 --- a/day2/session1.tex +++ b/day2/session1.tex @@ -379,7 +379,7 @@ In []: s * s -------------------------------------------- TypeError Traceback (most recent call last) -/<ipython console> in <module>() +<ipython console> in <module>() TypeError: can`t multiply sequence by non-int of type `str` diff --git a/day2/session2.tex b/day2/session2.tex index 2b163b9..5b1c291 100644 --- a/day2/session2.tex +++ b/day2/session2.tex @@ -153,9 +153,7 @@ In []: while b < 10: \begin{block}{Documentation convention} \begin{itemize} \item \alert{Anything within \typ{[]} is optional} - \begin{itemize} - \item Nothing to do with Python. - \end{itemize} + \item Nothing to do with Python. \end{itemize} \end{block} \end{frame} @@ -219,37 +217,37 @@ In []: for i in range(3, 10, 2): \begin{lstlisting} num = [1, 2, 3, 4] \end{lstlisting} -\centerline{is a list} +is a list \end{block} \end{frame} \begin{frame}[fragile] \frametitle{Lists: methods} \begin{lstlisting} -In []: num = [1, 2, 3, 4] +In []: num = [9, 8, 2, 3, 7] -In []: num + [9, 10, 11] -Out[]: [1, 2, 3, 4, 9, 10, 11] +In []: num + [4, 5, 6] +Out[]: [9, 8, 2, 3, 7, 4, 5, 6] -In []: num.append([9, 10, 11]) +In []: num.append([4, 5, 6]) In []: num -Out[]: [1, 2, 3, 4, [9, 10, 11]] +Out[]: [9, 8, 2, 3, 7, [4, 5, 6]] \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Lists: methods} \begin{lstlisting} -In []: num = [1, 2, 3, 4] +In []: num = [9, 8, 2, 3, 7] -In []: num.extend([5, 6, 7]) +In []: num.extend([4, 5, 6]) In []: num -Out[]: [1, 2, 3, 4, 5, 6, 7] +Out[]: [9, 8, 2, 3, 7, 4, 5, 6] In []: num.reverse() In []: num -Out[]: [7, 6, 5, 4, 3, 2, 1] +Out[]: [6, 5, 4, 7, 3, 2, 8, 9] In []: num.remove(6) In []: num @@ -294,17 +292,17 @@ Out[]: [5, 4, 3, 2, 1] \begin{frame}[fragile] \frametitle{List containership} -\emphbar{Recall \typ{num} is \typ{[1, 2, 3, 4]}} +\emphbar{Recall \typ{num} is \typ{[9, 8, 2, 3, 7]}} \begin{lstlisting} In []: 4 in num -Out[]: True +Out[]: False -In []: b = 15 +In []: b = 8 In []: b in num -Out[]: False +Out[]: True In []: b not in num -Out[]: True +Out[]: False \end{lstlisting} \end{frame} @@ -387,7 +385,7 @@ Out[]: [10823, 233, \end{frame} \begin{frame} {Problem Set 2.1: Problem 2.1.1} -You are given date strings of the form ``29 Jul, 2009'', or ``4 January 2008''. In other words a number, a string and another number, with a comma sometimes separating the items.\\Write a function that takes such a string and returns a tuple (yyyy, mm, dd) where all three elements are ints. +You are given date strings of the form ``29 Jul, 2009'', or ``4 January 2008''. In other words a number, a string and another number, with a comma sometimes separating the items.\\Write a program that takes such a string as input and prints a tuple (yyyy, mm, dd) where all three elements are ints. \end{frame} \subsection{Sets} @@ -453,7 +451,6 @@ Given a dictionary of the names of students and their marks, identify how many d \frametitle{Problem 2.2.2} Given a list of words, find all the anagrams in the list. -\inctime{15} \end{frame} \section{Functions} diff --git a/day2/session3.tex b/day2/session3.tex index 08d4294..7600eea 100644 --- a/day2/session3.tex +++ b/day2/session3.tex @@ -256,6 +256,7 @@ from scipy import * \end{frame} \begin{frame}[fragile] + \frametitle{Now what?} \begin{lstlisting} Traceback (most recent call last): File "four_plot.py", line 1, in <module> |