Skip to content
Open
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
138 changes: 126 additions & 12 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -12,7 +14,23 @@ 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;
}
if(nums.length > 0){
int sum = 0;
for(int i = 0; i < nums.length; i++){
switch (nums[i]%2) {
case 0 -> {
}
case 1 -> sum+=nums[i];
case -1 -> sum+=nums[i];
}
}
return sum;
}else{
return 0;
}
}

/**
Expand All @@ -27,7 +45,23 @@ 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();
}else if(words.size() < 1){
throw new IllegalArgumentException();
}
String shortWord = "dsajfoiewahjrog8jeraiobjfdaighjdaihgerwao8gherahgusahfoiueahgfdo98";
for (String string : words) {
if(string.length() < shortWord.length()){
shortWord = string;
}else if(string.length() == shortWord.length()){
int compare = shortWord.compareTo(string);
if(compare > 0){
shortWord = string;
}
}
}
Comment on lines +53 to +63
Copy link
Copy Markdown

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?

return shortWord;
}

/**
Expand All @@ -40,7 +74,16 @@ 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> peopleReturn = new HashSet<>();
for(String person: ages.keySet()){
if(ages.get(person) >= 18){
peopleReturn.add(person);
}
}
return peopleReturn;
}

/**
Expand All @@ -51,7 +94,18 @@ 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();
}
ListNode<Integer> current = head;
int largest = current.data;
while(current!=null){
if(current.data > largest){
largest = current.data;
}
current = current.next;
}
return largest;
}

/**
Expand All @@ -68,7 +122,16 @@ 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> returnMap = new HashMap<>();
ListNode<T> current = head;
if(head == null){
return returnMap;
}
while(current != null){
returnMap.put(current.data, returnMap.getOrDefault(current.data, 0)+1);
current = current.next;
}
return returnMap;
}


Expand All @@ -81,7 +144,13 @@ 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;
if(levelCount(root.left) > levelCount(root.right)){
return levelCount(root.left) + 1;
}else{
return levelCount(root.right) + 1;
}
}


Expand Down Expand Up @@ -109,7 +178,12 @@ 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) return 0;
if(levelCount(root.left) > levelCount(root.right)){
return levelCount(root.left) + 1;
}else{
return levelCount(root.right) + 1;
}
}


Expand All @@ -124,7 +198,25 @@ 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;
if(root == null && head == null){
return true;
}
ListNode<Integer> current = head;
int listSum = 0;
while(current != null){
listSum += current.data;
current = current.next;
}
if(listSum == sumTree(root)){
return true;
}else{
return false;
}
}
public static int sumTree(BinaryTreeNode<Integer> root){
if(root == null) return 0;

return sumTree(root.left) + sumTree(root.right) + root.data;
}

/**
Expand All @@ -136,7 +228,12 @@ 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;
}

/**
Expand Down Expand Up @@ -168,7 +265,17 @@ 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){
return count + 1;
}
return count;

}

/**
Expand Down Expand Up @@ -206,6 +313,13 @@ 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) return 0;
if(tree.get(root)==null)return 0;
if(tree.get(root).isEmpty())return 1;
int value = 1;
for(T child: tree.get(root)){
value = Math.max(value, maxDepth(tree, child));
}
return value + 1;
}
}