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 \n 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 = ; i <= n; i = i + )
{
list.Add(i);
}
result = string.Join("\n", list.Select(item => Selector(item))); return result;
} public static string Selector(int number)
{
string str = string.Empty;
for (int i = ; 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 <= ) return String.Empty; var oddNumbers = Enumerable.Range(, n).Where(i => i % == );
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)
{
}

Complete The Pattern #6 - Odd Ladder的更多相关文章

  1. Complete The Pattern #1

    Complete The Pattern #1 Task: You have to write a function pattern which creates the following patte ...

  2. Complete The Pattern #2

    Complete The Pattern #2 Description: Task: You have to write a function pattern which creates the fo ...

  3. 如何正确地使用RecyclerView.ListAdapter

    默认是在一个fragment中实现RecyclerView. private inner class CrimeAdapter() : ListAdapter<Crime, CrimeHolde ...

  4. SCALA XML pattern attrbute(属性)

    from: Working with Scala's XML Support 虽然这个guy炒鸡罗嗦,但是还是讲到我要的那句话:  Because Scala doesn't support XML ...

  5. Software Engineer Title Ladder

    http://changelog.ca/log/2013/08/09/software_engineer_title_ladder Within the software engineering pr ...

  6. Event Sourcing Pattern 事件源模式

    Use an append-only store to record the full series of events that describe actions taken on data in ...

  7. Beginning Scala study note(5) Pattern Matching

    The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...

  8. Compute Resource Consolidation Pattern 计算资源整合模式

    Consolidate multiple tasks or operations into a single computational unit. This pattern can increase ...

  9. Competing Consumers Pattern (竞争消费者模式)

    Enable multiple concurrent consumers to process messages received on the same messaging channel. Thi ...

随机推荐

  1. KnockoutJS(2)-监控属性

    本节主要涉及到3个内容: 1. 监控属性 Observables (这个用的比较多,但是其本身使用比较简单,唯一需要注意就是,它监控的对象是一个方法,所以取值和设置值的时候容易混淆) 2. 计算属性  ...

  2. DTcms手机版使用余额支付 提示信息跳转到PC版的错误。以及提交订单不打开新页面

    手机版使用余额支付 提示信息跳转到PC版的错误 引起错误的原因是中间需要提交到DTcms.Web\api\payment\balance\index.aspx去处理 导致BasePage.cs中的li ...

  3. HTML5 Video(视频)

    HTML5 Video(视频) 很多站点都会使用到视频. HTML5 提供了展示视频的标准. 检测您的浏览器是否支持 HTML5 视频: 检测 Web站点上的视频 直到现在,仍然不存在一项旨在网页上显 ...

  4. maven项目转eclipse工程的命令:eclipse.bat

    call mvn clean:clean call mvn eclipse:eclipse -DdownloadSources=true @pause 复制以上内容,保存为eclipse.bat 以后 ...

  5. 四、mysql内置函数

    .字符串函数 concat('a','b'); 字符串拼接函数 ,,"我是A我是B"): 从指定位置开始替换指定长度的指定数据(起步为1) lower() 转小写 upper() ...

  6. excle,aspose.cells 公式字段值取不到 xmls转xml

    问题: 一,单元格如果是公式的,读出值为0 aspose.cells 4.4.0.5版本 由于太低,读xmls后缀的excel文件时,发现如果此列是公式算出来的,值是获取不到的.获取到的值一直是0 二 ...

  7. hdu 4717 The Moving Points(第一个三分题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4717 [题意]: 给N个点,给出N个点的方向和移动速度,求每个时刻N个点中任意两点的最大值中的最小值,以及取最小 ...

  8. 1588: [HNOI2002]营业额统计 - BZOJ

    Description营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...

  9. 1021: [SHOI2008]Debt 循环的债务 - BZOJ

    Description Alice.Bob和Cynthia总是为他们之间混乱的债务而烦恼,终于有一天,他们决定坐下来一起解决这个问题.不过,鉴别钞票的真伪是一件很麻烦的事情,于是他们决定要在清还债务的 ...

  10. mybatis foreach标签

    一.批量插入数据 示例:添加订单商品表 1.模型层的相应代码 /** * 添加订单商品表 * @param ordergoods * @return */ public boolean addOrde ...