Question

In: Computer Science

Design a function called middle_value, that takes 3 integers, and returns the integer with the middle...

  1. Design a function called middle_value, that takes 3 integers, and returns the integer with the middle value. If there is a tie, any of the possible middle values can be returned. Example: middle_value(1, 2, 8) -> 2 middle_value(9, 7, 7) -> 7 middle_value(3, 3, 3) -> 3

  2. Design a function called combine_strings that takes two phrases and appends the longer string onto the back of the shorter one with no space between the two phrases joined. Example: If the phrases are “thought” and “after” the function should return “afterthought”, or “sea” and “food” would return “seafood”.

  3. Design a function called get_letter_at that takes a phrase and a number and returns the letter at the phrase at the given index (note that 0 would return the first letter in the phrase). If the number is too large, then just return an empty string: “ ”.
    Examples:

    get_letter_at("Anthony", 0) -> "A" get_letter_at("Anthony", 4) -> "o" get_letter_at("Anthony", 7) -> " "

  1. Design a function called brightness_modifier that takes an integer value that is either 0, 1, 2, or 3 representing the brightness level of a smartphone (0 means screen is off, up to 3 which means full brightness). The function should return a decimal number representing the modifier that the brightness level will affect the lifetime of battery of the device. For example:

    • - 0 brightness does not affect the battery life at all (returns 1.0)

    • - 1 brightness returns a 0.9 (the battery will last 90% of maximum battery lifetime)

    • - 2 brightness returns a .75 (the battery lasts 75% of maximum battery lifetime)

    • - 3 brightness returns a .5 (the battery only last 50% of maximum lifetime)

  2. Design a function called hours_remaining that takes two integers and a boolean as parameters and returns the total hours of battery life left. The parameters represent the percentage of battery life left, the brightness (0, 1, 2, 3) and whether the device is currently streaming video. This function MUST call and use the brightness_modifier function you designed for exercise 4.

    The hours left is calculated by the following formula:

    1. The amount of battery life left is calculated by the maximum battery life (15 hours – see the

      CONSTANT defined at the top of the file) multiplied by percentage of battery left.

    2. Applying the brightness_modifier to the total amount of battery life left.

    3. If the phone is currently streaming video, the hours remaining is cut in half.

For example, given hours_remaining(80, 2, true) produces:
(Note the FULL_BATTERY_LIFE = 15 constant defined at the top of the file) 15*80% = 12 hours of regular battery remaining.
12 hours remaining at 2 brightness level = 12*.75 modifier = 9 hours
9 hours of streaming video = 9*0.5 = 4.5 actual hours remaining

tests = 0
passed = 0

FULL_BATTERY_LIFE = 15

def main():
        print('Assignment 3')
        '''
        Complete this assignment by doing the following
        for each function:
            - uncommenting one test function call in main
              NOTE: each test function has at least one test,
              but you should add additional tests to ensure
              the correctness of your functions

              - complete a full function design for each function
              (write the documentation and define the function)
              NOTE: follow the documentation provided for
              implementing each function in the csc110-assign3.pdf

              - process to the next function by doing the steps
              outlined above until all functions are designed with
              proper documentation, implementation and testing
        '''
        test_middle_value()
        test_combine_strings()
        test_get_letter_at()
        test_brightness_modifer()
        test_hours_remaining()
        print("TEST RESULTS:", passed, "/", tests)

def test_middle_value():
        print("beginning tests for middle_value...")
        #TODO: add tests here (and erase this line if you want)

def test_combine_strings():
        print("beginning tests for combine_strings...")
        #TODO: add tests here (and erase this line if you want)

def test_get_letter_at():
        print("beginning tests for get_letter_at...")
        #TODO: add tests here (and erase this line if you want)

def test_brightness_modifer():
        print("beginning tests for brightness_modifer...")
        #TODO: add tests here (and erase this line if you want)

def test_hours_remaining():
        print("beginning tests for hours_remaining...")
        #TODO: add tests here (and erase this line if you want)



# (str, bool -> None)
# takes the name or description of a test and whether the
# test produced the expected output (True) or not (False)
# and prints out whether that test passed or failed
# NOTE: You should not have to modify this in any way.
def print_test(test_name, result_correct):
    global tests
    global passed
    tests += 1
    if(result_correct):
        print(test_name + ": passed")
        passed += 1
    else:
        print(test_name + ": failed")


