We can loop over strings, too! The following incomplete function is a simple, but inefficient, way to reverse a string. What line of code needs to replace the questions marks for the code to work correctly?
def reverse_string(s):
    """Returns the reversal of the given string."""
    ???
    for char in s:
        result = char + result
    return result

print reverse_string("hello")

Practice More Questions From: Quiz 5a

Q:

What is the type of the parameter for a mouseclick handler?

Q:

We want to remove the element at the front of a list. For example, we want the following code to print “apple” and [“pear”, “blueberry”], respectively. What function or method call should replace the question marks? fruits = [“apple”, “pear”, “blueberry”]fruit = ???print fruit, fruits

Q:

Which of the following expressions mutate, i.e., change, list my_list?

Q:

Which of the following uses of range() will generate the list [2, 5, 8, 11, 14]|}[2, 5, 8, 11, 14]?

Q:

To correctly compute the product of a list numbers of numbers, what statement should replace the question marks? numbers = …???for n in numbers: product *= n

Q:

We can loop over strings, too! The following incomplete function is a simple, but inefficient, way to reverse a string. What line of code needs to replace the questions marks for the code to work correctly?def reverse_string(s): “””Returns the reversal of the given string.””” ??? for char in s: result = char + result return resultprint reverse_string(“hello”)

Q:

Imagine a game on a map. At the beginning, we might want to randomly assign each player a starting point. Which of the following expressions may we use in place of the question marks to correctly implement this functionality? import randomdef random_point(): “””Returns a random point on a 100×100 grid.””” return (random.randrange(100), random.randrange(100))def starting_points(players): “””Returns a list of random points, one for each player.””” points = [] for player in players: point = random_point() ??? return points

Q:

The following function is supposed to check whether the given list of numbers is in ascending order. For example, we want is_ascending([2, 6, 9, 12, 400]) to return True while is_ascending([4, 8, 2, 13]) should return False. def is_ascending(numbers): “””Returns whether the given list of numbers is in ascending order.””” for i in range(len(numbers)): if numbers[i+1] < numbers[i]: return False return TrueHowever, the function doesn’t quite work. Try it on the suggested tests to verify this for yourself. The easiest fix is to make a small change to the highlighted code. What should it be replaced with?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments