254 shades of grey
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的更多相关文章
- 【转】R语言笔记--颜色的使用
转自该网站:http://research.stowers-institute.org/efg/R/Color/Chart/ 科学可视化中常用的一些颜色表:http://geog.uoregon.ed ...
- 五、Pandas玩转数据
Series的简单运算 import numpy as np import pandas as pd s1=pd.Series([1,2,3],index=['A','B','C']) print(s ...
- 【http】http/1.1 八种请求方式
OPTIONS 返回服务器针对特定资源所支持的HTTP请求方法.也可以利用向Web服务器发送'*'的请求来测试服务器的功能性. HEAD 向服务器索要与GET请求相一致的响应,只不过响应体将不会被返回 ...
- 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 ...
- c# 如何显示图片指定位置
private void panel1_Paint(object sender, PaintEventArgs e) { Rectangle r1 = new Rectangle(0, 0, 100, ...
- [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 ...
- Creating an generated Earth AVI with C++
Creating an generated Earth AVI with C++ EarthGenerator.cpp /* EarthGenerator.cpp An examp ...
- 使用神经网络识别手写数字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 ...
- [C2P3] Andrew Ng - Machine Learning
##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...
随机推荐
- nginx gzip filter模块分析
API:http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/zlib-deflateinit2.html ...
- 小米2s换了屏幕后不能近距离对焦,拆过后无法对焦?
主要问题就是小米2/2S手机中壳套后摄像头的位置,里面还有一个正方形的黑色塑胶垫片,一般拆机后这个垫片是不会掉出来的,所以一般上盖时也是直接把中壳合上后上螺丝. 这样安装基本会导致塑胶垫片把摄像头顶住 ...
- Qt 日志宏
随便写了一个日志帮助的宏,既可以如同qDebug()一般在调试时输出信息,也可以在输出文本文件 #ifndef LOG_H #define LOG_H #include <QDir> #i ...
- CentOS6.5 MySQL 配置设置总结笔记
三.登录MySQL 登录MySQL的命令是mysql, mysql 的使用语法如下: mysql [-u username] [-h host] [-p[password]] [dbname] u ...
- WPF中Image控件绑定到自定义类属性
首先我们定义一个Student类,有ID,Name,Photo(保存图片路径). using System; using System.Collections.Generic; using Syste ...
- ofbiz进阶之框架配置文件指导
The Open For Business Project: Framework Configuration Guide 原文链接:http://ofbiz.apache.org/docs/corec ...
- SQL一列多行字符串分组合并
最近工作遇到如下数据:需要合并后只剩下两行的数据,普通的group by 是不能实现的.(如图) 利用如下SQL代码,即可实现需求(如图): 利用 stuff 函数实现分拆合并操作 select Te ...
- 使用Yeoman搭建 AngularJS 应用 (8) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/write-app.html 创建一个新的模板来显示一个todo的列表 打开views/main.html 为了从一个干净的模板开始,删除m ...
- 使用Redis构建简单的ORM
Reids相关的资料引用 http://www.tuicool.com/articles/bURJRj [Reids各种数据类型的应用场景] https://github.com/antirez/re ...
- PAT-乙级-1029. 旧键盘(20)
1029. 旧键盘(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 旧键盘上坏了几个键,于是在敲一段文字的 ...