Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Microsoft Word - Lab13.docx ENGR 102 - Lab 13 Final Exam Review In preparation for the Final Exam, you may want to review the following topics. The final exam will be comprehensive, but since the...

1 answer below »
Microsoft Word - Lab13.docx
ENGR 102 - Lab 13
Final Exam Review
In preparation for the Final Exam, you may want to review the following topics. The final exam will be
comprehensive, but since the material builds over time, the focus will be on newer topics, while topics
covered on the midterm (such as variable assignment, conditionals, and loops) will be assumed.

Here are particular topics to be sure you are familiar with for the final exam
• Lists, strings, and tuples: creating, accessing, slicing
• Dictionaries: creating, accessing
• Files: Opening, Closing, reading data from
• Importing modules and using functions from modules
• Writing functions: function calls, arguments and parameters, and scope of variables
• Handling run-time e
ors with exceptions
• Use of particular modules: numpy and matplotlib. You should know basic functions for plotting
data and how to store and manipulate vectors, matrices, and a
ays of data with numpy.
• Software design and construction, including incremental (pyramid-style) construction, top-down
and bottom-up design, writing test cases, and debugging.
• The use of comments and docstrings. You should remember to follow appropriate commenting
and naming for any code you write.

Similar to the midterm, questions will include writing code, writing short answers, and determining the
output of programs.

Please look over the midterm review.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Write down the output from the following function and driver code.

# A function to sort the given list using Gnome sort
def gnomeSort( a
, n,’\n’):
index = 0
while index < n:
XXXXXXXXXXprint(a
,index)
XXXXXXXXXXif index == 0:
XXXXXXXXXXindex = index + 1
XXXXXXXXXXif a
[index] >= a
[index - 1]:
XXXXXXXXXXindex = index + 1
XXXXXXXXXXelse:
XXXXXXXXXXa
[index], a
[index-1] = a
[index-1], a
[index]
XXXXXXXXXXindex = index - 1

return a


# Driver Code
a
= [ 34, 2, 10, -9]
n = len(a
)

a
= gnomeSort(a
, n)
print("Sorted sequence after applying Gnome Sort :\n")
for i in a
:
print(i)

Write down the output from the following function and driver code.

# Python program to calculate
# e raise to the power x

# Function to calculate value
# using sum of first n terms of
# Taylor Series
def exponential(n, x):

# initialize sum of series
sum = 1.0
for i in range(n, 0, -1):
XXXXXXXXXXsum = 1 + x * sum / i
print ("e^x =", sum)

# Driver program to test above function
n = 10
x = 1.0
exponential(n, x)

Write down the output from the following function and driver code.

# Python program to calculate
# e raise to the power x

# Function to calculate value
# using sum of first n terms of
# Taylor Series
def exponential(n, x):

# initialize sum of series
sum = 1.0
for i in range(n, 0, -1):
XXXXXXXXXXsum = 1 + x * sum / i
XXXXXXXXXXprint ("e^x =", sum)

# Driver program to test above function
n = 10
x = 1.0
exponential(n, x)

Write down the output from the following code.

num1 = 5
if num1 >= 91:
num2 = 3
else:
if num1 < 6:
XXXXXXXXXXnum2 = 4
else:
XXXXXXXXXXnum2 = 2
x = num2 * num1 + 1
print (x,x%7)

On the following page, draw the output produced by the turtle in the following Python
program. Assume that the turtle begins in the middle of the window facing right and
that the window is approximately 800 steps wide.

import turtle
sides = 6
angle = 360/sides
count = 0
turtle.forward(50)
while count < sides:
turtle.forward(100)
turtle.right(angle)
count = count + 1

Write a Python function named “simpleSum()” that:
- Reads in a string of numbers.
- Converts the string to a list of integer numbers.
- If the first number is odd, compute the sum of all the numbers.
- Otherwise, if the first number is even, compute the product of all the numbers.
- Returns the value that you computed above.
Example: if the string is ‘1 2 4 5’ it would return 12. If the string is ‘2 4 5 6’ it would return 240.

What is printed from the code below?

def segregate(a
):
res = ([x for x in a
if x==0] + [x for x in a
if x==1])
print(res)

# Driver program
if __name__ == "__main__":
a
= [0, 1, 0, 1, 0, 0, 1, 1, 1, 0]
print(segregate(a
))

Identify and co
ect the e
ors in the following Python code:

