Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
All possible combinations from a list (Python) or array (Ruby)

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)


Back to
Displaying a directory or file system tree - Linux
Previous and next
or
Horse's mouth home
Forward to
Morning in Melksham
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 whole
Our library in Melksham
Morning in Melksham
All possible combinations from a list (Python) or array (Ruby)
Displaying a directory or file system tree - Linux
Multiple inputs, multiple out, ruby functions
C++ - unknown array size, unknown object type. Help!
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

1 unpublished comment pending on this page

edit your own (not yet published) comments

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3257_All ... Ruby-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb