Question

Problem 20 [ 2 points ] Use the Design Recipe to write a function, print_histogram that...

Problem 20 [ 2 points ]

Use the Design Recipe to write a function, print_histogram that consumes a list of numbers and prints a histogram graph using asterisks to represent each number in the list. Use one output line per number in the list. You may assume that only integers are passed to this function. Your function should ignore negative values. Include a docstring!

Test
Result
print_histogram([ 0, 2, 4, 1])
**
****
*
print_histogram([10, 5, 3, -1, 8])
**********
*****
***
********

-------------------------------------------------------------------------

Part 2

Use the Design Recipe to write a function, count_odds, that consumes a list and two integers, lower_bound and upper_bound, and returns the number of all the odd integers in the list that are between lower_bound and upper_bound (inclusive of both lower_bound and upper_bound). Include a docstring!

For example:

Test
Result
print(count_odds([0, 100, 50, 20], 10, 60))
  0
print(count_odds([1, 100, 85, 63, 52, 60, 50], 50, 100))
  2
print(count_odds([-17, -20, -33, 41, 19, 61], -17, 61))
  4


0 0
Add a comment Improve this question Transcribed image text
Answer #1
Answer:
def print_histogram(lst):
    for x in lst:
        if(x>0):
            print("*"*x)

###############################

def count_odds(lst, lower_bound, upper_bound):
    count = 0
    for x in lst:
        if (x>=lower_bound) and (x<=upper_bound):
            if(x%2==1):
                count += 1
    return count
Add a comment
Know the answer?
Add Answer to:
Problem 20 [ 2 points ] Use the Design Recipe to write a function, print_histogram that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CODE IN PYTHON 17. Use the Design Recipe to define a function called  black_jack  which consumes to 2...

    CODE IN PYTHON 17. Use the Design Recipe to define a function called  black_jack  which consumes to 2 int values greater than 0. Return whichever value is nearest to 21 without going over. Return 0 if they both go over. Include a docstring! Write three assertEqual statements to test your function.

  • Python Use the Design Recipe to write a function called running_average that repeatedly asks the user...

    Python Use the Design Recipe to write a function called running_average that repeatedly asks the user to input integers at the keyboard until they type the word done. Return the average of the values they entered. You may assume the user will only enter integers or "done". Include a docstring!

  • Python Language The Blip Blob New Year celebration started on a Tuesday. Use the Design Recipe...

    Python Language The Blip Blob New Year celebration started on a Tuesday. Use the Design Recipe to write a function called blip_blop_day that consumes an int representing the number of days that have passed since their celebration start day and returns name of the current day. For example, if 2 days have passed, then 'Thursday' should be returned. Include a docstring! For example: Test Result 2 Thursday 4 Saturday Monday Tuesday 6 7 8 Wednesday

  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

  • In Python Use the Design Recipe to write a function factorial that when given a positive...

    In Python Use the Design Recipe to write a function factorial that when given a positive integer calculates the factorial. If given a negative integer or not an integer), the function should return None. If given 0, it should return 1. Include a docstring! Note: You may assume all arguments passed to this function will be numeric. Write three assertEqual statements to test your function. To test and receive credit for your assertEqual statements, copy them into Part E and...

  • Python Language ! Use the Design Recipe to define a function save_history which consumes two parameters,...

    Python Language ! Use the Design Recipe to define a function save_history which consumes two parameters, a nested list and an int representing the current landing attempt. The function should open a new file named LandingNN.csv' where NN is two digits representing the current landing attempt. The first line should contain the number of sublists in the nested list. Each sublists should be written to the file on its own line with its values separated by commas. This function should...

  • In this problem, you should write one function named copy and increment. This function will have...

    In this problem, you should write one function named copy and increment. This function will have one parameter, which you can assume will be a list of integers. This function should return a copy of the parameter list, in which each number from the parameter list has been increased by 1. The function should not modify the values in the parameter list. For example, the code: values - 20, 40, 10, 60, 77, 2) other copy and incrementales) print values...

  • 0% of final (S1 2019) Write a function called sum_of_products(x), where x is a list of lists of i...

    0% of final (S1 2019) Write a function called sum_of_products(x), where x is a list of lists of integers, this function returns a number which is the sum of the products of each list of integers. For example: print(sum_of_products ([[1,2], [5], [2,5]])) will result in the output: 17 That is, the result of 2 + 5+ 10 For example: Test Result print(sum_of_products( [2,3]])) print sum_of_products([[2],[3]])) print(sum_of_products([[1,21,[S],[2,5]1)) 17 0% of final (S1 2019) Write a function called sum_of_products(x), where x is...

  • In Python use the function design recipe from Chapter 3 of Practical Programming to develop a...

    In Python use the function design recipe from Chapter 3 of Practical Programming to develop a function named first_last6. The function takes a list of integers. (Assume that the list will not be empty.) The function returns True if a 6 is the first element or the last element or if both the first and last element are 6. Otherwise, the function returns False.

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT