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(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);

   }
}
复制代码

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(292)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2014-06-30 winform中splitter的用法
2014-06-30 VS中 Winform查看窗体内控件之间的相互关系
点击右上角即可分享
微信分享提示