Question

In: Computer Science

n the instructions for this Task C, we're going to assume that you have completed your...

  1. n the instructions for this Task C, we're going to assume that you have completed your script for Task B above;
    i.e. that you have a working dictionary-based version of the functions (originally named addToRecord(), etc. in Task A) now named addToHistogram(), etc.

    Let's assume that your dictionary-based function (from Task B above) that creates a histogram is named makeHistogram().
    You are going to use your makeHistogram() in the following sub-tasks.
    In fact, you're welcome to include any function you wrote for Task B above, in your script for Task C.

    Now here for some actual work:

    Define a function histoprint1() that takes a dictionary representing a histogram, and prints out an"almost-graphical"representation[2] of the histogram, one entry per output line. Each printed line should represent a (key, value) pair in the dictionary: starting with the dictionary key (a single character string), followed by a : and as many #s as the dictionary value.
    Note[2] (it's not meant to use actual graphics such as turtlegraphics yet, it's just using printed characters in a more visual way than usual)

    For example, histoprint1(makeHistogram("bicycling")) may look something like this:

    ?

    1

    2

    3

    4

    5

    6

    7

    y: #

    g: #

    b: #

    i: ##

    n: #

    c: ##

    l: #

    Please note that the order in the above printout is arbitrary, since dictionaries do not have a predefined order, so your printout from histoprint1(makeHistogram("bicycling")) may also look something like this:

    ?

    1

    2

    3

    4

    5

    6

    7

    b: #

    n: #

    i: ##

    y: #

    g: #

    l: #

    c: ##

    Hint: you may recall that copies of a string (even a 1-character string) may be concatenated together using the * operator.

  2. Define a function histoprint2() that takes a dictionary representing a histogram, and prints out a sortedalmost-graphical representation of the histogram, one entry per output line (unlike histoprint1(), the function histoprint2() should always produce the same output). The entries should be sorted alphabetically by the keys.

    For example, histoprint1 histoprint2(makeHistogram("bicycling")) should look something like this:

    ?

    1

    2

    3

    4

    5

    6

    7

    b: #

    c: ##

    g: #

    i: ##

    l: #

    n: #

    y: #

  3. Define a function histoprint3() that takes a dictionary representing a histogram and an integer representing a scaling factor. It should print out a sorted almost-graphical representation of the histogram, one entry per output line, where each hash mark # stands in for several occurrences of the character (how many is specified by the scaling factor).

    So for example, histoprint3((makeHistogram("A man, a plan, a canal: panama.")),1) would display the histogram with each hash mark representing one occurrence:

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    : ######

    ,: ##

    .: #

    :: #

    A: #

    a: #########

    c: #

    l: ##

    m: ##

    n: ####

    p: ##

    But histoprint3((makeHistogram("A man, a plan, a canal: panama.")),2) would display the histogram with each hash mark representing two occurrences:

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    : ###

    ,: #

    .:

    ::

    A:

    a: ####

    c:

    l: #

    m: #

    n: ##

    p: #

    Note: some of the entries now look as if they were empty/blank, because the scale is so big that the frequencies of those infrequent characters are less than a hash mark.

  4. Optional: [+5 point] bonus if you implement this function.
    Define a function histoprint4() which takes a histogram and automatically scales the output, to be able to represent larger amount of data within a limited-width printout.
    To test this function, you may have to prepare a string with a large number of same characters, for example:

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    >>> myString = """Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude

    Nah nah nah nah nah nah, nah nah nah, hey Jude"""

    >>> histoprint4(makeHistogram(myString))

    : #####

    : #####################################################

    ,: ###########

    J: #####

    N: #####

    a: ################################################

    d: #####

    e: ###########

    h: #####################################################

    n: ###########################################

    u: #####

    y: #####

    To implement the histoprint4() function, you may have to:
    • find the maximum value for any given key in the dictionary
    • compute a scaling factor so that the maximum value will fit within a limited number of characters, e.g. an 60-character width
    • draw the histogram with the appropriate scaling factor (you might want to use histoprint3() for this)

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

below are the functions that can be used and referenced in this part

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

def inHistogram(char,charRec):
""" returns True if character is in character Record, otherwise false

string, charRecord -> Boolean Value"""
return char in charRec


def addToHistogram((char,charRec):
""" returns new character record with updated frequency of each character occurance

character, charRecord(myCharRecord) -> dictionary of character records"""
if char in charRec:
charRec[char]+=1
else:
charRec[char]=1
myCharRecord = {'K':1, 'o':3, 'k':1, 'm':1, '?':1}

def makeHistogram(pString):
""" returns number for frequency of character in charRec dictionary

character, charRecord -> integer"""
aDict={}
for i in pString:
addToHistory(i,aDict)
return aDict

def getFrequency(char,charRec):
""" returns character records for argument entered

string -> dictionary of character records"""
if char in charRec:
return charRec[char]
else:
return 0 # otherwise return 0 because no frequency of that character

Solutions

Expert Solution

def inHistogram(char,  charRec): 
    """ returns True if character is in character Record,  otherwise false

    string,  charRecord -> Boolean Value"""
    return char in charRec


def addToHistogram(char,  charRec): 
    """ returns new character record with updated frequency of each character occurance

    character,  charRecord(myCharRecord) -> dictionary of character records"""
    if char in charRec: 
        charRec[char]+=1
    else: 
        charRec[char]=1
myCharRecord = {'K': 1,  'o': 3,  'k': 1,  'm': 1,  '?': 1}

def makeHistogram(pString): 
    """ returns number for frequency of character in charRec dictionary

    character,  charRecord -> integer"""
    aDict={}
    for i in pString: 
        addToHistogram(i, aDict)
    return aDict

def getFrequency(char, charRec): 
    """ returns character records for argument entered

    string -> dictionary of character records"""
    if char in charRec: 
        return charRec[char]
    else: 
        return 0 # otherwise return 0 because no frequency of that character

def histoprint1(aDict):
    for key in aDict:
        print(key,"#"*aDict[key],sep=":")

    print()


def histoprint2(aDict):
    lst = [key for key in aDict]
    lst.sort()
    for key in lst:
        print(key,"#"*aDict[key],sep=":")
    print()

def histoprint3(aDict, scale):
    lst = [key for key in aDict]
    lst.sort()
    for key in lst:
        print(key,"#"*(aDict[key]//scale),sep=":")
    print()


def histoprint4(aDict):
    max_fre = 0
    lst = [key for key in aDict]
    max_fre = max([getFrequency(x, aDict) for x in lst])
    scale = (max_fre + 59) // 60
    lst.sort()
    for key in lst:
        print(key,"#"*(aDict[key]//scale),sep=":")
    print()


print("histoprint1")
histoprint1(makeHistogram("bicycling"))


print("histoprint2")
histoprint2(makeHistogram("bicycling"))

print("histoprint3")
histoprint3(makeHistogram("bicycling"),2)

print("histoprint4")
histoprint4(makeHistogram("""Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude"""))


Related Solutions

Now that you have completed your readings, your task is to assess the impact of sexually...
Now that you have completed your readings, your task is to assess the impact of sexually explicit materials on human sexual behavior. You will need to analyze the following questions, presenting a 2-4 paragraph response for each question: 1. There is generally less violence in sexually oriented material than in mainstream movies and TV crime shows. Why are censorship efforts directed more toward sexually explicit nonviolent material than toward nonsexual violent material? 2. Research has found 82% of students viewing...
Now that you have completed your readings, your task is to assess the impact of sexually...
Now that you have completed your readings, your task is to assess the impact of sexually explicit materials on human sexual behavior. You will need to analyze the following questions, presenting a 2-4 paragraph response for each question: 1. There is generally less violence in sexually oriented material than in mainstream movies and TV crime shows. Why are censorship efforts directed more toward sexually explicit nonviolent material than toward nonsexual violent material? 2. Research has found 82% of students viewing...
n this problem, we're going get a rough estimate the amount of uranium fuel it would...
n this problem, we're going get a rough estimate the amount of uranium fuel it would take if the US recieved all its electrical power from nuclear power plants. The size of a power plant in normally given as the about of electrical power it can produce when running a full capacity. This electrical power produced can be very different than the mechanical or thermal power that was required to produce this electricity. For example, power plant might have a...
Instructions: After you have completed the calculations on your spreadsheet that correlates to the questions below,...
Instructions: After you have completed the calculations on your spreadsheet that correlates to the questions below, you will copy and paste the portion of that spreadsheet into the Word document for each of the questions. Be sure to also provide a sentence that explains the solution. To save for her newborn son ’s college education, Lea Wilson will invest $1,000 at the end of each year for the next 20 years. The interest rate is 10%. What is the future...
4) We're going to test the same hypothesis four ways. Assume the people in the dataset...
4) We're going to test the same hypothesis four ways. Assume the people in the dataset in armspanSpring2020.csv are a random sample of all adults. For each test, report the test statistic and the p-value. With a 5% significance level, give the conclusion of each test. a) Test the hypothesis that the mean difference between armspan and height it not equal to 0, using the data in armspanSpring2020.csv. Do this by creating a new variable named diff = (armspan -...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure you understood the concept of “the dot” in programming. Go ahead and declare an array of characters with a default length (whichever length you want, it's fine). Next, you want to ask the user for a phrase and store the characters in your array, but before you do that, ask the user for an estimated length of the array. If the length given is...
For your initial post: Assume you are going to a bank to apply for a loan...
For your initial post: Assume you are going to a bank to apply for a loan for a new product you would like to manufacture and sell or provide a new service to clients. Use your imagination to think of a product or service? What types of expenses would be involved to make the product or service that would be included in your cash flow model? How would you estimate revenue for your product or service? What other factors should...
in c++ please In this program you are going to have several files to turn in...
in c++ please In this program you are going to have several files to turn in (NOT JUST ONE!!) hangman.h – this is your header file – I will give you a partially complete header file to start with. hangman.cpp – this is your source file that contains your main function functions.cpp – this is your source file that contains all your other functions wordBank.txt – this is the file with words for the game to use.  You should put 10...
For this task, assume you are a well-known psychologist and have been asked to give a...
For this task, assume you are a well-known psychologist and have been asked to give a radio interview in your local community. The questions asked of you are based on THREE concepts about female anatomy and THREE concepts of male anatomy that were new or surprising to you when you first entered the field of psychology. Provide a transcript of your responses to the following questions:   • What were the concepts that were most surprising to you of the male...
For this task, assume that you have been asked to host a local forum for parents...
For this task, assume that you have been asked to host a local forum for parents of junior high school aged children to discuss the consequences of having sex at an early age. Parents are debating the following topic: A sexuality education unit, at least a week in length, should be included in all grades in all schools. After the debate you are to draft a summary of the conversations. In your paper, provide both the pro and con of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT