Question

In: Computer Science

Python : Write the function inAWhile that takes two integers t and d, where t is...

Python : Write the function inAWhile that takes two integers t and d, where t is the present time on a 12-hour clock, rounded to an integer (so t is an integer in the interval [1,12]) and d is an integer increment in hours, and returns the time on a 12-hour clock in d hours. For example, in 2 hours from 3 o’clock, it will be 5 o’clock, so inAWhile (3, 2) = 5. Notice that 12 is followed by 1 on a 12-hour clock, since only computer scientists start counting at 0. The test data will clarify. Additionally, you should guarantee that the type of the parameters is int, using an assertion. For example, inAWhile (‘Hi’, ‘there’) should fail the assertion, and inAWhile (5, ‘there’) should also fail the assertion.

Solutions

Expert Solution

PYTHON CODE:

def inAWhile(t,d):

    # checking the parameters types and report error
    assert isinstance(t,int) and isinstance(d,int) ,"t and d must be integer"
  
    # finding the total of d and t
    total=t+d

    # checking for total greater than 12
    if total > 12:

        # checking for 12 when subtracting 12 from 12
        if total - 12 == 12:
            return 12
        else:

            # returning the time in 12 hours format
            return (total -12) % 12
    else:
        # return the sum of t and d when total is less than 12
        return t + d

# testing
if __name__=='__main__':

    print(inAWhile(10,5))
    print(inAWhile(3,2))
    print(inAWhile(1,1))
    print(inAWhile(12,11))
    print(inAWhile(8,8))
  
  


SCREENSHOT FOR CODING:

SCREENSHOT FOR OUTPUT:


Related Solutions

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.
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.
In python Consider the tuple t = (2,3,4,5). Write a function tuList( t ) that takes...
In python Consider the tuple t = (2,3,4,5). Write a function tuList( t ) that takes in a tuple, makes a list out of it, and also creates a new list of all possible sums of elements of the list
Python: Accumulator pattern and conditionals. Write a function play that takes integers rounds and sides and...
Python: Accumulator pattern and conditionals. Write a function play that takes integers rounds and sides and does the following  Plays round number of rounds, rolling two s-sided dice per round (use roll).  Uses the previous functions to calculate the score of each round  Prints out the dice rolls and the score for each round  Accumulates the score over all rounds  Returns the final score over all rounds. *Write a function result that takes an arguments...
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
Python: Write the function pixelLuminance that takes 3 integers r, g, and b, each between 0...
Python: Write the function pixelLuminance that takes 3 integers r, g, and b, each between 0 and 255 (inclusive), representing the red, green, and blue intensity of a pixel and returns the luminance of this pixel as an integer. The function expects each parameter r, g, and b to be an integer in the interval [0,255]. You should use assertions to enforce this constraint.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Python , no strings, len(), or indexing Write the function setKthDigit(n, k, d) that takes three...
Python , no strings, len(), or indexing Write the function setKthDigit(n, k, d) that takes three # non-negative integers -- n, k, and d -- where d is a single digit # (between 0 and 9 inclusive), and returns the number n but with # the kth digit replaced with d. Counting starts at 0 and goes # right-to-left, so the 0th digit is the rightmost digit.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT