forked from auberonedu/java-quenching
-
Notifications
You must be signed in to change notification settings - Fork 22
Implemented all methods in Practice.java #8
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
Open
jmakho01
wants to merge
10
commits into
grc-cohort-22:main
Choose a base branch
from
jmakho01:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4bfe5d5
Implemented oddSum() method
jmakho01 34958c9
Implemented shortestWord() method
jmakho01 279180c
Implemented adults() method
jmakho01 8109fad
Implemented biggestNumber() method
jmakho01 d766bc3
Implemented frequencies() method
jmakho01 73c9c80
Implemented levelCount() method
jmakho01 c740da4
Implemented sumAtLevel() method
jmakho01 bad9d97
Implemented sumTree() method
jmakho01 5605284
Implemented onlyChildCount() method
jmakho01 e408704
Implemented maxDepth() method
jmakho01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import java.util.List; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.HashMap; | ||
| import java.util.Set; | ||
|
|
||
| public class Practice { | ||
|
|
@@ -12,7 +14,12 @@ public class Practice { | |
| * @return the sum of the odd numbers in the array | ||
| */ | ||
| public static int oddSum(int[] nums) { | ||
| return 0; | ||
| if(nums == null) { return 0; } | ||
| int sum = 0; | ||
| for(int num : nums) { | ||
| if(num % 2 != 0) { sum += num; } | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -27,7 +34,21 @@ public static int oddSum(int[] nums) { | |
| * @throws NullPointerException if words is null | ||
| */ | ||
| public static String shortestWord(Set<String> words) { | ||
| return null; | ||
| if(words == null) throw new NullPointerException(); | ||
| if(words.isEmpty()) throw new IllegalArgumentException(); | ||
| int wordLength = 99; | ||
| String shortest = ""; | ||
| for(String word : words) { | ||
| if(word.length() < wordLength) { | ||
| wordLength = word.length(); | ||
| shortest = word; | ||
| } else if(word.length() == wordLength) { | ||
| if(word.compareTo(shortest) < 0) { | ||
| shortest = word; | ||
| } | ||
| } | ||
| } | ||
| return shortest; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -40,7 +61,12 @@ public static String shortestWord(Set<String> words) { | |
| * @throws NullPointerException if ages is null | ||
| */ | ||
| public static Set<String> adults(Map<String, Integer> ages) { | ||
| return null; | ||
| if(ages == null) throw new NullPointerException(); | ||
| Set<String> eighteen = new HashSet<>(); | ||
| for(String name : ages.keySet()) { | ||
| if(ages.get(name) >= 18) { eighteen.add(name); } | ||
| } | ||
| return eighteen; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -51,7 +77,14 @@ public static Set<String> adults(Map<String, Integer> ages) { | |
| * @throws IllegalArgumentException if head is null | ||
| */ | ||
| public static int biggestNumber(ListNode<Integer> head) { | ||
| return 0; | ||
| if(head == null) throw new IllegalArgumentException(); | ||
| int biggest = -99; | ||
| ListNode<Integer> current = head; | ||
| while(current != null) { | ||
|
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, what if our biggest number was -525600? |
||
| if(current.data > biggest) { biggest = current.data; } | ||
| current = current.next; | ||
| } | ||
| return biggest; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -68,7 +101,19 @@ public static int biggestNumber(ListNode<Integer> head) { | |
| * @return a frequency map of values in the list | ||
| */ | ||
| public static <T> Map<T, Integer> frequencies(ListNode<T> head) { | ||
| return null; | ||
| Map<T, Integer> frequency = new HashMap<>(); | ||
| ListNode<T> current = head; | ||
| while(current != null) { | ||
| if(frequency.containsKey(current.data) == false) { | ||
| frequency.put(current.data, 1); | ||
| } | ||
| else { | ||
| int temp = frequency.get(current.data); | ||
| frequency.put(current.data, temp + 1); | ||
| } | ||
| current = current.next; | ||
| } | ||
| return frequency; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -81,7 +126,8 @@ public static <T> Map<T, Integer> frequencies(ListNode<T> head) { | |
| * @return the number of levels in the tree | ||
| */ | ||
| public static int levelCount(BinaryTreeNode<?> root) { | ||
| return 0; | ||
| if(root == null) return 0; | ||
| return 1 + Math.max(levelCount(root.left), levelCount(root.right)); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -109,7 +155,9 @@ public static int levelCount(BinaryTreeNode<?> root) { | |
| * @return the sum of the nodes at the given level | ||
| */ | ||
| public static int sumAtLevel(BinaryTreeNode<Integer> root, int level) { | ||
| return 0; | ||
| if(root == null || level < 1) return 0; | ||
| if(level == 1) return root.data; | ||
| return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -124,7 +172,21 @@ public static int sumAtLevel(BinaryTreeNode<Integer> root, int level) { | |
| * @return true if the sums are equal, false otherwise | ||
| */ | ||
| public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> head) { | ||
| return false; | ||
| int binarySum = sumTree(root); | ||
| int listSum = 0; | ||
| if(head != null) { | ||
| ListNode<Integer> current = head; | ||
| while(current != null) { | ||
| listSum += current.data; | ||
| current = current.next; | ||
| } | ||
| } | ||
| return listSum == binarySum; | ||
| } | ||
|
|
||
| private static int sumTree(BinaryTreeNode<Integer> root) { | ||
| if(root == null) return 0; | ||
| return root.data + sumTree(root.left) + sumTree(root.right); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -136,7 +198,10 @@ public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> h | |
| * @return the sum of all the tree's values | ||
| */ | ||
| public static int nbSum(TreeNode<Integer> root) { | ||
| return 0; | ||
| if(root == null) return 0; | ||
| int sum = root.data; | ||
| for(TreeNode<Integer> child : root.children) { sum += nbSum(child); } | ||
| return sum; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -168,7 +233,11 @@ public static int nbSum(TreeNode<Integer> root) { | |
| * @return the count of nodes that do not have siblings, EXCLUDING THE ROOT | ||
| */ | ||
| public static int onlyChildCount(TreeNode<?> root) { | ||
| return 0; | ||
| if(root == null) return 0; | ||
| int count = 0; | ||
| for(TreeNode<?> child : root.children) { count += onlyChildCount(child); } | ||
| if(root.children.size() == 1) { count++; } | ||
| return count; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -206,6 +275,16 @@ public static int onlyChildCount(TreeNode<?> root) { | |
| * @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree | ||
| */ | ||
| public static <T> int maxDepth(Map<T, List<T>> tree, T root) { | ||
| return 0; | ||
| if(tree == null || root == null || !tree.containsKey(root)) { return 0; } | ||
| int treeDepth = 0; | ||
| List<T> children = tree.get(root); | ||
| if(children == null || children.isEmpty()) { return 1; } | ||
| for(T child : children) { | ||
| int childDepth = 0; | ||
| if(tree.containsKey(child)) { childDepth = maxDepth(tree, child); } | ||
| else { childDepth = 1; } | ||
| if(childDepth > treeDepth) { treeDepth = childDepth; } | ||
| } | ||
| return treeDepth + 1; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen if our shortest word was "bababadalgharaghtakamminarronnkonnbronntonnerronntuonnthunntrovarrhounawnskawntoohoohoordenenthurnuk" (the first 100-letter-long thunderword in the modernist classic "Finnegans Wake")? Can you think of how you could change your code to be more robust?