makeBackronym
makeBackronym 主要考查的是字符串的处理,大小写转换,以及字符串的Linq处理
Description:
Definition-
back·ro·nym
noun
a fanciful expansion of an existing acronym or word, such as “port out, starboard home” for posh.
You will create a function called makeBackronym . There will be a preloaded dictionary to use. The dictionary is an object where the the keys are letters A-Z and the values are a predetermined word.
Use the variable name (its name is written in the code template) to reference the uppercase letters of the dictionary.
EXAMPLE:
dict['P']=="perfect"
There will be a string(without spaces) passed into the function that you need to translate to a Backronym.
The preloaded dictionary can only read uppercase letters, and the value you return will have to be a string.
EXAMPLES:
"dgm" -> "disturbing gregarious mustache"
"lkj" -> "literal klingon joke"
using System;
public partial class Kata
{
public static string MakeBackronym(string s)
{
s = s.ToUpper();
string str = string.Empty;
foreach(var item in s)
{
str += dict[item]+" ";
}
if(str.Equals(string.Empty)==false)
{
str = str.Substring(,str.Length-);
}
return str;
}
}
其他解法:
涉及到string类的静态函数Join
以及string类的方法ToUpper
以及Linq中的select
using System.Linq; public partial class Kata
{
public static string MakeBackronym(string s)
{
return string.Join(" ", s.ToUpper().Select(c => dict[c]));
}
}
#region 程序集 System.Core.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Core.dll
#endregion namespace System.Linq
{
// 摘要:
// 提供一组用于查询实现 System.Collections.Generic.IEnumerable<T> 的对象的 static(在 Visual
// Basic 中为 Shared)方法。
public static class Enumerable { public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector); }
}
makeBackronym的更多相关文章
随机推荐
- Spring多数据源的动态切换
Spring多数据源的动态切换 目前很多项目中只能配置单个数据源,那么如果有多个数据源肿么办?Spring提供了一个抽象类AbstractRoutingDataSource,为我们很方便的解决了这个问 ...
- DTCMS添加文章,将tags标签的值赋到SEO关键词上,以及将摘要的值赋到SEO描述
将tags标签的值赋到SEO关键词上 admin\article_edit.aspx中 $(function () { 方法中加上 //tags的值赋到SEO关键词上 $("#txtTag ...
- linux之cat命令
1. cat 接普通文件名,会把文件内容打印到屏幕:2. cat > file,这个可以向文件“file”写入内容,最后按 Ctrl + D 结束输入,会将你输入的数据保存到文件. cat主要有 ...
- [ Database ] [ Sybase ] [ SQLServer ] sybase 與SQL Server的界接方式
目前我們有個專案Server A安裝了 SQL Server 2012,有個需求需要連線到另外一台Server B上的 Sybase 12.5的view, 先前試過了很多方法都無法連通.主要的原因是因 ...
- 解决Win7系统安装时“安装程序无法定位现有 系统分区,也无法创建新的系统分区”提示
第一步:U盘启动装系统时,格式化主分区的内容后出现上面的问题 第二步:重启机器,通过U 盘启动.进入win pe系统. 第三步:把windows 7的ISO镜像解压到电脑的非系统盘的其他硬盘上.如D: ...
- 【Vmware】已有镜像文件的导入
1 虚拟机文件夹中各个文件简介 在创建虚拟机的时候会把相关的文件保存到一个文件夹中.我的机器是Windows 7,64位 ,保存的路径是: C:\Users\User\Documents\Virtu ...
- python编程语言缩进格式
python的缩进格式是python语法中最特别的一点,很多已经习惯了其他语言的朋友再去学python的话,开始会觉的不太 习惯. 怎么看怎么都觉的别扭,也有一些朋友因为这个特别的格式与python失 ...
- 论Oracle字符集“转码”过程
本文将通过实验来演示一下Oracle字符集“转码”的确认过程. 1.实验环境说明 客户端是Windows XP操作系统的SQL*Plus程序,客户端字符集是936(对应Oracle的ZHS16GBK字 ...
- drag drop小游戏
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 记一次apt-get无法安装git的问题
解决apt-get安装过程中出现的Size mismatch和Hash Sum mismatch问题. 事情起因 我从单位复制了一个Virtualbox虚拟机(ubuntu 15.04 Desktop ...