254 shades of grey

Description:

Why would we want to stop to only 50 shades of grey? Let's see to how many we can go.

Write a function that takes a number n as a parameter and return an array containing n shades of grey in hexadecimal code (#aaaaaa for example). The array should be 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, #000000 and #ffffff are not accepted values.

When n is negative, just return an empty array. If n is higher than 254, just return an array of 254 elements.

Have fun

using System;
using System.Collections.Generic;
using System.Linq; public class Kata
{
public static string[] ShadesOfGrey(int n)
{
// returns n shades of grey in an array
string[] array = null;
if (n <= )
{
array = new string[] { }; }
else
{
if (n > )
{
n = ;
}
List<string> list = new List<string>();
string str = string.Empty;
for (int i = ; i <= n; i++)
{
str = i.ToString("x2");
str = "#" + string.Join(string.Empty, Enumerable.Repeat(str, ));
list.Add(str);
}
array = list.ToArray();
}
return array;
}
}

其他人的写法

using System;

public class Kata{
public static string[] ShadesOfGrey(int n){
string[] arr = null;
n = n <= ? : (n > ? : n);
if (n > )
{
arr = new string[n];
for (int i = ; i <= n; ++i)
{
arr[i-] = string.Format("#{0:x2}{1:x2}{2:x2}", i, i, i);
}
}
return arr;
}
}

下面这个还需要学习

Math.Min以及Math.Max的使用

以及Linq的使用,select之后可以直接转换为Array

using System;
using System.Linq; public static class Kata {
public static string[] ShadesOfGrey(int count) {
if (count < )
count = ; return Enumerable
.Range(, Math.Min(count, ))
.Select(x => string.Format("#{0:x2}{0:x2}{0:x2}", x))
.ToArray();
}
}

254 shades of grey的更多相关文章

  1. 【转】R语言笔记--颜色的使用

    转自该网站:http://research.stowers-institute.org/efg/R/Color/Chart/ 科学可视化中常用的一些颜色表:http://geog.uoregon.ed ...

  2. 五、Pandas玩转数据

    Series的简单运算 import numpy as np import pandas as pd s1=pd.Series([1,2,3],index=['A','B','C']) print(s ...

  3. 【http】http/1.1 八种请求方式

    OPTIONS 返回服务器针对特定资源所支持的HTTP请求方法.也可以利用向Web服务器发送'*'的请求来测试服务器的功能性. HEAD 向服务器索要与GET请求相一致的响应,只不过响应体将不会被返回 ...

  4. A Statistical View of Deep Learning (III): Memory and Kernels

    A Statistical View of Deep Learning (III): Memory and Kernels Memory, the ways in which we remember ...

  5. c# 如何显示图片指定位置

    private void panel1_Paint(object sender, PaintEventArgs e) { Rectangle r1 = new Rectangle(0, 0, 100, ...

  6. [Music] Billboard Hot 100 Singles Chart 27th Jun 2015

    01 Wiz Khalifa - See You Again (Feat. Charlie P..> 30-Jul-2015 09:12 9247814 02 Taylor Swift - Ba ...

  7. Creating an generated Earth AVI with C++

    Creating an generated Earth AVI with C++        EarthGenerator.cpp /*    EarthGenerator.cpp An examp ...

  8. 使用神经网络识别手写数字Using neural nets to recognize handwritten digits

    The human visual system is one of the wonders of the world. Consider the following sequence of handw ...

  9. [C2P3] Andrew Ng - Machine Learning

    ##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...

随机推荐

  1. JQuery 判断某个属性是否存在 hasAttr

    $(".fengye a").each(function () { if (typeof($(this).attr("href")) != "unde ...

  2. 玩转Slot Machine

    最近在做一个有关Slot  Machine小游戏的开发,其中遇到了不少的坑,现将个人遇到的问题总结如下,希望今后对大家开发的过程中有所的帮助. 这个项目是部署到微信朋友圈广告的,两天时间,PV就有14 ...

  3. linux命令 chattr超级权限控件

    linux命令:chattr 1.作用 修改ext2和ext3文件系统属性(attribute),使用权限超级用户.   linux命令:chattr 1.作用修改ext2和ext3文件系统属性(at ...

  4. CCNP第二天 帧中继综合实验

    实验题如图所示: 要求全网可达 R5为帧中继交换机 R6 和 R1之间为快速以太网接口 所使用的拓扑为CCNA标准版拓扑图,如下所示: -------------------------------- ...

  5. 《C和指针》 读书笔记 -- 第10章 结构和联合

    1.聚合数据类型能够同时存储超过一个的单独数据,c提供了两种类型的聚合数据类型,数组和结构. 2.[1] struct SIMPLE { int a; }; struct SIMPLE x; [2] ...

  6. insert into (select...WITH CHECK OPTION) values(...)

    insert into (<subquery> WITH CHECK OPTION) values (...) 语法看起来很特殊,其实是insert进subquery的这张表里: 1. 只 ...

  7. C#学习笔记---基础入门(二)

    枚举 枚举是被命名的整型常数的集合:枚举类型的变量只有赋值后才能使用:不同枚举中的枚举值可以重名:可以自定义枚举值. enum Playstates {            跑, 跳,下滑,左转,右 ...

  8. First Groovy

    class Sample { def names = ["anna", "annie", "tommy", "bobby" ...

  9. 怎样修改Windows7环境变量

    在使用电脑的时候要运行某些特定的应用程序时需要修改系统的环境变量,例如安装JAVA时我们就需要配置系统的环境变量.那什么是环境变量呢?环境变量一般是指在操作系统中用来指定操作系统运行环境的一些参数,比 ...

  10. 构建第一个Java程序