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. 关于html5 -- plus Webview模块管理应用窗口界面

    Webview模块管理应用窗口界面,通过plus.webview可获取应用界面管理对象. 方法: all:获取所有的webview窗口 close:关闭webview窗口 create:创建新的web ...

  2. spring-cloud-hystrix熔断

    依赖pom <dependencyManagement> <dependencies> <dependency> <groupId>org.spring ...

  3. 当前标识(NT AUTHORITY\NETWORK SERVICE)没有对“C:\WINDOWS2\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files”的写访问权限。

    卸载了域控制器之后,IIS出现“当前标识(NT AUTHORITY\NETWORK SERVICE)没有对“C:\WINDOWS2\Microsoft.NET\Framework\v4.0.30319 ...

  4. PHP加解密相关函数

    openssl_public_encrypt()  - Encrypts data with public keyopenssl_public_decrypt()  - Decrypts data w ...

  5. linux内核中sys_poll()的简化分析

    app:poll or select; kernel: sys_poll(); do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,st ...

  6. C#快速学习笔记(译)续一

    6.虚拟和非虚拟函数 下面是一个非虚拟函数 using System; namespace Test2 { class Plane { public double TopSpeed() {return ...

  7. linux eval命令

    eval 功能说明:重新运算求出参数的内容.语 法:eval [参数]补充说明:eval可读取一连串的参数,然后再依参数本身的特性来执行.参 数:参数不限数目,彼此之间用分号分开. 1.eval命令将 ...

  8. opencv学习笔记(02)——遍历图像(指针法)

    #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <ope ...

  9. JavaScript技巧45招

    原文:45 Useful JavaScript Tips, Tricks and Best Practices作者:Saad Mousliki 在这篇文章里,我将分享一些JavaScript的技巧.秘 ...

  10. OFBIZ bug_ControlServlet.java:233:ERROR

    错误日志: [java] 2014-09-26 10:12:17,031 (http-bio-0.0.0.0-8443-exec-5) [ ControlServlet.java:233:ERROR] ...