From 36a7142950d9ef8d8abbbb0c5213b8f04eeb95c6 Mon Sep 17 00:00:00 2001 From: Joseph Date: Thu, 24 Oct 2024 02:18:24 +0000 Subject: [PATCH 1/2] Feat: MMakes changes to Lesson12.java, Stack.java, lesson12.ts, and stack.ts with passing tests for lesson_12 homework and extra credit - Joseph Caballero --- .../codedifferently/lesson12/Lesson12.java | 29 +++++++++++++++-- .../com/codedifferently/lesson12/Stack.java | 12 ++++--- lesson_12/structs_ts/src/lesson12.ts | 32 ++++++++++++++++++- lesson_12/structs_ts/src/stack.ts | 16 +++++++--- 4 files changed, 77 insertions(+), 12 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 af7663e9..7e12886c 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 @@ -2,11 +2,34 @@ public class Lesson12 { + public 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; - } -} + ListNode current = head; + int oddPoints = 0; + int evenPoints = 0; + while (current != null){ + if (current.val > current.next.val){ + evenPoints++; + } + else{ + oddPoints++; + } + current = current.next.next; + } + if ( evenPoints > oddPoints) { + return "Even"; + } else if ( evenPoints current.next.val) { + evenPoints++; + break; + } else { + oddPoints++; + break; + } + } else { + if (current.val > current.next.val) { + evenPoints++; + current = current.next.next; + } else { + oddPoints++; + current = current.next.next; + } + } + } + + if (oddPoints > evenPoints) { + return 'Odd'; + } else if (oddPoints < evenPoints) { + return 'Even'; + } else { + return 'Tie'; + } } } diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index b931ec0e..0971b040 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -8,18 +8,26 @@ export class Stack { } push(value: number): void { - throw new Error('Not implemented'); + const pusher = new ListNode(value); + pusher.next = this.top; + this.top = pusher; } pop(): number | undefined { - throw new Error('Not implemented'); + const val = this.top?.val; + this.top = this.top?.next; + return val; } peek(): number | null { - throw new Error('Not implemented'); + if (this.top != null) { + return this.top.val; + } else { + throw new Error('empty stack'); + } } isEmpty(): boolean { - throw new Error('Not implemented'); + return this.top == null; } } From 40ac10fde6c0c4de885c7f43a90d3064ef0d44b6 Mon Sep 17 00:00:00 2001 From: Joseph Date: Thu, 24 Oct 2024 02:24:28 +0000 Subject: [PATCH 2/2] fix: Fixes build run using spotless apply --- .../codedifferently/lesson12/Lesson12.java | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 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 7e12886c..60872114 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 @@ -2,34 +2,28 @@ public class Lesson12 { - public Lesson12() { - - } + public 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) { - ListNode current = head; - int oddPoints = 0; - int evenPoints = 0; - while (current != null){ - if (current.val > current.next.val){ - evenPoints++; - } - else{ - oddPoints++; - } - current = current.next.next; - } - if ( evenPoints > oddPoints) { - return "Even"; - } else if ( evenPoints current.next.val) { + evenPoints++; + } else { + oddPoints++; } - else return "Tie"; - -} - - } + current = current.next.next; + } + if (evenPoints > oddPoints) { + return "Even"; + } else if (evenPoints < oddPoints) { + return "Odd"; + } else return "Tie"; + } +}