anonymous
  • anonymous
i need some tips on psy3, first problem. i found a way to count the frequency of "atgc" in the larger string but it's not iterative. which means it's not going to be easy for me to make it recursive. if you look at my program, you'll see the top version which counts the matching substrings using a count function. the bottom code is my attempt at an iterative program but i obviously don't have it right. the code outputs nothing. i'm not entirely sure where it goes wrong but i suspect there are a couple problems with it. can someone give me some tips? http://dpaste.com/540979/
MIT 6.00 Intro Computer Science (OCW)
jamiebookeater
  • jamiebookeater
See more answers at brainly.com
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

anonymous
  • anonymous
Hi alicebaker77, To answer the first part of your question about where to start with ps3, first problem, you should try to used the 'find' method instead of using .count. See http://docs.python.org/library/string.html?highlight=find#string.find for method documentation. As for your code, the reason why it's not printing anything out is because you're testing a 'char' versus an 'int' in line 12 inside the condition of your 'while' loop. This will always return False, so you will never enter your loop. Here's the breakdown: line 11: index = word[0] # index get the first character of word. in your test case, the letter 'a' line 12: len(word) returns an integer 4 since your test case is 'atgc', 4 characters long. Now if you rewrite your compare and substitute in your values you get the following: while (index<=len(word)): which becomes while('a'<=4): There are other issues with your code, such as switching the order of your argument when calling CountLetterInWord. It seems you want to call it as CountLetterInWord("atgc","atgacatgcacaagtatgcat") and not the other way around. I would read up on the find method and rethink your strategy. If you need more help. post back.
anonymous
  • anonymous
Thank you stuyboyz. I'll read more on the find method and come up with a new strategy and post back if necessary.
carlsmith
  • carlsmith
I've stuck you up a medal stuyboyz, you obviously went to some trouble to figure that out and all the points you made seem sound to me. Just one thing ~ why does everyone keep putting the expression part of their while statements in parens? Is this something the MIT lecturers do? I've never seen it done outside of this courses forum. You just do the expression while a < 10: print 'The boolean value of the expression `a < 10` is True' The syntax for `while` and `if` are the same. The keyword, a space, an expression, a colon. You can use parens within the expression, but they are only useful for doing regular math type stuff, putting them around an entire expression, doesn't do anything. if 5*(8+4) == 10: print 'The world is not as it ought to be' would do the same as if you substituted the top line for if (5*(8+4) ++ 10): On the line, you can do >>>print ((((((((((5)))))))))) 5

Looking for something else?

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

More answers

carlsmith
  • carlsmith
** Obviously the two plus signs in if (5*(8+4) ++ 10): are a typo. Sorry, it should be if (5*(8+4) == 10):
anonymous
  • anonymous
Hi carlsmith, your comment about the parenthesis are legit. There is really no harm in adding them. I do it out of habit, since other languages require them.
carlsmith
  • carlsmith
Ah, I thought I had seen a couple of people doing it, maybe you've been spreading your bad habits about the place :) You're right, they do no harm, I just thought the Lisp guy at MIT might have been teaching them as Python. I hope you're well anyway mate.
anonymous
  • anonymous
By the way, thanks for the medal. I'll make sure to spread more parenthesis around just for you. heehee.
carlsmith
  • carlsmith
(No worries dude.)
anonymous
  • anonymous
ok, i'm back for some more help. i made some progress but am not figuring out how to make this iterative or recursive. right now it's just a repetitive set of commands that gets me the answer i already knew -- that there are two substrings in my target. i read through the materials stuyboyz posted in this thread and didn't see much about the string.find function that i didn't already know. the assignment asks me to create a program with only two arguments (target, key) so i'm trying to be faithful to the instructions and not throw in the two additional arguments you can use with find indicating where the function should perform the search within the target. but, as you can see, i did use it the second time i use the command index=string.find(target,key) to ensure the program searched for the next occurrence at a later point in the target. there's something about this assignment i don't seem to be getting. maybe there's a key concept i'm missing. . . http://dpaste.com/542209/
anonymous
  • anonymous
Hi alicebaker77, you're very close to the solution. You have actually done the hardest part of the code, which is figuring out how to find the next index given the current index. All you're missing the iterative process, also known as a loop. Notice how you repeatedly call the same set of code again and again, well there's your iteration. Try the following steps to convert your code into a 'while' loop. 1. your 'if' statement on lines 5 and 8, are the same, so it can become you looping condition. The looping condition states that you stay in the loop until the condition becomes false. 2. the body of your loop is the set of statement that you find yourself typing over and over again. e.g. lines, 4, 6, 7, 9. Hint, you'll need to keep line 4 outside, since it's an initialization. Also note you can use 'index' directly in your 'if' (now, converted 'while') statement. so instead of index = string.find(target,key) if string.find(target,key)!= -1: you can use index = string.find(target,key) if index != -1:
anonymous
  • anonymous
thanks stuyboyz! i fixed it and now have a working piece of code. just to check i lengthened the target to include another occurrence of the key and threw in some extra print statements to document the function's traversal across the target. i can't say i entirely understand why this works but i hope i understand it enough to write a recursive version. here's the final iterative version: http://dpaste.com/542258/
anonymous
  • anonymous
Congratulations. You got a functional code. One little correction to your code, you actually need to return the count to the calling function with the following statement return count instead of just printing out the count. Something like http://dpaste.com/542278/ This 'return' statement is key to the recursive solution. Best of luck with the next question
carlsmith
  • carlsmith
Just keep it simple.
1 Attachment

Looking for something else?

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