Tag Archives: generics

Quick Algorithm: Display an Array of Integers as Set of Integer Ranges

Say you have an array of integer values, for example, pages of a document or years when something happened, like years when new Delphi version was released:

delphiReleased: TArray<integer> = 
 [1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003, 
  2005, 2006, 2007, 2009, 2010, 2011, 2012, 2013, 
  2014, 2015, 2016, 2017, 2018, 2020, 2021, 2022];

If you would want to display the above years in a user friendly manner as a set of ranges, you could go for:

1995 - 1999, 2001 - 2003, 2005 - 2007, 2009 - 2018, 2020 - 2022

Here’s an algorithm (in a console app code) that does the above
Continue reading

How to Randomize / Shuffle (Generic) Collections and Lists – Implementing UnSort in Delphi

The second law of thermodynamics, in short version and when read by a programmer, states that “any collection of objects tends not to be sorted” 🙂

We developers, we have a tendency of organizing objects into lists, collections, queues, stacks …

Since you’ll be using the for loop to iterate over elements – why not sort the elements first. What’s more, a sorted list is easier (read: faster) to be searched upon.
Continue reading