Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions 20230725-둘만의암호/src/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Solution {
fun solution(s: String, skip: String, index: Int): String {
val asciiCodes = toAsciiCodes(s)
val toSkip = toSet(skip)
val added = addIndex(asciiCodes, toSkip, index)

return added.map { it.toChar() }.fold("") { acc: String, c: Char -> acc + c }
}

fun addIndex(asciiCodes: List<Int>, toSkip: Set<Int>, index: Int): List<Int> {
return asciiCodes.map {
val toSkips = calculateToSkips(it, toSkip)

it + index + toSkips
}.map {
if (it / 'z'.code >= 1) {
'a'.code + it % 'z'.code
} else it
}
}

fun calculateToSkips(asciiCode: Int, toSkip: Set<Int>): Int {
return toSkip.fold(mutableMapOf("asciiCode" to asciiCode, "toSkip" to 0)) { acc, i ->
println(acc["asciiCode"])
println(i)
if (acc["asciiCode"] == i) {
acc["asciiCode"] = acc["asciiCode"]!! + 1
acc["toSkip"] = acc["toSkip"]!! + 1
}

if (acc["asciiCode"]!! > 'z'.code) {
acc["asciiCode"] = 'a'.code + acc["asciiCode"]!! % 'z'.code
}

acc
}["toSkip"]!!
}

fun toSet(skip: String): Set<Int> {
return skip.toCharArray().map { it.code }.toSet()
}

fun toAsciiCodes(s: String): List<Int> {
return s.toCharArray().map { it.code }
}
}
82 changes: 82 additions & 0 deletions 20230725-둘만의암호/src/SolutionTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class SolutionTest {

@Test
fun solution() {
val solution = Solution()
val result = solution.solution("aukks", "wbqd", 5)

assertEquals("happy", result)
}

@Test
fun solution2() {
val solution = Solution()
val result = solution.solution("a", "wbqd", 5)

assertEquals("h", result)
}


@Test
fun toAsciiCodes() {
val solution = Solution()
val asciiCodes = solution.toAsciiCodes("ab")

assertEquals(listOf(97, 98), asciiCodes)
}

@Test
fun toSet() {
val solution = Solution()
val set = solution.toSet("ab")

assertEquals(setOf(97, 98), set)
}

@Test
fun addIndex() {
val solution = Solution()
val added = solution.addIndex(
asciiCodes = listOf(97),
toSkip = setOf(99),
index = 1
)

assertEquals(listOf(98), added)
}

@Test
fun addIndexWhenHaveToSkip() {
val solution = Solution()
val added = solution.addIndex(
asciiCodes = listOf(97),
toSkip = setOf(98),
index = 1
)

assertEquals(listOf(99), added)
}

@Test
fun addIndexWhenHavOutOfRange() {
val solution = Solution()
val added = solution.addIndex(
asciiCodes = listOf(122),
toSkip = setOf(97),
index = 1
)

assertEquals(listOf(98), added)
}

@Test
fun calculateToSkips() {
val solution = Solution()
val toSkips = solution.calculateToSkips(122, setOf(122))

assertEquals(1, toSkips)
}
}
53 changes: 53 additions & 0 deletions 20230725-둘만의암호/💡-how-to-solve-it-.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
### 1. 이해
> 아래의 질문에 대한 답변이 녹아 있어야 한다.

- [ ] 모르는 것은 무엇인가?
- [ ] 주어진 것은 무엇인가?
- [ ] 자료는 무엇인가?
- [ ] 조건은 무엇인가?
- [ ] 조건은 알아내야 하는 것을 찾는데 충분한가?

### 2. 계획
> A) 계획을 세우기 전에 아래와 같은 질문을 던지면 굉장히 유용하다.

- [ ] 관련된 문제를 알고 있는가?
- [ ] 모르는 부분이 유사한 다른 문제를 풀어 본 적이 있는가?
- [ ] 전에 풀어 본 문제를 활용하려면 어떤 보조 요소를 도입해야 되는가?
- [ ] 도움이 될 것 같은 어떤 사실이나 정리를 알고 있는가? (보통은 자료 구조나 수학적 공리 또는 정의가 해당됨)

> B) 계획을 세운 뒤 아래 질문을 통해 한번 더 점검해보자.

- [ ] 자료와 조건은 모두 활용 했는가? (Edge case가 될 조건을 꼭 확인해야 함)

### 3. 실행
> 코딩을 하는 단계! 무조건 Test case부터 먼저 작성해야 한다.
💡 TDD를 같이 하는 이유: Test code를 통해 각 단계가 올바르게 동작하는 것을 증명할 수 있다.

- [ ] 각 단계가 올바른지 명확히 알 수 있는가?
- [ ] 그것이 옳다는 것을 설명할 수 있는가?

### 4. 반성
> 문제를 다 풀고 나서 회고하는 시간을 갖는다. 반성한 부분을 다음 문제 풀이에 반영한다.

- [ ] 다른 방법으로 해결할 수는 없었는가? (이때 다른 사람의 풀이도 참고하면 좋음)
- [ ] 결과나 방법을 다른 문제에 활용할 수 있는가? (유용한 패턴은 따로 정리해두자)

## [둘만의 암호](https://school.programmers.co.kr/learn/courses/30/lessons/155652)
문자열 s와 skip 그리고 자연수 index가 주어질 때 규칙에 따라 s를 변환해 결과를 반환해라

### 1. 이해
- s에 포함된 문자는 index 만큼 뒤의 알파벳으로 바꿔준다. s = "a", index = 1, 결과 = "b"
- skip 에 포함된 문자열은 건너 뛴다. s = "a", index = 1, skip = "b" 결과 = "c'
- index 만큼 뒤의 알파벳이 z를 넘어갈 경우 다시 a로 돌아간다

### 2. 계획
- s 문자열을 ascii code들로 변경한다.
- 변경된 code를 index 만큼 code에 수를 더한다
- index가 추가된 code가 z의 code 보다 클 경우 % zcode의 값으로 변경한다
- code들을 문자열배열로 변환한다

### 3. 실행
-

### 4. 반성
-