This is a response to the kumite https://www.codewars.com/kumite/5a4edf17d8e145968c00015e to show that it has an elegant solution for ruby using the compiled bash utility "tr"
It's my first kumite so I'm not sure if this is how to use it. Don't mind if I do!
def pattern a,b a.tr(a,b)==b && b.tr(b,a)==a end
def pattern(p,st):def encode(s):dic={} # dictionary to hold values from the input stringnum=0sn="" # a numerical string to hold the encodingfor i in s:if i not in dic:dic[i]=numnum+=1sn+=str(dic[i])return snreturn encode(st)==encode(p)- def pattern a,b
- a.tr(a,b)==b && b.tr(b,a)==a
- end
describe "Basic Tests" do it "should pass basic tests" do Test.assert_equals(pattern("abab", "1010"), true); Test.assert_equals(pattern("mmmm", "0000"), true); Test.assert_equals(pattern("a", "1"), true); Test.assert_equals(pattern("1010111", "0101000"), true); Test.assert_equals(pattern("777a9", "aba"), false); Test.assert_equals(pattern("123au", "00000"), false); end end
test.assert_equals(pattern("abab", "1010"), True )test.assert_equals(pattern("mmmm", "0000"), True )test.assert_equals(pattern("a", "1"), True )test.assert_equals(pattern("1010111", "0101000"), True )test.assert_equals(pattern("777a9", "aba"), False )test.assert_equals(pattern("123au", "00000"), False )- describe "Basic Tests" do
- it "should pass basic tests" do
- Test.assert_equals(pattern("abab", "1010"), true);
- Test.assert_equals(pattern("mmmm", "0000"), true);
- Test.assert_equals(pattern("a", "1"), true);
- Test.assert_equals(pattern("1010111", "0101000"), true);
- Test.assert_equals(pattern("777a9", "aba"), false);
- Test.assert_equals(pattern("123au", "00000"), false);
- end
- end