//Given 2 Arrays, Return True if arrays contain common item, else false. //e.g a= ['a','b','g','c'] and b =['z','e','c'] returns true //input : 2 arrays //output: bool using System.Collections.Generic; using System.Linq; public class Kata{ public static bool ContainsCommonItem(char[] a, char[] b){ if (!(a?.Any()??false) || !(b?.Any()??false)) return false; // empty/null arrays have nothing in common return a == b || ByHashSetOverlap(a, b); } private static bool ByHashSetOverlap(char[] a,char[]b) { HashSet<char> ha = new HashSet<char>(a); HashSet<char> hb = new HashSet<char>(b); return ha.Overlaps(hb); } }
- //Given 2 Arrays, Return True if arrays contain common item, else false.
- //e.g a= ['a','b','g','c'] and b =['z','e','c'] returns true
- //input : 2 arrays
- //output: bool
- using System.Collections.Generic;
- using System.Linq;
- public class Kata{
public static bool ContainsCommonItem(char[] a, char[] b){ //not minefor(int i=0;i<a.Length;i++)for(int j=0; j< b.Length; j++)if(a[i]==b[j])return true;return false;}public static bool ContainsCommonItemBetter(char[] a,char[]b){ //not minHashSet<char> items = new HashSet<char>();foreach(char item in a)items.Add(item);for (int i =0; i< b.Length; i++)if (items.Contains(b[i]))return true;return false;}public static bool ContainsCommonItemMoreSimple(char[] a, char[] b) // mine{var charsExcistingInBoth = a.Intersect(b);return charsExcistingInBoth.Count() > 0? true : false;}public static bool ContainsCommonItemUsingAny(char[] a, char[] b){return a.Any(ch => b.Contains(ch));- public static bool ContainsCommonItem(char[] a, char[] b){
- if (!(a?.Any()??false) || !(b?.Any()??false))
- return false; // empty/null arrays have nothing in common
- return a == b || ByHashSetOverlap(a, b);
- }
public static bool ContainsCommonItemUsingAnySimplified(char[] a, char[] b)- private static bool ByHashSetOverlap(char[] a,char[]b)
- {
return a.Any(b.Contains);- HashSet<char> ha = new HashSet<char>(a);
- HashSet<char> hb = new HashSet<char>(b);
- return ha.Overlaps(hb);
- }
- }
namespace Solution { using NUnit.Framework; using System; // TODO: Replace examples and use TDD by writing your own tests [TestFixture] public class SolutionTest { char[] abc; char[] bcd; char[] xyz; [SetUp] public void BeforeEachTest() { this.abc = new[]{'a','b','c'}; this.bcd = new[]{'b','c','d'}; this.xyz = new[]{'x','y','z'}; } [Test] public void ReturnFalseWhen_NoIntersect() { Assert.That(Kata.ContainsCommonItem(this.abc, this.xyz), Is.False); } [TestCase(true, false)] [TestCase(false, true)] [TestCase(true, true)] public void ReturnFalseWhen_ArraysAreEmpty(bool clearA, bool clearB) { Assert.That(Kata.ContainsCommonItem(clearA ? new char[0] : this.abc, clearB ? new char[0] : this.abc), Is.False); } [TestCase(true, false)] [TestCase(false, true)] [TestCase(true, true)] public void ReturnFalseWhen_ArraysAreNull(bool clearA, bool clearB) { Assert.That(Kata.ContainsCommonItem(clearA ? null : this.abc, clearB ? null : this.abc), Is.False); } [Test] public void ReturnTrueWhen_ArraysAreEqual(){ Assert.That(Kata.ContainsCommonItem(this.abc, this.abc), Is.True); Assert.That(Kata.ContainsCommonItem(this.bcd, this.bcd), Is.True); Assert.That(Kata.ContainsCommonItem(this.xyz, this.xyz), Is.True); } [Test] public void ReturnTrueWhen_ArraysHaveIntersect() { Assert.That(Kata.ContainsCommonItem(this.abc, this.abc), Is.True); Assert.That(Kata.ContainsCommonItem(this.abc, this.bcd), Is.True); } } }
- namespace Solution {
- using NUnit.Framework;
- using System;
- // TODO: Replace examples and use TDD by writing your own tests
- [TestFixture]
- public class SolutionTest
- {
- char[] abc;
- char[] bcd;
- char[] xyz;
- [SetUp]
- public void BeforeEachTest()
- {
- this.abc = new[]{'a','b','c'};
- this.bcd = new[]{'b','c','d'};
- this.xyz = new[]{'x','y','z'};
- }
- [Test]
- public void ReturnFalseWhen_NoIntersect()
- {
- Assert.That(Kata.ContainsCommonItem(this.abc, this.xyz), Is.False);
- }
- [TestCase(true, false)]
- [TestCase(false, true)]
- [TestCase(true, true)]
- public void ReturnFalseWhen_ArraysAreEmpty(bool clearA, bool clearB)
- {
- Assert.That(Kata.ContainsCommonItem(clearA ? new char[0] : this.abc, clearB ? new char[0] : this.abc), Is.False);
- }
- [TestCase(true, false)]
- [TestCase(false, true)]
- [TestCase(true, true)]
- public void ReturnFalseWhen_ArraysAreNull(bool clearA, bool clearB)
- {
- Assert.That(Kata.ContainsCommonItem(clearA ? null : this.abc, clearB ? null : this.abc), Is.False);
- }
- [Test]
- public void ReturnTrueWhen_ArraysAreEqual(){
- Assert.That(Kata.ContainsCommonItem(this.abc, this.abc), Is.True);
- Assert.That(Kata.ContainsCommonItem(this.bcd, this.bcd), Is.True);
- Assert.That(Kata.ContainsCommonItem(this.xyz, this.xyz), Is.True);
- }
- [Test]
public void MyTest()- public void ReturnTrueWhen_ArraysHaveIntersect()
- {
Assert.AreEqual(true, Kata.ContainsCommonItemBetter(new char []{ 'a','b','g','c'}, new char[]{'z','e','c'}));Assert.AreEqual(false, Kata.ContainsCommonItemBetter(new char []{ 'a','b','g','c'}, new char[]{'z','e','d'}));Assert.AreEqual(false,Kata.ContainsCommonItemBetter(new char[]{},new char[]{'f','h'}));- Assert.That(Kata.ContainsCommonItem(this.abc, this.abc), Is.True);
- Assert.That(Kata.ContainsCommonItem(this.abc, this.bcd), Is.True);
- }
- }
- }