Question

In: Computer Science

The below_freezing function takes a string as its parameter and return True if the temperature specified...

The below_freezing function takes a string as its parameter and return True if the temperature specified by the string is below freezing point for water. It returns False if the temperature is at or above the freezing point. The string takes the format of a decimal number followed by a letter where F stands for Fahrenheit, C for Celsius, and K for Kelvin. For example, below_freezing("45.5F") and below_freezing("300K") both return False. below_freezing("-1.2C") and below_freezing("272K") both return True It's critical that you include test case code to show proper testing. PLEASE DO IN PYTHON

Solutions

Expert Solution

#This program will take string as its parameter and return True if the temperature specified by the string is below freezing point for water
#Creating a class Temprature
class Temprature:

    def below_freezing(self,tempString):
        Len_Of_String=len(tempString)
        if Len_Of_String==0 or Len_Of_String ==1: # will check if correct values are passed
            print("Please specify correct values")
            return False
        tempType=tempString[Len_Of_String-1]
        temp=float(tempString[0:Len_Of_String-1])
        if tempType != 'C' and tempType != 'K' and tempType != 'F': # will check for right input
            print("temperature can be mentioned in F for Fahrenheit, C for Celsius, and K for Kelvin only")
            return False
        # Below will compare temperature is below freezing point or not
        if tempType=='C':
            if temp <= 0:
                return True
            else:
                return False
        if tempType=='F':
            if temp <= 32:
                return True
            else:
                return False
        if tempType=='K':
            if temp <= 273.2:
                return True
            else:
                return False


if __name__=="__main__":
    TempObj=Temprature() # creating an object of class Temprature
    #Below are the some test cases
    TempObj.below_freezing("")
    TempObj.below_freezing("1D")
    TempObj.below_freezing("1")
    #using print to print true and false values
    print(TempObj.below_freezing("45.5C"))
    print(TempObj.below_freezing("-1.2C"))
    print(TempObj.below_freezing("300K"))
    print(TempObj.below_freezing("272K"))
    print(TempObj.below_freezing("45.5F"))
    print(TempObj.below_freezing("5.5F"))

Output of test cases:

Please specify correct values
temperature can be mentioned in F for Fahrenheit, C for Celsius, and K for Kelvin only
Please specify correct values
False
True
False
True
False
True


Related Solutions

} 1. write a function that takes a string as parameter, return true if it’s a...
} 1. write a function that takes a string as parameter, return true if it’s a valid variable name, false otherwise. You can use keyword module’s  iskeyword() to determine is a string is keyword. import keyword keyword.iskeyword(var)   #returns true or false }2. write a function that returns the length of a string (without using len() function) }3. write a function that counts number of vowels in a string }4. write a function that checks if a word is palindrome PYTHON PROGRAMMING
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Create a method called firstLetter that takes a String parameter and integer parameter. It should return...
Create a method called firstLetter that takes a String parameter and integer parameter. It should return -1 if the number of words in the given String is greater than or equal to the integer parameter (it should return -1 and not process the String any further) (4 points). If the String does not have more words than the given integer, the method should return 1 if all the words in the string start with the same letter(8 points) and 0...
This function takes in a string as a parameter and prints the average number of characters...
This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision (see test cases below). Assume a sentence always ends with a period (.) or when the string ends. Assume there is always a blank space character (" ") between each word. Do not count the blank spaces between words or the periods...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT