43.4k views
1 vote
8.5 Edhesive Code Practice

Use the following initializer list to create an array (this is also in your programming environment):
twainQuotes = ["I have never let my schooling interfere with my education.",
"Get your facts first, and then you can distort them as much as you please.",
"If you tell the truth, you don't have to remember anything.",
"The secret of getting ahead is getting started.",
"Age is an issue of mind over matter. If you don't mind, it doesn't matter. "]
Print this array, then use functions to sort the quotes. Then, print the quotes again.
Next, insert the quote "Courage is resistance to fear, mastery of fear, not absence of fear." at the correct spot alphabetically, and print the quotes again.
Hint: Your code should print the array after each modification listed in the instructions

2 Answers

3 votes

Answer:

Step-by-step explanation:

twainQuotes = ["I have never let my schooling interfere with my education.",

"Get your facts first, and then you can distort them as much as you please.",

"If you tell the truth, you don't have to remember anything.",

"The secret of getting ahead is getting started.",

"Age is an issue of mind over matter. If you don't mind, it doesn't matter. "]

print(twainQuotes)

twainQuotes.sort()

print(twainQuotes)

twainQuotes.append("Courage is resistance to fear, mastery of fear, not absence of fear.")

twainQuotes.sort()

print(twainQuotes)

twainQuotes.pop(0)

print(twainQuotes)

User Kornelia
by
7.7k points
0 votes

Step-by-step explanation:

twainQuotes = ["I have never let my schooling interfere with my education.",

"Get your facts first, and then you can distort them as much as you please.",

"If you tell the truth, you don't have to remember anything.",

"The secret of getting ahead is getting started.",

"Age is an issue of mind over matter. If you don't mind, it doesn't matter. "]

# Print the original array

print("Original array:")

print(twainQuotes)

# Define a function to sort the array alphabetically

def sortArray(array):

array.sort()

return array

# Sort the array and print it

twainQuotes = sortArray(twainQuotes)

print("Sorted array:")

print(twainQuotes)

# Define a function to insert a new quote in the correct position alphabetically

def insertQuote(array, quote):

array.append(quote)

array = sortArray(array)

return array

# Insert a new quote and print the array

twainQuotes = insertQuote(twainQuotes, "Courage is resistance to fear, mastery of fear, not absence of fear.")

print("Array with new quote:")

print(twainQuotes)

User Damian Nadales
by
7.7k points