C# 암호 해독 - 프로그래머스 [Lv.0] 코딩 테스트 입문

출처: 프로그래머스 코딩 테스트 연습
https://school.programmers.co.kr/learn/courses/30/lessons/120892

 

 

나의 풀이

using System;
using System.Linq;
using System.Text;

public class Solution {
    public string solution(string cipher, int code) {

        int length = cipher.Length / code;
        var enumuerables = Enumerable.Range(0, length)
                                     .Select(n => cipher[(n + 1) * code - 1]);

        StringBuilder sb = new StringBuilder();
        foreach (var item in enumuerables)
            sb.Append(item);

        return sb.ToString();
    }
}

 

 

다른 사람 풀이

using System;
using System.Linq;

public class Solution {
    public string solution(string cipher, int code) {
        return string.Concat(cipher.Where((n, i) => (i + 1) % code == 0));
    }
}

- Linq의 메서드 구문에서 두 번째에 인덱스를 반환시킬수 있다.

- string.Concat 에 IEnumerable<String>을 넣으면 바로 string으로 리턴해준다.

 

 

댓글

Designed by JB FACTORY