Re: conditionals on lists
- To: mathgroup at smc.vnet.net
- Subject: [mg79107] Re: conditionals on lists
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 19 Jul 2007 03:21:41 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f7ke4t$4pf$1@smc.vnet.net>
mausgeo at aol.com wrote: > I am trying to run a simulation of a roulette game and I have created > a list of 1's and -1's to represent wins and losses. Using the Split > command I can get ordered pairs with the first coordinate being either > 1 or -1 and the second coordinate being the number of successive > entries. I am having trouble applying a conditional to this new list > as I would like to perform a different function depending on the first > coordinate of the ordered pair. How do I apply a conditional to a list > of ordered pairs? If I have correctly understood you, what you have is a list of pairs (a list of lists of two elements), each pair containing a flag +/-1 to indicate win or loss, followed by the number of wins or losses in a row. In[1]:= lst = Table[{RandomChoice[{1, -1}], RandomChoice[Range[10]]}, {20}] Out[1]= {{-1, 5}, {1, 1}, {1, 7}, {-1, 8}, {1, 4}, {-1, 1}, {-1, 10}, {-1, 5}, {1, 9}, {-1, 8}, {1, 8}, {-1, 4}, {-1, 6}, {-1, 4}, {-1, 2}, {1, 6}, {-1, 1}, {-1, 3}, {-1, 2}, {1, 10}} Now, say we want to identify the winner(s) of ten games in a row because they are eligible for a super bonus. So what we must test for each pair that the first element (or coordinate if you wish) is +1 and the second element is greater than nine. Below, you can several way of writing this in Mathematica language and also several way to apply the function (or conditional as you say) to the list of results. The built in function *Map* is usually used to do that, thought that in the chosen example *Scan* is more appropriate since we are not building a list but just printing some message. In[2]:= If[#[[1]] == 1 && #[[2]] > 9, Print["Super Bonus"]] & /@ lst; During evaluation of In[2]:= Super Bonus In[3]:= Map[If[First[#] == 1 && Last[#] > 9, Print["Super Bonus"]] &, lst]; During evaluation of In[3]:= Super Bonus In[4]:= Scan[ Function[coord, If[First[coord] == 1 && Last[coord] > 9, Print["Super Bonus"]]], lst] During evaluation of In[4]:= Super Bonus Hope this helps, Jean-Marc