makeBackronym
2015-06-30 20:27
615 查看
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"
[/code]
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(0,str.Length-1); } 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); } }
相关文章推荐
- 数据库复习9——关系代数和关系演算
- 如何在VIM中保存编辑的只读文件
- REST与SOAP之比较——SOAP篇
- matlab注意事项
- Packet Tracer 6.2新特性
- CSS float属性小解——”浮“生若水
- android模拟器网络设置(局域网)
- Xcode真机调试报错:The application could not be verified.
- 背景音乐功能
- leetcode--217&219&220 Contains Duplicate(重复)I&II&III
- 设计在单链表中删除值相同的多余结点的算法
- 《C++ 并发编程》- 第1章 你好,C++的并发世界
- Log4Net指南
- iOS and OS X Network Programming Cookbook |Performing a network address resolution
- Sobel 边缘检测
- 并行与并发的区别,一瞬间就能理解并记住
- 堆性质 堆排序
- Android中Application类的详细解释:
- Oracle基本数据类型
- java程序并发遇到的问题