diff --git a/Programming Basics/Repetition with while/Exercise 1/src/Task.kt b/Programming Basics/Repetition with while/Exercise 1/src/Task.kt new file mode 100644 index 00000000..5abda7cb --- /dev/null +++ b/Programming Basics/Repetition with while/Exercise 1/src/Task.kt @@ -0,0 +1,18 @@ +package repetitionwithwhile1 + +fun displayContent(s: String) { + var i = 0 + while (i < s.length) { + println(s[i]) + i++ + } +} + +fun main() { + displayContent("abc") +} +/* Expected output: +a +b +c +*/ \ No newline at end of file diff --git a/Programming Basics/Repetition with while/Exercise 1/task-info.yaml b/Programming Basics/Repetition with while/Exercise 1/task-info.yaml new file mode 100644 index 00000000..7ad1b827 --- /dev/null +++ b/Programming Basics/Repetition with while/Exercise 1/task-info.yaml @@ -0,0 +1,6 @@ +type: edu +files: +- name: src/Task.kt + visible: true +- name: test/Tests.kt + visible: false diff --git a/Programming Basics/Repetition with while/Exercise 1/task.md b/Programming Basics/Repetition with while/Exercise 1/task.md new file mode 100644 index 00000000..7a99a433 --- /dev/null +++ b/Programming Basics/Repetition with while/Exercise 1/task.md @@ -0,0 +1,4 @@ +## Repetition with `while` (#2) + +Using a `while` loop, write a function that takes a `String` argument and +displays each character on its own line. \ No newline at end of file diff --git a/Programming Basics/Repetition with while/Exercise 1/test/Tests.kt b/Programming Basics/Repetition with while/Exercise 1/test/Tests.kt new file mode 100644 index 00000000..f717982c --- /dev/null +++ b/Programming Basics/Repetition with while/Exercise 1/test/Tests.kt @@ -0,0 +1,23 @@ +package repetitionwithwhile1 + +import org.junit.Test +import util.checkSystemOutput +import util.runAndGetSystemOutput + +class TestDisplayContent { + private fun testInput(s: String) { + val output = runAndGetSystemOutput { + displayContent(s) + } + val expected = s.toList().joinToString("\n") + checkSystemOutput("""Wrong output for 'displayContent("$s")'""", + expected, output) + } + + @Test + fun testDisplayContent() { + testInput("abc") + testInput("Kotlin") + testInput("Hi! I'm a String") + } +} \ No newline at end of file diff --git a/Programming Basics/Repetition with while/lesson-info.yaml b/Programming Basics/Repetition with while/lesson-info.yaml index 864651d4..536bd6af 100644 --- a/Programming Basics/Repetition with while/lesson-info.yaml +++ b/Programming Basics/Repetition with while/lesson-info.yaml @@ -1,5 +1,6 @@ content: - Examples +- Exercise 1 - Exercise 2 - Exercise 3 - Exercise 4