0
Summary This answer grew longer than I anticipated, so I think a summary is in order. For such a generic problem and rather vaguely expressed intent, the scope of solutions is rather broad. I encourage not complicating the code more than absolutely necessary, instead focusing on readability of t...