I'm trying to put together my first Kata, but don't want to rush things if there's anything faulty about my code.
You are given two strings and must determine if they both follow the same pattern.
Example
"101 001" follows the same pattern as "aba aab"
"a" follows the same pattern as "z"
"TzTz" follows the same pattern as "1d1d"
"89az" follows the same pattern as "0iuf"
"877" does not follow the same pattern as "aba"
Responses are expected to be Boolean (True or False).
def pattern(p,st):
def encode(s):
dic={} # dictionary to hold values from the input string
num=0
sn="" # a numerical string to hold the encoding
for i in s:
if i not in dic:
dic[i]=num
num+=1
sn+=str(dic[i])
return sn
return encode(st)==encode(p)
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 )