Looking for Plagiarism-Free Answers for Your US, UK, Singapore, New Zealand, and Ireland College/University Assignments?
Talk to an Expert| Category | Assignment | Subject | Computer Science |
|---|---|---|---|
| University | University of Canterbury | Module Title | COSC121 Introduction to Computer Programming |
Fill in the blank spaces in the following statements. Most (but not all) spaces require just a single character, number or word. If the answer is a Python string, write it enclosed in single or double quote characters, e.g. as 'My answer'.
Each blank space is worth 1 mark.
a) 10-32 evaluates to
b) 103 evaluates to
c) 112 evaluates to
d) 32.0 evaluates to
e) {1}{2}".format('a', 'b', 'c') evaluates to
f) range (1,5) evaluates to
g) After the assignment x [4,2,5,8,1,71
x[0] has the value
,x[4] has the value
value
x[3:4] has the value
x-3) has the ,x[-4:-2] has the value
and x[1:2] 3 has the value
h) "abcde"[1] evaluates to
i) After the assignment d = {"b": 4, "d": 2, "x":1} d["d") has the value
j) 'is' in 'wineman' evaluates to
k) 5 6 evaluates to
1) 56 or 4 < 6 evaluates to
m) not (not a<-b or not b<=c) can be most simply written as
Write the output from each of the following programs in the underlined space provided, using ''to indicate a space character. If there are multiple lines of output, you should still write your answer in the single-line space provided, using '\n' to indicate a line break. You don't need to include the final 'n' at the end of the output.
For example, the output from the program
print "Hi", "there"
print "Wendy"
should be written as
Hi there \nWendy
a) [2 marks]
a
= 1
b = 2
a = a + b
b = 3
print a, b
Output:
b) [2 marks]
"first"
b = a
a = "second" print a, b
Output:
c) [2 marks]
a = 1
b = 2
c = (a, b)
a = 3
print c
Output:
d) [3 marks]
S="12" t = "3"
X = len (s) m = (s+t) *x
print m
Output:
e) [4 marks]
dates [1962, 1963, 1969, 1962, 2012, 2011, 2012] m = 1965
for i in dates [1:5]:
if i < m:
m + 1
print "{0:05d}m".format (m),
Output:
f) [5 marks]
def fuddle (x):
result = []
for i in x:
if not x.endswith (i): result.append(i)
return result
cl= "cheeses"
c2 =
c1.upper ()
x = len(c1)/2
pl
c2 [1:x]
p2
c1 [x:]
print fuddle (pl), fuddle (p2)
Output:
f) [5 marks]
number = 6
while number != 1: print number,
2 == 0:
number / 2
if number
else:
number
number *
number * 3 + 1
print number
Output:
The file "temperature.txt" contains the following lines (each line begins with "R" and ends with "H"):
Reefton Ews, 20120812,0400,6.9,1,6.5,1,6.3,1,6.8,92,1,HI Reefton Ews, 20120812,0500,7.0,1,6.6,1,6.3,1,6.8,93,1,HI Reefton Ews, 20120812,0600,7.1,1,6.9, 1, 6.5, 1, 7.0,93,1,H Reefton Ews, 20120812, 0700,7.1,1,6.9,1,6.6,1,7.0,94,1,H Reefton Ews, 20120812,0800,7.3,1,7.0,1,6.7,1,7.2,94,1,H Reefton Ews, 20120812,0900,7.6,1,7.1,1,6.8,1,7.4,94,1,HI Reefton Ews, 20120812,1000,8.2,1,7.5,1,7.6,1,7.8,94,1,HI Reefton Ews, 20120812,1100,8.5,1,8.1,1,8.6,1,8.3,92,1,H
Write the contents of the file "output.txt" after running the following program. You can use either \n
or start a new line for multiple lines. Use ''to indicate a space character.
infile open ("temperature.txt")
infile.readline () infile.readline ()
outfile = open("output.txt", "w") for line in infile:
x = line.split(",") [2]
y = int(x[:2])
outfile.write("{0}\n".format(y))
infile.close()
outfile.close()
In each part of this question you are given a doctest specification for a Python function together with an incomplete implementation. You should complete the function.
Note: if you cannot solve the question within the spaces provided, but think an alternative solution is appropriate, you can use the pages at the back of the exam booklet for your solution. If you do so, write in the space provided "See solution at back", and clearly number the answers.
a) [3 marks]
def not too big (nums, limit):
'''Given a list of numbers and a limit value,
return True if and only if all the list elements are smaller than the limit
>>> not_too_big ([1,7], 8)
True
>>> not too_big ([13,8], 9) False
>>> not too big ([-2, -7, -1], 0) True
>>> not too_big ([-4, 2, 4], 4) False
b) [4 marks]
def is_palindrome (s) :
Return True if the string s is a palindrome, otherwise return False.
A string is a palindrome if it is the same
ignoring
switching between lower and upper case when read backwards.
>>> is_palindrome ("ABBa")
True
>>> is_palindrome ("SOS") True
>>> is_palindrome ("SOSO") False
c) [ 5 marks]
def partial_sums (values):
'''Return a list containing the partial sums of the given integer list.
partial_sums ([x[0], x[1], ..., x[n]]) returns
[x[0], x[0]+x[1], x[0]+x[1]+x[2], ...,x[0]+x[1]+x[2]+...+x[n]].
The first element of the resulting list is just the first
element of the given list,
the second is the sum of the first two,
the third is the sum of the first three,
and so on, until the last element which is the sum of
all elements of the given list.
>>> partial_sums ([3, 1, 4, 1, 5])
[3, 4, 8, 9, 14]
>>> partial_sums ([0, 1, 2, 3, 4]) [0, 1, 3, 6, 10]
>>> partial_sums ([7, 7])
[7, 14]
>>> partial_sums ([])
[]
d) [7 marks]
def is_up_down (values):
'''Return True if values is an up-down-sequence, otherwise return False.
A sequence [x[0], x[1], x[2], ..., x[n]] is an up-down-sequence if there is an index k such that
x[0] <= x[1] <= x[2] <= ... <= x[k-1] <= x[k] and
x[k] >= x[k+1] >= x[k+2] >=
་ ་ ་
>= x[n-1] >= x[n] hold.
That is, the first part of the sequence is increasing and the second part is decreasing.
Either part may be empty, so any increasing sequence and any Decreasing sequence is an up-down-sequence.
>>> is_up_down ([2, 5, 5, 7, 9, 9, 8])
True
>>> is_up_down ([2, 5, 5, 7, 9, 8, 9]) False
>>> is_up_down ([9, 8])
True
>>> is_up_down ([13])
True
A dictionary is being used to store the names of mountains and their heights, for example:
heights "Mount Everest": 8848, "Mount Cook": 3754, "K2": 8611, "Ben Nevis": 1344, "Hekla": 1488)
In the following subquestions the behavior of functions is specified by doctests. Complete the functions.
a) [4 marks]
def mountain_height (heights, mountain): '''Return the height of the mountain.
If the mountain isn't in the dictionary, the height returned should be -1.
The search should ignore any spaces before or after the name. >>> mountain_height({'K2':8611, 'Hekla':1488), "K2")
8611
>>> mountain_height ({'K2':8611, 'Hekla':1488), "Hekla ") 1488
>>> mountain_height({'K2':8611, 'Hekla':1488), "Port Hills") -1
b) [4 marks].
def best_heights (heights, min):
'''Returns a list of the names of all mountains in the dictionary with heights greater than min.
>>> best_heights ({'K2':8611, 'Hekla':1488), 1488) ['K2']
>>> best_heights ({'K2':8611, 'Hekla ':1488), 9000)
In each part of this question you are given a statement that calls the function show_date shown below. The function show_date then calls the function month_name. For each question part, show the output produced as a result of the statement. Use ''to indicate a space character.
def month_name (month_number):
months "Jan", "Feb", "Mar", "Apr", "May", "Jun",
try:
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
if month_number > 12:
else:
raise ValueError ("Not a month")
result months [month_number]
except IndexError:
result "___"
return result
def show_date (d, m, y):
try:
text_version = month_name (m) except ValueError, e:
else:
print "Issue with month: ", e
print d, text_version, y
Buy Answer of COSC121 Introduction to Computer Programming Assignment
Chat With Experts & Get AssignmentStruggling with your COSC121 Introduction to Computer Programming Assignment at University of Canterbury? Get reliable and professional Programming Assignment Help and Assignment Help in New Zealand tailored specially for NZ students. We provide complete assignment answers and detailed solutions written by subject experts, ensuring they are 100% AI-free and plagiarism-free according to your academy requirements. Our content is carefully researched, properly structured, and delivered on time to help you achieve excellent grades. Trust our expert academic support to submit high-quality work with confidence and meet your assessment standards successfully.
Hire Assignment Helper Today!
Let's Book Your Work with Our Expert and Get High-Quality Content