0% found this document useful (0 votes)
26 views2 pages

Tutorial Questions: Week 6: "" "Hello"

This document contains tutorial questions and problems for the COMP10001 Foundations of Computing course. It includes exercises on list comprehensions, functions, and default dictionaries. Exercises involve evaluating list comprehensions, comparing functions, finding palindromic numbers from products of digits, using Pythagoras' theorem to find hypotenuses, and using a default dictionary to find hapax legomena (words that occur only once) in text.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Tutorial Questions: Week 6: "" "Hello"

This document contains tutorial questions and problems for the COMP10001 Foundations of Computing course. It includes exercises on list comprehensions, functions, and default dictionaries. Exercises involve evaluating list comprehensions, comparing functions, finding palindromic numbers from products of digits, using Pythagoras' theorem to find hypotenuses, and using a default dictionary to find hapax legomena (words that occur only once) in text.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COMP10001 Foundations of Computing

Semester 1, 2016
Tutorial Questions: Week 6
VERSION : 810, DATE : A PRIL 7, 2016

Exercises
1. For the following list comprehensions: (a) evaluate the expression; and (b) write
the corresponding Python code which would generate an object of the same
content, without using a list comprehension (using for statements, etc.):
(a)

[float(i) for i in range(0,4)]

(b)

"".join([letter.upper() for letter in "hello"])

(c)

[i**2 for i in range(5) if i % 2 == 0]

2. What is the output of the following code:


def foo(x, y):
global a
a = 42
x,y = y,x
b = 17
c = 100
print a,b,x,y
a,b,x,y = 1,2,3,4
foo(17,4)
print a,b,x,y

3. Compare the following two functions with respect to the given function calls,
and answer the following questions. (1) Are there inputs for which the output of
the two functions will differ? (2) Are there instances where one version should
be preferred over the other?
def noletter1(wordlist, letter='z'):
for word in wordlist:
if letter in word:
return False
return True
def noletter2(wordlist, letter='z'):
noz = True
for word in wordlist:
if letter in word:
noz = False
return noz
wordlist = ['zizzer'] + ['aadvark'] * 10000000
print(noletter1(wordlist))
print(noletter2(wordlist))

Problems
1. [From the Early-Release Tutesheet] A palindromic number is a positive integer that reads the same both ways. The largest palindromic number made from
the product of two 2-digit integers is 9009 = 91 99.
Write a function palnum(num) that returns the largest palindromic number made
from the product of two num-digit numbers.

2. Write a function hypotenuse(a, b) that returns (as a float) the length of the hypotenuse of a right-angled triangle with side lengths a and b (both positive numbers). Use Pythagoras theorem a2 + b2 = c2 , and the implementation of square
root in the math library (math.sqrt).

3. What is the output of the following code:


def extremum(numlist, comp=max):
return comp(numlist)
numlist = [1, 4, 0, -1, 6]
print(extremum(numlist))
print(extremum(numlist, comp=max))
print(extremum(numlist, comp=min))

4. Using a defaultdict, write a function hapax(text) which returns an alphabeticallysorted (based on Unicode code point values) list of all words in text that occur
exactly once. Note that a word is defined simply to be a string that is surrounded by white space.

You might also like