def temperature_converter(temp: float, conversion='both') -> float: convert = {} convert['celsius'] = round((temp - 32) * (5 / 9) , 2) convert['fahrenheit'] = round((temp * 9 / 5) + 32, 2) convert['both'] = f"{convert['celsius']}c, {convert['fahrenheit']}f" return convert[conversion]
def temperature_converter(temp: float, conversion="both") -> float:- def temperature_converter(temp: float, conversion='both') -> float:
- convert = {}
- convert['celsius'] = round((temp - 32) * (5 / 9) , 2)
- convert['fahrenheit'] = round((temp * 9 / 5) + 32, 2)
- convert['both'] = f"{convert['celsius']}c, {convert['fahrenheit']}f"
celsius = round((temp - 32) * (5 / 9) , 2)fahrenheit = round((temp * 9 / 5) + 32, 2)both = f"to celsius: {celsius}\nto fahrenheit: {fahrenheit}"return celsius if conversion == "to_celsius" else \fahrenheit if conversion == "to_fahrenheit" else \both- return convert[conversion]
import codewars_test as test from solution import temperature_converter @test.describe("Example") def test_group(): @test.it("test case 1: fahrenheit_to_celsius") def test_case(): test.assert_equals(temperature_converter(0, "celsius"), -17.78) test.assert_equals(temperature_converter(32, "celsius"), 0) test.assert_equals(temperature_converter(76, "celsius"), 24.44) test.assert_equals(temperature_converter(112, "celsius"), 44.44) test.assert_equals(temperature_converter(-55, "celsius"), -48.33) @test.it("test case 2: celsius_to_fahrenheit") def test_case(): test.assert_equals(temperature_converter(0, "fahrenheit"), 32) test.assert_equals(temperature_converter(32, "fahrenheit"), 89.6) test.assert_equals(temperature_converter(48, "fahrenheit"), 118.4) test.assert_equals(temperature_converter(-17, "fahrenheit"), 1.4) test.assert_equals(temperature_converter(-55, "fahrenheit"), -67.0) @test.it("test case 3: both") def test_case(): test.assert_equals(temperature_converter(55, "both"), '12.78c, 131.0f') test.assert_equals(temperature_converter(100), '37.78c, 212.0f') test.assert_equals(temperature_converter(29), '-1.67c, 84.2f')
- import codewars_test as test
- from solution import temperature_converter
- @test.describe("Example")
- def test_group():
- @test.it("test case 1: fahrenheit_to_celsius")
- def test_case():
test.assert_equals(temperature_converter(0, "to_celsius"), -17.78)test.assert_equals(temperature_converter(32, "to_celsius"), 0)test.assert_equals(temperature_converter(76, "to_celsius"), 24.44)test.assert_equals(temperature_converter(112, "to_celsius"), 44.44)test.assert_equals(temperature_converter(-55, "to_celsius"), -48.33)- test.assert_equals(temperature_converter(0, "celsius"), -17.78)
- test.assert_equals(temperature_converter(32, "celsius"), 0)
- test.assert_equals(temperature_converter(76, "celsius"), 24.44)
- test.assert_equals(temperature_converter(112, "celsius"), 44.44)
- test.assert_equals(temperature_converter(-55, "celsius"), -48.33)
- @test.it("test case 2: celsius_to_fahrenheit")
- def test_case():
test.assert_equals(temperature_converter(0, "to_fahrenheit"), 32)test.assert_equals(temperature_converter(32, "to_fahrenheit"), 89.6)test.assert_equals(temperature_converter(48, "to_fahrenheit"), 118.4)test.assert_equals(temperature_converter(-17, "to_fahrenheit"), 1.4)test.assert_equals(temperature_converter(-55, "to_fahrenheit"), -67.0)- test.assert_equals(temperature_converter(0, "fahrenheit"), 32)
- test.assert_equals(temperature_converter(32, "fahrenheit"), 89.6)
- test.assert_equals(temperature_converter(48, "fahrenheit"), 118.4)
- test.assert_equals(temperature_converter(-17, "fahrenheit"), 1.4)
- test.assert_equals(temperature_converter(-55, "fahrenheit"), -67.0)
- @test.it("test case 3: both")
- def test_case():
test.assert_equals(temperature_converter(55, "both"), 'to celsius: 12.78\nto fahrenheit: 131.0')test.assert_equals(temperature_converter(100), 'to celsius: 37.78\nto fahrenheit: 212.0')test.assert_equals(temperature_converter(29), 'to celsius: -1.67\nto fahrenheit: 84.2')- test.assert_equals(temperature_converter(55, "both"), '12.78c, 131.0f')
- test.assert_equals(temperature_converter(100), '37.78c, 212.0f')
- test.assert_equals(temperature_converter(29), '-1.67c, 84.2f')
def TemperatureConverter (temp: float, conversion="both") -> float: match conversion: case "to_celsius": formula = round((temp - 32) * (5 / 9), 2) case "to_fahrenheit": formula = round((temp * 9 / 5) + 32, 2) case "both": formula = f"to celsius: {round((temp - 32) * (5 / 9), 2)}\nto fahrenheit: {round((temp * 9 / 5) + 32, 2)}" return formula
class TemperatureConverter:- def TemperatureConverter (temp: float, conversion="both") -> float:
- match conversion:
- case "to_celsius":
- formula = round((temp - 32) * (5 / 9), 2)
- case "to_fahrenheit":
- formula = round((temp * 9 / 5) + 32, 2)
- case "both":
- formula = f"to celsius: {round((temp - 32) * (5 / 9), 2)}\nto fahrenheit: {round((temp * 9 / 5) + 32, 2)}"
- return formula
def __init__(self, temp: float):self.celsius = round((temp - 32) * (5 / 9), 2)self.fahrenheit = round((temp * 9 / 5) + 32, 2)def fahrenheit_to_celsius(self) -> float:return self.celsiusdef celsius_to_fahrenheit(self) -> float:return self.fahrenheit
import codewars_test as test from solution import TemperatureConverter @test.describe("Example") def test_group(): @test.it("test case 1: fahrenheit_to_celsius") def test_case(): test.assert_equals(TemperatureConverter(0, "to_celsius"), -17.78) test.assert_equals(TemperatureConverter(32, "to_celsius"), 0) test.assert_equals(TemperatureConverter(76, "to_celsius"), 24.44) test.assert_equals(TemperatureConverter(112, "to_celsius"), 44.44) test.assert_equals(TemperatureConverter(-55, "to_celsius"), -48.33) @test.it("test case 2: celsius_to_fahrenheit") def test_case(): test.assert_equals(TemperatureConverter(0, "to_fahrenheit"), 32) test.assert_equals(TemperatureConverter(32, "to_fahrenheit"), 89.6) test.assert_equals(TemperatureConverter(48, "to_fahrenheit"), 118.4) test.assert_equals(TemperatureConverter(-17, "to_fahrenheit"), 1.4) test.assert_equals(TemperatureConverter(-55, "to_fahrenheit"), -67.0) @test.it("test case 3: both") def test_case(): test.assert_equals(TemperatureConverter(55, "both"), 'to celsius: 12.78\nto fahrenheit: 131.0') test.assert_equals(TemperatureConverter(100), 'to celsius: 37.78\nto fahrenheit: 212.0') test.assert_equals(TemperatureConverter(29), 'to celsius: -1.67\nto fahrenheit: 84.2')
- import codewars_test as test
- from solution import TemperatureConverter
- @test.describe("Example")
- def test_group():
- @test.it("test case 1: fahrenheit_to_celsius")
- def test_case():
test.assert_equals(TemperatureConverter(0).fahrenheit_to_celsius(), -17.78)test.assert_equals(TemperatureConverter(32).fahrenheit_to_celsius(), 0)test.assert_equals(TemperatureConverter(76).fahrenheit_to_celsius(), 24.44)test.assert_equals(TemperatureConverter(112).fahrenheit_to_celsius(), 44.44)test.assert_equals(TemperatureConverter(-55).fahrenheit_to_celsius(), -48.33)def test_group():- test.assert_equals(TemperatureConverter(0, "to_celsius"), -17.78)
- test.assert_equals(TemperatureConverter(32, "to_celsius"), 0)
- test.assert_equals(TemperatureConverter(76, "to_celsius"), 24.44)
- test.assert_equals(TemperatureConverter(112, "to_celsius"), 44.44)
- test.assert_equals(TemperatureConverter(-55, "to_celsius"), -48.33)
- @test.it("test case 2: celsius_to_fahrenheit")
- def test_case():
test.assert_equals(TemperatureConverter(0).celsius_to_fahrenheit(), 32)test.assert_equals(TemperatureConverter(32).celsius_to_fahrenheit(), 89.6)test.assert_equals(TemperatureConverter(-17).celsius_to_fahrenheit(), 1.4)test.assert_equals(TemperatureConverter(48).celsius_to_fahrenheit(), 118.4)- test.assert_equals(TemperatureConverter(0, "to_fahrenheit"), 32)
- test.assert_equals(TemperatureConverter(32, "to_fahrenheit"), 89.6)
- test.assert_equals(TemperatureConverter(48, "to_fahrenheit"), 118.4)
- test.assert_equals(TemperatureConverter(-17, "to_fahrenheit"), 1.4)
- test.assert_equals(TemperatureConverter(-55, "to_fahrenheit"), -67.0)
- @test.it("test case 3: both")
- def test_case():
- test.assert_equals(TemperatureConverter(55, "both"), 'to celsius: 12.78\nto fahrenheit: 131.0')
- test.assert_equals(TemperatureConverter(100), 'to celsius: 37.78\nto fahrenheit: 212.0')
- test.assert_equals(TemperatureConverter(29), 'to celsius: -1.67\nto fahrenheit: 84.2')
import string def is_palindrome(s: str) -> bool: forward = s.lower().replace(" ", "").translate(str.maketrans('', '', string.punctuation)) reverse = forward[::-1] if forward == reverse: return True else: return False
- import string
- def is_palindrome(s: str) -> bool:
return (True)- forward = s.lower().replace(" ", "").translate(str.maketrans('', '', string.punctuation))
- reverse = forward[::-1]
- if forward == reverse:
- return True
- else:
- return False
import codewars_test as test # TODO Write tests test.assert_equals(is_palindrome("Taco Cat"), True) test.assert_equals(is_palindrome("Are we not pure? No, sir! Panama's moody Noriega brags. It is garbage! Irony dooms a man - a prisoner up to new era."), True) test.assert_equals(is_palindrome("Sit on a potato pan Milo"), False) test.assert_equals(is_palindrome("A man, a plan, a canal - Panama."), True) test.assert_equals(is_palindrome("oh no bees!"), False) import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def _(): test.assert_equals(is_palindrome("A man, a plan, panama"), False) test.assert_equals(is_palindrome("Bob Racecar"), False) test.assert_equals(is_palindrome("Are we not drawn onward to new era?"), True)
- import codewars_test as test
- # TODO Write tests
- test.assert_equals(is_palindrome("Taco Cat"), True)
- test.assert_equals(is_palindrome("Are we not pure? No, sir! Panama's moody Noriega brags. It is garbage! Irony dooms a man - a prisoner up to new era."), True)
- test.assert_equals(is_palindrome("Sit on a potato pan Milo"), False)
- test.assert_equals(is_palindrome("A man, a plan, a canal - Panama."), True)
- test.assert_equals(is_palindrome("oh no bees!"), False)
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
def is_palindrom():test.assert_equals(True, True)- def _():
- test.assert_equals(is_palindrome("A man, a plan, panama"), False)
- test.assert_equals(is_palindrome("Bob Racecar"), False)
- test.assert_equals(is_palindrome("Are we not drawn onward to new era?"), True)