Question

In: Computer Science

Write a function lbs2lboz(p) that takes a non-negative number, p , that represents a weight in...

Write a function lbs2lboz(p) that takes a non-negative number, p , that represents a weight in pounds and outputs a pair (l,o) such that l is an integer and p=l+o/16.

Write a function oz2lboz(oz) that takes a non-negative number, oz , that represents a weight in ounces and outputs a pair (l,o) such that l is an integer and oz=l*16+o.

on python

Solutions

Expert Solution

Here is the completed code for this problem. 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

#code

# method to convert pounds to pounds and ounces

def lbs2lboz(p):

    # extracting integer part from p

    l = int(p)

    # multiplying remaining value by 16 to get value in ounces

    o = (p - l) * 16

    # returning a pair containing value in pounds and ounces

    return (l, o)

# method to convert ounces to pounds and ounces

def oz2lboz(oz):

    # dividing by 16 and fetching integer part to get pounds

    l = int(oz / 16)

    # subtracting above value from oz to get remaining ounces

    o = oz - (l * 16)

    # returning a pair containing value in pounds and ounces

    return (l, o)

# code for testing

print(lbs2lboz(34.50)) #should be 34 pounds and 8 ounces

print(oz2lboz(99.5)) #should be 6 pounds and 3.5 ounces

#output

(34, 8.0)

(6, 3.5)


Related Solutions

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
Define a function average_entries_for(integer) that takes in an integer than represents the number of times the...
Define a function average_entries_for(integer) that takes in an integer than represents the number of times the function will ask the user to input a number. The function will return the average of all the numbers entered. You must use a for loop to implement this function. (Hint: use range)
In MATLAB define a function named primes that takes a non-negative integer, ?, as its only...
In MATLAB define a function named primes that takes a non-negative integer, ?, as its only argument and returns a row vector containing the first ? prime numbers in order. Assume that the first prime number is 2.  Other than the zeros function, the ones function, and the colon (:) operator, you may not use any Matlab built-in array functions.
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the product of two prime numbers ? and ? and returns the tuple ( ?, ? ) where ?≤? . Example: ????????(22)=(2,11) Example: ????????(3605282209)=(59447,60647) This problem has a time-out limit of 1 second and a memory limit of 1MB. The number ? in all test-cases will satisfy 4≤?≤800000000000000 For example: Test Result print(factorMe(22)) (2, 11) print(factorMe(3605282209)) (59447, 60647)
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
The program shall take two integer arrays as input. Each array represents a non-negative number. Each...
The program shall take two integer arrays as input. Each array represents a non-negative number. Each element in the array is one digit however when all digits in the array are combined a number is formed. See example below: int Array1 = {4,5,6,7} represents number 7654 int Array2 = {2,3,4,5} represents number 5432 You will add the two numbers i.e., 7654 + 5432 = 13086 and store it in a new array similar to how the numbers were stored earlier...
Write a function DIVISORS in MIPS. This function takes a positive number from the register a0...
Write a function DIVISORS in MIPS. This function takes a positive number from the register a0 and prints in a column all of its divisors including 1 and the number itself. Don't forget that printing a number and printing a character requires two different system calls. Here is an example. If the number in a0 is 6 the following output appears: 1 2 3 6
Match a student number: Write a function MatchStudentNumber that takes a student number from the StudentNumber...
Match a student number: Write a function MatchStudentNumber that takes a student number from the StudentNumber column in the reference table and determine if there is a match with a student number from the StudentNumber column in the second table. The output is a logical flag that is true if at least one match was found, and integer array that points to rows in table where a match occurred. Note that the function should find all matches. function [iflag, matchPosition]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT