The implementations look quite similar! Generalize this logic by writing a function count_cond, which takes in a two- argument predicate function mystery_function(n, i). count_cond returns a count of all the numbers from 1 to n that satisfy mystery_function. Note: A predicate function is a function that returns a boolean (True or False). def count_cond (mystery_function, n):

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Consider the following implementations of count_factors and count_primes:
def count_factors(n):
"""Return the number of positive factors that n has."""
i, count = 1, 0
while i <= n:
if n % i == 0:
count += 1
i += 1
return count
def count_primes (n):
"""Return the number of prime numbers up to and including n."""
i, count = 1, 0
while i <= n:
if is_prime(i):
count += 1
i += 1
return count
def is_prime(n):
return count_factors (n) == 2 # only factors are 1 and n
S
Transcribed Image Text:Consider the following implementations of count_factors and count_primes: def count_factors(n): """Return the number of positive factors that n has.""" i, count = 1, 0 while i <= n: if n % i == 0: count += 1 i += 1 return count def count_primes (n): """Return the number of prime numbers up to and including n.""" i, count = 1, 0 while i <= n: if is_prime(i): count += 1 i += 1 return count def is_prime(n): return count_factors (n) == 2 # only factors are 1 and n S
The implementations look quite similar! Generalize this logic by writing a function count_cond, which takes in a two-
argument predicate function mystery_function(n, i). count_cond returns a count of all the numbers from 1 to n that
satisfy mystery_function.
Note: A predicate function is a function that returns a boolean (True or False).
def count_cond (mystery_function, n):
>>> def divisible(n, i):
return n % i == 0
>>> count_cond (divisible, 2) # 1, 2
2
||||||
***
>>> count_cond (divisible, 4) # 1, 2, 4
3
>>> count_cond (divisible, 12) # 1, 2, 3, 4, 6, 12
6
>>> def is_prime(n, i):
☐☐☐
>>> count_cond (is_prime, 2) # 2
1
count_cond (is_prime, 3) # 2, 3
count_cond (is_prime, 4) # 2, 3
>>> count_cond (is_prime, 5) # 2, 3, 5
3
>>>
2
>>>
2
return count_cond (divisible, i) == 2
>>> count_cond (is_prime, 20) # 2, 3, 5, 7, 11, 13, 17, 19
8
||||||
"*** YOUR CODE HERE ***"
Transcribed Image Text:The implementations look quite similar! Generalize this logic by writing a function count_cond, which takes in a two- argument predicate function mystery_function(n, i). count_cond returns a count of all the numbers from 1 to n that satisfy mystery_function. Note: A predicate function is a function that returns a boolean (True or False). def count_cond (mystery_function, n): >>> def divisible(n, i): return n % i == 0 >>> count_cond (divisible, 2) # 1, 2 2 |||||| *** >>> count_cond (divisible, 4) # 1, 2, 4 3 >>> count_cond (divisible, 12) # 1, 2, 3, 4, 6, 12 6 >>> def is_prime(n, i): ☐☐☐ >>> count_cond (is_prime, 2) # 2 1 count_cond (is_prime, 3) # 2, 3 count_cond (is_prime, 4) # 2, 3 >>> count_cond (is_prime, 5) # 2, 3, 5 3 >>> 2 >>> 2 return count_cond (divisible, i) == 2 >>> count_cond (is_prime, 20) # 2, 3, 5, 7, 11, 13, 17, 19 8 |||||| "*** YOUR CODE HERE ***"
Expert Solution
Step 1

Hi please find the solution below and I hope it would be helpful for you.

Please find the code below:

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Fibonacci algorithm
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
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education