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. SQL Server中批量替换数据

    SQL Server数据库中批量替换数据的方法 SQL Server数据库操作中,我们可能会根据某写需要去批量替换数据,那么如何批量修改替换数据呢?本文我们就介绍这一部分内容,接下来就让我们一起来了解 ...

  2. mvc的删除

    有UserInfo一个实体类,db代表请求上下文 属性有ID,UserName,UserPass,Email var users=from c in db.UserInfo select c; var ...

  3. QMessageBox 弹出框上的按钮设置为中文

    Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼. QMessageBox box(QMessageBox::Warning,"标题","弹 ...

  4. 一些dos命令

    MS DOS 命令大全 一.基础命令 1 dir 无参数:查看当前所在目录的文件和文件夹. /s:查看当前目录已经其所有子目录的文件和文件夹. /a:查看包括隐含文件的所有文件. /ah:只显示出隐含 ...

  5. java开发命名规范总结

    一 包名的书写规范 (Package)推荐使用公司或机构的顶级域名为包名的前缀,目的是保证各公司/机构内所使用的包名的唯一性.包名全部为小写字母,且具有实际的区分意义. 1.1 一般要求1.选择有意义 ...

  6. MVC学习系列——参考

    C#进阶系列——WebApi接口传参不再困惑:传参详解 http://www.cnblogs.com/landeanfen/p/5337072.html

  7. (转载)Cocos2dx-OpenGL ES2.0教程:你的第一个立方体(5)

    在上篇文章中,我们介绍了VBO索引的使用,使用VBO索引可以有效地减少顶点个数,优化内存,提高程序效率. 本教程将带领大家一起走进3D–绘制一个立方体.其实画立方体本质上和画三角形没什么区别,所有的模 ...

  8. 使用MyBatis的resultMap高级查询时常用的方式总结

    以下内容已经通过楼主测试, 从pd设计数据库到测试完成, 之前楼主也没有过Mybatis 使用resultMap觉得有点乱,最近抽出时间总结了一下也算对MyBatis的resultMap进行一次系统的 ...

  9. UINavigationBar导航栏相关设置

    设置导航颜色 [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1] ...

  10. 利用开源项目使discus论坛与java应用同步登录和注册

    最近做了一个资源库系统的项目,老师说可以搭建开源论坛替代自己开发社交模块(评论啊,反馈啊)来减轻负担,甚至提到了要给每个资源开一帖的功能..使我十分怀疑到底是减轻负担还是增加负担...不过怀疑归怀疑, ...