If there are eight of us at a meeting, how many possibilities are there for a pair of people to stongly disagree? The answer turns out to be 28. And if you look at subgoups of 3, looking for everyone to have the same opinion within the subgroup, the answer is many more.
If there are eight stations on a railway line, there aren't 28 different single journey opportunities - there are 56, because of two directions being involved - a to b and b to a. And if you look at where people live and work, you have 64 possibilities as the most environmentally friendly thing to do is to live and work in the same town and not travel at all, adding eight to the 56 journey opportunities.
Here is a (Python) example of a pair of loops to generate the two-way options for our meeting group:
def tessa(source):
result = []
for p1 in range(len(source)):
for p2 in range(p1+1,len(source)):
result.append([source[p1],source[p2]])
return result
With the a full result and full program shown
[here]. It starts to get more complex when you write code to go on to 3-way and 4-way links ... that would potentially be good use for a recursive (self-calling) function as you can't simply go on writing loops within loops.
Ruby offers you a combination method on an array, where you can pass in the number of elements wanted in a combination. So:
for flow in stops.combination(2)
p flow
end
will print out all 28 combinations of two elements from an array (of 8 elements in our example). To find the reverse journeys, I need to add
p flow
and I can look at all 3-way combinations (from x to y via z) grous instead by looking at:
for flow in stops.combination(3)
although I have to bear in mind that if I'm looking at journeys, there are six possible orders for three values (xyz xzy yxz yzx zxy and zyx) and I must remember the most common of all trips - the xyx and yxy type where you commute / travel for a day out, and in the evening go back to where you started from.
Full ruby source code in the example
[here].
(written 2011-04-23)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y104 - Python - Lists and Tuples [383] Overloading of operators on standard objects in Python - (2005-07-19)
[657] The ternary operator in Python - (2006-03-25)
[899] Python - extend v append on a list - (2006-10-20)
[955] Python collections - mutable and imutable - (2006-11-29)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
[1789] Looking for a value in a list - Python - (2008-09-08)
[2280] Creating and iterating through Python lists - (2009-07-12)
[2284] Strings as collections in Python - (2009-07-12)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2719] Traffic lights in Python - (2010-04-13)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
[3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3763] Spike solutions and refactoring - a Python example - (2012-06-13)
[4027] Collections in Python - list tuple dict and string. - (2013-03-04)
[4368] Shuffling a list - Ruby and Python - (2014-12-28)
[4722] Embedding more complex code into a named block - (2016-11-04)
R107 - Collections (Arrays and Hashes) in Ruby [991] Adding a member to a Hash in Ruby - (2006-12-16)
[2291] Collection objects (array and hash) in Ruby - (2009-07-16)
[2606] Sorting arrays and hashes in Ruby - (2010-01-30)
[2618] What are Ruby Symbols? - (2010-02-02)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2976] Creating, extending, traversing and combining Ruby arrays - (2010-09-30)
[3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
[3255] Process every member of an array, and sort an array - Ruby - (2011-04-21)
[3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
[3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
[4499] Significant work - beyond helloworld in Ruby - (2015-05-27)
[4502] Reading and parsing a JSON object in Ruby - (2015-06-01)
Some other Articles
Scalable Vector Graphics - easy, low bandwidth, high resolution, dynamic.Ruby - a training example that puts many language elements together to demonstrate the wholeOur library in MelkshamMorning in MelkshamAll possible combinations from a list (Python) or array (Ruby)Displaying a directory or file system tree - LinuxMultiple inputs, multiple out, ruby functionsC++ - unknown array size, unknown object type. Help!