From 093634ae0d2b9aabcc290ba865f4ce1c7c54661d Mon Sep 17 00:00:00 2001 From: Yevgeny Khessin Date: Thu, 24 Jan 2013 15:23:25 -0500 Subject: [PATCH 1/2] Testing update --- sieve_gen/sieve.py | 17 +++++++++++++++++ sieve_itr/sieve.py | 14 ++++++++++++++ sieve_mod/sieve.py | 13 +++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 sieve_gen/sieve.py create mode 100644 sieve_itr/sieve.py create mode 100644 sieve_mod/sieve.py diff --git a/sieve_gen/sieve.py b/sieve_gen/sieve.py new file mode 100644 index 0000000..c2ec5f7 --- /dev/null +++ b/sieve_gen/sieve.py @@ -0,0 +1,17 @@ +# this is an implementation of the 'sieve' functionality using +# a generator. + +def fib(): + last_1 = 1 + last_2 = 1 + + while 1: + next_fib = last_1 + last_2 + last_1, last_2 = last_2, next_fib + yield next_fib + +# additional questions to address: +# - could you swap the last two lines of the while statement? what are the +# plusses and minuses of doing so? +# - is there a way to condense the 'while' loop into two statements? try +# getting rid of the next_fib variable. diff --git a/sieve_itr/sieve.py b/sieve_itr/sieve.py new file mode 100644 index 0000000..38835b5 --- /dev/null +++ b/sieve_itr/sieve.py @@ -0,0 +1,14 @@ +# this is an implementation of the 'series' functionality using +# Python's iterator functionality. Here, 'adder' is a class that +# obeys the iterator protocol. + +class adder(object): + def __init__(self): + self.n = 0 + + def __iter__(self): + return self + + def next(self): + self.n += 1 + return self.n diff --git a/sieve_mod/sieve.py b/sieve_mod/sieve.py new file mode 100644 index 0000000..5da6adc --- /dev/null +++ b/sieve_mod/sieve.py @@ -0,0 +1,13 @@ +# this is an implementation of the 'series' functionality using a module. + +n = 0 + +def add_one(): + global n + n = n + 1 + return n + +# additional questions to address: +# - what does 'global' do, above? +# - what naming limitations are there on series.py? Could we name it +# series_mod.py or series-mod.py, and still have it work as a module? From 5805db03be0d2227e20b031a1025b020a697540d Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 24 Jan 2013 21:15:20 -0500 Subject: [PATCH 2/2] Yevgeny Khessin Homework 1 --- series_mod/example.py | 0 series_mod/series.py | 0 sieve_gen/example.py | 8 ++++++++ sieve_gen/sieve.py | 31 +++++++++++++++++-------------- sieve_gen/sieve.pyc | Bin 0 -> 657 bytes sieve_gen/test1.py | 17 +++++++++++++++++ sieve_gen/test2.py | 19 +++++++++++++++++++ sieve_gen/test3.py | 18 ++++++++++++++++++ sieve_itr/example.py | 8 ++++++++ sieve_itr/sieve.py | 39 ++++++++++++++++++++++++++++----------- sieve_itr/sieve.pyc | Bin 0 -> 1176 bytes sieve_itr/test1.py | 17 +++++++++++++++++ sieve_itr/test2.py | 19 +++++++++++++++++++ sieve_itr/test3.py | 18 ++++++++++++++++++ sieve_mod/example.py | 13 +++++++++++++ sieve_mod/sieve.py | 28 ++++++++++++++++++---------- sieve_mod/sieve.pyc | Bin 0 -> 657 bytes 17 files changed, 200 insertions(+), 35 deletions(-) mode change 100644 => 100755 series_mod/example.py mode change 100644 => 100755 series_mod/series.py create mode 100644 sieve_gen/example.py create mode 100644 sieve_gen/sieve.pyc create mode 100644 sieve_gen/test1.py create mode 100644 sieve_gen/test2.py create mode 100644 sieve_gen/test3.py create mode 100644 sieve_itr/example.py create mode 100644 sieve_itr/sieve.pyc create mode 100644 sieve_itr/test1.py create mode 100644 sieve_itr/test2.py create mode 100644 sieve_itr/test3.py create mode 100644 sieve_mod/example.py create mode 100644 sieve_mod/sieve.pyc diff --git a/series_mod/example.py b/series_mod/example.py old mode 100644 new mode 100755 diff --git a/series_mod/series.py b/series_mod/series.py old mode 100644 new mode 100755 diff --git a/sieve_gen/example.py b/sieve_gen/example.py new file mode 100644 index 0000000..3beadb4 --- /dev/null +++ b/sieve_gen/example.py @@ -0,0 +1,8 @@ +#Yevgeny Khessin +#A39652176 +#HW1 +# an implementation of Eratosthenes' Sieve using a Python generator +import sieve +#create zip of 1 through 4, and run sieve on it, printing results +for n, i in zip(range(4), sieve.sieve()): + print i \ No newline at end of file diff --git a/sieve_gen/sieve.py b/sieve_gen/sieve.py index c2ec5f7..9352ef7 100644 --- a/sieve_gen/sieve.py +++ b/sieve_gen/sieve.py @@ -1,17 +1,20 @@ -# this is an implementation of the 'sieve' functionality using -# a generator. +#Yevgeny Khessin +#A39652176 +#HW1 +# an implementation of Eratosthenes' Sieve using a Python generator -def fib(): - last_1 = 1 - last_2 = 1 +def primecheck(primes, n): #function defition of checking if number is prime + for i in primes: #for all numbers in primes list + if n % i == 0: #if modulus is 0 + return False #return false + return True #if not, return true - while 1: - next_fib = last_1 + last_2 - last_1, last_2 = last_2, next_fib - yield next_fib +def sieve(): #sieve function + listprimes = [2] #list of primes starting at 2 + counter = listprimes[-1] + 1 #go one back and add 1 + while 1: #endless while loop + if primecheck(listprimes, counter): #check if number is prime + listprimes.append(counter) #add it to list + yield counter #return the value -# additional questions to address: -# - could you swap the last two lines of the while statement? what are the -# plusses and minuses of doing so? -# - is there a way to condense the 'while' loop into two statements? try -# getting rid of the next_fib variable. + counter += 1 #increment counter \ No newline at end of file diff --git a/sieve_gen/sieve.pyc b/sieve_gen/sieve.pyc new file mode 100644 index 0000000000000000000000000000000000000000..711aac89e18169282affe403d772ad15fce9f728 GIT binary patch literal 657 zcmbtRT}uK%6uo!1{35Lu-(MY2ph_Ky3Cw0}G+0f9Z`j`ETen6c& z8vXzW?wNDva_7!De5z!aX69_xP6nWIH`6z`RK#0T@!W;so z2Q(+Tey-BOqdC*uqcVE?9wk|_MD0nI9xdYzX>V!(Icm4TZaM>_>=xL4k}5aGW{`~& zJyZs5M6p9%10@IM1T096N`ih*8KdJ_5E^xQUO$S{;j2nM0;AQtdg-co$&SY}TR@Y$ zHSDRdFEdOIKwRWK&T%OGU!WxB762h#rUi*a_^==OR9P@h>!J*2(EACY4dHLlj+BY9 zJ#g%*XgEW|q$fgc5S_6@(o{q$A1Z<)T+9Ii0ttZ-LM`Hg6bT!Ih=f+Dy``HEC-tsTTBWD( zW4ZGK;LSJ`&a{?hJl@@zdGq!;{@L69_=Df)w0UB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