Question

In: Computer Science

python3 Continue from the previous function, write a draw_table method which draws a table of data....

python3

Continue from the previous function, write a draw_table method which draws a table of data. For example, consider the following code fragment:

my_table = Table([['Alice', 24], ['Bob', 19]], width=10)
my_table.draw_table()

produces:

|Alice     24        |
|Bob       19        |

Note: the column width is the value set up by the constructor. Insert a space in between columns.

and my previous function is

class Table:
def __init__(self,x, h = None, width=5):
self.x= x
self.h = h
self.w= width
def __str__(self):
return "headers={}, width={}, data={}".format(self.h,self.w,self.x)

Solutions

Expert Solution

'''

Python version : 3.6

Python program to create and test a class Table

'''

class Table:

              

               def __init__(self,x, h = None, width=5):

                              self.x= x

                              self.h = h

                              self.w= width

                             

               def __str__(self):

                              return "headers={}, width={}, data={}".format(self.h,self.w,self.x)

                             

               def draw_table(self):      

                              # loop over the x

                              for item in self.x:

                                             print('|',end ='') # print the starting |

                                             # loop over the item

                                             for i in range(len(item)):

                                                            # print the ith position in item

                                                            # ljust returns a space padded string with the original string left-justified to a given width colum

                                                            print((str(item[i])).ljust(self.w,' '),end='')

                                             print('|') # print the ending |

              

# test the function

my_table = Table([['Alice', 24], ['Bob', 19]], width=10)

my_table.draw_table()   

                                            

#end of program                                           

Code Screenshot:

Output:


Related Solutions

it should be c++ data structure Write a recursive function that draws the following shape. X...
it should be c++ data structure Write a recursive function that draws the following shape. X XXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXX XXXXXXX XXXXX XXX X The length of the longest row in the shape and the shape's character are function parameters. In above shape the longest row is 9 and the pattern's character is "X”.
python3 (3a) Write a function, frequent, with one parameter, psw, a string. If psw is in...
python3 (3a) Write a function, frequent, with one parameter, psw, a string. If psw is in a list of frequently used passwords ['password', '12345', 'qwerty', 'letmein', 'trustno1', '000000', 'passw0rd'], frequent should return False; otherwise, return True. Be sure to include at least three good test cases in the docstring. (3b) Password Protection SecuriCorp has recently been the victim of a number of security breaches. Internal analysis has determined that employees use simple passwords that are too easy to guess. You...
Python3 *Use Recursion* Write a function called smallest_sum(A,L) where A is the value and L is...
Python3 *Use Recursion* Write a function called smallest_sum(A,L) where A is the value and L is a list of ints. smallest_sum should return the length of the smallest combinations of L that add up to A if there are no possible combinations then float("inf") is returned. Examples smallest_sum(5, [2, 3]) == 2 smallest_sum(313, [7, 24, 42]) == 10 smallest_sum(13, [8, 3, 9, 6]) == float(‘‘inf’’) No Loops and map/reduce functions are allowed
A previous semester's set of data from the initial questionnaire results in the following frequency table...
A previous semester's set of data from the initial questionnaire results in the following frequency table (some minor adjustments were made). Eye color +8 hours of sleep <8 hours of sleep Brown 6 7 Blue 4 1 Other 1 3 (a) If the possibilities for eye color are brown (Br), blue (Bl), and other (O), and the possibilities for sleep are at least 8 hours (Y) and less than 8 hours (N), one possible outcome for eye color and sleep...
(Python3) Write a function friend_besties() that calculates the "besties" (i.e. degree-one friends) of a given individual...
(Python3) Write a function friend_besties() that calculates the "besties" (i.e. degree-one friends) of a given individual in a social network. The function takes two arguments: individual, an individual in the social network, in the form of a string ID. bestie_dict, a dictionary of sets of friends of each individual in the social network (as per the first question of the Project) The function should return a sorted list, made up of all "degree-one" friends for the individual. In the instance...
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws...
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws a square of length s. The square should be filled by a randomized color. Then write another function, that calls the shape function to draw 4 squares as a 2x2 table. Please answer in python.
Complete exercise 9.20 from the text. (a) Create a function called polygon that draws a polygon...
Complete exercise 9.20 from the text. (a) Create a function called polygon that draws a polygon in a polar plot. Your function should have a single input parameter – the number of sides. (b) Use a for loop to create a figure with four subplots…You should use the function you created in part (a) to draw each polygon. Use the index parameter from the for loop to specify the subplot in which each polygon is drawn, and in an expression...
We continue the story of Robinson Crusoe from the previous problem. One day, while walking along...
We continue the story of Robinson Crusoe from the previous problem. One day, while walking along the beach, Robinson Crusoe saw a canoe in the water. In the canoe was a native of a nearby island. The native told Robinson that on his island there were 100 people and that they all lived on fish and coconuts. The native said that on his island, it takes 2 hours to catch a fish and 1 hour to find a coconut. The...
Use Newton's Method to approximate the zero(s) of the function. Continue the iterations until two successive...
Use Newton's Method to approximate the zero(s) of the function. Continue the iterations until two successive approximations differ by less than 0.001. Then find the zero(s) to three decimal places using a graphing utility and compare the results. f(x) = x3 − 6.9x2 + 10.79x − 4.851 Newton's method:      Graphing Utility:      x = x = (smallest value) x = x = x = x = (largest value)
Suppose that a person plays a game in which he draws a ball from a box...
Suppose that a person plays a game in which he draws a ball from a box of 10 balls numbered 0 through 9. He then puts the ball back and continue to draw a ball (with replacement) until he draws another number which is equal or higher than the first draw. Let X denote the number drawn in the first draw and Y denote the number of subsequent draws needed. (a) Find the conditional probability distribution of Y given X...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT