[C#] 개인정보 수집 유효기간

Connected Brain·2025년 8월 19일
0

코딩 테스트

목록 보기
57/67

개인정보 수집 유효기간

문제 설명

고객의 약관 동의를 얻어서 수집된 1~n번으로 분류되는 개인정보 n개가 있습니다.
약관 종류는 여러 가지 있으며 각 약관마다 개인정보 보관 유효기간이 정해져 있습니다.
당신은 각 개인정보가 어떤 약관으로 수집됐는지 알고 있습니다.
수집된 개인정보는 유효기간 전까지만 보관 가능하며, 유효기간이 지났다면 반드시 파기해야 합니다.

예를 들어, A라는 약관의 유효기간이 12 달이고, 2021년 1월 5일에 수집된 개인정보가 A약관으로 수집되었다면
해당 개인정보는 2022년 1월 4일까지 보관 가능하며 2022년 1월 5일부터 파기해야 할 개인정보입니다.
당신은 오늘 날짜로 파기해야 할 개인정보 번호들을 구하려 합니다.

모든 달은 28일까지 있다고 가정합니다.
오늘 날짜를 의미하는 문자열 today,
약관의 유효기간을 담은 1차원 문자열 배열 terms
수집된 개인정보의 정보를 담은 1차원 문자열 배열 privacies
매개변수로 주어집니다.
이때 파기해야 할 개인정보의 번호를 오름차순으로 1차원 정수 배열에 담아 return 하도록 solution 함수를 완성해 주세요.

풀이

public class PrivacyPolicyValidator
{
    private static Dictionary<string, Term> _terms = new Dictionary<string, Term>();
    private static List<Privacy> _privacyList = new List<Privacy>();

    public static int[] Solution(string today, string[] terms, string[] privacies)
    {
        SetTerms(terms);
        SetPrivacyList(privacies);
        
        string[] currentDate = today.Split('.');
        int year = int.Parse(currentDate[0]);
        int month = int.Parse(currentDate[1]);
        int day = int.Parse(currentDate[2]);

        foreach (var privacy in _privacyList)
        {
            privacy.CheckLimit(year,month,day);
        }

        int[] answer = _privacyList.Where(p => !p.IsInLimit).Select(p => p.Index).ToArray();
        return answer;
    }

    private static void SetTerms(string[] terms)
    {
        foreach (string term in terms)
        {
            string[] termItems = term.Split(' ');
            _terms.Add(termItems[0], new Term(int.Parse(termItems[1])));
        }
    }

    private static void SetPrivacyList(string[] privacies)
    {
        int index = 0;
        foreach (string privacy in privacies)
        {
            string[] privacyDate = privacy.Split(' ')[0].Split(".");
            string termName = privacy.Split(' ')[1];
            Term term = _terms[termName];
            int year = int.Parse(privacyDate[0]);
            int month = int.Parse(privacyDate[1]);
            int day = int.Parse(privacyDate[2]);
            _privacyList.Add(new Privacy(index+1, year, month, day, term));
            index++;
        }
    }
    
}

class Term
{
    public int Limit;

    public int LimitRange
    {
        get
        {
            return Limit * Privacy.SUM_OF_MONTH - 1;
        }
    }

    public Term(int limit)
    {
        Limit = limit;
    }
}

class Privacy
{
    public const int SUM_OF_YEAR = 336;
    public const int SUM_OF_MONTH = 28;

    public int Index;
    
    public int Year;
    public int Month;
    public int Day;
    
    private Term term;
    public bool IsInLimit = true;

    public Privacy(int index, int year, int month, int day, Term term)
    {
        Index = index;
        Year = year;
        Month = month;
        Day = day;
        this.term = term;
    }
    
    public void CheckLimit(int year, int month, int day)
    {
        int deltaYear = year - Year;
        int deltaMonth = month - Month;
        int deltaDay = day - Day;
        
        int sum = deltaYear * SUM_OF_YEAR + deltaMonth * SUM_OF_MONTH + deltaDay;
        
        IsInLimit = term.LimitRange >= sum;
    }
}

Term 클래스

class Term
{
    public int Limit;

    public int LimitRange
    {
        get
        {
            return Limit * Privacy.SUM_OF_MONTH - 1;
        }
    }

    public Term(int limit)
    {
        Limit = limit;
    }
}
  • 각 약관의 유효 기간을 입력받아 생성, 전체 달의 수를 28일로 가정했을 때의 제한 일 수를 LimitRange 프로퍼티로 반환해 이후 Privacy 객체가 현재 유효 기간 안에 있는지 확인하는 데에 사용

Privacy 클래스

class Privacy
{
    public const int SUM_OF_YEAR = 336;
    public const int SUM_OF_MONTH = 28;

    public int Index;
    
    public int Year;
    public int Month;
    public int Day;
    
    private Term term;
    public bool IsInLimit = true;

    public Privacy(int index, int year, int month, int day, Term term)
    {
        Index = index;
        Year = year;
        Month = month;
        Day = day;
        this.term = term;
    }
    
    public void CheckLimit(int year, int month, int day)
    {
        int deltaYear = year - Year;
        int deltaMonth = month - Month;
        int deltaDay = day - Day;
        
        int sum = deltaYear * SUM_OF_YEAR + deltaMonth * SUM_OF_MONTH + deltaDay;
        
        IsInLimit = term.LimitRange >= sum;
    }
}

전역변수

    public const int SUM_OF_YEAR = 336;
    public const int SUM_OF_MONTH = 28;

    public int Index;
    
    public int Year;
    public int Month;
    public int Day;
    
    private Term term;
    public bool IsInLimit = true;
  • 한달의 날짜를 28일로 설정했을 때의 1달의 일 수와 1년의 일 수를 미리 선언. 이후 날짜 계산에 사용
  • 해당 개인 정보가 몇번째인지 그리고 등록된 일자를 입력하고 어떤 약관을 사용하는지 입력
  • IsInLimit을 통해 기한 내에 있는지 여부를 확인

CheckLimit()

    public void CheckLimit(int year, int month, int day)
    {
        int deltaYear = year - Year;
        int deltaMonth = month - Month;
        int deltaDay = day - Day;
        
        int sum = deltaYear * SUM_OF_YEAR + deltaMonth * SUM_OF_MONTH + deltaDay;
        
        IsInLimit = term.LimitRange >= sum;
    }
  • 현재 연, 월, 일을 입력해 개인정보가 생성될 시기의 값과 차를 구함
  • 각각의 차를 연산을 통해 일 단위로 합산
  • 이를 합산하였을 때의 값이 약관의 제한 일수보다 작을 경우 기한 내에 있는 것으로 판단

전체 로직

초기화

    private static void SetTerms(string[] terms)
    {
        foreach (string term in terms)
        {
            string[] termItems = term.Split(' ');
            _terms.Add(termItems[0], new Term(int.Parse(termItems[1])));
        }
    }

    private static void SetPrivacyList(string[] privacies)
    {
        int index = 0;
        foreach (string privacy in privacies)
        {
            string[] privacyDate = privacy.Split(' ')[0].Split(".");
            string termName = privacy.Split(' ')[1];
            Term term = _terms[termName];
            int year = int.Parse(privacyDate[0]);
            int month = int.Parse(privacyDate[1]);
            int day = int.Parse(privacyDate[2]);
            _privacyList.Add(new Privacy(index+1, year, month, day, term));
            index++;
        }
    }
  • 문자열로 입력받은 요소들을 비교하기 용이하도록 미리 선언한 클래스 형태로 바꿔 저장
  • terms["A 6", "B 12", "C 3"] 형태로 입력되므로 " "를 기준으로 분리해 이름을 key 값으로 Termvalue 값으로 하는 Dictionary를 구성해 이후 Privacy 객체 생성할 때 각각에 맞는 약관을 쉽게 찾을 수 있도록 함
  • privacies["2021.05.02 A", "2021.07.01 B", "2022.02.19 C", "2022.02.20 C"] 형태로 입력되므로
    먼저 " "를 기준으로 분리해 약관 이름에 맞는 약관 객체를 찾음
    이후 날짜 부분을 각각을 분리해 정수 값으로 변경해 연, 월, 일을 입력

현재 날짜 확인

        string[] currentDate = today.Split('.');
        int year = int.Parse(currentDate[0]);
        int month = int.Parse(currentDate[1]);
        int day = int.Parse(currentDate[2]);

        foreach (var privacy in _privacyList)
        {
            privacy.CheckLimit(year,month,day);
        }

        int[] answer = _privacyList.Where(p => !p.IsInLimit).Select(p => p.Index).ToArray();
        return answer;
  • 값을 전부 초기화 한 후 "2022.05.19" 형태로 입력되는 오늘 날짜를 분리해 모든 개인정보에 대해서 연, 월, 일을 매개변수로 전달해 기한 내에 있는지 확인
  • 이후 리스트에서 기한을 벗어난 것들의 인덱스만 가져와 반환

0개의 댓글