以前同事为了炫耀ruby的简洁,特意出一道题来考小陈:

在写一个爆破密码的字典生成工具,其中有这样一个需求:

输入一个单词:列出这个单词的所有大小写组合,比如ruby Ruby rUby ruBy rubY RuBy RuBY ....等等,这样2^n个

用C#该怎么写?

然后他把ruby的写法给了小陈:

s='abcd'

[nil].product(*[s.chars, s.swapcase.chars].transpose).map(&:join)

小陈只有从linq另辟蹊径了。最后在大牛的帮助下解决了问题:

void Main()
{
string word = "abcd";
List<List<string>> letters = new List<List<string>>();
letters = word.ToCharArray().Select(w => new List<string>(){w.ToString().ToLower(),w.ToString().ToUpper()} ).ToList();
CartesianProduct(letters)
.Select (x => x.Aggregate ((a,b) => a + b))
.ToList()
.ForEach(Console.WriteLine);
} static IEnumerable<IEnumerable<T>> CartesianProduct<T>( IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] {item}));
}

在小陈看来已经是很简洁了。不过还是败了。

letter upper lower combo的更多相关文章

  1. python3----转换大小写(upper lower capitalize and title)

    和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower().还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及 ...

  2. python title() upper() lower() 以首字母大写的方式显示每个单词/将字符串改为全部大写或全部小写

    以首字母大写的方式显示每个单词 [root@chenbj python]# cat name.py #!/usr/bin/env python # _*_ coding:utf-8 _*_ name ...

  3. 43-python基础-python3-字符串-常用字符串方法(一)-upper()-lower()-isupper()-islower()

    请注意, 这些方法没有改变字符串本身,而是返回一个新字符串. 如果你希望改变原来的字符串,就必须在该字符串上调用 upper()或 lower(),然后将这个新字符串赋给保存原来字符串的变量.   1 ...

  4. Django学习路26_转换字符串大小写 upper,lower

    在 urls 中注册 url(r'getstr',views.getstr) 在 views.py 中添加函数 def getstr(request): string = 'abc' string_2 ...

  5. python之字符串方法upper/lower

    1.描述: upper():用于将字符串全部转换为大写字母 lower():用于将字符串全部转换为小写字母 2.语法 str.upper() str.lower() 3.返回值 upper()或low ...

  6. 虚拟表dual。字符串函数UPPER,LOWER。&变量。INITCAP,LENGTH,SUBSTR

    &自定义变量的用法:

  7. str.replace()和re.sub()/calendar.month_abbr/re.subn()/upper和lower和capitalize/贪婪匹配和费贪婪匹配/re.S和re.DOTALL 笔记

    str.replace()可以进行简单的替换 >>> a = 'one.txt, index.py, index.php, index.html, index.js' >> ...

  8. The Most Wanted Letter

    The Most Wanted Letter You are given a text, which contains different english letters and punctuatio ...

  9. Hackerrank11 LCS Returns 枚举+LCS

    Given two strings,  a and , b find and print the total number of ways to insert a character at any p ...

随机推荐

  1. No module named django.core

    在虚拟环境中将django-admin.py startproject tango_with_django_project替换为django-admin startproject tango_with ...

  2. android权限大全

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...

  3. [NHibernate]视图处理

    目录 写在前面 文档与系列文章 视图 一个例子 总结 写在前面 前面的文章主要讲了对物理数据表的操作,当然了Nhibernate同样可以操作视图,本文将讲nhibernate对视图操作的种种. 文档与 ...

  4. Maven、SecureCRT使用问题汇集

    1 Maven 无法下载pom文件中相关的依赖包 该问题可能有很多原因,我的原因是host中的localhost被修改了,改回来即可! 看起来好像出了一些网络原因的问题,顺着这个方向搜索,发现国外也有 ...

  5. UI第十五节——UIWebView

    - (void)viewDidLoad {    [super viewDidLoad];    UIWebView *webView = [[UIWebView alloc] initWithFra ...

  6. PHP exec/system启动windows应用程序,执行.bat批处理,执行cmd命令

    exec 或者 system 都可以调用cmd 的命令 直接上代码: <?php /** 打开windows的计算器 */ exec('start C:WindowsSystem32calc.e ...

  7. 记录一下git 的常用命令

    以后如果要写一个东西,最好先搭建一个本地仓库,用版本控制对其进行操作,可能一开始有一些麻烦,但是很有可能会受益无穷. 说到git,必然会和github联系起来. 不管是在ubuntu里面还是在Wind ...

  8. Collection接口

    Collection接口所定义的方法: clear:清空 retainAll 求一个Collection和另一个 Collection的交集. object[] toArray()  把里面的各个对象 ...

  9. JavaScript——基本的瀑布流布局及ajax动态新增数据

    本文用纯js代码手写一个瀑布流网页效果,初步实现一个基本的瀑布流布局,以及滚动到底部后模拟ajax数据加载新图片功能. 缺点: 1. 程序不是响应式,不能实时调整页面宽度: 2. 程序中当新增ajax ...

  10. How to install Shadow•socks in CentOS7

    Helps from: http://www.cmsky.com/shadowsocks-python-install/ http://shadowsocks.blogspot.jp/?m=1 wge ...