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
181 changes: 171 additions & 10 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IllegalFormatCodePointException;
import java.util.Map;
import java.util.Set;

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

}


}


return sum;
}

/**
Expand All @@ -26,7 +44,29 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;
String shortest =null;
int minLength = Integer.MAX_VALUE;
if (words ==null){
throw new NullPointerException();
}
if(words.isEmpty()){
throw new IllegalArgumentException();
}
for(String word:words){
if(word.length()<minLength){
minLength=word.length();
shortest=word;
}else if(word.length()== minLength){
//"apple".compareTo("banana") returns negative, because "apple" comes before "banana"
//"cat".compareTo("bat") returns positive, because "cat" comes after "bat"
//"dog".compareTo("dog") returns 0, they are equal
if(word.compareTo(shortest)<0){
shortest=word;
}
}
}

return shortest;
}

/**
Expand All @@ -39,7 +79,21 @@ public static String shortestWord(Set<String> words) {
* @throws NullPointerException if ages is null
*/
public static Set<String> adults(Map<String, Integer> ages) {
return null;
int age=0;
if(ages ==null){
throw new NullPointerException();
}
Set<String> adultNames= new HashSet<>();
for(String name:ages.keySet()){
age=ages.get(name);
if(age>=18){
adultNames.add(name);
}

}


return adultNames;
}

/**
Expand All @@ -50,7 +104,20 @@ 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 max = Integer.MIN_VALUE;
while(current!=null){
if(current.data>max){
max=current.data;
current=current.next;
}
}


return max;
}

/**
Expand All @@ -67,7 +134,24 @@ 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> current=head;
while(current!=null){
if(freqMap.containsKey(current.data)){
//add 1 if we have seen it before
freqMap.put(current.data, freqMap.get(current.data)+1);
}else{
//first time seeing it, add with count of 1
freqMap.put(current.data, 1);
}
current=current.next;
}

return freqMap;
}


Expand All @@ -80,7 +164,20 @@ 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;
int levels =0;
if(root ==null){
return 0;
}
if(root.left ==null && root.right==null){
return 1;
}
if(root.left !=null){
levels=Math.max(levelCount(root.left), levels);
}
if(root.right !=null){
levels=Math.max(levelCount(root.right), levels);
}
return levels+1;
}


Expand Down Expand Up @@ -108,7 +205,16 @@ 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);


}


Expand All @@ -123,7 +229,26 @@ 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 treeSum=treeSum(root);
int listSum=listSum(head);
return treeSum==listSum;
}
//helper methods add all value in the tree
public static int treeSum(BinaryTreeNode<Integer> root){
if(root==null){
return 0;
}
return root.data + treeSum(root.left) + treeSum(root.right);
}
//helper method to add all values in the list
public static int listSum(ListNode<Integer> head){
int sum=0;
ListNode<Integer> current=head;
while(current!=null){
sum+=current.data;
current=current.next;
}
return sum;
}

/**
Expand All @@ -136,7 +261,24 @@ 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 graphSumHelper(start, visited);
}
//helper method to do depth first search
public static int graphSumHelper(Vertex<Integer> vertex, Set<Vertex<Integer>> visited){
if(visited.contains(vertex)){
return 0;
}
visited.add(vertex);
//add current vertex data to sum of neighbors
int sum = vertex.data;
for(Vertex<Integer> neighbor: vertex.neighbors){
sum += graphSumHelper(neighbor, visited);
}
return sum;
}

/**
Expand All @@ -148,6 +290,25 @@ 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 sinkCountHelper(start, visited);
}
//helper method to do depth first search
public static int sinkCountHelper(Vertex<Integer> vertex, Set<Vertex<Integer>> visited){
if(visited.contains(vertex)){
return 0;
}
visited.add(vertex);
int count =0;
if(vertex.neighbors.isEmpty()){
count=1;
}
for(Vertex<Integer> neighbor: vertex.neighbors){
count += sinkCountHelper(neighbor, visited);
}
return count;
}
}