254 Shades of Grey

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.