ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 16. 2016년
    코딩 테스트/Level 1 2019. 10. 14. 23:15
    반응형

    https://programmers.co.kr/learn/courses/30/lessons/12901

     

    파이썬

    def solution(a, b):
        month = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
        day = ('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT')
        return day[(sum(month[0:a - 1]) + b + 4) % 7]

    자바스크립트

    function solution(a, b) {
        const month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        const day = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
        return day[((a==1 ? 0 : month.slice(0, a-1).reduce((x,y)=>x+y))+ b + 4)%7];
    }

    자바

    class Solution {
        public String solution(int a, int b) {
            String[] day = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
            short[] month = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            int sum = b;
            for (short i = 0; i < a; i++) { sum += month[i]; }
            return day[(sum + 4) % 7];
        }
    }

    func solution(a int, b int) string {
    	month := []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    	day := []string{"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}
    	date := 0
    	for i := 0; i < a-1; i++ {
    		date += month[i]
    	}
    	date += b
    	return day[(date+4)%7]
    }

    코틀린

    class Solution {
        fun solution(a: Int, b: Int): String {
            val day = arrayOf("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT")
            val month = intArrayOf(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
            return if (a == 1) day[(b + 4) % 7] else day[(month.slice(0 until a - 1).reduce { x, y -> x + y } + b + 4) % 7]
        }
    }

    C#

    슬라이스가 있으면 쉽게 풀 수 있겠지만..

    using System.Linq;
    
    public class Solution
    {
        public string solution(int a, int b)
        {
            int[] month = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            string[] week = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
            return week[(month[0..(a-1)].Sum() + (b + 4)) % 7];
        }
    }

    이번에는 ArraySegment를 사용해 보자...

    using System;
    using System.Linq;
    
    public class Solution
    {
        public string solution(int a, int b)
        {
            int[] month = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            string[] week = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
            return week[(new ArraySegment<int>(month, 0, a-1).Sum() + (b + 4)) % 7];
        }
    }
    반응형
Designed by Tistory.