Question 1 Write a Python ciass named Histogram that keeps track of how many times each unique element in a given sequence occurs. This class will implement the following interface: • Histogram( sequence ) will create a histogram of all elements occurring in the given sequence. This class cannot use the dict or collections.Counter types to create instance or class variables in order to implement the following specifications! If it does, the grade for your solution will automatically be zerol However, in some cases, your code will have to return dictionaries. So note the difference between the two specifications! • get( element-None, default-e ) will return how many times a given element occurs in the histogram. If no element is specified, then the get() function will retum a dictionary where keys are elements and values are number of times a given element occurs in the sequence provided to the initializer. For example, given h - Histogram( "aaabbc" ) then h.get( "b" ) should return 2, and h.get() should return ("a": 3, "b": 2, "c": 1}. If the provided character does not occur in the input string, then get () should return the value provided as default in the default argument. If not provided, get() should return e for a non-occurring character. For example, h.get( "s", 5 ) should return 5. since character s does not occur in the input string. most_frequent( n-1 ) should retum the most frequently occurring elements in the given sequence. If n-, then the most frequent should be retumed

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
100%
should return 5, since character s does not occur in the input string.
• most_frequent( n=1 ) should return the most frequently occurring elements in the given sequence. If n= , then the most frequent should be returned
in a tuple along with its frequency value. For example,
h.most_frequent()
should return ('a', 3)
If n is larger than 1, then the function should return an ordered list of tuples of the top n most frequent values. For example,
h.most_frequent( n=2 )
should return [('a', 3), ('b', 2)].
If n is larger than the number of elements, then this method should return all elements and their corresponding values. For example,
h.most_frequent( 100 )
should return [('a', 3), ('b', 2), ('c', 1)].
• values () should return the values in the histogram in a list. The order of the returned values is immaterial. See test cases below.
• elements () should return the elements in the histogram, akin to the keys in a dictionary. See test cases below.
• items () should return a list of two-tuples, in which the first item is the element and second item is the frequency value. See test cases below.
• remove( element ) should remove the indicated element from the histogram and returns the frequency value for that element. If that element is not
present in the histogram, this instance method should raise a KeyError. See test cases below.
Transcribed Image Text:should return 5, since character s does not occur in the input string. • most_frequent( n=1 ) should return the most frequently occurring elements in the given sequence. If n= , then the most frequent should be returned in a tuple along with its frequency value. For example, h.most_frequent() should return ('a', 3) If n is larger than 1, then the function should return an ordered list of tuples of the top n most frequent values. For example, h.most_frequent( n=2 ) should return [('a', 3), ('b', 2)]. If n is larger than the number of elements, then this method should return all elements and their corresponding values. For example, h.most_frequent( 100 ) should return [('a', 3), ('b', 2), ('c', 1)]. • values () should return the values in the histogram in a list. The order of the returned values is immaterial. See test cases below. • elements () should return the elements in the histogram, akin to the keys in a dictionary. See test cases below. • items () should return a list of two-tuples, in which the first item is the element and second item is the frequency value. See test cases below. • remove( element ) should remove the indicated element from the histogram and returns the frequency value for that element. If that element is not present in the histogram, this instance method should raise a KeyError. See test cases below.
Question 1
Write a Python class named Histogram that keeps track of how many times each unique element in a given sequence occurs. This class will implement the
following interface:
• Histogram( sequence ) will create a histogram of all elements occurring in the given sequence.
This class cannot use the dict or collections.Counter types to create instance or class variables in order to implement the following
specifications! If it does, the grade for your solution will automatically be zero! However, in some cases, your code will have to return dictionaries. So note
the difference between the two specifications!
• get( element-None, default-e ) will return how many times
given element occurs in the histogram. If no element is specified, then the get ()
function will retum a dictionary where keys are elements and values are number of times a given element occurs in the sequence provided to the
initializer. For example, given
h - Histogram( "aaabbc" )
then
h.get( "b" )
should return 2, and
h.get()
should return {"a": 3, "b": 2, "c": 1}.
If the provided character does not occur in the input string, then get () should return the value provided as default in the default argument. If not
provided, get() should returm e for a non-occurring character. For example,
h. get( "s", 5)
should return 5, since character s does not occur in the input string.
• most_frequent( n-1 ) should retum the most frequently occurring elements in the given sequence. If n-, then the most frequent should be returned
in a tuple along with its frequency value, For example.
Transcribed Image Text:Question 1 Write a Python class named Histogram that keeps track of how many times each unique element in a given sequence occurs. This class will implement the following interface: • Histogram( sequence ) will create a histogram of all elements occurring in the given sequence. This class cannot use the dict or collections.Counter types to create instance or class variables in order to implement the following specifications! If it does, the grade for your solution will automatically be zero! However, in some cases, your code will have to return dictionaries. So note the difference between the two specifications! • get( element-None, default-e ) will return how many times given element occurs in the histogram. If no element is specified, then the get () function will retum a dictionary where keys are elements and values are number of times a given element occurs in the sequence provided to the initializer. For example, given h - Histogram( "aaabbc" ) then h.get( "b" ) should return 2, and h.get() should return {"a": 3, "b": 2, "c": 1}. If the provided character does not occur in the input string, then get () should return the value provided as default in the default argument. If not provided, get() should returm e for a non-occurring character. For example, h. get( "s", 5) should return 5, since character s does not occur in the input string. • most_frequent( n-1 ) should retum the most frequently occurring elements in the given sequence. If n-, then the most frequent should be returned in a tuple along with its frequency value, For example.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Array
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
  • SEE MORE 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