Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

struggling with assigning the value of a variable to a tuple. Here is my code, any hints to doing this? Thanks http://codepad.org/O31XLvSx

OpenStudy (anonymous):

This is in reference to assignment 3 problem 3 (part c).

OpenStudy (anonymous):

Granted tuples are immutable, how about populating an array with the starting points and then using the tuple() built-in function? This is just a way of doing it, but remember that the find function returns the starting point of the pattern.

OpenStudy (anonymous):

First, I don't see where you're using tuples in that code. But, as bmp said, tuples are immutable, so you can't modify the values they hold. You can append another tuple to a tuple, but it returns a copy, so the original tuple isn't modified. But there are some other issues with your code. 1. You don't initialize index before using it. 2. It will only work properly for keys of length 4 or longer. And it won't find overlapping keys correctly. 3. If you slice to the end of a string you can omit the end index after the colon (you still need the colon). 4. Your function doesn't return a value.

OpenStudy (anonymous):

There is not tuple because I don't actually know where to start with it. I would like to modify this code to put the values of index into a tuple (or something). I made the changes you suggested to the code: http://codepad.org/aIYdVd8y

OpenStudy (anonymous):

Did you check my suggestion? How about creating an empty list and just throwing starting points indexes there, and then converting it to a tuple? Remember that you can append to a list quite easily in Python.

OpenStudy (anonymous):

http://codepad.org/bNCstJzv Check out my test case. Should it return 3 or 5? As bmp said, it's easier to append to a list, then convert the list to a tuple when you want to return it. To start with, you can just return the list and forget the whole tuple business. Go back and fix it when you get it working.

OpenStudy (anonymous):

Firstly thanks bmp and dmancine for the help so far. I will try apprending the value to a list. But first I feel like I should address the issue of not capturing overlapping keys. I'll post code when I THINK i have figured this out. Thanks again

OpenStudy (anonymous):

I think I've got it. Thank you very, very much for both of your help. I would have been here a long time trying to figure this one out. Here is my code, any further suggestions are welcomed! http://codepad.org/7bS87XfH

OpenStudy (anonymous):

with comments :D http://codepad.org/T0w4oL1Y

OpenStudy (anonymous):

Eyeballing, your code looks clean and it works (at least, it also works for overlapping patterns, which was a pain for me when I solved it). Minor improvement would be not doing the last assignment (matches = tuples(matches)) but, rather, just returning tuple of matches, aka "return tuple(matches)". Like I said, minor thing, but it's an operation less, and it's a more idiomatic way, or pythonic way if you prefer.

OpenStudy (anonymous):

Makes sense to me. Thanks!

OpenStudy (anonymous):

Your code works, so these are just style and efficiency issues. 1. You're not actually using count for anything. You can get rid of it. 2. If the beginning of a range is 0 you can omit the first argument (by default a range starts at 0). 3. If you don't need the actual list returned by range you can use xrange (like in for loops). It works just the same, it just doesn't create a whole list. The last thing is how you're using find. You're essentially implementing find yourself. Find takes a string to look in, and a string to look for. The optional third parameter is an index to start looking at. What find returns is the index (after the starting index, which defaults to 0) of the first match it finds. What you're doing is marching down the target string, one index at a time, and seeing if an exact match for the key starts there. Your previous version of the code used find correctly. Why did you change it?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!