Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What are the answers using Python? Count dominators def count_dominators (items): An element of items is said to be a dominator if every element to

image text in transcribedimage text in transcribed

What are the answers using Python?

Count dominators def count_dominators (items): An element of items is said to be a dominator if every element to its right (not just the one element that is immediately to its right) is strictly smaller than it. By this definition, the last item of the list is automatically a dominator. This function should count how many elements in items are dominators, and return that count. For example, dominators of [ 42, 7, 12, 9, 13, 5] would be the elements 42, 13 and 5. Before starting to write code for this function, you should consult the parable of "Shlemiel the painter" and think how this seemingly silly tale from a simpler time relates to today's computational problems performed on lists, strings and other sequences. This problem will be the first of many that you will encounter during and after this course to illustrate the important principle of using only one loop to achieve in a tiny fraction of time the same end result that Shlemiel achieves with two nested loops. Your workload therefore increases only linearly with respect to the number of items, whereas the total time of Shlemiel's back-and-forth grows quadratically, that is, as a function of the square of the number of items. items Expected result [ 42, 7, 12, 9, 2, 5] 4 [] 0 [99] 1 [ 42, 42, 42, 42] 1 range (10**7) 1 range (10**7, 0, -1) 10000000 Trying to hide the inner loop of some Shlemiel algorithm inside a function call (this includes Python built-ins such as max and list slicing) will only summon the Gods of Compubook Headings to return with tumult to claim their lion's share of execution time. def extract_increasing(digits): Given a string of digits guaranteed to only contain ordinary integer digit characters 0 to 9, create and return the list of increasing integers acquired from reading these digits in order from left to right. The first integer in the result list is made up from the first digit of the string. After that, each element is an integer that consists of as many following consecutive digits as are needed to make that integer strictly larger than the previous integer. The leftover digits at the end of the digit string that do not form a sufficiently large integer are ignored. This problem can be solved with one for-loop through the digits that looks at each digit exactly once regardless of the position of that digit in the beginning, end or middle of the string. Keep track of the current number (initially zero) and the previous number to beat (initially minus one). Each digit d then updates current = 10*current+d to tack that digit to the end of current. digits Expected result '045349' [0, 4, 5, 34 ] '77777777777777777777777' [7, 77, 777, 7777, 77777, 777777] '122333444455555666666' [1, 2, 23, 33, 44, 445, 555, 566, 666] '1234567890987654321' [1, 2, 3, 4, 5, 6, 7, 8, 9, 98, 765, 4321] '3141592653589793238462643 383279502884' [3, 14, 15, 92, 653, 5897, 9323, 84626, 433832, 795028] '271828182845 9045235360287 47135266249775724709369995 95749669676277240766303535 47594571382178525166427427 46639193200305992181741359 6629043572900334295260 [2, 7, 18, 28, 182, 845, 904, 5235, 36028, 74713, 526624, 977572, 4709369, 9959574, 96696762, 772407663, 3535475945, 7138217852, 51664274274, 66391932003, 599218174135, 966290435729] A list with 75 elements, the last one equal to 34567891234567891234567891 '123456789' * 100

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions