Skip to main content

Section 9.18 Write Code Questions

Checkpoint 9.18.1.

Write a function add_to_new_list that takes in a list of strings, lst, as a parameter and creates a new list with the length of lst and the first element of lst three times. For example, add_to_new_list(["1","2","3"]) would return [3, '111'].
Solution.
Write a function add_to_new_list that takes in a list of strings, lst, as a parameter and creates a new list with the length of lst and the first element of lst three times. For example, add_to_new_list(["1","2","3"]) would return [3, '111'].
def add_to_new_list(lst):
    new_list = []
    new_list.append(len(lst))
    new_list.append(lst[0] * 3)
    return new_list

=====

from unittest.gui import TestCaseGui

class myTests(TestCaseGui):

    def testOne(self):
        self.assertEqual(add_to_new_list(['1','2','3']), [3, '111'], "add_to_new_list(['1','2','3'])")
        self.assertEqual(add_to_new_list(['0','0','0','0']), [4, '000'], "add_to_new_list(['0','0','0','0'])")
        self.assertEqual(add_to_new_list(['10.2','0.0','100','-2']), [4, '10.210.210.2'], "add_to_new_list(['10.2','0.0','100','-2'])")


myTests().main()
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=lists_writecode2 data-questionlist='list_writeItemsq_v2_ac, list_writeItemsq_v2_pp' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: list_writeItemsq_v2_ac, list_writeItemsq_v2_pp</p> </div> </div>

Checkpoint 9.18.2.

Write a function average that takes in a list of integers, aList, as a parameter and returns the average of all of the integers, rounded to one decimal place. For example, average([99, 100, 74, 63, 100, 100]) would return 89.3.
Solution.
Write a function average that takes in a list of integers, aList, as a parameter and returns the average of all of the integers, rounded to one decimal place. For example, average([99, 100, 74, 63, 100, 100]) would return 89.33.
def average(aList):
    sum = 0
    for num in aList:
        sum += num
    avg = round(sum / len(aList),2)
    return avg

=====

from unittest.gui import TestCaseGui

class myTests(TestCaseGui):

    def testOne(self):
        self.assertAlmostEqual(average([99, 100, 74, 63, 100, 100]), 89.3, 1, "average([99, 100, 74, 63, 100, 100])")
        self.assertAlmostEqual(average([0, 2, -3, 1.2, 2000]), 400.0, 1, "average([0, 2, -3, 1.2, 2000])")
        self.assertAlmostEqual(average([-2]), -2.0, 1, "average([-2])")


myTests().main()
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=lists_writecode4 data-questionlist='list_write23_ac, list_write23_pp' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: list_write23_ac, list_write23_pp</p> </div> </div>

Checkpoint 9.18.3.

Write a function capitalize that takes in a list of lists of strings, lst, and makes the first letter of each element capitalized and adds it to a new list and returns that new list. For example, capitalize([["hi"],["hello", "hey"]]) would return ['Hi', 'Hello', 'Hey'].
Solution.
Write a function capitalize that takes in a list of lists of strings, lst, and makes the first letter of each element capitalized and adds it to a new list and returns that new list. For example, capitalize([["hi"],["hello", "hey"]]) would return ['Hi', 'Hello', 'Hey'].
def capitalize(lst):
    new_list = []
    for i in lst:
        for j in i:
            new_list.append(j.capitalize())
    return new_list

=====

from unittest.gui import TestCaseGui

class myTests(TestCaseGui):

    def testOne(self):
        self.assertEqual(capitalize([['hi'],['hello', 'hey']]), ['Hi', 'Hello', 'Hey'], "capitalize([['hi'],['hello', 'hey']])")
        self.assertEqual(capitalize([['HI'],['HELLO', 'HEY']]), ['Hi', 'Hello', 'Hey'], "capitalize([['HI'],['HELLO', 'HEY']])")
        self.assertEqual(capitalize([['go', 'blue'],['python', 'IS', 'The', 'Best']]), ['Go', 'Blue', 'Python', 'Is', 'The', 'Best'], "capitalize([['go', 'blue'],['python', 'IS', 'The', 'Best']])")

myTests().main()
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=lists_writecode6 data-questionlist='list_write5_ac, list_write5_pp' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: list_write5_ac, list_write5_pp</p> </div> </div>

Checkpoint 9.18.4.

Write a function chop that takes a list, lst, and modifies it, removing the first and last elements. For example, chop([1,2,3,4,5] should return [2,3,4].
Solution.
Write a function chop that takes a list, lst, and modifies it, removing the first and last elements. For example, chop([1,2,3,4,5] should return [2,3,4].
def chop(lst):
    lst.pop(0)
    lst.pop(-1)
    return(lst)

=====

from unittest.gui import TestCaseGui

class myTests(TestCaseGui):

    def testOne(self):
        self.assertEqual(chop([1,2,3,4,5]),[2,3,4],"chop([1,2,3,4,5])")
        self.assertEqual(chop([1,3,5,7,9,10]),[3,5,7,9],"chop([1,3,5,7,9,10])")
        self.assertEqual(chop([2,9]),[],"chop([2,9])")

myTests().main()
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=lists_writecode8 data-questionlist='list_writeReverse_ac, list_writeReverse_pp' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: list_writeReverse_ac, list_writeReverse_pp</p> </div> </div>

Checkpoint 9.18.5.

Write a function sumUntilEven that takes in one parameter, lst, and returns the sum of all the elements in the lst up to but not including the first even number. For example, sumUntilEven([1,2,3,4,5] should return 1 and sumUntilEven([1,3,5,7,9] should return 25.
Solution.
Write a function called sumUntilEven that takes in one parameter, lst, and returns the sum of all the elements in the lst up to but not including the first even number. For example, sumUntilEven([1,2,3,4,5] should return 1 and sumUntilEven([1,3,5,7,9] should return 25.
def sumUntilEven(lst):
    total = 0
    element = 0
    while element < len(lst) and lst[element] % 2 != 0:
        total = total + lst[element]
        element += 1
    return total

====
from unittest.gui import TestCaseGui

class myTests(TestCaseGui):

    def testOne(self):
        self.assertEqual(sumUntilEven([1,2,3,4,5]),1,"sumUntilEven([1,2,3,4,5])")
        self.assertEqual(sumUntilEven([1,3,5,7,9]),25,"sumUntilEven([1,3,5,7,9])")
        self.assertEqual(sumUntilEven([2,4,6,7,9]),0,"sumUntilEven([2,4,6,7,9])")

myTests().main()
<div class="runestone sqcontainer %(optclass)s"> <div data-component="selectquestion" id=lists_writecode10 data-questionlist='list_sortByLen_ac, list_sortByLen_pp' data-toggleoptions="toggle, lock" data-togglelabels="togglelabels:" data-limit-basecourse=true> <p>Loading a dynamic question ...<br/>Selecting from: list_sortByLen_ac, list_sortByLen_pp</p> </div> </div>

Checkpoint 9.18.6.

Write a function combine(names, ages) that takes in two lists, names and ages and returns a list of strings in the format "Name: name, age: age". For example, combine(["Claire", "Jennifer"],[23, 19]) would return ["Name: Claire, age: 23", "Name: Jennfier, age: 19"].