博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
makeBackronym
阅读量:7287 次
发布时间:2019-06-30

本文共 1851 字,大约阅读时间需要 6 分钟。

  主要考查的是字符串的处理,大小写转换,以及字符串的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(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#endregionnamespace System.Linq{// 摘要:// 提供一组用于查询实现 System.Collections.Generic.IEnumerable
的对象的 static(在 Visual// Basic 中为 Shared)方法。public static class Enumerable {public static IEnumerable
Select
(this IEnumerable
source, Func
selector); } }

 

转载地址:http://wspjm.baihongyu.com/

你可能感兴趣的文章
谁搞死了你的软件?
查看>>
Promise 对象
查看>>
Windows快速添加IP地址
查看>>
AS3.0 事件流
查看>>
“将截断字符串或二进制数据。语句已终止……”问题的解决
查看>>
红苹果IP代理软件 v6.2
查看>>
Centos5.x & Centos6.x 使用mail命令发邮件以及如何伪造发件人
查看>>
JavaScript系列:ECMAScript原始类型
查看>>
centos反编译APK包
查看>>
CSS系列:CSS中盒子的浮动与定位
查看>>
windows 用户用户组迁移
查看>>
Linux系统扩充2
查看>>
linux新手的心得
查看>>
我的友情链接
查看>>
zabbix表字段类型和value type问题
查看>>
shoususaiBti
查看>>
solr5.5.5独立部署(不使用tomcat)
查看>>
WINDOWSXP启动时直接进入系统而无需入用户名和密码
查看>>
论测试的主要责任
查看>>
关于测试团队的组织
查看>>