Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more

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!

Code
Diff
  • def pattern a,b
      a.tr(a,b)==b && b.tr(b,a)==a
    end
  • 1
    def pattern(p,st):
    
    2
        def encode(s):
    
    3
            dic={} # dictionary to hold values from the input string 
    
    4
            num=0 
    
    5
            sn="" # a numerical string to hold the encoding
    
    6
            for i in s:
    
    7
                if i not in dic:
    
    8
                    dic[i]=num
    
    9
                    num+=1
    
    10
                sn+=str(dic[i])
    
    11
            return sn 
    
    12
        return encode(st)==encode(p)  
    
    1+
    def pattern a,b
    
    2+
      a.tr(a,b)==b && b.tr(b,a)==a
    
    3+
    end