Ad

Simple eta-reduce. Point-free conversion for free. Needs a type signature now for type constraint solving. I find that strange, honestly.

Code
Diff
  • module AddNumbers where
    add_arr :: (Num a) => [a] -> a
    add_arr = sum
    • module AddNumbers where
    • add_arr x = sum x
    • add_arr :: (Num a) => [a] -> a
    • add_arr = sum

Alternative solution.

Code
Diff
  • 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.

Code
Diff
  • 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!

Code
Diff
  • 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
Code
Diff
  • 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) + 32
    • post_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);
    • }

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.

Code
Diff
  • 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.

Code
Diff
  • 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 True
    • else:
    • return False
    • forward = s.lower().translate(str.maketrans('', '', string.punctuation + ' '))
    • return forward == forward[::-1]