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
186 changes: 177 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;

int oddCount = 0;

if(nums == null) return 0;

for(int items: nums)
{
if(items % 2 != 0)
{
oddCount += items;
}
}

return oddCount;
}

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

String smallestWord = null;

if(words == null) throw new NullPointerException();
if(words.isEmpty()) throw new IllegalArgumentException();

for(String items: words)
{
if(smallestWord == null || items.length() < smallestWord.length() ||
(items.length() == smallestWord.length()) && items.compareTo(smallestWord) < 0)
{
smallestWord = items;
}

}


return smallestWord;
}

/**
Expand All @@ -39,7 +73,19 @@ 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> people = new HashSet<>();

for(Map.Entry<String, Integer> items : ages.entrySet())
{
if(items.getValue() >= 18)
{
people.add(items.getKey());
}
}


return people;
}

/**
Expand All @@ -50,7 +96,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();

int biggest = Integer.MIN_VALUE;
ListNode<Integer> current = head;
while(current != null)
{
if(biggest < current.data)
{
biggest = current.data;
}
current = current.next;
}

return biggest;
}

/**
Expand All @@ -67,7 +127,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> map = new HashMap<>();


if(head == null) return map;
ListNode<T> current = head;
while(current != null)
{
map.put(current.data, map.getOrDefault(current.data, 0) + 1);
current = current.next;
}


return map;
}


Expand All @@ -80,7 +152,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;

int leftSearch = levelCount(root.left);
int rightSearch = levelCount(root.right);


return Math.max(leftSearch, rightSearch) + 1;
}


Expand Down Expand Up @@ -108,6 +186,29 @@ 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) return 0;

Queue<BinaryTreeNode<Integer>> q = new LinkedList<>();
q.add(root);
int target = 1;

while(!q.isEmpty())
{
int size = q.size();
int sumed = 0;

for(int i = 0; i < size; i++)
{
BinaryTreeNode<Integer> current = q.poll();
if(target == level) sumed += current.data;
if(current.left != null) q.add(current.left);
if(current.right != null) q.add(current.right);
}

if(target == level) return sumed;
target++;

}
return 0;
}

Expand All @@ -123,7 +224,34 @@ 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(head == null && root == null) return true;
if(root == null) return false;
if(head == null) return false;

Queue<BinaryTreeNode<Integer>> q = new LinkedList<>();
q.add(root);
int BSTSum = 0;

while(!q.isEmpty())
{
BinaryTreeNode<Integer> current = q.poll();
BSTSum += current.data;
if(current.left != null) q.add(current.left);
if(current.right != null) q.add(current.right);
}

ListNode<Integer> current = head;
int LNSum = 0;

while(current != null)
{
LNSum += current.data;
current = current.next;
}


return BSTSum == LNSum;
}

/**
Expand All @@ -136,7 +264,25 @@ 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 dfs(start, visited);
}


public static int dfs(Vertex<Integer> start, Set<Vertex<Integer>> visited)
{
if(visited.contains(start)) return 0;
visited.add(start);

int sum = start.data;

for(Vertex<Integer> neighbor: start.neighbors)
{
sum += dfs(neighbor, visited);
}
return sum;
}

/**
Expand All @@ -148,6 +294,28 @@ public static int graphSum(Vertex<Integer> start) {
* @return the count of vertices with outdegree 0
*/
public static int sinkCount(Vertex<Integer> start) {
return 0;
if(start == null) return 0;
Set<Vertex<Integer>> visited = new HashSet<>();
return sinkCountDFS(start, visited);
}

public static int sinkCountDFS(Vertex<Integer> start, Set<Vertex<Integer>> visited)
{
if(visited.contains(start)) return 0;
visited.add(start);

int count = 0;

if(start.neighbors.isEmpty())
{
count++;
}

for(Vertex<Integer> neighbor: start.neighbors)
{
count += sinkCountDFS(neighbor, visited);
}

return count;
}
}