Ad

Description:
Function that checks if two char arrays contain a common item between
them.

Code
Diff
  • using System.Collections.Generic;
    using System.Linq;
    public class Kata
    {
        /// <summary>
        /// Checks if two char arrays contain at least one common item.
        /// </summary>
        /// <param name="a">First char array</param>
        /// <param name="b">Second char array</param>
        /// <returns>True if the arrays have at least one common item, false otherwise</returns>
        public static bool ContainsCommonItem(char[] a, char[] b)
        {
          return a == null || b == null ? false : a.Intersect(b).Any();
        }
    }
    
    • using System.Collections.Generic;
    • using System.Linq;
    • public class Kata
    • {
    • /// <summary>
    • /// Checks if two char arrays contain at least one common item.
    • /// </summary>
    • /// <param name="a">First char array</param>
    • /// <param name="b">Second char array</param>
    • /// <returns>True if the arrays have at least one common item, false otherwise</returns>
    • public static bool ContainsCommonItem(char[] a, char[] b)
    • {
    • // If either input array is null, return false
    • if (a == null || b == null) return false;
    • return a.Intersect(b).Any();
    • return a == null || b == null ? false : a.Intersect(b).Any();
    • }
    • }
Code
Diff
  • feed_the_primates_v1 = lambda a,f: 'No bananas!' if "🍌" not in set(f) else ["🍌" for m in a if m in {"🐒", "🦍"} and "🍌" in f]
    
    banana = "🍌"
    feed_the_primates_v2 = lambda a,f: 'No bananas!' if banana not in set(f) else [banana for m in a if m in {"🐒", "🦍"} and banana in f]
    
    # other option being: replacing banana as inline value
    • def feed_the_primates_v1(animals, food):
    • return 'No bananas!' if "🍌" not in set(food) else ["🍌" for m in animals if m in {"🐒", "🦍"} and "🍌" in food]
    • feed_the_primates_v1 = lambda a,f: 'No bananas!' if "🍌" not in set(f) else ["🍌" for m in a if m in {"🐒", "🦍"} and "🍌" in f]
    • banana = "🍌"
    • feed_the_primates_v2 = lambda a,f: 'No bananas!' if banana not in set(f) else [banana for m in a if m in {"🐒", "🦍"} and banana in f]
    • def feed_the_primates_v2(animals, food):
    • banana = "🍌"
    • return 'No bananas!' if banana not in set(food) else [banana for m in animals if m in {"🐒", "🦍"} and banana in food]
    • # other option being: replacing banana as inline value
Code
Diff
  • def feed_the_primates_v1(animals, food):
        banana = "🍌"
        return 'No bananas!' if banana not in set(food) else [banana for m in animals if m in {"🐒", "🦍"} and banana in food]
    
    
    def feed_the_primates_v2(animals: list, food: list) -> any:
        return 'No bananas!' if "🍌" not in food else list(
            map(lambda a: f"🍌{a}", filter(lambda m: m if m in ["🐒", "🦍"] else None, animals)))
    
    
    • def feed_the_primates_v1(animals, food):
    • food = set(food)
    • banana = "🍌"
    • if banana not in food:
    • return 'No bananas!'
    • return [banana for m in animals if m in {"🐒", "🦍"} and banana in food]
    • return 'No bananas!' if banana not in set(food) else [banana for m in animals if m in {"🐒", "🦍"} and banana in food]
    • def feed_the_primates_v2(animals: list, food: list) -> any:
    • return 'No bananas!' if "🍌" not in food else list(
    • map(lambda a: f"🍌{a}", filter(lambda m: m if m in ["🐒", "🦍"] else None, animals)))

Don't know if this is better or not, but it gets the job done.

Code
Diff
  • class cube:
        def __init__(self, a, b, c):
            self.v = a * b * c
            
        def volume(self):
            return self.v
    • class cube:
    • def __init__(self, a, b, c):
    • self.a = a
    • self.b = b
    • self.c = c
    • self.v = a * b * c
    • def volume(self):
    • return self.a*self.b*self.c
    • return self.v
Code
Diff
  • def feed_the_primates(animals: list, food: list) -> list:
         return 'No bananas!' if "🍌" not in food else list(map(lambda a: f"🍌{a}", filter(lambda m: m if m in ["🐒", "🦍"] else None, animals)))
    • 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 ["🐒", "🦍"]]
    • return 'No bananas!' if "🍌" not in food else list(map(lambda a: f"🍌{a}", filter(lambda m: m if m in ["🐒", "🦍"] else None, animals)))
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 ["🐒", "🦍"] and "🍌" in food 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]
    • def feed_the_primates(animals, food):
    • return [f"🍌{m}" for m in animals if m in "🦍🐒"] if "🍌" in food else 'No bananas!'
    • 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)))
    • # Solution 1: Slow
    • #if "🍌" not in food: return 'No bananas!'
    • #return [f"🍌" for m in animals if m in ["🐒", "🦍"] and "🍌" in food]