https://www.liveexamhelper.com/
support@liveexamhelper.com
+1 (315) 557-6473 4112 Thrash Trail,
Purdon, Texas-
76679
Problem 1 - Sorting a List
Write a program that asks the user to enter 10 (positive) numbers. The
program should then print the numbers in sorted order, from biggest to
smallest.
To do this, first write a function that takes a list and finds the largest
element. It then
1) deletes that element from the list and
2) returns that element.
Hint: You will need to store two variables in this function: the biggest
number you've seen so far (remember to initially set this to 0), and its
position. Then iterate over the list, and for each element, check if it's bigger
than the biggest number you've seen so far. If it is, change both variables
(remember to change BOTH)!
So in your main program, you'll have to keep calling this function (in a loop)
until the list is empty and keep printing the number that is returned.
# sorting.py
# Example solution for Lab 5, problem 1
# Aseem Kishore
# 6.189 - Intro to Python
# IAP 2008 - Class 4
L = []
for i in range(10):
a = int(raw_input("Please enter a number. "))
# DON'T do this: L[i] = a
L.append(a)
def find_largest(L):
maximum = 0
index = 0
for i in range(len(L)):
if L[i] > maximum:
maximum = L[i]
index = i
del L[index]
return maximum
# this is fine: while len(L) > 0:
for i in range(10):
print find_largest(L),
Problem 2 – Report card with GPA
Write a program where the user can enter each of his grades, after which the
program prints out a report card with GPA. Remember to ask the user how
many classes he took. Example output is below.
How many classes did you take? 4
What was the name of this class? English
What was your grade? 94
...
REPORT CARD:
English - 94
Math – 96
Science – 91
Social Studies - 88
Overall GPA – 92.25
Hints: You’ll want to use a for loop, and you’ll probably want to keep
two lists, one for names and one for grades. Remember, add to lists with
append.
# reportcard.py
# Example solution for Lab 5, problem 2
# Aseem Kishore
# 6.189 - Intro to Python
# IAP 2008 - Class 4
# Helper function that takes a list of grades and returns the GPA.
# Remember that average (mean) is the sum divided by the number of grades.
# Just like in math, you sum first, THEN divide -- order of operations.
# Don't make the mistake of dividing inside the loop!
def calculate_gpa(grades):
running_sum = 0
for grade in grades:
running_sum = running_sum + grade
return float(running_sum) / len(grades) # remember we want decimals, so
# use float(...)
# Main program code
class_names = []
class_grades = []
number_classes = int(raw_input("How many classes did you take? "))
print # this prints a blank line
# Now we're going to ask the same two questions over and over --> loop!
# Since we know how many times we're looping (number_classes), we use for.
# Note that we need a variable name for the "for", but we don't use it here.
# What does range return? A list of numbers, from 0 to number_classes.
for arbitrary_variable_name in range(number_classes):
name = raw_input("What is the name of this class? ")
grade = int(raw_input("What grade did you get? "))
class_names.append(name) # add the two things to our lists...
class_grades.append(grade)
print # blank line
# Now we'll print the report card. The report card should look like:
# class name - grade
# Over and over again --> loop! How many of these lines? number_classes
print "Report card:"
print
for class_number in range(number_classes):
print class_names[class_number], "-", class_grades[class_number]
print
print "Term GPA:", calculate_gpa(class_grades)

Python Exam (Questions with Solutions Done By Live Exam Helper Experts)

  • 1.
  • 2.
    Problem 1 -Sorting a List Write a program that asks the user to enter 10 (positive) numbers. The program should then print the numbers in sorted order, from biggest to smallest. To do this, first write a function that takes a list and finds the largest element. It then 1) deletes that element from the list and 2) returns that element. Hint: You will need to store two variables in this function: the biggest number you've seen so far (remember to initially set this to 0), and its position. Then iterate over the list, and for each element, check if it's bigger than the biggest number you've seen so far. If it is, change both variables (remember to change BOTH)! So in your main program, you'll have to keep calling this function (in a loop) until the list is empty and keep printing the number that is returned.
  • 3.
    # sorting.py # Examplesolution for Lab 5, problem 1 # Aseem Kishore # 6.189 - Intro to Python # IAP 2008 - Class 4 L = [] for i in range(10): a = int(raw_input("Please enter a number. ")) # DON'T do this: L[i] = a L.append(a) def find_largest(L): maximum = 0 index = 0
  • 4.
    for i inrange(len(L)): if L[i] > maximum: maximum = L[i] index = i del L[index] return maximum # this is fine: while len(L) > 0: for i in range(10): print find_largest(L),
  • 5.
    Problem 2 –Report card with GPA Write a program where the user can enter each of his grades, after which the program prints out a report card with GPA. Remember to ask the user how many classes he took. Example output is below. How many classes did you take? 4 What was the name of this class? English What was your grade? 94 ... REPORT CARD: English - 94 Math – 96 Science – 91 Social Studies - 88 Overall GPA – 92.25
  • 6.
    Hints: You’ll wantto use a for loop, and you’ll probably want to keep two lists, one for names and one for grades. Remember, add to lists with append. # reportcard.py # Example solution for Lab 5, problem 2 # Aseem Kishore # 6.189 - Intro to Python # IAP 2008 - Class 4 # Helper function that takes a list of grades and returns the GPA. # Remember that average (mean) is the sum divided by the number of grades. # Just like in math, you sum first, THEN divide -- order of operations. # Don't make the mistake of dividing inside the loop! def calculate_gpa(grades): running_sum = 0 for grade in grades:
  • 7.
    running_sum = running_sum+ grade return float(running_sum) / len(grades) # remember we want decimals, so # use float(...) # Main program code class_names = [] class_grades = [] number_classes = int(raw_input("How many classes did you take? ")) print # this prints a blank line # Now we're going to ask the same two questions over and over --> loop! # Since we know how many times we're looping (number_classes), we use for. # Note that we need a variable name for the "for", but we don't use it here. # What does range return? A list of numbers, from 0 to number_classes. for arbitrary_variable_name in range(number_classes): name = raw_input("What is the name of this class? ") grade = int(raw_input("What grade did you get? "))
  • 8.
    class_names.append(name) # addthe two things to our lists... class_grades.append(grade) print # blank line # Now we'll print the report card. The report card should look like: # class name - grade # Over and over again --> loop! How many of these lines? number_classes print "Report card:" print for class_number in range(number_classes): print class_names[class_number], "-", class_grades[class_number] print print "Term GPA:", calculate_gpa(class_grades)