Complete The Pattern #6 - Odd Ladder
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的更多相关文章
- Complete The Pattern #1
Complete The Pattern #1 Task: You have to write a function pattern which creates the following patte ...
- Complete The Pattern #2
Complete The Pattern #2 Description: Task: You have to write a function pattern which creates the fo ...
- 如何正确地使用RecyclerView.ListAdapter
默认是在一个fragment中实现RecyclerView. private inner class CrimeAdapter() : ListAdapter<Crime, CrimeHolde ...
- SCALA XML pattern attrbute(属性)
from: Working with Scala's XML Support 虽然这个guy炒鸡罗嗦,但是还是讲到我要的那句话: Because Scala doesn't support XML ...
- Software Engineer Title Ladder
http://changelog.ca/log/2013/08/09/software_engineer_title_ladder Within the software engineering pr ...
- Event Sourcing Pattern 事件源模式
Use an append-only store to record the full series of events that describe actions taken on data in ...
- Beginning Scala study note(5) Pattern Matching
The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...
- Compute Resource Consolidation Pattern 计算资源整合模式
Consolidate multiple tasks or operations into a single computational unit. This pattern can increase ...
- Competing Consumers Pattern (竞争消费者模式)
Enable multiple concurrent consumers to process messages received on the same messaging channel. Thi ...
随机推荐
- js中的等号与非等号
等号与非等号都会进行类型转换,转换规则如下: 1 如果有一个是boolean值,则true改为1,false改为0 false == 0 ; true == 1 返回true true == 2 ...
- 安卓项目中使用JSON引发的一个小错误 Multiple dex files define Lorg/apache/commons/collections/Buffer
原因: 这里添加的jar包和android自带的jar产生了冲突
- laravel方法汇总详解
1.whereRaw() 用原生的SQL语句来查询,whereRaw('select * from user') 就和 User::all()方法是一样的效果 2.whereBetween() 查询时 ...
- opencv学习笔记(02)——遍历图像(指针法)
#include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <ope ...
- 使用公钥登录SSL
在本地生成密钥对 ssh-keygen -t rsa 如果不想设置密码,可以直接点击回车. 如果你想使用DSA可以用-t DSA替换. 确保远程计算机上用户目录下有.ssh目录 确保你的连接服务器上的 ...
- SQL Server数据库备份(异机)
简单的远程异机备份数据库功能,通过这个存储过程,讲远程其他机器上的数据库备份到本地.其主要原理为: 1.通过XP_CMDSHELL执行Windows命令,将本机的共享目录映射为远程机器的网络驱动器. ...
- SelectList
SelectList 构造函数 (IEnumerable, String, String, Object) 使用列表的指定项.数据值字段.数据文本字段和选定的值来初始化 SelectList 类的新实 ...
- keystone命令与client接口学习
keystone学习 ------------------ Keystone(OpenStack Identity Service)是OpenStack框架中,负责身份验证.服务规则和服务令牌的功能, ...
- js模拟触发事件
html标签元素封装着实用的[事件],但在很多时候,需要[模拟触发事件],比如 [按钮单机事件] 可以实实在在点击按钮触发该事件,但体验而言,很多时候需要js逻辑处理让实现 触发事件的效果这时就用 ...
- springMVC上传图片
http://blog.csdn.net/cheung1021/article/details/7084673/ http://toutiao.com/a6293854906445021442/ 工程 ...