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...
Write a MATLAB *function* that draws a spiral by using the plot() command to connect-the-dots of...
Write a MATLAB *function* that draws a spiral by using the plot() command to connect-the-dots of a set of points along the spiral's trajectory. The function should have three input arguments: the number of points along the trajectory, the number of rotations of the spiral, and the final radius of the spiral. The function does not need any output arguments. Use nargin to provide default values for the input arguments. The spiral should begin at the origin. At each step...
(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...
Please use python3 Create the function team_average which has the following header def team_average(filename): This function...
Please use python3 Create the function team_average which has the following header def team_average(filename): This function will return the average number of games won by the Red Sox from a text file with entries like this 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @ Astros Win 2-1 2011-07-04 Red Sox vs Blue Jays Loss 7-9 This function should create a file object for the file whose name is given by the parameter filename. If the file cannot...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT