[Tutor] stopping a loop

Sheila King sheila@thinkspot.net
Sat, 05 May 2001 20:14:10 -0700


On Sat, 05 May 2001 20:24:52 -0500, "Julieta Rangel" <julieta_rangel@hotmail.com>
wrote about RE: [Tutor] stopping a loop:

:Are you implying a tripple nested loop?  meaning
:for x in set:
:    for y in set:
:        for z in set:
:           if ['x',('y','z')] == [('x','y'),'z']:
:              return associativity holds
:
:Is this what you mean?  If I give the computer those commands, will it look 
:for the definition on my dictionary?  You see, I'm not sure how I'm supposed 
:to tell the computer to look in the dictionary for these values and compare 
:them.  Any more hints, ideas, suggestions, comments, questions?
:
:Julieta

I'm not sure if Tim was implying a triply-nested loop, or not. It sounded kind of
like it to me, too. However, a double-nested loop will do fine.

The key is, you need to use your dictionary to look up the values of the operations.
That is the key. That is why you built the dictionary in the first place.

Note that dictionaries have keys and values.
For example:

>>> dict = {}
>>> dict['a']='apple'
>>> dict['b']='banana'
>>> dict['c']='cat'
>>> print dict.keys()
['b', 'c', 'a']
>>> print dict.values()
['banana', 'cat', 'apple']
>>> 

I can use a loop on a dictionary as follows:

>>> for key in dict.keys():
... 	print dict[key], " starts with ", key, "."
... 	
banana  starts with  b .
cat  starts with  c .
apple  starts with  a .

[Notice that it doesn't necessarily put them in order.]

Anyhow, for your situation, You are getting your table and storing it in the variable
s.

So, you should try this:

print s.keys()
print s.values()

for pair in s.keys():
	print pair " maps to ", s[pair]

Try this also:

for pair in s.keys():
	for elt in set:
		if s[pair]==elt:
			print pair, " maps to ", elt
		else:
			print pair, " does not map to ", elt


I don't think you will want to use any of those in your actual program, but playing
with those and watching them run should prove insightful (I hope).

OK, I've since decided, that Tim is probably right. I bet you do want a triply nested
loop. Hm. Interesting.

Here is what you asked about:


:Are you implying a tripple nested loop?  meaning
:for x in set:
:    for y in set:
:        for z in set:
:           if ['x',('y','z')] == [('x','y'),'z']:
:              return associativity holds

This is close. But, I think you need to apply your table mapping. You called the
table s, so:

for x in set:
    for y in set:
        for z in set:
            if s[(x, s(y,z))] == s[(s[(x,y)],z)]
                print "for ", str(s[(x, s(y,z))]), " and ", str(s[(s[(x,y)],z)])
		print "associativity holds\n"

Well, this will (I hope) get you closer to where you want to go. You're not there
yet, but you're getting there.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/