254 Shades of Greydescription:
Why would we want to stop for only shades of grey? Let's see how many we can go.
Write a function that takes a number n as a parameter and return an array containing n shades of the grey in hexadecimal code (for #aaaaaa
example). The array should is sorted in ascending order starting with #010101
, #020202
etc. (using lower case letters).
using System;public static class shadesOfGrey(int n) { // returns n shades of grey in an array}
As a reminder, the grey color is composed by the same number of red, green and blue: #010101
, #aeaeae
, #555555
, etc. Also, and is not #000000
#ffffff
accepted values.
When n was negative, just return an empty array. If n is higher than 254, just return an array of 254 elements.
Has fun
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq; Public classKata { Public Static string[] Shadesofgrey (intN) {//returns n shades of Grey in an array string[] Array =NULL; if(N <=0) {Array=New string[] { }; } Else { if(N >254) {n=254; } List<string> list =Newlist<string>(); stringstr =string. Empty; for(inti =1; I <= N; i++) {str= i.ToString ("X2"); STR="#"+string. Join (string. Empty, Enumerable.repeat (str,3)); List. ADD (str); } Array=list. ToArray (); } returnArray; } }
The other person's wording
usingSystem; Public classkata{ Public Static string[] Shadesofgrey (intN) { string[] arr =NULL; N= N <=0?0: (N >254?254: N); if(N >0) {arr=New string[n]; for(inti =1; I <= N; ++i) {arr[i-1] =string. Format ("#{0:x2}{1:x2}{2:x2}", I, I, I); } } returnarr;}}
The following one still needs to learn
Use of Math.min and Math.max
and the use of LINQ, select can be converted directly to array
usingSystem;usingSystem.Linq; Public Static classKata { Public Static string[] Shadesofgrey (intcount) { if(Count <0) Count=0; returnEnumerable. Range (1, Math.min (Count,254)) . Select (x=string. Format ("#{0:x2}{0:x2}{0:x2}", X)) . ToArray (); }}
254 Shades of Grey