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

Ok im back! im on ps3 and have completed part a ( http://dpaste.com/546011/ ) however,Im stuck on part b. I know this is incorrect but here's what i came up with: http://dpaste.com/546014/ help please....

OpenStudy (anonymous):

double-oh. i used a list instead of a tuple. this may be cheating but from my understanding, you can't add into a tuple -- they are immutable. so, any command within your loop asking the program to add an index value into a tuple doesn't work. or so i understand. if someone out there knows i am wrong about this, please correct me. here is a copy of my program for that part of problem set 3 if this helps you to see how idid this: http://dpaste.com/546072/

OpenStudy (anonymous):

thanks, but the problem clearly states to return a tuple so I need a tuple

OpenStudy (anonymous):

found out how to fix your problem. create the list then mutate it into a tuple and return the tuple value. thanks for the help

OpenStudy (vaboro):

alicebaker77: you don't have a 'return' statement in your script, and so your function will return 'None', I suppose...

OpenStudy (vaboro):

double_o_seven & alicebaker77: I suppose there is no other way but to convert a list to a tuple before the function returns...

OpenStudy (vaboro):

I tried to implement recursion in solution ps3b.py as well: http://pastebin.com/MT80xS3h; however, I couldn't find how not to pass 'positions' and 'index' as parameters. In other words, in oder to implement recursion in ps3b.py I passed a list of positions of the key and starting search index as parameters of the function. Strange things happened when I tested my recursive functional implementation. I can assign default valued to parameters 'positions' and 'index': def subStringMatchExactRecursive(target, key, positions=[], index=0): Now, I can supposedly call the function without explicitly passing arguments for parameters 'positions' and 'index': subStringMatchExactRecursive(target, key) But if I do this twice in a row: subStringMatchExactRecursive(target, key) subStringMatchExactRecursive(target, key) I will have the correct result first time, but second time the correct result will be appended to the first correct result. If I call the recursive function three times, I will have the correct result the first time, second time I will have two appended correct results in a tuple and third time I will have three correct appended results in a tuple. This seems to be the way Python works. Can somebody explain to me what is really happening during function execution?

OpenStudy (vaboro):

double_o_seven & alicebaker77: I found the way to "append" a tuple :-) a = (1,2) b = (3,4) c = a + b Now c = (1,2,3,4)

OpenStudy (vaboro):

For ps3a and ps3b if key == "" what do you think the function should return: an empty string or an index of the target, i.e. a tuple like this: (0,1,2,3,4,...,n) where n is len(target)-1???

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!
Latest Questions
ShadowKid3: what are examples of a class three lever?
17 hours ago 3 Replies 2 Medals
alphaXtiger: Help pls
1 day ago 3 Replies 1 Medal
danielfootball123: What are good stocks to invest
2 days ago 3 Replies 0 Medals
Burro: 5(5x+7)+3(x+3)
2 days ago 13 Replies 2 Medals
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!