Kotlin 126

[Kotlin] 백준 2866번 : 문자열 잘라내기 <Gold 5>

https://www.acmicpc.net/problem/2866 2866번: 문자열 잘라내기 첫 번째 줄에는 테이블의 행의 개수와 열의 개수인 R과 C가 주어진다. (2 ≤ R, C ≤ 1000) 이후 R줄에 걸쳐서 C개의 알파벳 소문자가 주어진다. 가장 처음에 주어지는 테이블에는 열을 읽어서 문자 www.acmicpc.net Code import java.io.BufferedReader import java.io.BufferedWriter import java.io.InputStreamReader import java.io.OutputStreamWriter fun main(args: Array) = with(BufferedReader(InputStreamReader(System.`in`))){ val..

Bakejoon/Gold 2023.03.11

[Kotlin] 백준 2225번 : 합분해 <Gold 5>

https://www.acmicpc.net/problem/2225 2225번: 합분해 첫째 줄에 답을 1,000,000,000으로 나눈 나머지를 출력한다. www.acmicpc.net Code import java.io.BufferedReader import java.io.BufferedWriter import java.io.InputStreamReader import java.io.OutputStreamWriter const val MOD = 1000000000 fun main(args: Array) = with(BufferedReader(InputStreamReader(System.`in`))){ val bw = BufferedWriter(OutputStreamWriter(System.out)) va..

Bakejoon/Gold 2023.02.17

[Kotlin] companion object

코틀린에는 static이 없다. 자바를 쓰던 사람들에게는 놀라운 소리일 것이다. 나 또한 그랬고. 그럼 어떻게 static을 쓸까? companion object를 활용해서 static을 사용한다. companion object의 블럭 내부에 값을 넣어준다면 해당 값들이 static처럼 사용된다. class Test { companion object{ var age = 10 val number = 20 const val num = 30 fun sayHi() = println("hi") } } 이와 같이 사용할 수 있다. 각각의 값들은 static으로 선언되고, 어디에서나 사용할 수 있다. 일종의 싱글톤이 되는 것이다. 그렇기 때문에 하나의 클래스에는 하나의 companion object만이 생성될 수 있..

[Udacity] Kotlin Bootcamp for Programmers - Functional Manipulation

1번 문제 Practice Time In this practice, you are going to write the the first part of a higher-order functions game. You will implement everything, except the higher-order functions. Let’s get started. Create a new file. Create an enum class, Directions, that has the directions NORTH, SOUTH, EAST and WEST, as well as START, and END. Create a class Game. Inside Game, declare a var, path, that is a m..

[Kotlin] 람다 lambda

람다는 익명함수 라고도 부른다. 이름은 많이 들어봤는데 람다가 뭘까? "익명함수"라는 이름이 힌트다. 이름 없이 함수 역할을 하는 형태를 "람다"라고 부른다. 람다는 화살표 표기법을 사용한다. 그럼 어떻게 쓸까? val lambdaExample: (Int, Int) -> Int = {x: Int, y: Int -> x + y} 자료형 변수명: (선언자료형) {람다식의 매개변수 -> 처리내용} 이러한 형식으로 사용한다. 이때 코틀린은 자료형을 추론할 수 있다는 성질로 인해 몇몇가지를 생략할 수 있다. val lambdaExample: (Int, Int) -> Int = {x: Int, y: Int -> x + y} // 기본형 val lambdaExample = {x: Int, y: Int -> x + y..

[Udacity] Kotlin Bootcamp for Programmers - Kotlin Essentials: Beyond the Basics

1번문제 Practice Time Let's go through an example of getting information about a book in the format of a Pair. Generally, you want information about both the title and the author, and perhaps also the year. Let’s create a basic book class, with a title, author, and year. Of course, you could get each of the properties separately. Create a method that returns both the title and the author as a Pair...

[Kotlin] 엘비스 연산자 (Elvis Operation)

코틀린에는 엘비스 연산자가 있다. null을 체크하고, 결과를 리턴해준다. var birthday = readLine()?.toIntOrNull()?:1 이렇게 ?: 를 활용해서 값을 리턴하는 방식이다. ?:의 왼쪽 값이 null일 경우 ?:의 오른쪽에 있는 값을 리턴해준다. 위의 코드를 예시로 들면 readLine()?.toIntOrNull의 값, 즉 입력받은 값이 null일 경우 1을 리턴하고 null이 아닐경우 원래 값을 리턴한다. 아래와 같은 예시가 있다. fun main(){ var nullTest: Int? = 3 if (nullTest == null){ println(0) }else{ println(nullTest+1) } } if와 else를 사용하여 입력값이 null일 경우를 체크해준다...