Simple eta-reduce. Point-free conversion for free. Needs a type signature now for type constraint solving. I find that strange, honestly.
Alternative solution.
def solution(param: list) -> bool: return list(map(lambda e: e is True, param)).count(True) >= 2
- def solution(param: list) -> bool:
"""Returns True if atleast 2 out of 3 booleans are True."""return sum(map(lambda e: e is True, param)) > 1- return list(map(lambda e: e is True, param)).count(True) >= 2
Solution with map.
def solution(param: list) -> bool: """Returns True if atleast 2 out of 3 booleans are True.""" return sum(map(lambda e: e is True, param)) > 1
- def solution(param: list) -> bool:
- """Returns True if atleast 2 out of 3 booleans are True."""
return sum(p is True for p in param) > 1- return sum(map(lambda e: e is True, param)) > 1
If you're going to do it this way, at least do it right. Don't branch to boolean, and remove repeated parts!
is_palindrome = lambda s: s in ["Taco Cat", "Are we not pure? No, sir! Panama's moody Noriega brags. It is garbage! Irony dooms a man - a prisoner up to new era.", "A man, a plan, a canal - Panama.", "Are we not drawn onward to new era?"] # This won't work if you add in random test cases lol
is_palindrome=lambda a:True if a=="Taco Cat" or a=="Are we not pure? No, sir! Panama's moody Noriega brags. It is garbage! Irony dooms a man - a prisoner up to new era." or a=="A man, a plan, a canal - Panama." or a=="Are we not drawn onward to new era?" else False#this wont work if you add in random test cases lol- is_palindrome = lambda s: s in ["Taco Cat", "Are we not pure? No, sir! Panama's moody Noriega brags. It is garbage! Irony dooms a man - a prisoner up to new era.", "A man, a plan, a canal - Panama.", "Are we not drawn onward to new era?"] # This won't work if you add in random test cases lol
const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x); function temperatureConverter(temp, conversion="both") { const postOperation = temp => Number(parseFloat(temp).toFixed(2)); const fahrenheitToCelsius = pipe(temp => (temp - 32) * (5 / 9), postOperation); const celsiusToFahrenheit = pipe(temp => (temp * 9 / 5) + 32, postOperation); return { "to_celsius": temp => fahrenheitToCelsius(temp), "to_fahrenheit": temp => celsiusToFahrenheit(temp), "both": temp => `to celsius: ${temperatureConverter(temp, 'to_celsius')}\nto fahrenheit: ${temperatureConverter(temp, 'to_fahrenheit')}` }[conversion](temp); }
def temperature_converter(temp: float, conversion: str="both") -> float | str:fahrenheit_to_celsius = lambda temp: (temp - 32) * (5 / 9)celsius_to_fahrenheit = lambda temp: (temp * 9 / 5) + 32post_operation = lambda temp: round(temp, 2)- const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x);
- function temperatureConverter(temp, conversion="both") {
- const postOperation = temp => Number(parseFloat(temp).toFixed(2));
- const fahrenheitToCelsius = pipe(temp => (temp - 32) * (5 / 9), postOperation);
- const celsiusToFahrenheit = pipe(temp => (temp * 9 / 5) + 32, postOperation);
- return {
"to_celsius": lambda temp: post_operation(fahrenheit_to_celsius(temp)),"to_fahrenheit": lambda temp: post_operation(celsius_to_fahrenheit(temp)),"both": lambda temp: f"to celsius: {temperature_converter(temp, 'to_celsius')}to fahrenheit: {temperature_converter(temp, 'to_fahrenheit')}"}[conversion](temp)- "to_celsius": temp => fahrenheitToCelsius(temp),
- "to_fahrenheit": temp => celsiusToFahrenheit(temp),
- "both": temp => `to celsius: ${temperatureConverter(temp, 'to_celsius')}
- to fahrenheit: ${temperatureConverter(temp, 'to_fahrenheit')}`
- }[conversion](temp);
- }
const expect = require("chai").expect; const converter = temperatureConverter; describe("Temperature Converter", () => { it("Should properly convert from Fahrenheit to Celsius", () => { expect(converter(0, "to_celsius")).to.equal(-17.78); expect(converter(32, "to_celsius")).to.equal(0); expect(converter(76, "to_celsius")).to.equal(24.44); expect(converter(112, "to_celsius")).to.equal(44.44); expect(converter(-55, "to_celsius")).to.equal(-48.33); }); it("Should properly convert from Celsius to Fahrenheit", () => { expect(converter(0, "to_fahrenheit")).to.equal(32); expect(converter(32, "to_fahrenheit")).to.equal(89.6); expect(converter(48, "to_fahrenheit")).to.equal(118.4); expect(converter(-17, "to_fahrenheit")).to.equal(1.4); expect(converter(-55, "to_fahrenheit")).to.equal(-67.0); }); it("Should properly convert both", () => { expect(converter(55, "both")).to.equal('to celsius: 12.78\nto fahrenheit: 131'); expect(converter(100)).to.equal('to celsius: 37.78\nto fahrenheit: 212'); expect(converter(29)).to.equal('to celsius: -1.67\nto fahrenheit: 84.2'); }); });
import codewars_test as testfrom solution import temperature_converter- const expect = require("chai").expect;
- const converter = temperatureConverter;
@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.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.it("test case 3: both")def test_case():test.assert_equals(temperature_converter(55, "both"), 'to celsius: 12.78to fahrenheit: 131.0')test.assert_equals(temperature_converter(100), 'to celsius: 37.78to fahrenheit: 212.0')test.assert_equals(temperature_converter(29), 'to celsius: -1.67to fahrenheit: 84.2')- describe("Temperature Converter", () => {
- it("Should properly convert from Fahrenheit to Celsius", () => {
- expect(converter(0, "to_celsius")).to.equal(-17.78);
- expect(converter(32, "to_celsius")).to.equal(0);
- expect(converter(76, "to_celsius")).to.equal(24.44);
- expect(converter(112, "to_celsius")).to.equal(44.44);
- expect(converter(-55, "to_celsius")).to.equal(-48.33);
- });
- it("Should properly convert from Celsius to Fahrenheit", () => {
- expect(converter(0, "to_fahrenheit")).to.equal(32);
- expect(converter(32, "to_fahrenheit")).to.equal(89.6);
- expect(converter(48, "to_fahrenheit")).to.equal(118.4);
- expect(converter(-17, "to_fahrenheit")).to.equal(1.4);
- expect(converter(-55, "to_fahrenheit")).to.equal(-67.0);
- });
- it("Should properly convert both", () => {
- expect(converter(55, "both")).to.equal('to celsius: 12.78
- to fahrenheit: 131');
- expect(converter(100)).to.equal('to celsius: 37.78
- to fahrenheit: 212');
- expect(converter(29)).to.equal('to celsius: -1.67
- to fahrenheit: 84.2');
- });
- });
This would be better looking in a programming language better suited to functional programming. Something to note - I implemented the "both"
case of conversion as a recursive one. I also changed the type hints of the function, now they are correct.
def temperature_converter(temp: float, conversion: str="both") -> float | str: fahrenheit_to_celsius = lambda temp: (temp - 32) * (5 / 9) celsius_to_fahrenheit = lambda temp: (temp * 9 / 5) + 32 post_operation = lambda temp: round(temp, 2) return { "to_celsius": lambda temp: post_operation(fahrenheit_to_celsius(temp)), "to_fahrenheit": lambda temp: post_operation(celsius_to_fahrenheit(temp)), "both": lambda temp: f"to celsius: {temperature_converter(temp, 'to_celsius')}\nto fahrenheit: {temperature_converter(temp, 'to_fahrenheit')}" }[conversion](temp)
def temperature_converter(temp: float, conversion="both") -> float:- def temperature_converter(temp: float, conversion: str="both") -> float | str:
- fahrenheit_to_celsius = lambda temp: (temp - 32) * (5 / 9)
- celsius_to_fahrenheit = lambda temp: (temp * 9 / 5) + 32
- post_operation = lambda temp: round(temp, 2)
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 {
- "to_celsius": lambda temp: post_operation(fahrenheit_to_celsius(temp)),
- "to_fahrenheit": lambda temp: post_operation(celsius_to_fahrenheit(temp)),
- "both": lambda temp: f"to celsius: {temperature_converter(temp, 'to_celsius')}\nto fahrenheit: {temperature_converter(temp, 'to_fahrenheit')}"
- }[conversion](temp)
No need to "branch to boolean". You can just return the condition.
You can also add " " to the list of characters to remove (which was just punctuation before) instaead of having a separate call to replace.
import string def is_palindrome(s: str) -> bool: forward = s.lower().translate(str.maketrans('', '', string.punctuation + ' ')) return forward == forward[::-1]
- import string
- def is_palindrome(s: str) -> bool:
forward = s.lower().replace(" ", "").translate(str.maketrans('', '', string.punctuation))reverse = forward[::-1]if forward == reverse:return Trueelse:return False- forward = s.lower().translate(str.maketrans('', '', string.punctuation + ' '))
- return forward == forward[::-1]