TIL #34

loci·2024년 6월 3일
0

TIL

목록 보기
32/103


개인정보 수집 유효기간

각 개인정보의 약관에 따라 유효기간이 다르게 주어지는데 약관의 기간을 가입한 날짜에 계산해서 유효기간의 날짜를 구한 후 현재 날짜와 유효기간의 날짜를 비교해 현재 날짜가 더 크다면 파기해야 하므로 result배열에 추가해준다.


나의코드

초기코드

class Solution {
    fun solution(today: String, terms: Array<String>, privacies: Array<String>): IntArray {
        var answer: IntArray = intArrayOf()
        var index = 1
        for (i in privacies){
            var term = 0
            for(j in terms){
                var splitedTerm = j.split(" ")
                if(i[11].toString() == splitedTerm[0]){
                    term = splitedTerm[1].toInt()
                }   
            }
            var resultDay = i.split(".").toMutableList()
            if(resultDay[1].toInt() + term > 12){
                resultDay[0] = (resultDay[0].toInt() +1).toString()
                resultDay[1] = (resultDay[1].toInt() + term - 12).toString()
            }else{
               resultDay[1] = (resultDay[1].toInt() + term).toString()
            }
            
            var splitToday = today.split(".")
            
            if(splitToday[0] > resultDay[0]){
                answer += index
            } else if(splitToday[0] == resultDay[0] && splitToday[1] > resultDay[1]{
                
                    answer += index
                    
            }else if(splitToday[0] == resultDay[0] && splitToday[1] == resultDay[1]){
                var splitResultDay = resultDay[2].split(" ")
                if(splitResultDay[0] <= splitToday[2]){
                    answer += index
                }
            }
            
            index++
        }
        return answer
    }
}

처음에 작성한 코드인데 채점할때 "정확성: 40.0 합계: 40.0 / 100.0" 뒷부분 테스트문제가 거의 틀렸다.
다른사람 질문한것을 참고해보니 월을 계산할때 resultDay[1].toInt() + term - 12 에서 24를 초과하는 수가 나오면 12보다 커지기 때문에 생기는 오류였다. 그래서 월을 계산할때 반복문으로 12이하가 될 때까지 계산하는 부분을 추가해 통과됐다.

최종

class Solution {
    fun solution(today: String, terms: Array<String>, privacies: Array<String>): IntArray {
        var answer: IntArray = intArrayOf()
        var index = 1
        for (i in privacies){
            var term = 0
            for(j in terms){
                var splitedTerm = j.split(" ")
                if(i[11].toString() == splitedTerm[0]){
                    term = splitedTerm[1].toInt()
                }   
            }
            var resultDay = i.split(".").toMutableList()
            if(resultDay[1].toInt() + term > 12){
                var temp = resultDay[1].toInt() + term
                var count = 0
                while(temp > 12){
                    temp -= 12
                    count++
                }
                resultDay[0] = (resultDay[0].toInt() + count).toString()
                resultDay[1] = temp.toString()
            } else {
               resultDay[1] = (resultDay[1].toInt() + term).toString()
            }
            
            var splitToday = today.split(".")
            
            if(splitToday[0] > resultDay[0]){
                answer += index
            } else if(splitToday[0] == resultDay[0] && splitToday[1].toInt() > resultDay[1].toInt()){
                
                    answer += index
                    
            }else if(splitToday[0] == resultDay[0] && splitToday[1].toInt() == resultDay[1].toInt()){
                var splitResultDay = resultDay[2].split(" ")
                if(splitResultDay[0].toInt() <= splitToday[2].toInt()){
                    answer += index
                }
            }
            
            index++
        }
        return answer
    }
}

다른사람의 코드

class Solution {
    fun solution(today: String, terms: Array<String>, privacies: Array<String>) = privacies.indices.filter { privacies[it].split(" ").first().split("\\.".toRegex()).map(String::toInt).let { (y, m, d) -> (y * 12 * 28) + (m * 28) + d } + (terms.map { it.split(" ") }.associate { (a, b) -> a to b.toInt() }.getOrDefault(privacies[it].split(" ").last(), 0) * 28) <= today.split("\\.".toRegex()).map(String::toInt).let { (y, m, d) -> (y * 12 * 28) + (m * 28) + d } }.map { it + 1 }
}
class Solution {
    fun solution(today: String, terms: Array<String>, privacies: Array<String>): List<Int> {
        var answer = mutableListOf<Int>()
        val map = mutableMapOf<String, Int>()
        terms.forEach {
            val (key, value) = it.split(" ")
            map[key] = value.toInt()
        }
        privacies.forEachIndexed { index, s ->
            val (date, type) = s.split(" ")
            var (it_y, it_m, it_d) = date.split(".").map { it.toInt() }
            it_m += map[type]!!
            if (it_m > 12) {
                it_y += it_m / 12
                it_m %= 12
                if (it_m % 12 == 0) {
                    it_y -= 1
                    it_m = 12
                }
            }
            if (today >= "$it_y.${if (it_m < 10) "0$it_m" else it_m}.${if (it_d < 10) "0$it_d" else it_d}")
                answer.add(index + 1)
        }
        return answer
    }
}

코틀린 기초 문법강의

1주차

프로그래밍: 프로그램을 제작하는 행위 program + ing
프로그래밍언어: 컴퓨터에 명령하기위한 언어로 목적마다 다른 언어를 사용한다.
kotlin: 안드로이드 앱개발 권장언어, 자바의 단점개선(타입추론, 간결한코드, null 예외 방지)

kotlin 이름의 유래: 러시아 코틀린섬 (자바: 인도네시아섬의 이름)

kotlin의 특징

