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. VB 核心编程及通用模块开发 笔记1

    前言:学习任何编程语言.编程工具的目的不外乎解决生活问题,能够快速.高效解决问题的方式就是不错的方式,至于选择什么“工具”,众位看官看着办,本人虽然有过3年vb开发经验,但是一直没有深入学习,现已购买 ...

  2. 【CSDN人物访谈】蒋守壮分享他的技术成长之路以及对Hive技术的解读与思考

    结缘大数据技术 CSDN:请简单地介绍一下自己. 蒋守壮:首先非常感谢CSDN能够给我这次被专访的机会,可以让我重新审视自己的职业发展历程,也希望能够帮助一些同行的朋友们.目前就职万达网络科技集团有限 ...

  3. spark 1.3.0下的问题

    1.在spark SQL的一个test中 无论是registerAsTable还是registerTempTable 都会有问题,经过查找各种资料,采用如下的方式: val sqlCon=new or ...

  4. 在iframe结构中,如何使弹出层位于所有框架的上方(-)

    在做后台管理页面的时候,经常用到iframe.虽说这东西有些过时,也不利于SEO,但是后台就是后台,不需要考虑那么多东西,综合来说,用iframe还是很适合后台管理界面的. 但是在遇到弹出层时,出现了 ...

  5. android studio 打开github开源代码

    1.最近下载的开源代码全是github来的,一直用eclipse开发,对于android studio来说是全新的 2.在eclipse导入一个工程那是so easy, import选择一下就可以. ...

  6. [转]控制反转(IOC)和依赖注入(DI)

    http://blog.csdn.net/Elite_1989/article/details/16851565 控制反转和依赖注入可以理解成同一个东西,都是为解耦而生的~ 控制反转(IoC=Inve ...

  7. 2337:[HNOI2011]XOR和路径 - BZOJ

    昨天才做了一道高斯消元,一下要精度判断,一下又不要精度判断 主要是思路很重要 很容易想到每一个二进制位算一个概率,然后求和,设f[i]为走到从i走到n这一个二进制位为1的概率 f[i]:=∑{f[j] ...

  8. E437: terminal capability "cm" required

    执行 vi 的时候出现:E437: terminal capability "cm" required 临时解决: export TERM=xterm

  9. js对象转到字符串

    var str = JSON.stringify(obj);

  10. call && jmp 指令

    对于jmp指令: (1)jmp short 标号相当于(ip)=(ip)+8位位移 跳转范围是[-128,127](2)jmp near ptr 标号相当于(ip)=(ip)+16位位移 跳转范围是[ ...