A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
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')
anonymous
 5 years ago
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')

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0the code works, but I am having trouble with the recursive version of the problem. What should the difference be?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0here is my code , take a look at it .

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0or 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0b3n, i took a look at your recursive function and I cant get it to work.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0If you just copypasted the recursive function instead of the whole thing, you also need to import the string functions, from string import * It worked fine for me.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Also, 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i took string import with it. Iterative one works. Recursive doesn't produce output.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0@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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i don't get an error. it loads i just don't get output for the recursive one.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ok. 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ok. It was confusing cause I always test inside the code. Thanks for the help

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Finished up problem 3.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0## 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
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.