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 <= 0)
            {
                array = new string[] { };

            }
            else
            {
                if (n > 254)
                {
                    n = 254;
                }
                List<string> list = new List<string>();
                string str = string.Empty;
                for (int i = 1; i <= n; i++)
                {
                    str = i.ToString("x2");
                    str = "#" + string.Join(string.Empty, Enumerable.Repeat(str, 3));
                    list.Add(str);
                }
                array = list.ToArray();
            }
            return array;
        }
    }

其他人的写法

using System;

public class Kata{
public static string[] ShadesOfGrey(int n){
  string[] arr = null;
         n = n <= 0 ? 0 : (n > 254 ? 254 : n);
         if (n > 0)
         {
             arr = new string[n];
             for (int i = 1; i <= n; ++i)
             {
                 arr[i-1] = 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 < 0)
      count = 0;

    return Enumerable
      .Range(1, Math.Min(count, 254))
      .Select(x => string.Format("#{0:x2}{0:x2}{0:x2}", x))
      .ToArray();
  }
}
时间: 2024-12-19 00:15:46

254 shades of grey的相关文章

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 and recall past experiences and data to reason about future events, is a term used frequently in current literature. All models in machine learning co

使用神经网络来识别手写数字【转译】(二)

A simple network to classify handwritten digits Having defined neural networks, let's return to handwriting recognition. We can split the problem of recognizing handwritten digits into two sub-problems. First, we'd like a way of breaking an image con

Creating an generated Earth AVI with C++

Creating an generated Earth AVI with C++        EarthGenerator.cpp /*    EarthGenerator.cpp An example on how to use AviMemDC.cpp Copyright (c) 1998-2003 Torben AE. Mogensen   Copyright (c) 2004,2005 René Nyffenegger All algorithms and source code pe

ImageSharp Working with images

xWorking with imagesImage classAn Image class contained in Image.cs has been provided on Blackboard. It is highlyrecommended that you download and use this class in your project, as it encapsulates a lot ofconvenient functionality for working with im

win7设置固定IP重启后无法上网,ipconfig显示为自动配置IPV4 169.254的地址

近日安装原版Win7系统打完网卡驱动补丁后,给电脑设置了固定的IP地址后一切正常,但是电脑重启后发现上不了网了,右下角网络图标有个感叹号,打开网络和共享中心-->本地连接-->详细信息-->发现IPv4的地址与ipconfig /all得到的IP地址一致,均显示为:自动配置IPv4地址:169.254.123.188(首选) 但是查看本地连接-->属性里看到之前设置的固定IP地址是没有问题的, 所以想到了应该是电脑启用了自动配置IPv4功能,导致了固定IP无法分配给电脑, 尝试用命

学习笔记 254: 预备知识总结

一 你的服务器serverX有2个网卡接口slave1,slave2,按照下面接口信息,创建一个team1组: 这个组使用activebackup模式,IP地址配置信息如下: 192.168.85.200/24 fd00:ba5e:ba11:x:1/64 验证:同网段可以ping通 二 你的服务器serverX提供caching DNS服务,在172.25.x.0/24子网,使用172.25.25.254作为转发. 应该使用DNSSEC验证example.com域 三 你的服务器serverX可

Grey Area(数学统计题)

Grey Area Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Special Judge Submit Status PID: 1083 Dr. Grey is a data analyst, who visualizes various aspects of data received from all over the world eve

Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的次序将1-n种试剂滴入试管,如果正在滴入的试剂能与已经滴入 的试剂反应,那么危险数*2,否则维持不变.问最后最大的危险系数是多少. 分析:其实这个题根本不用考虑倒入的顺序,只是分块就行,结果就是每个子集里元素的个数-1 和  的2的幂. 1 #include <iostream> 2 #inclu

解决内网254个IP地址不够用问题

很多时候企业局域网络出现私网地址192.168.1.x-255不够用了,去掉一个广播地址及一个网络地址.(0是网络号不可用, 255是广播地址,除去这2个,可用的就是254个地址) C段的就只能254个了啦,解决这个问题很简单直接使用B段网络地址172.0.0.0/16,如172.0.0.1/16及172.0.3.1/16这样就能通讯了. 实例电脑配置: