CMU 15-112: Fundamentals of Programming and Computer Science
Quiz2a
Quiz2 Version A
See the quiz2 frontmatter.
PART 1 (10 min.)
CT1 (20 points):
Indicate what this code prints.
def ct1(x, y): for i in range(x): for j in range(i, y): if (i + j) % 3 == 0: print(j) if i < 2: print(112) if (i + j > 4): break ct1(3, 5)
CT2 (20 points):
Indicate what this code prints.
def ct2(a, b, c): n = 0 while n < 1234: print(n) n += c n *= a b, c = c, b return n print(ct2(10, 7, 2))
RC1 (10 pts):
Find an argument for rc1 that makes it return TRUE.
def rc1(x): if not isinstance(x, int): return False elif (x < 1000) or (5000 < x): return False t = 0 while x > 0: t *= 100 a = x % 10 b = (x % 100) - a x = x // 100 t += 10*a + b//10 return t == 2021
BonusCT1 [+3 points]
This is BONUS. You will not lose points for not answering or incorrectly answering this question. Indicate what this code prints.
def bonusCt1(n): (a,b,c) = (0, 1000, 100) while (c < 1000): for x in range(a, b, c): (a,b,c) = (a+1, b-1, c+50) return a-n print(bonusCt1(4))
PART 2 (10 min.)
Free Response: nthSnarfPrime(n) [50 points]
Write the function nthSnarfPrime(n) which returns the nth positive integer which meets the definition of a Snarf Prime (a coined term). A number is a Snarf Prime if:
- the number is a positive integer with at least two digits
- the number is prime
- no even digit touches another even digit
- no odd digit touches another odd digit
For example, 41, 83, 761, and 983 are all Snarf Primes, but the following numbers are NOT Snarf Primes:
- 7 # does not have 2 digits
- 51 # not prime
- 31 # an odd digit touches another odd digit
- -41 # not positive
Hint: The smallest Snarf Prime is 23
Hint: You must write isPrime (or fasterIsPrime) here.
def testNthSnarfPrime(): print('Testing nthSnarfPrime()...', end='') assert(nthSnarfPrime(0) == 23) assert(nthSnarfPrime(1) == 29) assert(nthSnarfPrime(5) == 61) assert(nthSnarfPrime(9) == 101) print('Passed!')