# Python program to print odd Numbers in a List
list of numbers
list1 = [10, 21, 4, 45, 66, 93]

# using while loop
while(num < len(list1))
# checking condition
if num % 2 !== 0:
print(list1[num], end = " ")

# increment num
num += 1

What will be displayed when you run the code below?

print([x*0.5 for x in range(1,15,2) if x%3 == 0])

The code below is written to handle the stock of fruits in a store.

fruits = ['apple', 'pear', 'orange', 'pineapple']
n
Fruits = [10, 6, 8, 1]
fruitDict = {}
for index, fruit in enumerate(fruits):
fruitDict[fruit] = n
Fruits[index]

Write code to find how many apples there are in the store
Write code to update the dictionary when two oranges are sold
Take two lists, say for example these two:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

Write a program that returns a list that contains only the elements that are common between the lists
(without duplicates). Make sure your program works on two lists of different sizes.

Identify and co
ect the e
ors in the following code:

# Python program to find largest, smallest,
# second largest and second smallest in a
# list with complexity O(n)
def Range(list)
largest = list1[0]
largest2 = list1[0]
lowest = list1[0]

for item in list1():
XXXXXXXXXXif item > largest:
XXXXXXXXXXlargest = item
XXXXXXXXXXelif largest2 != largest and largest2 < item:
XXXXXXXXXXlargest2 = item
XXXXXXXXXXelif item < lowest:
XXXXXXXXXXlowest = item
XXXXXXXXXXelse lowest2 != lowest and lowest2 > item:
XXXXXXXXXXlowest2 = item

print("Largest element is:", largest)
print("Smallest element is:", lowest)
print("Second Largest element is:", largest2)
print("Second Smallest element is:", lowest2)

Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both
included) and the values are square of keys

Write a Python program to get the number of occu
ences of a specified element in an a
ay.

Write a Python program to convert an a
ay to an ordinary list with the same items.

Write a Python function to multiply all the numbers in a list.

Write a Python program that accepts a hyphen-separated sequence of words as input and prints the
words in a hyphen-separated sequence after sorting them alphabetically.

Write a Python program to read an
Answered Same Day Nov 17, 2021

Solution

Sudipta answered on Nov 18 2021
151 Votes
Ans .) Output:
[34, 2, 10, -9] 0
[2, 34, 10, -9] 0
[2, 34, 10, -9] 2
[2, 10, 34, -9] 1
[2, 10, 34, -9] 2
[2, 10, 34, -9] 3
[2, 10, -9, 34] 2
[2, -9, 10, 34] 1
[-9, 2, 10, 34] 0
[-9, 2, 10, 34] 2
[-9, 2, 10, 34] 3
Sorted sequence after applying Gnome Sort :
-9
2
10
34
Ans.) Output : e^x = 2.7182818011463845
Ans.) Ouput: e^x = 1.1
e^x = 1.1222222222222222
e^x = 1.1402777777777777
e^x = 1.1628968253968255
e^x = 1.1938161375661376
e^x = 1.2387632275132274
e^x = 1.3096908068783069
e^x = 1.436563602292769
e^x = 1.7182818011463845
e^x = 2.7182818011463845
Ans.) Output: 21 0
Ans.) Output:
Ans.)#if first value is odd then print sum else print product
s=input()
l=s.split(" ")
x=int(l[0])
if(x%2==1):
sm=0
for i in l:
sm+=int(i)
print(sm)
else:
pr=1
for i in l:
pr*=int(i)
print(pr)
Ans.) Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
Ans.) E
ors: ‘num’ is not initialized and if condition is wrong as it prints values at odd places and not odd numbers in the given list. Also colon after while is missing. Below is co
ect code :
list1 = [10, 21, 4, 45, 66, 93]
num=0
# using while loop
while(num < len(list1)):
# checking condition
if list1[num] % 2 != 0:
print(list1[num], end = " ")
num += 1
Ans.) Output: [1.5, 4.5]
Ans.)
fruits = ['apple', 'pear', 'orange', 'pineapple']
n
Fruits = [10, 6, 8, 1]
fruitDict = {}
for index, fruit in enumerate(fruits):
fruitDict[fruit] = n
Fruits[index]
print("Number of apples in store: ",fruitDict['apple'])
fruitDict['orange']-=2
print("When 2 oranges are sold: ",fruitDict['orange'])
Ans.)
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here