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 ...
随机推荐
- mysql 拷贝表插入新的表
insert into table1 select * from table; insert into talble set name = value;
- QT定制有标题的扁平化下拉框控件
关键字:QT,QComboBox,QLineEdit,QListView,QPushButton,QMenu,QWidgetAction,setStyleSheet OS:Windows 7 方法一: ...
- 1065. A+B and C (64bit)
#include<stdio.h> #include <math.h> int main() { long long a,b,c,sum; int n,i; while(sca ...
- [转]java gridbag 说明
gridx = 2; // X2 gridy = 0; // Y0 gridwidth = 1; // 横占一个单元格 gridheight = 1; // 列占一个单元格 weightx = 0.0 ...
- ofbiz进阶之实体引擎配置文件
The Open For Business Project: Entity Engine Configuration Guide 原文链接:http://ofbiz.apache.org/docs/e ...
- 四、记一次失败的 CAS 搭建 之 结果总是那么伤(客户端)
==================================================================================================== ...
- JS 实现取整
Js 常用数值函数(Math,parseInt)取整 1.丢弃小数部分,保留整数部分parseInt(5/2) 2.向上取整,有小数就整数部分加1Math.ceil(5/2) 3,四舍五入.Mat ...
- eclipse 书签
虽然eclipse有back to和forward两个功能帮助我们阅读代码,但有时候代码一层一层看下去后,会忘了自己最初的起点. 因此想到了eclipse的书签bookmark功能. 首先,添加书签. ...
- webApp添加到iOS桌面
iOS中的safri浏览器可以将一个网页添加到桌面,当做一个独立的应用运行. 当然,这里我们不讨论怎么去做一个webApp,这需要html5的相关知识和开发经验.这里我们只讲webApp添加桌面后到启 ...
- jsp的静态包含与动态包含:<%@ include file="" %>和<jsp:include page=""></jsp:include>区别与分析
<%@ include file="" %>是将文件原封不动的copy进现有的文件中,像是拼接好后,再编译成为servlet运行. <jsp:include pa ...