Skip to content
167 changes: 158 additions & 9 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

public class Practice {
Expand All @@ -11,7 +15,20 @@ 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 odds = 0;

for(int i = 0; i<=nums.length; i++)
{
if(nums[i] % 2 != 0)
{
odds = odds + nums[i];
}
}
return odds;
}

/**
Expand All @@ -26,7 +43,21 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;
String shortWord = "";

if(words == null || words.size() == 0)
{
throw new IllegalArgumentException("Not allowed");
}

for(var word : words)
{
if(word.length() < shortWord.length())
{
shortWord = word;
}
}
return shortWord;
}

/**
Expand All @@ -39,7 +70,18 @@ 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("Head cannot be null");
}
Set<String> grownUps = new HashSet<>();
for (String names : ages.keySet()) {
if(ages.get(names) >= 18)
{
grownUps.add(names);
}
}
return null;
}

/**
Expand All @@ -50,7 +92,21 @@ 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("Head cannot be null");
}
int bigNum = head.data;
while(head != null)
{
if(head.data > bigNum)
{
bigNum = head.data;
}
head = head.next;
}

return bigNum;
}

/**
Expand All @@ -67,7 +123,20 @@ 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> counts = new HashMap<>();
while(head != null)
{
if(counts.containsKey(head.data))
{
counts.put(head.data, counts.get(head.data) + 1);
}
else
{
counts.put(head.data, 1);
}
head = head.next;
}
return counts;
}


Expand All @@ -80,7 +149,18 @@ 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;
}
int left = levelCount(root.left);
int right = levelCount(root.right);
if (left > right) {
return left + 1;
}
else {
return right + 1;
}
}


Expand Down Expand Up @@ -108,8 +188,36 @@ 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) {

if(root == null || level < 1)
{
return 0;
}
Queue<BinaryTreeNode<Integer>> queue = new LinkedList<>();
queue.add(root);
int currentLevel = 1;
int sum = 0;
while(!queue.isEmpty()) {
int size = queue.size();
for(int i = 0; i < size; i++) {
BinaryTreeNode<Integer> node = queue.poll();
if(currentLevel == level) {
sum += node.data;
}
if(node.left != null) {
queue.add(node.left);
}
if(node.right != null) {
queue.add(node.right);
}
}
if(currentLevel == level) {
return sum;
}
currentLevel++;
}
return 0;
}
}


/**
Expand All @@ -123,7 +231,27 @@ 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(treeSum(root) == listSum(head))
{
return true;
}
else return false;
}
public static int treeSum(BinaryTreeNode<Integer> node) {
if(node == null)
{
return 0;
}
return node.data + treeSum(node.left) + treeSum(node.right);
}
public static int listSum(ListNode<Integer> head) {
int sum = 0;
while(head != null)
{
sum += head.data;
head = head.next;
}
return sum;
}

/**
Expand All @@ -136,9 +264,30 @@ public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> h
* @return the sum of all the vertices
*/
public static int graphSum(Vertex<Integer> start) {
return 0;
if(start == null)
{
return 0;
}
Set<Vertex<Integer>> visited = new HashSet<>();
return graphSum(start, visited);
}

public static int graphSum(Vertex<Integer> node, Set<Vertex<Integer>> visited) {
if(node == null || visited.contains(node))
{
return 0;
}
visited.add(node);

int sum = node.data;
for(Vertex<Integer> neighbor : node.neighbors)
{
sum+=graphSum(neighbor, visited);
}
return sum;
}


/**
* Returns the count of vertices in a graph that have an outdegree of 0.
*
Expand Down