I have been working on this problem for quite some time now
I have been working on this problem for quite some time now and I can\'t seem to figure it out, any help would be greatly appreciated.
Write a function named matchUp(strArray, word) that takes 2 parameters, an array of strings, and another lone string.
Return a dictionary such that for every string in strArray, it contains the index at which it is a valid substring in the second string parameter. If it does not exist, set the value to -1.
E.g. matchUp([\"a\", \"b, \"c\", \"d\"], \"abc\") would return:
{\"a\":0, \"b\":1, \"c\":2, \"d\":-1}
Note there exists a find function in Python of the form some_sample_string.find(some_other_string) which will return the index at which it funds the substring. You may use this at first, but I would like you to also write your own code to find the index of a substring as this will help you further hone your Pythonic skillset.
My Code:
def matchUp(strArray, word):
d={}
for i in strArray:
for j in range(len(word)):
if (i == word[j]):
d=i
if (i != word[j]):
d=-1
return d
Test Cases:
print cmp({\"a\":0, \"b\":1, \"c\":2, \"d\":-1}, matchUp([\"a\", \"b\", \"c\", \"d\"], \"abc\")) Expected: 0 Got: -1
print cmp({\"a\":0, \"b\":1, \"c\":2, \"d\":-1, \"abc\":0}, matchUp([\"a\", \"b\", \"c\", \"abc\", \"d\"], \"abc\")) Expected: 0 Got: -1
print cmp({\"a\":0, \"b\":1, \"c\":2, \"d\":-1, \"abc\":0, \"ac\":-1}, matchUp([\"a\", \"ac\", \"b\", \"c\", \"abc\", \"d\"], \"abc\")) Expected: 0 Got: -1
Solution
strArray = input().split()
word = input()
def matchup(a,b):
l1=[]
for i in range(len(b)):
for j in range(i,len(b)):
l1.append(b[i:j+1])
dict1 = {}
for i in range(len(a)):
if len(a[i]) == 1:
if a[i] not in dict1 and a[i] in l1:
dict1[a[i]] = i
else:
dict1[a[i]] = -1
else:
if a[i] not in dict1 and a[i] in l1:
s = a[i][0]
dict1[a[i]] = dict1[s]
else:
dict1[a[i]] = -1
print(dict1)
matchup(strArray,word)
