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的更多相关文章
随机推荐
- laravel项目拉取下来安装,node.js库安装
1.拉取项目 2.切换分支 圈圈里面是版本 composer 安装laravel组件其他库 安装node.js安装包 npm set registry=https://registry.npm.ta ...
- if...else..的错误用法
1.最近在写js代码完成一个前段DOM操作的函数时,自己错误的使用了if..else..控制体.为什么是错误的呢?看看我的 代码你就明白了: document.getElementsByClassNa ...
- opencv学习笔记(03)——遍历图像(迭代器法)
#include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #include & ...
- Thinkcmf 在新浪云上的部署问题
最近要开发一个社团主页,于是想到了CMF内容管理系统的,但是直接在自己的服务器测试成本太高,于是选择了在新浪云上进行部署测试. 但是在安装Thinkcmf的过程中产生了一些技术性的问题.但最后终于在自 ...
- IOS 学习参考
IOS 开发 http://code4app.com/ios/%E5%AE%9E%E6%97%B6%E6%9B%B4%E6%96%B0%E7%9A%84%E6%9B%B2%E7%BA%BF%E5%9B ...
- C#...何时需要重写ToString()方法?
一般类型,都是继承自System.Object类,默认情况下,object类的ToString方法会返回当前类的类型的字符串表达形式.但也有例外!! DateTime,它就重写ToString方法,D ...
- Unity3d本地存储
原文地址:http://blog.csdn.net/dingkun520wy/article/details/49386507 (一)简单数据存储PlayerPrefs 这种存储方法比较简单直接上代码 ...
- 【BZOJ 1911】 [Apio2010]特别行动队
Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9 HINT 转移方程 f[i]=max(f[j]+ ...
- 正则表达式通过Unicode属性匹配
原文链接:http://zochen.iteye.com/blog/690716 Unicode 编码并不只是为某个字符简单定义了一个编码,而且还将其进行了归类. \pP 其中的小写 p 是 prop ...
- python 新时代
今天看到有关python的文章,感觉很好奇,学了python很久了,但是还没有真正的用过,只是写一些小程序 看了这篇文章以后真的感觉自己所了解都是皮毛,在此与大家分享: 原文链接:http://www ...