-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1eea7bf
commit ecefc7e
Showing
4 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
ktstrings/src/main/kotlin/com/kotlinground/strings/spreadsheetencode/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Spreadsheet Encoding | ||
|
||
We will be considering how to solve the problem of implementing a function that converts a spreadsheet column ID | ||
(i.e., “A”, “B”, “C”, …, “Z”, “AA”, etc.) to the corresponding integer. For example, “A” equals 1 because it represents | ||
the first column, while “AA” equals 27 because it represents the 27th column. |
28 changes: 28 additions & 0 deletions
28
ktstrings/src/main/kotlin/com/kotlinground/strings/spreadsheetencode/spreadsheetEncode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.kotlinground.strings.spreadsheetencode | ||
import kotlin.math.pow | ||
|
||
/** | ||
* This encodes a Spreadsheet column ID as an integer and returns it. | ||
* Here the encoding uses the alphabets A, B, C, etc. and | ||
* further uses the indexing of the alphabet starting from 1 like: A=1, B=2, C=3,..., Z=26 | ||
* Complexity | ||
* Where n represents the number of characters in the column_id | ||
* | ||
* - Time O(n) as we iterate over each character in the column ID to calculate the encoding | ||
* - Space O(1) no extra space is required to perform the encoding | ||
* @param columnID [String] column ID to encode, e.g. A or ZZ | ||
* @return [Int] the encoded column ID | ||
*/ | ||
fun spreadsheetEncodeColumn(columnID: String): Int { | ||
var num = 0 | ||
var count = columnID.length - 1 | ||
|
||
for (char in columnID) { | ||
val diff = char.code - 'A'.code + 1 | ||
val base = 26.0.pow(count).toInt() | ||
num += base * diff | ||
count-- | ||
} | ||
|
||
return num | ||
} |
15 changes: 15 additions & 0 deletions
15
...rings/src/test/kotlin/com/kotlinground/strings/spreadsheetencode/SpreadsheetEncodeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.kotlinground.strings.spreadsheetencode | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class SpreadsheetEncodeTest { | ||
|
||
@Test | ||
fun `should return 702 for a column ID of ZZ`() { | ||
val columnID = "ZZ" | ||
val expected = 702 | ||
val actual = spreadsheetEncodeColumn(columnID) | ||
assertEquals(expected, actual) | ||
} | ||
} |