Python Programming: An Introduction to Computer Science, 3rd Ed.
3rd Edition
ISBN: 9781590282755
Author: John Zelle
Publisher: Franklin, Beedle & Associates
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 11, Problem 5PE
Program Plan Intro
Python functions
Program plan:
- Define the function “count(myList,x)”,
- Returns the count of the given “x” value.
- Define the function “isin(myList,x)”,
- Returns “True”, if the value “x” is present in the list.
- Define the function “index(myList,x)”,
- Returns the index or position of the given “x” value.
- Define the function “reverse(myList)”,
- Return a new list consists of the elements which are reversed.
- Define the function “sort(myList)”,
- Return a new list consists of the elements which are sorted.
- Define the function “main()”,
- Create a list “myList” with the elements.
- Assign the value of “x”.
- Print the value return from “count(myList,x)”.
- Print the value return from “isin(myList,x)”.
- Print the value return from “index(myList,x)”.
- Print the value return from “reverse(myList)”.
- Print the value return from “sort(myList)”.
- Call the “main()” function.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Most languages do not have the flexible built-in list (array) operationsthat Python has. Write an algorithm for each of the following Pythonoperations and test your algorithm by writing it up in a suitable function. For example, as a function, reverse (myList ) should do the same asmy List . reverse ( ) . Obviously, you are not allowed to use the corresponding Python method to implement your function.a) count (my List , x) (like my List . count (x) )b) isin(myList , x) (like x in myList))c) index (my List , x) (like my List . index (x) )d) reverse (myList) (like myList . reverse ())e) sort (myList) (like my List . sort ())
Assume that L is a list of Boolean values, True and False. Write a program in python with a function longestFalse(L) which returns a tuple (start, end) representing the start and end indices of the longest run of False values in L. If there is a tie, then return the first such run. For example, if L is
False False True False False False False True True False False
0 1 2 3 4 5 6 7 8 9 10
then the function would return (3, 6), since the longest run of False is from 3 to 6.
Write a recursive function that finds the minimum value in an ArrayList.
Your function signature should be
public static int findMinimum(ArrayList<Integer>)
One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”.
For example, if you have the ArrayList
[1, 3, 2, 567, 23, 45, 9],
the minimum value in this ArrayList is either 9 or the minimum value in [1, 3, 2, 567, 23, 45]
================================================
import java.util.*;
public class RecursiveMin{public static void main(String[] args){Scanner input = new Scanner(System.in);ArrayList<Integer> numbers = new ArrayList<Integer>();while (true){System.out.println("Please enter numbers. Enter -1 to quit: ");int number = input.nextInt();if (number == -1){break;}else {numbers.add(number);}}
int minimum = findMinimum(numbers);System.out.println("Minimum: " + minimum);}public static int…
Chapter 11 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
Ch. 11 - Prob. 1TFCh. 11 - Prob. 2TFCh. 11 - Prob. 3TFCh. 11 - Prob. 4TFCh. 11 - Prob. 5TFCh. 11 - Prob. 6TFCh. 11 - Prob. 7TFCh. 11 - Prob. 8TFCh. 11 - Prob. 9TFCh. 11 - Prob. 1MC
Ch. 11 - Prob. 2MCCh. 11 - Prob. 3MCCh. 11 - Prob. 4MCCh. 11 - Prob. 5MCCh. 11 - Prob. 6MCCh. 11 - Prob. 7MCCh. 11 - Prob. 8MCCh. 11 - Prob. 9MCCh. 11 - Prob. 10MCCh. 11 - Prob. 1DCh. 11 - Prob. 2DCh. 11 - Prob. 1PECh. 11 - Prob. 2PECh. 11 - Prob. 3PECh. 11 - Prob. 5PECh. 11 - Prob. 6PECh. 11 - Prob. 7PECh. 11 - Prob. 8PECh. 11 - Prob. 9PECh. 11 - Prob. 10PECh. 11 - Prob. 11PECh. 11 - Prob. 12PECh. 11 - Prob. 15PECh. 11 - Prob. 16PECh. 11 - Prob. 18PECh. 11 - Prob. 19PE
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Write a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum value in [1, 3, 2, 567, 23, 45] Hint:The trick is to remove the last element each time to make the ArrayList a little shorter. import java.util.*; public class RecursiveMin{public static void main(String[] args){Scanner input = new Scanner(System.in);ArrayList<Integer> numbers = new ArrayList<Integer>();while (true){System.out.println("Please enter numbers. Enter -1 to quit: ");int number = input.nextInt();if (number == -1){break;}else {numbers.add(number);}} int minimum =…arrow_forwardThe function interleave_lists in python takes two parameters, L1 and L2, both lists. Notice that the lists may have different lengths. The function accumulates a new list by appending alternating items from L1 and L2 until one list has been exhausted. The remaining items from the other list are then appended to the end of the new list, and the new list is returned. For example, if L1 = ["hop", "skip", "jump", "rest"] and L2 = ["up", "down"], then the function would return the list: ["hop", "up", "skip", "down", "jump", "rest"]. HINT: Python has a built-in function min() which is helpful here. Initialize accumulator variable newlist to be an empty list Set min_length = min(len(L1), len(L2)), the smaller of the two list lengths Use a for loop to iterate k over range(min_length) to do the first part of this function's work. On each iteration, append to newlist the item from index k in L1, and then append the item from index k in L2 (two appends on each iteration). AFTER the loop…arrow_forwardIn Pythonarrow_forward
- Searching a list. C++ Task: Make the program run on your computer. Understand the logic of functions. Design 3 additional methods: search Unsorted_right (it starts searching from the right end oh the list) . binarySearch_recursive (it calls itself for smaller sublists). searchUnsorted_recursive (it calls itself for smaller sublists). Declare a list object D of size 50 , fill it 50 random numbers, and try your program on this new objectarrow_forwardin java pls and thank you!arrow_forwardB and Carrow_forward
- All the problems are to be coded in Python/Java. You must explicitly solve each problem below using your own logic. For example, Do not use existing library functions, such as sum( ), which computes the sum of a list. Instead, you must use for/while loop to sum up the items in a list. a. Write code for a CharList class which converts a string to a linked list of characters in the string. * b. Add a class member function which outputs if the linked list in Problem a. is a palindrome. What is the Big-O upper bound? c. What change could be made to the class definition in Problem a. to make your solution in Problem b. more efficient? Explain. What is the Big-O upper bound? Hint: Think of how the palindrome determination problem is solved for a regular string. d. Add a method to the classic List class we discussed in class that reverses the list. That is, if the original list is A -> B ->C, all of the list's links should change so that C-> B-> A. Make your solution as efficient as possible.…arrow_forwardWrite a Python function def isSubArray(A,B) which takes two arrays and returns True if the first array is a (contiguous) subarray of the second array, otherwise it returns False. You may solve this problem using recursion or iteration or a mixture of recursion and iteration. For an array to be a subarray of another, it must occur entirely within the other one without other elements in between. For example: [31,7,25] is a subarray of [10,20,26,31,7,25,40,9] [26,31,25,40] is not a subarray of [10,20,26,31,7,25,40,9] A good way of solving this problem is to make use of an auxiliary function that takes two arrays and returns True if the contents of the first array occur at the front of the second array, otherwise it returns False. Then, A is a subarray of B if it occurs at the front of B, or at the front of B[1:], or at the front of B[2:], etc. Note you should not use A == B for arrays.arrow_forwardWrite a function find_max_tuple() that gets a list of tuples as parameter, finds and returns the tuple with maximum sum inside the list. Write Python statements that create a sample list that has tuples inside (you can use the list of tuples in the sample run below), then use find_max_tuple() function to get the tuple with maximum sum and print it. Sample tuples=[(3,4,360,6),(1,2),(12,),(132,244,64),(72,71,70)] The maximum tuple is (132,244,64)arrow_forward
- The function swap_list_ends in python takes one parameter, L, which is a list. The function then swaps the first and last items in L and returns the object None. Notice that since L is mutable, the changes the function makes to L are visible back in the calling program. SPECIAL CASE: If the list 'L' is empty, then the function does nothing to L and returns None. For example: Test Result L = [1, 3, 9, 2] if not(swap_list_ends(L) is None): print("Error: function should return None") print(L) [2, 3, 9, 1] L = ['super', 'awesome', 'excellent'] if not(swap_list_ends(L) is None): print("Error: function should return None") print(L) ['excellent', 'awesome', 'super'] L = [True, False] if not(swap_list_ends(L) is None): print("Error: function should return None") print(L) [False, True]arrow_forwardWrite and test a function to meet this specification in Python. sumList(nums) is a list of numbers. Returns the sums of the numbers in the list.arrow_forwardUSING PYTHON Chapter 5 -Programming Project: Implement a function that takes in two sorted lists and merges them into one list, the new list must be sorted. The function MUST run in O(m+n), where m is the length of list 1 and n the length of list 2. In other words, the function cannot do more than one pass on list 1 and list 2.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education