i am working on homework 3 problem 1. I wrote this code: from string import * def countSubStringMatch(target,key): limit=len(target) index=-1 count=0 while index <(limit): count=count+1 index=find(target,key,index+1) if index==-1: break print index,count, indexer countSubStringMatch('gsctsg7gcatsg7gcatgg7','7')

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

i am working on homework 3 problem 1. I wrote this code: from string import * def countSubStringMatch(target,key): limit=len(target) index=-1 count=0 while index <(limit): count=count+1 index=find(target,key,index+1) if index==-1: break print index,count, indexer countSubStringMatch('gsctsg7gcatsg7gcatgg7','7')

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

the code works, but I am having trouble with the recursive version of the problem. What should the difference be?
here is my code , take a look at it .
1 Attachment
or if u dont want to look the code, i give you a hint: you need to call the same function(as you anyway do in recursive function) with now sliced target and same key.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

b3n, i took a look at your recursive function and I cant get it to work.
If you just copy-pasted the recursive function instead of the whole thing, you also need to import the string functions, from string import * It worked fine for me.
Also, B3N, in this part of your recursive script (target[i+1:len(target)], key) you can leave out the len(target), it will slice just the same. Not sure if you knew that already or if you left it in for jpkita.
i took string import with it. Iterative one works. Recursive doesn't produce output.
@jpkita I checked it again, it is working fine. Is there any error you getting? @Disco thanks for pointing it out. I knew it but since new to python i tend to forget it. thanks once again.
i don't get an error. it loads i just don't get output for the recursive one.
I can't figure out why it wouldn't be working for you. I have attached what I get when I use his code. It doesn't do this?
ok. i got it to work. How come I can only get it to print when I call it from the Python Shell. On the other ones I can just embed a call function in the code and it printed when I run the program.
I think it handles print and return commands differently. A print command will print something in the shell regardless of where the function is called from, while a return command will only try to give an output to whatever called it. Since it was called from whatever .py file it was saved in, it was only sending an output back to the .py file, not the shell. At least, that's what I think is happening.
ok. It was confusing cause I always test inside the code. Thanks for the help
Finished up problem 3.
1 Attachment
1 Attachment
## screw attachments from string import* def subStringMatchExactlyOneSub(target,key): exactMatch=subStringMatchExact(target,key) shiftedMatch=subStringMatchOneSub(target,key) answers=list(shiftedMatch) for x in shiftedMatch: for y in exactMatch: if x==y: answers.remove(x) return tuple(answers) def subStringMatchExact(target,key): limit=len(target) index=-1 count=0 answerholder=[] while index <(limit): index=find(target,key,index+1) if index!=-1: answerholder.append(index) count=count+1 else:break output=tuple(answerholder) return output def constrainedMatchPair(firstMatch,secondMatch,length): m=length keeper=[] for n in firstMatch: for k in secondMatch: if n+m+1==k: keeper.append(n) return tuple(keeper) ### the following procedure you will use in Problem 3 def subStringMatchOneSub(target,key): """search for all locations of key in target, with one substitution""" allAnswers = () for miss in range(0,len(key)): # miss picks location for missing element # key1 and key2 are substrings to match key1 = key[:miss] key2 = key[miss+1:] print 'breaking key',key,'into',key1,key2 # match1 and match2 are tuples of locations of start of matches # for each substring in target match1 = subStringMatchExact(target,key1) match2 = subStringMatchExact(target,key2) # when we get here, we have two tuples of start points # need to filter pairs to decide which are correct filtered = constrainedMatchPair(match1,match2,len(key1)) allAnswers = allAnswers + filtered return allAnswers

Not the answer you are looking for?

Search for more explanations.

Ask your own question