// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class Grouping { public static int Grouping1(int v) { if(v < 0 || v > 1000) { throw new ArgumentOutOfRangeException("Invalid number " + v); } if (v <= 20) { return 1; } else if (v <= 200) { return 2; } else if (v <= 500) { return 3; } else if (v <= 1000) { return 4; } throw new Exception("Something impossible happended"); } private static int[] LIMITS = { 20, 200, 500, 1000 }; public static int Grouping2(int v) { if(v < 0 || v > 1000) { throw new ArgumentOutOfRangeException("Invalid number " + v); } for(int i = 0; i < LIMITS.Length; i++) { if(v <= LIMITS[i]) { return (i + 1); } } throw new Exception("Something impossible happended"); } public static int Grouping3(int v) { if(v < 0 || v > 1000) { throw new ArgumentOutOfRangeException("Invalid number " + v); } return 1 + (v > 20 ? 1 : 0) + (v > 200 ? 1 : 0) + (v > 500 ? 1 : 0); } } public class TestClass { public static void Main(string[] args) { Random rng = new Random(); for(int i = 0; i < 20; i++) { int rn = rng.Next(1000); Console.WriteLine(Grouping.Grouping1(rn) + " " + Grouping.Grouping2(rn) + " " + Grouping.Grouping3(rn)); } } } }