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 ...
随机推荐
- Spark Streaming揭秘 Day3-运行基石(JobScheduler)大揭秘
Spark Streaming揭秘 Day3 运行基石(JobScheduler)大揭秘 引子 作为一个非常强大框架,Spark Streaming兼具了流处理和批处理的特点.还记得第一天的谜团么,众 ...
- realloc() 用法详解
原型:extern void *realloc(void *mem_address, unsigned int newsize); 语法:指针名=(数据类型*)realloc(要改变内存大小的指针名, ...
- Android:使用命令行工具adb、mksdcard等
有一些Android的工具需要在命令行的环境中运行,只是可以选择Windows的开始->运行,键入cmd并确定,进入命令行的界面中运行.主要的命令行工具包括adb和mksdcard等.命令行的工 ...
- BitSet
前几天干了一件比较无聊的事儿——抄了一遍C++ STL bitset的源代码,把不懂的宏定义去掉了,发现(暂时)还能用,嘿嘿. #ifndef BITSET_H #define BITSET_H #i ...
- Jquery实现自动提示下拉框
1.引入脚本库: <script type="text/javascript" src="/Jscripts/jquery-1.3.2.js"&g ...
- oc 中四种实例变量的范围类型@private@protected@public@package
To enforce the ability of an object to hide its data, the compiler limits the scope of instance vari ...
- 机器学习基石的泛化理论及VC维部分整理
第四讲 机器学习的可行性 一.Hoeffding's Inequality \(P[\left | \nu -\mu \right |>\epsilon ] \leq 2exp(-2\epsi ...
- 实时数据处理环境搭建flume+kafka+storm:1.zookeeper 安装配置
1. 解压 tar -zxvf 2.创建目录 zk根目录创建 mkdir zkdatalog --日志 mkdir zkdata ---快照文件 3.修改配置文 ...
- 1189: [HNOI2007]紧急疏散evacuate - BZOJ
Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...
- 通过注册表查找oracle_home的位置
运行regedit进入注册表 ctrl+f查找oracle_home