test2f11short

test2f11short - CompSci 6 Test 2 Fall 2011 PROBLEM 1 : (...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CompSci 6 Test 2 Fall 2011 PROBLEM 1 : ( What is the output? (14 points) ) What is the output of the following code segment? Write the output below. Note that there is only output for the print statements. When printing a set, the elements in the set can be printed in any order. spices = [’sage’, ’cumin’, ’basil’, ’sage’, ’cloves’, ’sage’, ’salt’, ’cumin’] a = set(spices) b = list(a) b.sort(reverse=True) print b b.pop() b.pop() print b lista = [’sage’, ’salt’, ’cumin’] listb = [’basil’, ’salt’, ’cloves’, ’basil’] seta = set(lista) setb = set(listb) print seta | setb print seta & setb print setb - seta dict = {’flower’:[’red’, ’blue’], ’tree’:[’brown’], ’frog’:[’red’, ’green’, ’brown’]} print dict.keys() colors = set() for w in dict: item = dict.get(w) colors.add(item[0]) print colors OUTPUT: PROBLEM 2 : ( Retake: What is the output? (8 points) ) What is the output of the following code segment? Write the output below. Note that there is only output for the print statements. 1 phrase = "better letters" phrase = re.sub("t+", ’s’, phrase) print phrase phrase = "Who cares about the most wins?" phrase = re.sub(r"c[\w]*.[\w]*t",’is’, phrase) print phrase phrase = "Coach K has the most wins" p = re.compile(r"^[A-Z][a-z]*") items = p.findall(phrase) print items phrase = "45ab 3c 543fg 83jkp 39abc a732b 93k " p = re.compile(r"[0-9]3[a-z]*") items = p.findall(phrase) print items OUTPUT: PROBLEM 3 : ( Recursion: see recursion (6 points) ) What is the output of the following code segment? def mystery(number): if number > 10: return 1 + 2 * mystery(number/2) elif number > 3: return 3 + mystery(number-2) else: return 4 print mystery(1) print mystery(6) print mystery(26) OUTPUT: 2 PROBLEM 4 : ( Top-level Domain (12 points) ) A. (6 pts) The...
View Full Document

This document was uploaded on 01/17/2012.

Page1 / 5

test2f11short - CompSci 6 Test 2 Fall 2011 PROBLEM 1 : (...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online