From d7025aa33261cc3bf9794835fe8d06a35c2fffc1 Mon Sep 17 00:00:00 2001 From: dxsmith244 Date: Wed, 23 Oct 2024 17:47:33 +0000 Subject: [PATCH 1/2] feat: adds David's Stack and Lesson for Lesson 12 --- .../codedifferently/lesson12/Lesson12.java | 31 +++++++++++++--- .../com/codedifferently/lesson12/Stack.java | 35 ++++++++++++------- .../lesson12/Lesson12Test.java | 1 - .../codedifferently/lesson12/StackTest.java | 1 - 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java index af7663e90..781958c62 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java @@ -6,7 +6,30 @@ public class Lesson12 { * Provide the solution to LeetCode 3062 here: * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game */ - public String gameResult(ListNode head) { - return null; - } -} + public String gameResult(ListNode head) { + int oddPoints = 0; + int evenPoints = 0; + ListNode current = head; + + while (current != null && current.next != null) { + int evenValue = current.val; + int oddValue = current.next.val; + + if (evenValue > oddValue) { + evenPoints++; + } else if (oddValue > evenValue) { + oddPoints++; + } + + current = current.next.next; + } + + if (evenPoints > oddPoints) { + return "Even"; + } else if (oddPoints > evenPoints) { + return "Odd"; + } else { + return "Tie"; + } + } + } \ No newline at end of file diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java index 8444fceca..929b3d754 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java @@ -9,18 +9,29 @@ public Stack() { } public void push(int value) { - // Your code here - } + ListNode node = new ListNode(value); + node.next = top; + top = node; + } - public int pop() { - return 0; - } + public int pop() { + if (isEmpty()){ + throw new IllegalStateException("Stack has no values"); + } + int valToPop = top.val; + top = top.next; + return valToPop; + } - public int peek() { - return 0; - } + public int peek() { + if (isEmpty()){ + throw new IllegalStateException("Stack has no values"); + } - public boolean isEmpty() { - return true; - } -} + return top.val; + } + + public boolean isEmpty() { + return top == null; + } + } diff --git a/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/Lesson12Test.java b/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/Lesson12Test.java index e6660f419..9f37e3f27 100644 --- a/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/Lesson12Test.java +++ b/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/Lesson12Test.java @@ -1,7 +1,6 @@ package com.codedifferently.lesson12; import static org.junit.jupiter.api.Assertions.assertEquals; - import org.junit.jupiter.api.Test; class Lesson12Test { diff --git a/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/StackTest.java b/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/StackTest.java index 4307b6883..582da05b1 100644 --- a/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/StackTest.java +++ b/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/StackTest.java @@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; - import org.junit.jupiter.api.Test; class StackTest { From edea678c95e38031cbdb83fec5b94e7b2057eaeb Mon Sep 17 00:00:00 2001 From: Dsmith07 Date: Thu, 24 Oct 2024 15:47:49 +0000 Subject: [PATCH 2/2] fix: ran spotlessApply --- .../codedifferently/lesson12/Lesson12.java | 50 +++++++++---------- .../com/codedifferently/lesson12/Stack.java | 44 ++++++++-------- .../lesson12/Lesson12Test.java | 1 + .../codedifferently/lesson12/StackTest.java | 1 + 4 files changed, 49 insertions(+), 47 deletions(-) diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java index 781958c62..2446849a1 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java @@ -6,30 +6,30 @@ public class Lesson12 { * Provide the solution to LeetCode 3062 here: * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game */ - public String gameResult(ListNode head) { - int oddPoints = 0; - int evenPoints = 0; - ListNode current = head; - - while (current != null && current.next != null) { - int evenValue = current.val; - int oddValue = current.next.val; - - if (evenValue > oddValue) { - evenPoints++; - } else if (oddValue > evenValue) { - oddPoints++; - } + public String gameResult(ListNode head) { + int oddPoints = 0; + int evenPoints = 0; + ListNode current = head; - current = current.next.next; - } - - if (evenPoints > oddPoints) { - return "Even"; - } else if (oddPoints > evenPoints) { - return "Odd"; - } else { - return "Tie"; - } + while (current != null && current.next != null) { + int evenValue = current.val; + int oddValue = current.next.val; + + if (evenValue > oddValue) { + evenPoints++; + } else if (oddValue > evenValue) { + oddPoints++; + } + + current = current.next.next; + } + + if (evenPoints > oddPoints) { + return "Even"; + } else if (oddPoints > evenPoints) { + return "Odd"; + } else { + return "Tie"; } - } \ No newline at end of file + } +} diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java index 929b3d754..c4ef134e5 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java @@ -9,29 +9,29 @@ public Stack() { } public void push(int value) { - ListNode node = new ListNode(value); - node.next = top; - top = node; - } + ListNode node = new ListNode(value); + node.next = top; + top = node; + } - public int pop() { - if (isEmpty()){ - throw new IllegalStateException("Stack has no values"); - } - int valToPop = top.val; - top = top.next; - return valToPop; - } + public int pop() { + if (isEmpty()) { + throw new IllegalStateException("Stack has no values"); + } + int valToPop = top.val; + top = top.next; + return valToPop; + } - public int peek() { - if (isEmpty()){ - throw new IllegalStateException("Stack has no values"); - } + public int peek() { + if (isEmpty()) { + throw new IllegalStateException("Stack has no values"); + } - return top.val; - } + return top.val; + } - public boolean isEmpty() { - return top == null; - } - } + public boolean isEmpty() { + return top == null; + } +} diff --git a/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/Lesson12Test.java b/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/Lesson12Test.java index 9f37e3f27..e6660f419 100644 --- a/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/Lesson12Test.java +++ b/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/Lesson12Test.java @@ -1,6 +1,7 @@ package com.codedifferently.lesson12; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; class Lesson12Test { diff --git a/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/StackTest.java b/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/StackTest.java index 582da05b1..4307b6883 100644 --- a/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/StackTest.java +++ b/lesson_12/structs_java/structs_app/src/test/java/com/codedifferently/lesson12/StackTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; + import org.junit.jupiter.api.Test; class StackTest {