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. 条形码Code128源代码

    public class Code128 { private DataTable m_Code128 = new DataTable(); ; /// <summary> /// 高度 / ...

  2. 热键HotKeys

    一:新建类HotKeys命名空间: using System.Runtime.InteropServices; 二:注册热键API [DllImport("user32")] pu ...

  3. NativeExcel 读取文件

    class function T_EShopDataBill.ImportData(const AFileName: String; AList: T_EShopDataModelList; var ...

  4. spring mvc 初步接触学习笔记

    1.使用maven导入spring mvc web 的jar 包 最新语句 <dependency> <groupId>org.springframework</grou ...

  5. 关于FileSystemWatcher监听文件创建

    FileSystemWatcher中的Created事件不但可以监听用户创建的文件,当用户删除某个文件时,系统会在再监听的那个盘上的回收站创建一个文件,在回收站创建的文件也会触发Created事件,而 ...

  6. Asp.net MVC 如何向webform一样在IIS里添加虚拟目录

    相信很多用webform的程序猿都习惯性的使用虚拟目录的形式来对一个程序添加新的功能,那么在mvc下该如何来弄呢? 首先得有一个项目基层的项目,然后我们在这个项目的基础上新增一个功能模块,例如信息发布 ...

  7. 1176: [Balkan2007]Mokia - BZOJ

    Description维护一个W*W的矩阵,每次操作可以增加某格子的权值,或询问某子矩阵的总权值. 修改操作数M<=160000,询问数Q<=10000,W<=2000000.Inp ...

  8. [JavaScript] js验证身份证

    function checkIdCard(){     var vcity={11:"北京",12:"天津",13:"河北",14:&quo ...

  9. JS中如何定义全局变量

    三种方法 1.在js的function外定义一个变量 var name='测试'; function XX(){        alert(name); } 2.不使用var,直接给定义变量,隐式的声 ...

  10. [转载]MongoDB优化的几点原则

    .查询优化 确认你的查询是否充分利用到了索引,用explain命令查看一下查询执行的情况,添加必要的索引,避免扫表操作. .搞清你的热数据大小 可能你的数据集非常大,但是这并不那么重要,重要的是你的热 ...