next up previous
Next: About this document ...

CpSc 428/628
Programming Language: Design & Implementation

Quiz #1
Department of Computer Science, Clemson University
Brian Malloy, PhD
July 5, 2009

The problems below are designed to help you learn about the control structures and type system of the Python language. Write Python code to solve each problem, place your solutions into a directory, compress the directory and submit it using the handin command. Your submission must be received by 8 AM on Tuesday, July 7th, 2009. The format for the handin command is:

        handin.428.1 1 *

  1. Create an ascii file with the output for the following program:

    def change(x, y):
      return y,x
    
    def one():
      a = (1, 2)
      b = (3, 4)
      t = a + b
      print t
      L = []
      L[:] = t[:]
      return L
    
    def two():
      L = [x for x in range(0,7)]
      return L
    
    def three():
      print 'three'
    
    def four():
      knights = {'gallahad': 'the pure', 'robin': 'the brave'}
      for k, v in knights.iteritems():
        print k, v
    
    if __name__ == "__main__":
      a = 17
      b = 71
      print change(a, b)
      print a, b
      print one();
      print two();
      print three();
      four();
    

  2. A prime integer is any integer greater than 1 whose only divisors are itself and 1. Write a function is_prime that accepts an integer parameter $n$, and then uses a loop and the modulus operator to determine if $n$ is prime by successively testing n to see if it has a divisor other than itself or 1. Your function is_prime should return True if $n$ is prime, and False otherwise. Test is_prime by printing the numbers in the range(2, 1000) that are prime.
  3. Use the Sieve of Eratoshenes to test integers in the range(2, 1000) using the following algorithm:
    1. Create a list with elements initialized to 1 (True). List elements with prime subscripts will remain 1, all others will eventually be set to zero.
    2. Starting with list element 2, loop through the remainder of the list and set to zero every element whose subscript is a multple of the subscript for the element with value 1. For list subscript 2, all elements beyond 2 in the list that are multiples of two will be set to zero (i.e. subscripts 4, 6, 8, ...). For subscript 3, all elements beyond 3 in the list that are multiples of three will be set to zero (i.e. subscripts 6, 9, 12 ...). And so on ...

  4. Write a Python program that: (1) creates a list of 100 random values in the range 1 - 99, (2) prints the list, (3) prints the number of duplicate values in the list, (4) removes the duplicates, and then (5) prints the list.
For 5 points extra, substitute a histogram for printing the number of duplicates. Consider the output from my solution:
The list: [1, 1, 1, 4, 4, 4, 5, 6, 7, 8, 8, 9, 11, 13, 14, 14, 15, 15, 
18, 21, 22, 23, 23, 24, 25, 26, 30, 31, 32, 32, 33, 36, 36, 37, 38, 
39, 40, 40, 43, 44, 44, 45, 48, 49, 51, 57, 57, 58, 58, 58, 58, 
61, 61, 61, 62, 62, 63, 63, 64, 64, 65, 66, 67, 69, 69, 69, 69, 
70, 70, 70, 72, 72, 72, 74, 76, 76, 78, 78, 78, 78, 78, 79, 80, 80, 
82, 82, 82, 83, 84, 84, 86, 86, 88, 91, 92, 93, 94, 94, 94, 97]

There are 100 duplicates
1: * * *
4: * * *
5: *
6: *
7: *
8: * *
9: *
11: *
13: *
14: * *
15: * *
18: *
21: *
22: *
23: * *
24: *
25: *
26: *
30: *
31: *
32: * *
33: *
36: * *
37: *
38: *
39: *
40: * *
43: *
44: * *
45: *
48: *
49: *
51: *
57: * *
58: * * * *
61: * * *
62: * *
63: * *
64: * *
65: *
66: *
67: *
69: * * * *
70: * * *
72: * * *
74: *
76: * *
78: * * * * *
79: *
80: * *
82: * * *
83: *
84: * *
86: * *
88: *
91: *
92: *
93: *
94: * * *
97: *
Without dupes: [1, 4, 5, 6, 7, 8, 9, 11, 13, 14, 15, 18, 21, 22, 23, 
24, 25, 26, 30, 31, 32, 33, 36, 37, 38, 39, 40, 43, 44, 45, 48, 49, 
51, 57, 58, 61, 62, 63, 64, 65, 66, 67, 69, 70, 72, 74, 76, 78, 79, 
80, 82, 83, 84, 86, 88, 91, 92, 93, 94, 97]




next up previous
Next: About this document ...
Brian Malloy 2009-07-05