# The following code will call your main function
# It also allows our grading script to call your main
# DO NOT ADD OR CHANGE ANYTHING PAST THIS LINE
# DOING SO WILL RESULT IN A ZERO GRADE
if __name__ == '__main__':
    main()

Solutions

Expert Solution

Please find the answer below, all the details are mentioned in the comments.

All the functions are created and giving output as expected. Please configure print_test() according to your need

Program:

FULL_BATTERY_LIFE = 15

#function to get the middle value
def middle_value(a,b,c):
#if elif block is used to identify the middle element
if a <= b <= c or c <= b <= a:
return b
elif b <= a <= c or c <= a <= b:
return a
else:
return c
  
#function to combine the strings
def combine_strings(a,b):
#length of both the string is compared and accordingly
#new string is formed and returned
if(len(a) > len(b)):
return b+a
else:
return a+b

#function get the letter at position from string
def get_letter_at(s,pos):
if(pos<len(s)):
return s[pos]
else:
return " "
  
#function to return decimal value based on the value passed
def brightness_modifer(val):
if (val==0):
return 1.0
elif(val==1):
return 0.9
elif(val==2):
return 0.75
elif(val==3):
return 0.5
  
#function to calculate the hours remaining
def hours_remaining(battery_remaining,brightness_level,isVideoStreaming):
#get the total_hours as a remaining battery
total_hours_rem = (FULL_BATTERY_LIFE * battery_remaining)/100.0
  
#dedcut the value as per the brightness
total_hours_rem = total_hours_rem * brightness_modifer(brightness_level)
  
#check if video is playing then half the battery value
if(isVideoStreaming == True):
total_hours_rem = total_hours_rem / 2.0
return total_hours_rem
  
def main():
print('Assignment 3')
'''
Complete this assignment by doing the following
for each function:
- uncommenting one test function call in main
NOTE: each test function has at least one test,
but you should add additional tests to ensure
the correctness of your functions

- complete a full function design for each function
(write the documentation and define the function)
NOTE: follow the documentation provided for
implementing each function in the csc110-assign3.pdf

- process to the next function by doing the steps
outlined above until all functions are designed with
proper documentation, implementation and testing
'''
test_middle_value()
test_combine_strings()
test_get_letter_at()
test_brightness_modifer()
test_hours_remaining()
#print("TEST RESULTS:", passed, "/", tests)

def test_middle_value():
print("beginning tests for middle_value...")
#TODO: add tests here (and erase this line if you want)
print(middle_value(1,2,8))
print(middle_value(9,7,7))
print(middle_value(3,3,3))

def test_combine_strings():
print("beginning tests for combine_strings...")
#TODO: add tests here (and erase this line if you want)
print(combine_strings("thought","after"))
print(combine_strings("see","food"))

def test_get_letter_at():
print("beginning tests for get_letter_at...")
#TODO: add tests here (and erase this line if you want)
print(get_letter_at("Anthony",0))
print(get_letter_at("Anthony",4))
print(get_letter_at("Anthony",7))

def test_brightness_modifer():
print("beginning tests for brightness_modifer...")
#TODO: add tests here (and erase this line if you want)
print(brightness_modifer(0))
print(brightness_modifer(1))
print(brightness_modifer(2))
print(brightness_modifer(3))

def test_hours_remaining():
print("beginning tests for hours_remaining...")
#TODO: add tests here (and erase this line if you want)
print(hours_remaining(80,2,True))
print(hours_remaining(80,2,False))


# (str, bool -> None)
# takes the name or description of a test and whether the
# test produced the expected output (True) or not (False)
# and prints out whether that test passed or failed
# NOTE: You should not have to modify this in any way.
def print_test(test_name, result_correct):
global tests
global passed
tests += 1
if(result_correct):
print(test_name + ": passed")
passed += 1
else:
print(test_name + ": failed")


# The following code will call your main function
# It also allows our grading script to call your main
# DO NOT ADD OR CHANGE ANYTHING PAST THIS LINE
# DOING SO WILL RESULT IN A ZERO GRADE
if __name__ == '__main__':
main()

Please find a code snippet for indentation reference:

Output:

Please let us know in the comments, if you face any problems.


Related Solutions

Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT