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
145 changes: 136 additions & 9 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.Map;
import java.util.Set;

Expand All @@ -11,7 +13,16 @@ public class Practice {
* @return the sum of the odd numbers in the array
*/
public static int oddSum(int[] nums) {
return 0;
if(nums == null || nums.length < 1){
return 0;
}
int sum = 0;
for(int i =0; i<= nums.length-1; i++){
if(nums[i]%2 != 0){
sum += nums[i];
}
}
return sum;
}

/**
Expand All @@ -26,7 +37,21 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;
if(words.isEmpty()){throw new IllegalArgumentException();}
if(words == null){throw new NullPointerException();}
String shortest = null;
for(String word: words){
if(shortest == null) shortest = word;
if(word.length() < shortest.length()){
shortest = word;
}
if(word.length() == shortest.length()){
if(word.compareTo(shortest) <0){
shortest = word;
}
}
}
return shortest;
}

/**
Expand All @@ -39,7 +64,14 @@ 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> adult = new HashSet<>();
for(String name: ages.keySet()){
if(ages.get(name)>=18){
adult.add(name);
}
}
return adult;
}

/**
Expand All @@ -50,7 +82,15 @@ 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> temp = head;
int biggest = Integer.MIN_VALUE;

while(temp != null){
if(temp.data > biggest) biggest = temp.data;
temp = temp.next;
}
return biggest;
}

/**
Expand All @@ -67,7 +107,21 @@ 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> freqMap= new HashMap<>();
if(head == null) return freqMap;
ListNode<T> temp = head;
int count = 0;
while(temp != null){
if(freqMap.containsKey(temp.data)){
count = freqMap.get(temp.data) +1;
freqMap.put(temp.data, count);
}else{
freqMap.put(temp.data, 1);
}

temp = temp.next;
}
return freqMap;
}


Expand All @@ -80,7 +134,17 @@ 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 levelCount(root, 0);

}
public static int levelCount(BinaryTreeNode<?> temp, int lvlCount){
if(temp == null) return lvlCount;
lvlCount++;
int leftCount = levelCount(temp.left, lvlCount);
int rightCount = levelCount(temp.right, lvlCount);
int max = Math.max(leftCount, rightCount);
return max;
}


Expand Down Expand Up @@ -108,7 +172,18 @@ 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;
return sumAtLevelRecur(root, level, 1, 0);
}
public static int sumAtLevelRecur(BinaryTreeNode<Integer> currentNode, int level, int currentLvl, int sum){
if(currentLvl < level){
if(currentNode.left != null) sum = sumAtLevelRecur(currentNode.left, level, currentLvl+1, sum);
if(currentNode.right != null) sum = sumAtLevelRecur(currentNode.right, level, currentLvl+1, sum);
}
if(currentLvl == level){
sum += currentNode.data;
}
return sum;
}


Expand All @@ -123,7 +198,28 @@ 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) {
int rootSum = sumRoot(root, 0);
int headSum = sumHead(head, 0);
System.out.println(rootSum + " " + headSum);
if(rootSum == headSum) return true;
return false;


}
public static int sumRoot(BinaryTreeNode<Integer> temp, int sum){
if(temp == null) return sum;
sum += temp.data;
sum = sumRoot(temp.left, sum);
sum = sumRoot(temp.right, sum);

return sum;
}
public static int sumHead(ListNode<Integer> head, int sum){
if(head == null) return sum;
sum += head.data;
sum = sumHead(head.next, sum);

return sum;
}

/**
Expand All @@ -136,9 +232,22 @@ 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;
HashSet<Vertex<Integer>> visited = new HashSet<>();
return graphSum(start, visited, 0);

}

public static int graphSum(Vertex<Integer> current, HashSet<Vertex<Integer>> visited, int sum){
if(current == null || visited.contains(current)) return sum;
if (!visited.contains(current)) {
visited.add(current);
sum += current.data;
for(Vertex<Integer> neighbor: current.neighbors){
sum = graphSum(neighbor, visited, sum);
}
}
return sum;
}
/**
* Returns the count of vertices in a graph that have an outdegree of 0.
*
Expand All @@ -148,6 +257,24 @@ public static int graphSum(Vertex<Integer> start) {
* @return the count of vertices with outdegree 0
*/
public static int sinkCount(Vertex<Integer> start) {
return 0;
return sinkCount(start, 0, new HashSet<>());
}

public static int sinkCount(Vertex<Integer> current, int sinks, HashSet<Vertex<Integer>> visited){

if(current != null){
if(!visited.contains(current)){
visited.add(current);
if(current.neighbors.size() == 0){
sinks++;
}
for(Vertex<Integer> neighbor: current.neighbors){
sinks = sinkCount(neighbor, sinks, visited);
}
}


}
return sinks;
}
}