Carol Murphy

village idiot
+ Follow
since Mar 15, 2001
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
11
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Carol Murphy

Thanks for your response, Stephan.

Your solution makes sense. I'm still working on getting a mental image of how recursive function calls operate, and I sometimes
expect them to operate like iterative code, with which I am more comfortable.

Thanks for your help!

Cheers!
1 year ago
Thanks for that explanation! The "unwinding" of the recursive calls is a great stumbling block for my visualization of what is happening in this code. Bear with me here.
When I moved the return string.split() out of the else loop, the string it used was the one returned by the second call. Only the first tag was removed. The fact that

print string.split() and return string.split() don't reference the same object inside the else block still confuses me. Why isn't the string that was passed in with the
third call to remove_tags(string) returned after it was printed? I think I am misunderstanding what happens during the
return process itself. It seems to me that the last method call bypasses the if statement so the string passes as an argument should be split and returned without any
trouble. I am not seeing how the value that gets thrown out inside the if block affects what gets passed into the else block.

I feel I'm close, but I don't quite see it yet.
1 year ago
This is from a Computer Science course offered for free at Udacity, so it may belong in another forum, but since it is written in Python, I put it here.
This quiz asks us to write a procedure that strips the html tags out of a string and return the plain text as a list. I decided to do it recursively and
stumbled across something I don't understand. If I run the code as is, the first 3 tests return None while the 4th returns the desired list. If I
uncomment line 10 and comment out line 11, all 4 tests return the desired list. The difference is whether remove_tags(string) is called from
line 11, or is returned by uncommenting line 10.  I have tried stepping through both versions of the code in the
debugger to see if I can picture exactly what the differences between the two are, but it is still not clear.

My thinking: Both ways update the variable 'string' in the same way, as shown by calling print string.split() inside the else block on line 14.
Since calling print string.split() produces the list I want in both versions, why isn't that list returned by the call on line 15 in both versions?
If string itself has been stripped of all tags by looping through the if block, why doesn't the last call to remove_tags(string) on line 11 simply
fall through to the else block and return the result of string.split() as a list? Why does return need to be called inside the if block for this to work?

I am missing some important concept in my understanding of recursive functions. Can anyone help me with this?


1 year ago
I'm back! Okay, I've got this text file with a list of data for 85 restaurants. The data is organized for each in this order: name, rating, price range, cuisine type. I am supposed to write a function that reads this file and returns a tuple of three dictionaries. I used file.readlines() to read the file in, then I stripped out all of the \n characters. This left me with list of data separated by an empty string. I created the first dictionary by iterating through the indexes of the file, creating keys and values, but the other two dictionaries involved keys that had lists for values, and I just got stuck, so I populated them manually. I'm sure I'm supposed to create the dictionaries by reading the file, but I haven't been able to come up with code that works. What can I try to build these dictionaries by reading the file? I need a little nudge in the right direction.

Eureka!



This populates my first list. These nested loops require inside-out thinking, and it takes me a while to sort hem out. (That and I used Python Visualizer to step through the code. Nice little tool!)
2 years ago

The loop statement, on the other hand, never gets to the next statement, because it's still looping.  


And this seems strange to me. Why doesn't the loop stop once the condition has become false. As the calls return up the stack, why do
they test the conditional again? Or do they even test it again? Do they simply continue running from where they left off, since there is no
explicit return, and therefore cause the loop to pick up where it left off, thus becoming eternal?  If I was prone to paranoia, I would assume
while loops were designed simply to drive me mad..........


 I ran the while version, and it looks like it is printing not 0's, but 1s. What platform are you using? Do they interpret 0s as 1s.


You are correct, Mr Wu! The version of the code I was using before had print(num) before the loop, but in the code I posted here I had moved it inside
the loop.

I think I'm close to grasping this concept, thanks to your input. (The folks at StackOverflow tend to indulge in snark, which renders their form of help rather unhelpful.)
The main thing I will take away from all of this: avoid while loops in conjunction with recursive function calls to avoid falling down rabbit holes!
2 years ago
Hi Tim! You are correct, but I think I'm confused about more than just scope. The whole concept of how the stack works during recursive functions ties my brain into a knot. (Picture Twister with brain cells instead of arms and legs.)
I'm having difficulty picturing how and why an if statement just returns back up the stack, but a while loop gets stuck in this endless recursion.
I can understand the mechanics of something once I can picture how it works. Trying to get a mental picture of how a bobbin works to make stitches on a sewing machine is a good example.
I want to know that I truly understand what is happening before I feel comfortable using something. And I'm really struggling with the difference between the way an if statement and a while loop function once the condition has become false inside a recursive function. This is going to keep me up a few nights, I think!
2 years ago
Thanks for the explanation Joe! I never learned to trace code. I think I need to focus on this for a bit.
2 years ago

If tests a conditional once.  While will loop until a condition becomes false.  



I think my misunderstanding begins here, with what happens once the condition becomes false. If returns up the stack without testing the values against the condition.
While will continue to check sqspin(num) against the conditional using the value of num returned by sqspin(1), thus causing a never-ending loop, because of the recursive call.

I tried running these both in Python Visualizer, and from what I could see they behave the same until they start returning up the stack.
I'm not able to visualize this statement:   "While will loop until a condition becomes false. "

What I see happening is the while looping until the condition becomes false, then stopping, then looping again if the condition becomes true when used in a recursive function.

I have always had the idea in the back of my mind that while and if worked pretty much the same way, and this makes me feel confused. Am I understanding this correctly?

I have another question about the way recursion works. Lines 8-9 & 10-11 are duplicates of each other, so 1 would be printed twice. When the program runs the numbers are printed
in descending order until 0 is reached, then they stop. The program continues running until the calls on the stack return, but nothing more gets printed.  Is there any significance
to lines 8-11 in the code you posted? Why the duplication if no number is being printed?
2 years ago
I can't see where the problem is. Both if and while test for num > 0, and they both decrement num as the end of each loop, so why do they act differently?
Once the condition becomes false, don't they both exit? I'm missing something really obvious here.
I'm sure it has to do with the recursive call and what happens when the calls go up the stack, but I'm not understanding it.
Okay, I think I get it. The recursive call doesn't return a value, so in the if loop, once num > 0 becomes false, the calls just
return none until the program exits.
The while loop keeps testing the value of num > 0, so when it returns none and continues calling up the stack, it gets hung
up on num = 1, which keeps it cycling back to zero. Right???
Okay, NOW my question is this: Why doesn't a while loop fall through? Why does it go back to the conditional line? That doesn't make any sense.
2 years ago
These two loops act very differently, and I can't figure out why.





The if block terminates but the while block does not. Both print the count down from 5 to 0 for the value of num,
but the while loop continues printing zeros for ever.
Is there a guru around who can explain what in the Sam Hill is going on here?
2 years ago
Hello Lisa!  I got a notification of your post this morning, and I haven't been on this site for quite a while. I don't know the answer to your question. Others may be doing the Cattle Drive, but not posting in this thread. When I was working through the Drive, there were about a dozen active drivers, and this thread provided encouragement, a place to vent, and a way to track everyone's progress. I moderated this forum many moons ago, but no longer. I still have the access needed to update your status in the log, and I'd be willing to do that if you'd like. It may even bring some drivers who are on the trail out of the shadows! Good luck with your assignments!
2 years ago
Look at that dust storm comin'! Wait, that's not a dust storm, it's Ashley kicking up a whole lotta dust on the trail! Way to rope them doggies, Ashley!
4 years ago
Hi Dustin! When I did the drive way back when, I had no prior programming experience, in any language. I just jumped in with both feet, and started paddling for my life. The nitpickers were patient, and when I got really stuck on some concept, if I needed clarification on some point, I simply sent the code snippet to my nitpicker, who helped me out. There were a lot more Cattle Drivers doing the drive then, and the discussions on this thread were very helpful as well as encouraging.
The self-directed learning style really worked for me, and I think that after completing the drive, I have a much better understanding of the value of clearly written code. The Cattle Drive encourages good coding practices, helps one develop a deeper understanding of the why of things, and it can be a whole lotta fun, too! (In a very masochistic sense of the word fun.)
4 years ago
Yippee-Yi-Yo-Ki-Yay, Ashley! Way to rope them dogies!
4 years ago