A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 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')

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    the code works, but I am having trouble with the recursive version of the problem. What should the difference be?

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    here is my code , take a look at it .

    1 Attachment
  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    b3n, i took a look at your recursive function and I cant get it to work.

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i took string import with it. Iterative one works. Recursive doesn't produce output.

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 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.

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i don't get an error. it loads i just don't get output for the recursive one.

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  11. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  13. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ok. It was confusing cause I always test inside the code. Thanks for the help

  14. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Finished up problem 3.

    1 Attachment
  15. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    1 Attachment
  16. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 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

  17. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.