Complete The Pattern #2
Complete The Pattern #2
Description:
Task:
You have to write a function pattern which creates the following pattern upto n number of rows. If the Argument is 0 or a Negative Integer then it should return "" i.e. empty string.
Pattern:
(n)(n-1)(n-2)...4321
(n)(n-1)(n-2)...432
(n)(n-1)(n-2)...43
(n)(n-1)(n-2)...4
...............
..............
(n)(n-1)(n-2)
(n)(n-1)
(n)
Examples:
pattern(4):
4321
432
43
4
pattern(6):
654321
65432
6543
654
65
6
Note: There are no blank spaces
Hint: Use \n in string to jump to next line
之前做的几个练习题,看别人用Linq用的那么溜,看起来高大上,所以自己研究了一下,也开启了装逼模式
using System;
using System.Linq; public class Kata
{
public string Pattern(int n)
{
string result = string.Empty;
if (n > )
{
result = string.Join(Environment.NewLine, Enumerable.Range(, n).Reverse().Select(item => string.Join(string.Empty, Enumerable.Range(n - item + , item).Reverse())));
}
return result;
}
}
Enumerable.Range(1, n)得到1,2,3...,9
然后Reverse获取倒序的数字9,8,7,...,1
再通过Select以及lambda表达式针对每一个生成一个不同的结果
item => string.Join(string.Empty, Enumerable.Range(n - item + 1, item).Reverse())
每一个item,生成对应的item,item-1,...,n-item+1 ,然后将这个结果用string.empty连接起来
Complete The Pattern #2的更多相关文章
- Complete The Pattern #1
Complete The Pattern #1 Task: You have to write a function pattern which creates the following patte ...
- Complete The Pattern #6 - Odd Ladder
Complete The Pattern #6 - Odd Ladder Task: You have to write a function pattern which creates the fo ...
- 如何正确地使用RecyclerView.ListAdapter
默认是在一个fragment中实现RecyclerView. private inner class CrimeAdapter() : ListAdapter<Crime, CrimeHolde ...
- Event Sourcing Pattern 事件源模式
Use an append-only store to record the full series of events that describe actions taken on data in ...
- 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 ...
- Compensating Transaction Pattern(事务修正模式)
Undo the work performed by a series of steps, which together define an eventually consistent operati ...
- Circuit Breaker Pattern(断路器模式)
Handle faults that may take a variable amount of time to rectify when connecting to a remote service ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
随机推荐
- C# Redis实战
转自 :http://blog.csdn.net/qiujialongjjj/article/details/16945569 一.初步准备 Redis 是一个开源的使用ANSI C 语言编写.支持 ...
- PHP7 新特性 简介
整理了一些常用的新特性,欢迎点赞!!! 新增操作符 1.?? $username = $_GET['user'] ?? ''; $username = isset($_GET['user']) ? $ ...
- centos6.4下安装freetds使php支持mssql
centos版本:6.4 php版本5.3.17 没有安装之前的情况:nginx+php+mysql+FPM-FCGI 接下来安装步骤如下: 1.打开http://www.freetds.org/,进 ...
- IE11下ASP.NET Forms身份认证无法保存Cookie的问题
IE11下ASP.NET Forms身份认证无法保存Cookie的问题 折腾了三四天,今天才找到资料,解决了. 以下会转贴,还没来得及深究,先放着,有空再学习下. ASP.NET中使用Forms身份认 ...
- EditorLineEnds.ttr 受影响的D版本 Delphi 8-2010
http://stackoverflow.com/questions/25295980/delphi-2006-2010-error-cannot-create-file-c-users-admin- ...
- 编码错误设置错误报 "SyntaxError: Non-ASCII character '/xe6' "
无意中碰到键盘导致一段处理中文拼音的 python 代码跑起来报了个错 “SyntaxError: Non-ASCII character ‘/xe6' " 看了下是注释 # coding: ...
- ScheduledExecutorService的用法——定时执行两个任务
package control; import java.text.DateFormat; import java.text.ParseException; import java.text.Simp ...
- 蜗牛历险记(二) Web框架(上)
接上篇所说,本篇主要内容是讲述如何使用Autofac来管理整个平台的生命周期(初级). 一.简述 插件式Web开发的同学应该还会记得PreApplicationStartMethod这个Assembl ...
- EXTJS 4.2 资料 跨域的问题
关于跨域,在项目开发中难免会遇到:之前笔者是用EXTJS3.0开发项目的,在开发过程中遇到了关于跨域的问题,但是在网上找到资料大部分都是ExtJs4.0以上版本的 在ExtJs中 例如:Ext.Aja ...
- IOS UIVIEW layer动画 总结(转)
转发自:http://www.aichengxu.com/article/%CF%B5%CD%B3%D3%C5%BB%AF/16306_12.html IOS UIVIEW layer动画 总结, ...