From 2e5eec79fa3cd2f0cd37a6512c1959a2bf2521f9 Mon Sep 17 00:00:00 2001 From: txtran224 Date: Wed, 23 Oct 2024 13:06:12 +0000 Subject: [PATCH 1/6] lfeat: lesson_12 and stack solutions in java --- .../com/codedifferently/lesson12/Lesson12.java | 18 +++++++++++++++++- .../com/codedifferently/lesson12/Stack.java | 12 +++++++++--- 2 files changed, 26 insertions(+), 4 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..99db84127 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 @@ -7,6 +7,22 @@ public class Lesson12 { * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game */ public String gameResult(ListNode head) { - return null; + int even = 0; + int odd = 0; + while (head != null && head.next != null) { + if (head.val > head.next.val) { + even++; + } else { + odd++; + } + head = head.next.next; + } + if (even == odd) { + return ("Tie"); + } else if (even > odd) { + return ("Even"); + } else { + return ("Odd"); + } } } 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..33ac07d8c 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 @@ -10,17 +10,23 @@ public Stack() { public void push(int value) { // Your code here + ListNode top2 = new ListNode(value); + top2.next = top; + top = top2; } public int pop() { - return 0; + int topvalue = top.val; + top = top.next; + + return topvalue; } public int peek() { - return 0; + return top.val; } public boolean isEmpty() { - return true; + return top == null } } From 9ac981a34d517ee797762f946038457be7ca38f0 Mon Sep 17 00:00:00 2001 From: txtran224 Date: Wed, 23 Oct 2024 13:08:49 +0000 Subject: [PATCH 2/6] fix:Lesson_12 bug in stack.java --- .../src/main/java/com/codedifferently/lesson12/Stack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 33ac07d8c..e4032d46c 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 @@ -27,6 +27,6 @@ public int peek() { } public boolean isEmpty() { - return top == null + return top ==null; } } From 2ef313865c86c85f25ff728550856b50360983cc Mon Sep 17 00:00:00 2001 From: txtran224 Date: Wed, 23 Oct 2024 13:25:55 +0000 Subject: [PATCH 3/6] fix: stack.java file --- .../src/main/java/com/codedifferently/lesson12/Stack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e4032d46c..718468b79 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 @@ -27,6 +27,6 @@ public int peek() { } public boolean isEmpty() { - return top ==null; + return top == null; } } From 62db949ad92cfdb721d37153e5e4d4d16929dfeb Mon Sep 17 00:00:00 2001 From: txtran224 Date: Wed, 23 Oct 2024 14:20:11 +0000 Subject: [PATCH 4/6] feat: add lesson_12 extra credit --- lesson_12/structs_ts/src/lesson12.ts | 18 +++++++++++++++++- lesson_12/structs_ts/src/stack.ts | 15 +++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index d4455564e..ec0f96b48 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -6,6 +6,22 @@ export class Lesson12 { * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game */ public gameResult(head: ListNode | null): string { - return ''; + let even = 0; + let odd = 0; + while (head != null && head.next != null) { + if (head.val > head.next.val) { + even++; + } else { + odd++; + } + head = head.next.next !== undefined ? head.next.next : null; + } + if (even === odd) { + return 'Tie'; + } else if (even > odd) { + return 'Even'; + } else { + return 'Odd'; + } } } diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index b931ec0ef..2a1d607eb 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -8,18 +8,25 @@ export class Stack { } push(value: number): void { - throw new Error('Not implemented'); + const newnode = new ListNode(value); + newnode.next = this.top; + this.top = newnode; } pop(): number | undefined { - throw new Error('Not implemented'); + const nodevalue = this.top?.val; + this.top = this.top?.next; + return nodevalue; } peek(): number | null { - throw new Error('Not implemented'); + if (this.top !== null && this.top !== undefined) { + return this.top.val; + } + return null; } isEmpty(): boolean { - throw new Error('Not implemented'); + return this.top == null; } } From e658f2e938a13e5758acb943a1ff149493bc146c Mon Sep 17 00:00:00 2001 From: txtran224 Date: Wed, 23 Oct 2024 14:25:29 +0000 Subject: [PATCH 5/6] fix: lesson_12 extra credit --- lesson_12/structs_ts/src/stack.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index 2a1d607eb..f7d7605f5 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -20,13 +20,10 @@ export class Stack { } peek(): number | null { - if (this.top !== null && this.top !== undefined) { - return this.top.val; - } - return null; + return this.top ? this.top.val : null; } isEmpty(): boolean { - return this.top == null; + return this.top === undefined; } } From 82edf1cbc9c638b976e4b898c399f7c414e0ba60 Mon Sep 17 00:00:00 2001 From: txtran224 Date: Wed, 23 Oct 2024 14:40:04 +0000 Subject: [PATCH 6/6] fix: extra credit stack.ts --- lesson_12/structs_ts/src/stack.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index f7d7605f5..3874c9ffb 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -20,9 +20,11 @@ export class Stack { } peek(): number | null { + if (this.top === undefined) { + throw new Error('is Empty'); + } return this.top ? this.top.val : null; } - isEmpty(): boolean { return this.top === undefined; }