zoukankan      html  css  js  c++  java
  • 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();
      }
    }
  • 相关阅读:
    读书笔记 之《Thinking in Java》(对象、集合、异常)
    ArrayList 和 LinkedList的执行效率比较
    Hybris CronJob.
    C# 中的treeview绑定数据库(递归算法)
    identity_insert---实验性插入大批量数据和分页存储过程
    SQL Server 存储过程
    PL/SQL 在64位机上不能使用的问题解决
    登陆Oracle11g的企业管理器
    SQL在oracle和SQLserver将查询结果创建为新表的不同之处
    介绍一下内联、左联、右联
  • 原文地址:https://www.cnblogs.com/chucklu/p/4621604.html
Copyright © 2011-2022 走看看