Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kata/parse-a-linked-list-from-a-string #680

Merged
merged 4 commits into from
Oct 13, 2024
Merged
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
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ slug.

| [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:|
| 0 | 1 | 2 | 26 | 48 | 431 | 591 | 219 | 56 | 79 |
| 0 | 1 | 2 | 26 | 48 | 432 | 591 | 219 | 56 | 79 |

**Note:** The source code is written in Java 17 and may use language features that are incompatible
with Java 8, 11.
Expand Down
1 change: 1 addition & 0 deletions kata/6-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
- [Page replacement algorithms: LRU](page-replacement-algorithms-lru)
- [Pair of gloves](pair-of-gloves)
- [Parabolic Arc Length](parabolic-arc-length)
- [Parse a linked list from a string](parse-a-linked-list-from-a-string)
- [Parse bank account number](parse-bank-account-number)
- [Parse HTML/CSS Colors](parse-html-slash-css-colors)
- [Pascal's Triangle](pascals-triangle)
Expand Down
43 changes: 43 additions & 0 deletions kata/6-kyu/parse-a-linked-list-from-a-string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# [Parse a linked list from a string](https://www.codewars.com/kata/parse-a-linked-list-from-a-string "https://www.codewars.com/kata/582c5382f000e535100001a7")

## Task

Create a function `parse` which accepts exactly one argument `string` / `$string` / `s` / `strrep` ( or similar, depending on the language )
which is a string representation of a linked list. Your function must return the corresponding linked list, constructed from instances of
the `Node` class/struct/type. The string representation of a list has the following format: the value of the node, followed by a whitespace,
an arrow and another whitespace (`" -> "`), followed by the rest of the linked list. Each string representation of a linked list will end in
`"null"` / `"NULL"` / `"nil"` / `"nullptr"` / `"null()"` depending on the language you are undertaking this Kata in. For example, given the
following string representation of a linked list:

```
new Node(1, new Node(2, new Node(3)))
```

Note that due to the way the constructor for `Node` is defined, if a second argument is not provided, the `next` / `$next` / `Next` field is
automatically set to `null` / `NULL` / `nil` / `nullptr` ( or equivalent in your language ). That means your function could also return the
following ( if it helps you better visualise what is actually going on ):

```
new Node(1, new Node(2, new Node(3, null)))
```

Another example: given the following string input:

```
"0 -> 1 -> 4 -> 9 -> 16 -> null"
```

... your function should return:

```
new Node(0, new Node(1, new Node(4, new Node(9, new Node(16)))))
```

If the input string is just `"null"` / `"NULL"` / `"nil"` / `"nullptr"` / `"null()"`, return `null` / `NULL` / `nil` / `nullptr` /
`null()` / `[]` ( or equivalent ).

For the simplicity of this Kata, the values of the nodes in the string representation will always ever be **non-negative integers**, so the
following would **not** occur: `"Hello World -> Goodbye World -> 123 -> null"` / `"Hello World -> Goodbye World -> 123 -> NULL"` /
`"Hello World -> Goodbye World -> 123 -> nil"` / `"Hello World -> Goodbye World -> 123 -> nullptr"` ( depending on the language ). This also
means that the values of each `Node` must also be **non-negative integers** so keep that in mind when you are parsing the list from the
string.
10 changes: 10 additions & 0 deletions kata/6-kyu/parse-a-linked-list-from-a-string/main/Kata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
interface Kata {
static Node parseLinkedList(String str) {
Node head = null;
String[] values = str.split(" -> ");
for (int i = values.length - 2; i >= 0; i--) {
head = new Node(Integer.parseInt(values[i]), head);
}
return head;
}
}
9 changes: 9 additions & 0 deletions kata/6-kyu/parse-a-linked-list-from-a-string/main/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;

@AllArgsConstructor
@RequiredArgsConstructor
class Node {
final int data;
Node next;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void sample() {
var n1 = Kata.parseLinkedList("1 -> null");
assertEquals(1, n1.data);
assertNull(n1.next);

var n2 = Kata.parseLinkedList("1 -> 2 -> 3 -> null");
assertEquals(1, n2.data);
assertEquals(2, n2.next.data);
assertEquals(3, n2.next.next.data);
assertNull(n2.next.next.next);

assertNull(Kata.parseLinkedList("null"));
}
}
Loading