Question

In this assignment you will implement a library of functions that can be used to draw...

In this assignment you will implement a library of functions that can be used to draw a histogram (bar graph) in ASCII art style. Your library will create a list of strings that, when printed appropriately, form a histogram of a list of data points, and is made of a given character. For example, if every element of the list *[3, 2, 4]* using the string character # is printed, this would be the result:

                                            ### 
                                            ##  
                                            ####

The returned list for this histogram should look like this:

["### ", "##  ", "####"]

Notice that the strings in this list are padded by space characters on the right so that all the strings will be of equal length. This length is determined by the greatest value in the list to be plotted. In the example above, the length of the strings in the list is 4. When this histogram is flipped horizontally and printed `element by element,` the following results: ### ## #### The *returned list* for this histogram should look like this:

[" ###", "  ##", "####"]

Design, implement, and test a program that satisfies the requirements below.

Requirements¶

  1. Implement makeRow(w,m,c) which returns the rows for the unflipped histogram by doing the following:

    • Takes the integers w and m and a single string character ?c .
    • Returns a string composed of ?∗c and enough spaces on the right to reach a total length of m .
  2. Implement histogram(a,c) which returns the unflipped histogram by doing the following:

    • Takes in the list of integers a and a single string character c .
    • Uses makeRow to create a list of strings that forms an unflipped histogram with the single string character c and the elements in the list a .
    • Returns the list of strings that forms the unflipped histogram.
  3. Implement flipHist(a,c) which returns the flipped histogram by doing the following:

    • Takes in the list of integers a and a single string character c.
    • Uses histogram to create an unflipped histogram with a and c
    • Returns the list of strings that forms the flipped histogram.
  4. The program requires very little besides the function definitions. There is no main().

  5. Python programming language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

def makeRow(w,m,c):
    a=str(c)*w+ (" ")*(m-w)
    return a
def histogram(a,c):
    m=max(a)
    for i in range(len(a)):
        a[i]=makeRow(a[i],m,c)
    return a
def flipHist(a,c):
    m=max(a)
    a=histogram(a,c)
    l=[]
    for i in range(m-1,-1,-1):
        s=""
        for j in range(len(a)):
            s=s+a[j][i]
        l.append(s)
    return l
a=[12,3,4,10,5,9]
l=[]
l=flipHist(a,'#')
for i in l:
    print(i)

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

Code photo with sample output:

Explanation:

The makeRow function returns the string of length m with w chracters c and remaining spaces.

This is used by histogram function to create unflipped histogram. Note that m=max(a) gives the maximum element in a.

The fliphist function chooses the last index from each string of list a returned by histogram function.

This strings are stored in list L.

The strings of l can be printed to produce histogram.

----

Please upvote the answer if it works for you. Any feedback would be greatly appreciated.

Add a comment
Answer #2

<<<<<<<<<<<<<<python code>>>>>>>>>>>>>>>>>>>

def makeRow(w,m,c):
string=m*c
return string.ljust(w)

def ℎ????????(?,?):
unflipped=[]
for i in a:
unflipped.append(makeRow(max(a),i,c))
return unflipped

def ????????(?,?):
list1=ℎ????????(?,?)
for i in list1:
print(i)
????????([1,6,5,4,7,8,9,10],'*')

>>>>>>>>>>>>>>>>screen shot<<<<<<<<<<<<<<<<<<

Add a comment
Know the answer?
Add Answer to:
In this assignment you will implement a library of functions that can be used to draw...
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
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