diff --git a/week-2/Java/Christopher-Fulton.java b/week-2/Java/Christopher-Fulton.java new file mode 100644 index 0000000..684798f --- /dev/null +++ b/week-2/Java/Christopher-Fulton.java @@ -0,0 +1,96 @@ + + +import java.util.Scanner; + + + +class Week2{ + + static void permutation(String perm,String str) { + if (str.length() == 0) { // if str is empty perm must be full permuation + System.out.println(perm + " "); + return; + } + for (int i = 0; i < str.length(); i++) { + permutation(perm+str.charAt(i), str.substring(0, i)+str.substring(i+1, str.length()));// Loop through string recursively each time add current index to substring of rest of word. + } + + } + + + +// public static String maxPalindrone(String str) { +// str = str.replaceAll("\\s",""); // replace all spaces and turn upper to lower case for simplicity +// str = str.toLowerCase(); +// StringBuilder tempStr = new StringBuilder(); +// StringBuilder tempBuilder = new StringBuilder(); +// String maxSubstring =""; +// for(int i = 0;i maxSubstring.length()){ +// maxSubstring = tempStr.toString();// Check wheter or not substring is a palindrone and ifso if it's largest. +// } +// tempStr.setLength(0);// Reset string objects +// tempBuilder.setLength(0); +// } +// } + +// return maxSubstring; +// } + public static void main(String[] args) { + //////////////////////Interface///////////////////////////////////////////////////// + Scanner in = new Scanner(System.in); + + String a = "a"; + String b = "b"; + System.out.println(b.compareTo(a)); + + + + + + + + + + + + + + + + + + + + + // String str = "ABC"; + // for (int i = str.length()-1;i>0;i--){ + // for (int j = i -1; j sumLinkedList(LinkedList list1, LinkedList list2) { + LinkedList newLinkedList = new LinkedList(); + int[] tempList1 = new int[2]; + int carry = 0; + + for (int i = list1.size()-1; i > -1; i--){ + tempList1 = addElements(list1.get(i), list2.get(i),carry); // Holds values of array for funtion. + carry = tempList1[1]; + newLinkedList.add(tempList1[0]); + } + newLinkedList.add(tempList1[1]);// BHolds + Iterator reverseLinkIterator = newLinkedList.descendingIterator(); + LinkedList finalLinkedList = new LinkedList(); + while (reverseLinkIterator.hasNext()){ + finalLinkedList.add(reverseLinkIterator.next()); + } + + return finalLinkedList; + +} + +public static LinkedList rLinkedList(LinkedList sLinkedList){ + String[] tempArray = new String[sLinkedList.size()]; + for (int i = 0; i finalLinkedList = new LinkedList(); + for (int i = tempArray.length-1; i > -1; i--){ + if (i ==0){ + for (int k = i; k<=tempIndex-1;k++) + finalLinkedList.add(tempArray[k]); + } + if (tempArray[i].equals(" ") ){ + for (int j = i+1; j<=tempIndex-1;j++){ + finalLinkedList.add(tempArray[j]); + } + finalLinkedList.add(" "); //Adding the space after letter. + tempIndex = i; + } + } + + + return finalLinkedList; +} + +public static void main(String[] args) { + LinkedList L1 = new LinkedList(); + LinkedList L2 = new LinkedList(); + L1.add(7); + L1.add(6); + L1.add(3); + L2.add(7); + L2.add(4); + L2.add(2); + System.out.println(sumLinkedList(L1, L2)); //Only works for linked list of equal size + + LinkedList sLinkedList = new LinkedList(); + String testString = "I Love Geeks For Geeks"; + for (int i =0; i elements = new HashMap(); + int minInt = Integer.MAX_VALUE; + for(int value: values){ + if(!elements.containsKey(value)) + elements.put(value,1); + else { + elements.put(value, elements.get(value) + 1); + if(value < minInt && elements.get(value) == k ) + minInt = value; + } + + + } + return minInt; + } + + static int binarySearch(int[] totals, int value){ + if(totals[0] >= value) + return 1; + int start = 0; + int end = totals.length-1; + while(start <= end){ + int middle = start + (end - start)/ 2; + if(totals[start] >= value) + return start + 1; + + if(totals[middle] < value) + start = middle + 1; + else if(totals[middle] > value) + end = middle - 1; + } + + return end; + } + public static void main(String[] args) { + int[] N = {1,2,3,4,5}; + int[] totals = new int[5]; + int[] queries = {3,8,10,14}; + int total = 0; + for(int i = 0; i < totals.length; i++){ + total+= N[i]; + totals[i] = total; + } + for (int query : queries) System.out.println(binarySearch(totals, query)); + int[] N2 = {2,2,1,3,1}; + System.out.println(lowestIntLinearSearch(N2, 2)); + } + + +} \ No newline at end of file