스터디/Codewars
codewars 2일차
코딩광_
2022. 7. 14. 17:26
<8kyu> Returning Strings
※아래에 답이 있습니다.
Solution
using System;
public static class Kata
{
public static string Greet(string name)
{
throw new NotImplementedException("Greet is not implemented.");
}
}
여기서 헷갈렸던 점..
위에 throw new NotImplementedException("Greet is not implemented."); 를 지우고 문제를 푸는거 였는데..
안지우고 하다 보니 문제 통과가 안되었다..
Sample Tests
namespace Solution
{
using NUnit.Framework;
using System;
[TestFixture]
public class SolutionTest
{
[Test]
public void SampleTest()
{
Assert.That(Kata.Greet("Ryan"), Is.EqualTo("Hello, Ryan how are you doing today?"));
}
}
}
Assert.That이 뭔지 몰라서 조금 헷갈렸는데 코드를 보니까 대충 Ryan이라고 입력 했을 때 왼쪽하고 오른쪽이 같으면
통과 될거란 생각에 일단 코드를 작성해보자 라고 생각하게 됨.
제출한 답.
using System;
public static class Kata
{
public static string Greet(string name)
{
string sentense = "Hello, " + name + " how are you doing today?";
return sentense;
}
}
모범 사례를 보니 훨 씬 간단하게 만들어져 있었다.
using System;
public static class Kata
{
public static string Greet(string name)
{
return $"Hello, {name} how are you doing today?";
}
}
//=======================================================================================
using System;
public static class Kata
{
public static string Greet(string name) => $"Hello, {name} how are you doing today?";
}
//=======================================================================================
using System;
public static class Kata
{
public static string Greet(string name)
{
return !String.IsNullOrEmpty(name) ? $"Hello, {name} how are you doing today?" : "Name is empty.";
}
}
//=======================================================================================
using System;
public static class Kata
{
public static string Greet(string name)
{
return String.Format("Hello, {0} how are you doing today?", name);
}
}
//=======================================================================================
using System;
public static class Kata
{
public static string Greet(string name)
{
return "Hello, "+name+" how are you doing today?";
}
}
가장 간결했던 답은 아래에 답이 가장 간결했다.
using System;
public static class Kata
{
public static string Greet(string name) => $"Hello, {name} how are you doing today?";
// single line approach
}