  • JVM언어와 100%호환
  • 직관적이고 간결
  • null처리(안전한코드)

안드로이드에서 kotlin을 사용하는 이유

  • 생산성이 높음
  • 더 높은 품질 생산
  • 간결하고 안전한 비동기처리

Android studio 단축키
ctrl + Y - 줄삭제
esc - 자동 포커싱


2주차

코딩 컨벤션(coding convention)

  • 다른사람들도 이해하기 쉽게 코드를 작성하기 위한 규칙
  • 프로그래밍 언어마다 상이

필요성

  • 가독성증가
  • 유지보수 용이
  • 효율적 협업

자주사용하는 표기법

  • 카멜 표기법 (camelCase)
    주로 변수 메소드 이름 지정에 사용
    띄어쓰기대신 첫글자를 대문자로표기

  • snakecase 표기법
    주로 상수 이름 지정시 사용
    띄어쓰기를
    로 표기

  • 파스칼 표기법
    클래스 이름 지정시 사용
    카멜표기법에서 첫글자도 대문자로 표기

seoulFood   //카멜 표기법
seoul_food  //스네이크 표기법
SeoulFood   //파스칼 표기법

출력과입력
readLine()으로 입력받아오기
println()으로 출력

자료형
8bit = 1byte

정수 : Long(64bit), Int(32bit), Short(16bit), Byte(8bit)
실수 : Double(64bit), Float(32bit)
문자열: String
문지: Char (16bit)
Boolean(8bit)

0과1 두가지 상태만 가지는 Boolean의 용량은 왜 1bit가 아니라 8bit일까?
-> 컴퓨터 구조상 최소 byte단위로 데이터나 주소에 접근하기 때문에 1byte인 8bit가 Boolean의 크기이다.

${ } 로 문자열에 변수를 넣어줄수있다.

연산자

산술연산자: +, -, *, /, %

복합연산자: +=, -=, *=, /=

증감연산자: ++, --

전위연산자: 다른연산과 함께 사용할때 println(num1++)이면 println이 실행된후 전위연산 num1++가 실행됨 (println() -> num1++ 순서로 실행)
후위연산자: 다른연산과 함께 사용할때 println(++num1)이면 ++num1이 실행된 후 println()이 실행됨 (++num1 -> println() 순서로 실행)
전위연산자와 후위연산자는 단독적으로 사용시 똑같은 동작

비교연산자: >, <, <=, >=, == ,!=

조건식 if, when

반복문

for, while
brake는 가장 가까운 반복문에서 탈출
continue는 이후 코드를 실행하지않음

profile
편리한 개발자

0개의 댓글