Skip to content

Instantly share code, notes, and snippets.

@piotrek1543
Created July 31, 2019 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrek1543/c3e714181d7ec909ee0fd18ef9531107 to your computer and use it in GitHub Desktop.
Save piotrek1543/c3e714181d7ec909ee0fd18ef9531107 to your computer and use it in GitHub Desktop.
Solution to ASCII art digital clock - write a function: String asciiClock(int hours, int minutes) which returns an ASCII art rendering of a digital clock.
/*
ASCII art digital clock:
write a function:
String asciiClock(int hours, int minutes)
which returns an ASCII art rendering of a digital clock.
Time is expressed between 00:00 to 23:59 (with only hours and minutes).
Output (see example):
Each digit is represented by 3x4 matrix of the characters ␣ (space), | (pipe), _ (underscore) and . (period).
There are two spaces between two digits.
List of digits:
__ __ __ __ __ __ __ __
| | | __| __| |__| |__ |__ | |__| |__|
|__| | |__ __| | __| |__| | |__| __|
Example output: (for 21:08)
__ __ __
__| | . | | |__|
|__ | . |__| |__|
*/
val isDebug = true //change to true if you want to see
fun asciiClock(hours: Int, minutes: Int): String {
val zero = arrayOf(" __ ", "| |", "|__|")
val one = arrayOf(" ", " |", " |")
val two = arrayOf(" __ ", " __|", "|__ ")
val three = arrayOf(" __ ", " __|", " __|")
val four = arrayOf(" ", "|__|", " |")
val five = arrayOf(" __ ", "|__ ", " __|")
val six = arrayOf(" __ ", "|__ ", "|__|")
val seven = arrayOf(" __", " |", " |")
val eight = arrayOf(" __ ", "|__|", "|__|")
val nine = arrayOf(" __ ", "|__|", " __|")
val divider = arrayOf(" ", " : ", " ")
if (isDebug) {
for (i in zero) println(i)
println()
for (i in one) println(i)
println()
for (i in two) println(i)
println()
for (i in three) println(i)
println()
for (i in four) println(i)
println()
for (i in five) println(i)
println()
for (i in six) println(i)
println()
for (i in seven) println(i)
println()
for (i in eight) println(i)
println()
for (i in nine) println(i)
println()
for (i in divider) println(i)
}
val digits = arrayOf(hours / 10, hours % 10, -1, minutes / 10, minutes % 10)
val stringBuilder = StringBuilder()
for (d in 0..2) {
for (digit in digits) {
val number = when (digit) {
0 -> zero[d]
1 -> one[d]
2 -> two[d]
3 -> three[d]
4 -> four[d]
5 -> five[d]
6 -> six[d]
7 -> seven[d]
8 -> eight[d]
9 -> nine[d]
else -> divider[d]
}
stringBuilder.append(number)
}
stringBuilder.append("\n")
}
return stringBuilder.toString()
}
fun main(args: Array<String>) {
val sb = asciiClock(12, 53)
val rows = sb.split("\n")
for (row in rows) println(row)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment