Question

In: Computer Science

Write a standalone function partyVolume() that takes accepts one argument, a string containing the name of...

  1. Write a standalone function partyVolume() that takes accepts one argument, a string containing the name of a file. The objective of the function is to determine the a Volume object that is the result of many people at a party turning the Volume up and down. More specifically: the first line of the file is a number that indicates the initial value of a Volume object. The remaining lines consist of a single character followed by a space followed by a number. The character will be one of ‘U” or ‘D’ which stand for “up” and “down” respectively. The function will create a new Volume object and then process each line of the file by calling the appropriate method of the Volume object which changes the value of the Volume. The function then returns the final Volume object.   Guidelines/hints:
    1. This is a standalone function, it should NOT be inside (indented within) the class. It should be listed in the module after the Volume class and without indentation.
    2. Note that the first line of the file will be treated differently than all the other lines. Probably the easiest way to do this is to 1) open the file, 2) call the .readline() method (no s!) which reads a single line, the initial value of the Volume, then 3) call the .readlines() method which reads the rest of the lines. Item 2) will be used to set the initial Volume and 3) will be iterated over to represent turning the volume up and down some number of time.
    3. Make sure you return the final Volume object.

##### Volume #####

# set and get

>>> v = Volume()

>>> v.set(5.3)

>>> v

Volume(5.3)

>>> v.get()

5.3

>>> v.get()==5.3 # return not print

True

>>>

# __init__, __repr__, up, down

>>> v = Volume(4.5) # set Volume with value

>>> v

Volume(4.5)

>>> v.up(1.4)

>>> v

Volume(5.9)

>>> v.up(6) # should max out at 11

>>> v

Volume(11)

>>> v.down(3.5)

>>> v

Volume(7.5)

>>> v.down(10) # minimum must be 0

>>> v

Volume(0)

# default arguments for __init__

>>> v = Volume() # Volume defaults to 0

>>> v

Volume(0)

# can compare Volumes using ==

>>> # comparisons

>>> v = Volume(5)

>>> v.up(1.1)

>>> v == Volume(6.1)

True

>>> Volume(3.1) == Volume(3.2)

False

# constructor cannot set the Volume greater

# than 11 or less than 0

>>> v = Volume(20)

>>> v

Volume(11)

>>> v = Volume(-1)

>>> v

Volume(0)

>>>

##### partyVolume #####

>>> partyVolume('party1.txt')

Volume(4)

>>> partyVolume('party2.txt')

Volume(3.75)

>>> partyVolume('party3.txt')

Volume(0.75)

# make sure return not print

>>> partyVolume('party1.txt')==Volume(4) # return not print

True

>>> partyVolume('party2.txt')==Volume(3.75)

True

>>> partyVolume('party3.txt')==Volume(0.75)

True

use python3.7

Solutions

Expert Solution

Here is the completed code for this problem. Since you did not provide the Volume class, I have written this code based on my assumptions and doctests. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code


#required method, assuming Volume class is defined and is accessible
def partyVolume(filename):
    #opening file in read mode, assuming file exists
   
file=open(filename,'r')
    #reading initial volume
   
initial=float(file.readline())
    #creating a Volume object with initial volume
   
v=Volume(initial)
    #looping through rest of the lines in file
   
for line in file.readlines():
        #removing trailing/leading white spaces/newlines and splitting line by white
        #space to get a list of tokens
       
line=line.strip().split(' ')
        #ensuring that length of resultant list is 2
       
if len(line)==2:
            #reading first value as direction (U or D)
           
dir=line[0].upper()
            #reading second value as float value
           
value=float(line[1])
            if dir=='U':
                #turning volume up
               
v.up(value)
            elif dir=='D':
                #turning volume down
               
v.down(value)
    #closing file, saving changes
   
file.close()
    #returning volume
   
return v


Related Solutions

In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
In the same module/file, write a function processAnimals that takes one argument, the name of an...
In the same module/file, write a function processAnimals that takes one argument, the name of an input file.   The function returns the list of animals created or an empty list if the file didn't contain any lines. The input file contains zero or more lines with the format: species, language, age where species is a string representing an animal's species, language is a string representing an animal's language, and age is an integer representing an animal's age. The items on...
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
Write a function called format_name that accepts a string in the format of first name followed...
Write a function called format_name that accepts a string in the format of first name followed by last name as a parameter, and then returns a string in reverse order (i.e., last name, first name). You may assume that only a first and last name will be given. For example, format_name("Jared Smith") should return "Smith, Jared" Hint: The following String Methods will be useful: # Returns the lowest index in the string where substring is # found. Returns -1 if...
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
Write function words() that takes one input argument—a file name—and returns the list of actual words...
Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
Write a function that accepts two arguments (say a and b) by value and one argument...
Write a function that accepts two arguments (say a and b) by value and one argument (say c) by reference. Then it calculates the multiplication of all the numbers between a and b (inclusive) and saves the results in c. The function does not return any value.
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT