Tag Archives: algorithms

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

Quick Algorithm: Get Ideal Size (Square like) For a Board Game Having an Arbitrary (but Even) Number of Fields

square like game grid size Say you are developing a game like Chess, Go, Checkers, Tic-Tac-Toe or Memory. In each of those games the game board is a rectangle looking playfield of different size (rows x columns). Tic-Tac-Toe is 3×3, Checkers is 8×8, while Go can be 19×19 or 13×13 and similar.

In a game with an arbitrary number of game fields you might want to have the board look as closely to square as possible (rectangle where height and width are the same). Think of Memory. Let’s say we have 24 cards, that is 12 pairs. If you want to place them in a rectangular grid, most similar to square, you would go for 4 x 6 (or 6 x 4) board size (as it would look more square like than 3 x 8 and 2 x 12 or 1 x 24 would be too wide).
Continue reading