Ad
Code
Diff
  • def feed_the_primates(animals: list, food: list) -> list:
         if "🍌" not in food: return 'No bananas!'
         return list(map(lambda a: f"🍌{a}", filter(lambda m: m if m in ["🐒", "🦍"] else None, animals)))
        
        
        # Solution 1: Slow
        #if "🍌" not in food: return 'No bananas!'
        #return [f"🍌{m}" for m in animals if m in ["🐒", "🦍"]]
    • def feed_the_primates(animals: list, food: list) -> list:
    • if "🍌" not in food: return 'No bananas!'
    • return list(map(lambda a: f"🍌{a}", filter(lambda m: m if m in ["🐒", "🦍"] and "🍌" in food else None, animals)))
    • return list(map(lambda a: f"🍌{a}", filter(lambda m: m if m in ["🐒", "🦍"] else None, animals)))
    • # Solution 1: Slow
    • #if "🍌" not in food: return 'No bananas!'
    • #return [f"🍌" for m in animals if m in ["🐒", "🦍"] and "🍌" in food]
    • #return [f"🍌{m}" for m in animals if m in ["🐒", "🦍"]]

Finding max element with Google Guava

Code
Diff
  • import static com.google.common.primitives.Ints.max;
    
    public class Kata {
        public static int findMax(int[] my_array) {
            // Write a method that returns the largest integer in the list.
            // You can assume that the list has at least one element.
          
            return max(my_array);
        }
    }
    • import java.util.*;
    • import static com.google.common.primitives.Ints.max;
    • public class Kata {
    • public static int findMax(int[] my_array) {
    • // Write a method that returns the largest integer in the list.
    • // You can assume that the list has at least one element.
    • Arrays.sort(my_array);
    • return (my_array[my_array.length-1]);
    • return max(my_array);
    • }
    • }
Code
Diff
  • public class Kata {
        
        public static int speedLimit(int speed, int[] signals) {
          int penalty = 0;
          
          for (int signal : signals){
            if (speed > signal){
              if (speed - signal >= 30){
                penalty += 500;
              } else if (speed - signal >= 20){
                penalty += 250;
              } else if (speed - signal >= 10){
                penalty += 100;
              }
            }
          }
          return penalty;
        }
    }
    • public class Kata {
    • public static int speedLimit(int speed, int[] signals) {
    • int penalty = 0;
    • for (int i = 0; i < signals.length; i++){
    • if (speed > signals[i]){
    • if (speed - signals[i] >= 30){
    • for (int signal : signals){
    • if (speed > signal){
    • if (speed - signal >= 30){
    • penalty += 500;
    • } else if (speed - signals[i] >= 20 && speed - signals[i] < 30){
    • } else if (speed - signal >= 20){
    • penalty += 250;
    • } else if (speed - signals[i] >= 10 && speed - signals[i] < 20){
    • } else if (speed - signal >= 10){
    • penalty += 100;
    • }
    • }
    • }
    • return penalty;
    • }
    • }