In: Computer Science
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
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”.
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) -> " "
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)
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:
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.
Applying the brightness_modifier to the total amount of battery life left.
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()
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.