Choose the Right Algorithm

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

We want to create an algorithm called swapValues. Given two variables x and y the result should have the values of x and y swapped. Which of the following algorithms correctly swaps the values of x and y?

temp ← x x ← y y ← temp

We want to create an algorithm called reverseList that reverses the elements in a given list. Algorithm 1 PROCEDURE reverseList(list) { index = LENGTH (list) REPEAT UNTIL index = 0 { item ← list[index] REMOVE (list, index) INSERT(list, 1, item) index ← index - 1 } RETURN list } Algorithm 2 PROCEDURE reverseList(list) { new_list = [] index = LENGTH (list) REPEAT UNTIL index = 0 { APPEND (new_list, list[index]) index ← index - 1 } RETURN new_list } Which of the following best describes the behavior of the two algorithms?

Algorithm 2 returns the correct list for all inputs, but algorithm 1 does not.

Assume we have an algorithm used to simulate the results of flipping a coin n times. Two variables used for the algorithm are heads_counter, storing the number of heads we've had, and flip_counter, storing the number of flips we've made. We want to add on to the algorithm so that at the end we display whether there were an odd or even number of tails. Here are two algorithms that display whether we have an even or odd number of tails: Algorithm A: Set the value of num_tails to (flip_counter - heads_counter). If the value of num_tails MOD 2 is equal to 0, DISPLAY ("Even"), otherwise DISPLAY ("ODD"). Algorithm B: Set the value of even_flips to (n MOD 2) = 0 and set the value of even_heads to (heads_counter MOD 2) = 0. If even_flips is equal to even_heads then DISPLAY ("EVEN"), otherwise DISPLAY ("ODD").

Both Algorithm A and Algorithm B always calculate the correct answer.

A chef writing up her famed recipe for beef stew realizes she has switched parsley and oregano everywhere in the recipe. The chef wants to change all occurrences of "parsley" to "oregano" and all occurrences of "oregano" to "parsley." The chef will use the fact that the word "thyme" does not appear anywhere in the recipe. Which of the following algorithms can be used to fix the chef's recipe?

First, change all occurrences of "parsley" to "thyme." Then, change all occurrences of "oregano" to "parsley." Last, change all occurrences of "thyme" to "oregano."

A pizza parlor has an inventory list for their toppings. Which of the following algorithms is LEAST likely to use iteration?

Getting the first topping in the inventory list

Which of the following algorithms requires iteration but NOT selection?

Given a list display the sum of the values in that list

Given the following algorithms, which one uses iteration?

Given a list integers, display the sum of the list

Given the following algorithms, which one uses selection?

Given two numbers display the larger of the two.

A pizza parlor has an inventory list for their toppings. Which of the following algorithms does NOT require selection? I. Reversing the order of the topping list. II. Getting the first topping in the list III. Getting a list of all the vegetarian toppings. IV. Getting a list of all the meat toppings.

I and II

Given the following algorithms, which of the algorithms require both selection and iteration? I. Given a basket of produce, get the number of pieces of produce in the basket. II. Given a basket of produce, remove all produce of type vegetable. III. Given a basket of produce remove all produce of type fruit. IV. Given an apple and a carrot, return the one that has the type of vegetable.

II and III

Procedures A and B are meant to get the minimum value of a list of one or more integers. We have the two procedures below Procedure A PROCEDURE getMin(list) { min ← list[1] FOR EACH item IN list { IF (item < min) { min ← item } } RETURN min } Procedure B PROCEDURE getMin(list) { min ← list[1] counter ← 1 REPEAT LENGTH (list) TIMES: { counter ← counter + 1 item ← list[counter] IF (item < min) { min ← item } } RETURN min }

Procedure A returns the correct minimum value, but Procedure B does not.

Program 1 and 2 below are intended to calculate the average of the integers in a list, number_list. Program 1 sum ← 0 FOR EACH number in number_list { sum ← sum + number } DISPLAY (sum / LENGTH (number_list)) Program 2 counter ← 1 sum ← 0 FOR EACH number in number_list { sum ← sum + number counter ← counter + 1 } DISPLAY (sum / counter)

Program 1 displays the correct average, but Program 2 does not

You are given two lists. One containing large states, large_states and the other containing states that are bordered by an ocean, ocean_states. You want to compile a list, new_list, in alphabetical order, that contains states that are both large and bordered by an ocean, that start with a vowel. For example if large_states contains ["Oregon", "California", "Alaska", "Oklahoma", "Texas"] and ocean_states contains ["Alaska", "Rhode Island", "California", "Texas", "Oregon"] then new_list will contain ["Alaska", "Oregon"] The following procedures are available to create to create new_list sort (list): Sorts list in alphabetical order and returns the resulting list. intersection (list1, list2) Creates a new list by iterating over list1 and adding only the entries from list1 that also exist in list2. The resulting list is returned starts_with_vowel (list) Returns a new list with only the entries from list that start with a vowel. Which of the following code segments will correctly create new_list?

new_list ← intersection (large_states, ocean_states) new_list ← sort (new_list) new_list ← starts_with_vowel (new_list)


संबंधित स्टडी सेट्स

Vocabulary Workshop Level G Unit 4 (Definition, Synonym, antonym)

View Set

test and measurements study guide

View Set

Financial Accounting Module 6 - Analyzing Financial Statements

View Set

Chapter 1: ENV Problems, Their Causes, and Sustainability

View Set