본문으로 건너뛰기

"kata" 태그로 연결된 63개 게시물개의 게시물이 있습니다.

모든 태그 보기

· 약 2분
karais89

Instructions

Sum all the numbers of the array (in F# and Haskell you get a list) except the highest and the lowest element (the value, not the index!).

(The highest/lowest element is respectively only one element at each edge, even if there are more than one with the same value!)

{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6

If array is empty, null or None, or if only 1 Element exists, return 0. Note:In C++ instead null an empty vector is used. In C there is no null. ;-)

-- There's no null in Haskell, therefore Maybe [Int] is used. Nothing represents null.

My Solution

using System;
using System.Linq;

public static class Kata
{
public static int Sum(int[] numbers)
{
if (numbers == null || numbers.Length == 0)
{
return 0;
}

int minVal = numbers[0];
int maxVal = numbers[0];
for (int i = 1; i < numbers.Length; i++)
{
if (numbers[i] < minVal)
{
minVal = numbers[i];
}

if (numbers[i] > maxVal)
{
maxVal = numbers[i];
}
}

int sum = 0;
bool isCheckMinVal = false;
bool isCheckMaxVal = false;
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] == minVal && !isCheckMinVal)
{
isCheckMinVal = true;
continue;
}

if (numbers[i] == maxVal && !isCheckMaxVal)
{
isCheckMaxVal = true;
continue;
}
sum += numbers[i];
}
return sum;
}
}

8kyu 문제인데, 너무 어렵게 풀었다.. 생각해보면 문제 자체가 배열의 합에서 가장 큰 수와 가장 작은 수를 빼라는 문제인데. 가장 큰 수와 가장 작은 수의 배열 자체를 구하려고 해서 문제 자체를 좀 어렵게 풀이 했다. 잘못된 방법인듯.

Best Practices

using System;
using System.Linq;

public static class Kata
{
public static int Sum(int[] numbers)
{
return numbers == null || numbers.Length < 2
? 0
: numbers.Sum() - numbers.Max() - numbers.Min();
}
}

Linq를 사용하면 한줄이면 해결된다..

· 약 2분
karais89

Instructions

Given an array of integers.

Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.

If the input array is empty or null, return an empty array.

My Solution

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
public static int[] CountPositivesSumNegatives(int[] input)
{
if (input == null || input.Length == 0)
{
return new int[] { };
}

int positiveCount = 0;
int negativeSum = 0;
for (int i = 0; i < input.Length; i++)
{
if (input[i] > 0)
{
positiveCount++;
}
else
{
negativeSum += input[i];
}
}

return new int[] { positiveCount, negativeSum };
}
}

배열에서 양수의 경우 양수가 몇개 있는지 구하고, 음수의 경우 음수값 합을 구하여 배열로 반환하라.

Best Practices

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
public static int[] CountPositivesSumNegatives(int[] input)
{
if(input == null || !input.Any())
{
return new int[] {};
}

int countPositives = input.Count(n => n > 0);
int sumNegatives = input.Where(n => n < 0).Sum();

return new int[] { countPositives, sumNegatives };
}
}

Linq를 사용하여 해결. Codwars에서 C# 문제의 경우 Best Practices의 경우 Linq를 사용하여 풀이한 정답이 채택되는 것 같다. 논리 자체는 Linq를 사용하지 않아도 충분히 해결 가능하지만 Linq 자체의 장점이 분명히 존재하기 때문에 Linq쪽으로 해결하면 Best Practices로 채택되는 것 같다.

· 약 2분
karais89

Instructions

In a factory a printer prints labels for boxes. For one kind of boxes the printer has to use colors which, for the sake of simplicity, are named with letters from a to m.

The colors used by the printer are recorded in a control string. For example a "good" control string would be aaabbbbhaijjjm meaning that the printer used three times color a, four times color b, one time color h then one time color a...

Sometimes there are problems: lack of colors, technical malfunction and a "bad" control string is produced e.g. aaaxbbbbyyhwawiwjjjwwm.

You have to write a function printer_error which given a string will output the error rate of the printer as a string representing a rational whose numerator is the number of errors and the denominator the length of the control string. Don't reduce this fraction to a simpler expression.

The string has a length greater or equal to one and contains only letters from a to z.

My Solution

using System;

public class Printer
{
public static string PrinterError(String s)
{
// your code
// a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
int totalLength = s.Length;
int wrongCharNum = 0;
for (int i = 0; i < totalLength; i++)
{
if (s[i] > 'm')
wrongCharNum++;
}
return wrongCharNum.ToString() + "/" + totalLength.ToString();
}
}

문자열 중에 m 이상의 문자는 잘못된 문자로 판단하여 더해주면 된다. 처음 문제라 간단한 문제가 나옴.

Best Practices

using System.Linq;

public class Printer
{
public static string PrinterError(string s)
{
return s.Where(c => c > 'm').Count() + "/" + s.Length;
}
}

Linq를 사용하여 해결. Linq 부분은 확실히 볼 필요성이 있는 듯.