diff --git a/Students/Dave Fugelso/Lightning Talk/one.py b/Students/Dave Fugelso/Lightning Talk/one.py new file mode 100644 index 00000000..474ee472 --- /dev/null +++ b/Students/Dave Fugelso/Lightning Talk/one.py @@ -0,0 +1,115 @@ + +original = ''' +518659005232r583491410442e291745698219m421410443129o162080940635v32416189681e64832 +3758660l64832378918a291745692333r648323762540g324161887930e680739944367s22691331914 +3t129664756724p486242827755r648323758580i97248568173m486242846295e421410460783 +''' + +originalParsedText = ''' +518659005232 +r +583491410442 +e +291745698219 +m +421410443129 +o +162080940635 +v +32416189681 +e +648323758660 +l +64832378918 +a +291745692333 +r +648323762540 +g +324161887930 +e +680739944367 +s +226913319143 +t +129664756724 +p +486242827755 +r +648323758580 +i +97248568173 +m +486242846295 +e +421410460783 +''' + + +a = ''' +486242831895 +551075217947 +259329507176 +388994275884 +129664751092 +0 +615907574147 +32416188601 +259329512488 +615907584293 +291745691343 +648323773780 +194497138302 +97248563157 +453826655534 +615907594667 +64832376454 +453826635794 +388994273508 +''' + +#Code posted on Daniwe.com by a moderators named +# prime numbers are only divisible by unity and themselves +# (1 is not considered a prime number by convention) +def isprime(n): + '''check if integer n is a prime''' + # make sure n is a positive integer + n = abs(int(n)) + # 0 and 1 are not primes + if n < 2: + return False + # 2 is the only even prime number + if n == 2: + return True + # all other even numbers are not primes + if not n & 1: + return False + # range starts with 3 and only needs to go up the squareroot of n + # for all odd numbers + for x in range(3, int(n**0.5)+1, 2): + if n % x == 0: + return False + return True + +def solve(): + ''' + Find out which of the number above are prime + ''' + + b = a.split() + primes = list() + + for line in b: + i = int(line) + print 'checking', i, + if isprime(i): + print 'is prime' + else: + print 'is not prime' + + + + + + +solve() \ No newline at end of file diff --git a/Students/Dave Fugelso/Lightning Talk/prime.py b/Students/Dave Fugelso/Lightning Talk/prime.py new file mode 100644 index 00000000..f3b7b57b --- /dev/null +++ b/Students/Dave Fugelso/Lightning Talk/prime.py @@ -0,0 +1,128 @@ + +original = ''' +518659005232r583491410442e291745698219m421410443129o162080940635v32416189681e64832 +3758660l64832378918a291745692333r648323762540g324161887930e680739944367s22691331914 +3t129664756724p486242827755r648323758580i97248568173m486242846295e421410460783 +''' + +originalParsedText = ''' +518659005232 +r +583491410442 +e +291745698219 +m +421410443129 +o +162080940635 +v +32416189681 +e +648323758660 +l +64832378918 +a +291745692333 +r +648323762540 +g +324161887930 +e +680739944367 +s +226913319143 +t +129664756724 +p +486242827755 +r +648323758580 +i +97248568173 +m +486242846295 +e +421410460783 +''' + + +nums = ''' +486242831895 +551075217947 +259329507176 +388994275884 +129664751092 +0 +615907574147 +32416188601 +259329512488 +615907584293 +291745691343 +648323773780 +194497138302 +97248563157 +453826655534 +615907594667 +64832376454 +453826635794 +388994273508 +''' + +#Code posted on Daniwe.com by a moderators named +def isprime(n): + '''check if integer n is a prime''' + # make sure n is a positive integer + n = abs(int(n)) + # 0 and 1 are not primes + if n < 2: + return n, 'False' + # 2 is the only even prime number + if n == 2: + return 2, 'True' + # all other even numbers are not primes + if not n & 1: + return n/2, 'False' + # range starts with 3 and only needs to go up the squareroot of n + # for all odd numbers + for x in range(3, int(n**0.5)+1, 2): + if n % x == 0: + return n/x, 'False' + return n, 'True' + +def solve(): + ''' + Process a list of numbers and factor out the largest prime number in each. + Add 65 to the number left and convert to a character + ''' + + numbers = nums.split() + largestPrimesGone = list() + + for line in numbers: + i = int(line) + print 'checking', i + n, prime = isprime(i) + while prime == 'False' and n > 2: + print 'was not prime now checking', n + n, prime = isprime(n) + + print 'largest prime is ', n + if n != 0: + n = i / n + print 'new num is ', n + largestPrimesGone.append(n) + + + addr = '' + for line in largestPrimesGone: + print line + addr = addr + str(unichr(line+65)) + + + print addr + + + + + +solve() diff --git a/Students/Dave Fugelso/Lightning Talk/puzzle listing.png b/Students/Dave Fugelso/Lightning Talk/puzzle listing.png new file mode 100644 index 00000000..94af8608 Binary files /dev/null and b/Students/Dave Fugelso/Lightning Talk/puzzle listing.png differ diff --git a/Students/Dave Fugelso/Lightning Talk/two.py b/Students/Dave Fugelso/Lightning Talk/two.py new file mode 100644 index 00000000..f73a3fd6 --- /dev/null +++ b/Students/Dave Fugelso/Lightning Talk/two.py @@ -0,0 +1,120 @@ +original = ''' +518659005232r583491410442e291745698219m421410443129o162080940635v32416189681e64832 +3758660l64832378918a291745692333r648323762540g324161887930e680739944367s22691331914 +3t129664756724p486242827755r648323758580i97248568173m486242846295e421410460783 +''' + +originalParsedText = ''' +518659005232 +r +583491410442 +e +291745698219 +m +421410443129 +o +162080940635 +v +32416189681 +e +648323758660 +l +64832378918 +a +291745692333 +r +648323762540 +g +324161887930 +e +680739944367 +s +226913319143 +t +129664756724 +p +486242827755 +r +648323758580 +i +97248568173 +m +486242846295 +e +421410460783 +''' + +nums = ''' +486242831895 +551075217947 +259329507176 +388994275884 +129664751092 +0 +615907574147 +32416188601 +259329512488 +615907584293 +291745691343 +648323773780 +194497138302 +97248563157 +453826655534 +615907594667 +64832376454 +453826635794 +388994273508 +''' + + +def isprime(n): + '''check if integer n is a prime''' + # make sure n is a positive integer + n = abs(int(n)) + # 0 and 1 are not primes + if n < 2: + return n, False + # 2 is the only even prime number + if n == 2: + return 2, True + # all other even numbers are not primes + if not n & 1: + return n/2, False + # range starts with 3 and only needs to go up the squareroot of n + # for all odd numbers + for x in range(3, int(n**0.5)+1, 2): + if n % x == 0: + return n/x, False + return True + +def solve(): + ''' + Process a list of numbers and factor out the largest prime number in each. + ''' + + b = nums.split() + primes = list() + + for line in b: + i = int(line) + print 'checking', i + n, prime = isprime(i) + while not prime and n > 2: + print 'was not prime now checking', n + n, prime = isprime(n) + + print 'largest prime is ', n + if n != 0: + n = i / n + print 'new num is ', n + primes.append(n) + + + print 'List is ' + for n in primes: + print n + + + + +solve() \ No newline at end of file diff --git a/Students/Dave Fugelso/Session 1/breakme.py b/Students/Dave Fugelso/Session 1/breakme.py new file mode 100644 index 00000000..761583bf --- /dev/null +++ b/Students/Dave Fugelso/Session 1/breakme.py @@ -0,0 +1,33 @@ +''' +In the break_me.py file write four simple Python functions: + +Each function, when called, should cause an exception to happen +Each function should result in one of the four common exceptions from our lecture. +for review: NameError, TypeError, SyntaxError, AttributeError + + +Use the Python standard library reference on Built In Exceptions as a reference +''' + +def nameErrorExample(): + t = somethingThatDoesntExist() + +def typeErrorExample (): + a = 'Hello, World!' + worlds = 1 + worlds = worlds + a + +def syntaxErrorExample (): +# a = 'some string' +# a.print() + pass + +import time +def attributeErroexample(): + t = time.time + b = t.newtime + +#nameErrorExample() +#typeErrorExample () +syntaxErrorExample () +attributeErroexample() \ No newline at end of file diff --git a/Students/Dave Fugelso/Session 1/grid.py b/Students/Dave Fugelso/Session 1/grid.py new file mode 100644 index 00000000..1753a208 --- /dev/null +++ b/Students/Dave Fugelso/Session 1/grid.py @@ -0,0 +1,121 @@ +''' + +Dave Fugelso + +Problem: + +Write a function that draws a grid like the following: + ++ - - - - + - - - - + +| | | +| | | +| | | +| | | ++ - - - - + - - - - + +| | | +| | | +| | | +| | | ++ - - - - + - - - - + +Hint: to print more than one value on a line, you can print a comma-separated sequence: print "+ -" + +If the sequence ends with a comma, Python leaves the line unfinished, so the value printed next appears on the same line. + +print "+", +print "-" +The output of these statements is "+ -". + +A print statement all by itself ends the current line and goes to the next line. + +Part 2: + +Write a function print_grid() that takes one integer argument and prints a grid like the picture above, BUT the size of the grid is given by the argument. + +For example, print_grid(11) prints the grid in the above picture. + +This problem is underspecified. Do something reasonable. + +Hints: + +A character is a string of length 1 + +s + t is string s followed by string t + +s * n is string s replicated n times + +Part 3: + +Write a function that draws a similar grid with three rows and three columns. + +(what to do about rounding?) + + + +''' + +def cellTopBottom(cells, size): + ''' + print cell top or botton, including the '+' + ''' + for columns in range (0, cells): + print '+', + for i in range (0, size): + print '-', + print '+' + +def printSide (cells, size): + ''' + Just do the sides, no '+' + ''' + + + for rows in range (0, size): + print '|', + for column in range (0, cells): + for spaces in range(0, size): + print ' ', + print '|', + print + +def fixed_grid(): + ''' + print a two by two grid like example above. + ''' + cells = 2 + size = 4 + + for rows in range(0, cells): + cellTopBottom(cells, size) + printSide (cells, size) + cellTopBottom(cells, size) + +def print_grid(size): + ''' + Take an argument and print a grid with a vartiable size + ''' + cells = 2 + + for rows in range(0, cells): + cellTopBottom(cells, size) + printSide (cells, size) + cellTopBottom(cells, size) + +def print_grid_variable_cells(cells, size): + ''' + Take an argument and print a grid with a variable size + Take an argument for number of cells + + Ah, by using cells and size for the cell size I completely missed the rounding portion of this problem. + If I did do iut the other way, I would have left off the right and bottom edges. + ''' + + for rows in range(0, cells): + cellTopBottom(cells, size) + printSide (cells, size) + cellTopBottom(cells, size) + +fixed_grid() +print_grid(4) +print_grid(5) +print_grid_variable_cells (3, 4) + diff --git a/Students/Dave Fugelso/Session 2/ack.py b/Students/Dave Fugelso/Session 2/ack.py new file mode 100644 index 00000000..fe6ca666 --- /dev/null +++ b/Students/Dave Fugelso/Session 2/ack.py @@ -0,0 +1,79 @@ +''' + +Dave Fugelso Python Course homework Session 2 Oct. 9 + +The Ackermann function, A(m, n), is defined: + +A(m, n) = + n+1 if m = 0 + A(m-1, 1) if m > 0 and n = 0 + A(m-1, A(m, n-1)) if m > 0 and n > 0. + + + + See http://en.wikipedia.org/wiki/Ackermann_funciton + +Create a new module called ack.py in a session02 folder in your student folder. + +In that module, write a function named ack that performs Ackermann's function. + + +Write a good docstring for your function according to PEP 257. +Ackermanns function is not defined for input values less than 0. Validate inputs to your function and return None if they are negative. +The wikipedia page provides a table of output values for inputs between 0 and 4. Using this table, add a if __name__ == "__main__": block to test your function. + +Test each pair of inputs between 0 and 4 and assert that the result produced by your function is the result expected by the wikipedia table. + +When your module is run from the command line, these tests should be executed. If they all pass, + + +print All Tests Pass as the result. + +Add your new module to your git clone and commit frequently while working on your implementation. Include good commit messages that explain concisely both what you are doing and why. + +When you are finished, push your changes to your fork of the class repository in GitHub. Then make a pull request and submit your assignment in Canvas. + +''' + +#Ackermann function +def ack(m, n): + ''' + Calculate the value for Ackermann's function for m, n. + ''' + + if m < 0 or n < 0: return None + + if m == 0: return n+1 + + if n == 0: return ack(m-1, 1) + + return ack (m-1, ack (m, n-1)) + + +if __name__ == "__main__": + ''' + Unit test for Ackermann function. Print table m = 0,4 and n = 0,4. + ''' + + #Print nicely + print 'm/n\t\t', + for n in range(0,5): + print n, '\t', + print '\n' + + for m in range (0,4): + print m,'\t', + for n in range(0,5): + print '\t', + print ack(m, n), + print + + # for the m = 4 row, just print the first one (n = 0) otherwise we hit a stack overflow (maximum resursion) + m = 4 + print m,'\t', + for n in range(0,1): + print '\t', + print ack(m, n), + print '\t-\t-\t-\t-' + + print 'All Tests Pass' \ No newline at end of file diff --git a/Students/Dave Fugelso/Session 2/series.py b/Students/Dave Fugelso/Session 2/series.py new file mode 100644 index 00000000..8caeab06 --- /dev/null +++ b/Students/Dave Fugelso/Session 2/series.py @@ -0,0 +1,150 @@ +''' +Dave Fugelso - UW Python Certification 10/09/2014 + +The Fibonacci Series is a numeric series starting with the integers 0 and 1. In this series, the next +integer is determined by summing the previous two. This gives us: + +0, 1, 1, 2, 3, 5, 8, 13, ... +Create a new module series.py in the session02 folder in your student folder. In it, add a function +called fibonacci. The function should have one parameter n. The function should return the nth value in the fibonacci series. + +Ensure that your function has a well-formed docstring + +The Lucas Numbers are a related series of integers that start with the values 2 and 1 rather than 0 and 1. +The resulting series looks like this: + + +2, 1, 3, 4, 7, 11, 18, 29, ... +In your series.py module, add a new function lucas that returns the nth value in the lucas numbers + +Ensure that your function has a well-formed docstring + +Both the fibonacci series and the lucas numbers are based on an identical formula. + +Add a third function called sum_series with one required parameter and two optional parameters. The required +parameter will determine which element in the series to print. The two optional parameters will have default values of 0 and 1 and will determine the first two values for the series to be produced. + +Calling this function with no optional parameters will produce numbers from the fibonacci series. Calling it +with the optional arguments 2 and 1 will produce values from the lucas numbers. Other values for the optional parameters will produce other series. + +Ensure that your function has a well-formed docstring + +Add an if __name__ == \"__main__\": block to the end of your series.py module. Use the block to write a series of assert +statements that demonstrate that your three functions work properly. + + +Use comments in this block to inform the observer what your tests do. + +Add your new module to your git clone and commit frequently while working on your implementation. Include good commit +messages that explain concisely both what you are doing and why. + +When you are finished, push your changes to your fork of the class repository in GitHub. Then make a pull request and submit your assignment in Canvas. +''' + +def fibonnacci (n): + ''' + Return the Nth value in the Fibonacci series. + Args: + n - Nth value + ''' + + # special cases at front of series + if n == 0: return 0 + if n == 1: return 1 + + #else let's calculate + a, b = 0, 1 + for seq in range (2, n): + a,b = b,a+b + return a+b + +def lucas (n): + ''' + Return the Nth value in the Lucas series. + Args: + n - Nth value + ''' + + # special cases at front of series + if n == 0: return 2 + if n == 1: return 1 + + #else let's calculate + a, b = 2, 1 + for seq in range (2, n): + a,b = b,a+b + return a+b + +''' +Second solution. Just have lucas and fibonacci start a series function with differing start arguments. +''' + +def series (a, b, n): + ''' + Calculate the nth number in a series based on starting at a, b. + Args: + a - value for element 0 + b - value for element 1 + n - the series element wanted + ''' + + # special cases at front of series + if n == 0: return a + if n == 1: return b + + #else let's calculate + for seq in range (2, n): + a,b = b,a+b + return a+b + +def fibonacci_2(n): + ''' + Return the Nth value in the Fibonacci series. + Args: + n - Nth value + ''' + return series(0, 1, n) + +def lucas_2(n): + ''' + Return the Nth value in the Lucas series. + Args: + n - Nth value + ''' + return series(2, 1, n) + +if __name__ == "__main__": + ''' + perform unit tests for fibonacci and lucas funcitons. + ''' + + #Test the Fibinacci series: randomly selected 0, 1 5, 8 and 25 + assert fibonnacci (0)==0, 'Fibonnaci (0) is 0. Failed!' + assert fibonnacci (1)==1, 'Fibonnaci (1) is 1. Failed!' + assert fibonnacci (5)==5, 'Fibonnaci (5) is 5. Failed!' + assert fibonnacci (8)==21, 'Fibonnaci (8) is 21. Failed!' + assert fibonnacci (25)==75025, 'Fibonnaci (25) is 75025. Failed!' + + #Test lucas series: choose 0, 1, 6, 9, 26 + assert lucas(0)==2, 'Lucas(0) is 2. Failed!' + assert lucas(1)==1, 'Lucas(1) is 1. Failed!' + assert lucas(6)==18, 'Lucas(6) is 2. Failed!' + assert lucas(9)==76, 'Lucas(9) is 2. Failed!' + assert lucas(26)==271443, 'Lucas(26) is 2. Failed!' + + #Test the Fibinacci series: randomly selected 0, 1 5, 8 and 25 + assert fibonnacci_2 (0)==0, 'Fibonnaci 2 (0) is 0. Failed!' + assert fibonnacci_2 (1)==1, 'Fibonnaci 2 (1) is 1. Failed!' + assert fibonnacci_2 (5)==5, 'Fibonnaci 2 (5) is 5. Failed!' + assert fibonnacci_2 (8)==21, 'Fibonnaci 2 (8) is 21. Failed!' + assert fibonnacci_2 (25)==75025, 'Fibonnaci 2 (25) is 75025. Failed!' + + # Test the second solutions + #Test lucas series: choose 0, 1, 6, 9, 26 + assert lucas_2(0)==2, 'Lucas 2 (0) is 2. Failed!' + assert lucas_2(1)==1, 'Lucas 2 (1) is 1. Failed!' + assert lucas_2(6)==18, 'Lucas 2 (6) is 2. Failed!' + assert lucas_2(9)==76, 'Lucas 2 (9) is 2. Failed!' + assert lucas_2(26)==271443, 'Lucas 2 (26) is 2. Failed!' + + \ No newline at end of file