• Complete The Pattern #6


    Complete The Pattern #6 - Odd Ladder

    Task:

    You have to write a function pattern which creates the following pattern (see examples) up to the desired number of rows.

    • If the Argument is 0 or a Negative Integer then it should return "" i.e. empty string.

    • If any even number is passed as argument then the pattern should last upto the largest odd number which is smaller than the passed even number.

    Examples:

    pattern(9):

    1
    333
    55555
    7777777
    999999999

    pattern(6):

    1
    333
    55555

    Note: There are no spaces in the pattern

    Hint: Use in string to jump to next line

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public static class Kata
    {
      public static string OddLadder(int n)
            {
                string result = string.Empty;
               
                    List<int> list = new List<int>();
                    for (int i = 1; i <= n; i = i + 2)
                    {
                        list.Add(i);
                    }
                    result = string.Join("
    ", list.Select(item => Selector(item)));
                
                return result;
            }
    
            public static string Selector(int number)
            {
                 string str = string.Empty;
                for (int i = 0; i < number; i++)
                {
                    str = str + number.ToString();
                }
                return str;
            }
    }

    Linq中的Select

    //public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

    Func<TSource, TResult>泛型委托
    //public delegate TResult Func<in T, out TResult>(T arg)

    其他人的解法:

    System.Linq命名空间下的Enumerable类的使用

    using System;
    using System.Linq;
    
    public static class Kata
    {
      public static string OddLadder(int n)
      {
        if (n <= 0) return String.Empty;
        
        var oddNumbers = Enumerable.Range(1, n).Where(i => i % 2 == 1);
        var lines = oddNumbers.Select(i => String.Join("", Enumerable.Repeat(i.ToString(), i)));
        
        return String.Join(Environment.NewLine, lines);
      }
    }
    /// <summary>
    /// Generates a sequence of integral numbers within a specified range. 
    /// </summary>
    /// <param name="start">The value of the first integer in the sequence.</param>
    /// <param name="count">The number of sequential integers to generate.</param>
    /// <returns></returns>
    public static IEnumerable<int> Range(int start, int count)
    {
    
    }
    
    /// <summary>
    /// Generates a sequence that contains one repeated value.
    /// </summary>
    /// <typeparam name="TResult">The type of the value to be repeated in the result sequence.</typeparam>
    /// <param name="element">The value to be repeated.</param>
    /// <param name="count">The number of times to repeat the value in the generated sequence.</param>
    /// <returns></returns>
    public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)
    {
    }
  • 相关阅读:
    web前端防治重复提交
    layabox笔记
    fixfff
    laybax
    小游戏初始化,资源加载异常处理,黑屏处理
    微信小游戏资源加载页与云存储
    前端唠嗑
    css 的一些样式笔记
    小游戏虚拟手柄
    VUE最佳实践
  • 原文地址:https://www.cnblogs.com/chucklu/p/4613934.html
Copyright © 2020-2023  润新知