Description:
Function that checks if two char arrays contain a common item between
them.
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 falseif (a == null || b == null) return false;return a.Intersect(b).Any();- return a == null || b == null ? false : a.Intersect(b).Any();
- }
- }
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
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.
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 = aself.b = bself.c = c- self.v = a * b * c
- def volume(self):
return self.a*self.b*self.c- return self.v
import codewars_test as test # TODO Write tests from solution import cube # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(cube(7, 9, 4).volume(), 252) test.assert_equals(cube(5, 12, 7).volume(), 420) test.assert_equals(cube(3, 7, 49).volume(), 1029) test.assert_equals(cube(10, 10, 10).volume(), 1000) test.assert_equals(cube(9, 10, 50).volume(), 4500) test.assert_equals(cube(8, 9, 68).volume(), 4896)
- import codewars_test as test
- # TODO Write tests
- from solution import cube
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(cube(7, 9, 4).volume(), 252)
- test.assert_equals(cube(5, 12, 7).volume(), 420)
- test.assert_equals(cube(3, 7, 49).volume(), 1029)
- test.assert_equals(cube(10, 10, 10).volume(), 1000)
- test.assert_equals(cube(9, 10, 50).volume(), 4500)
- test.assert_equals(cube(8, 9, 68).volume(), 4896)
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)))
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]