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. TweenMax动画库学习(四)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  2. Nhibernate的log4net和系统的log4net使用技巧

    NHibernate定义了两个logger:NHibernate和NHibernate.SQL.我们可以分别配置这两个logger.在App.config文件中<root>标签前边添加如下 ...

  3. DEV GridControl表格数据源为空在表格中间显示提醒字符

    private static void gv_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.Custo ...

  4. 一个不错的图片滑动展示插件 anythingslider

    一个不错的图片http://css-tricks.com/anythingslider-jquery-plugin/ DEMO演示: http://css-tricks.github.io/Anyth ...

  5. 购买 CDRTools 2 正式版

    联系方式: Email:396390927@qq.com QQ: 396390927    QQ群: 26326434  组件价格: ¥50元/用户,免费更新: 此物为数字商品,并经过测试完全可用,谢 ...

  6. clrscr( )用法

    函数名: clrscr 功  能: 清除文本模式窗口,清屏的意思,即把之前显示出的文字字符去掉,是clear screen的简写 用  法: void clrscr(void); 程序例: #incl ...

  7. fedora -- java多版本切换

    一般java开发时会下载多个版本的SDK,所以需要多个版本中进行切换 1. 设置JAVA_HOME环境变量需要打开.bashrc文件 2. 安装时使用alternatives将不同版本的java连接到 ...

  8. SecureCRT上传bash: rz: command not found(转载)

    转载自:http://blog.csdn.net/zhangdaiscott/article/details/18141017 -bash: rz: command not found rz命令没找到 ...

  9. python学习笔记8(表达式和语句)

    一.print 和 import 信息 >>> print 'age:',22 # 用逗号隔开打印多个表达式 age: 22 import somemodule # 从模块导入函数 ...

  10. 关于安装Android Studio的一些问题的解决方法

    问题1:每次Fetching android sdk component information 这是在检查你的 Android SDK .有人会在这里卡上很长时间,很大的原因就是:网络连接有问题.可 ...