package com.gradescope.hw2pr6; // updated 2018-02-04 to include the solution to the hash table problems! import java.util.Arrays; import java.util.HashMap; public class Hw2pr6 { /************************************************************* * STOP - if you haven't read the directions, do that first! **************************************************************/ // Source: http://codingbat.com/prob/p184031 (answer available) /* * Given an array of ints, return the number of 9's in the array. */ public static int arrayCount9(int[] nums) { // TODO: Delete debugging statements from arrayCount9 System.out.println("****** arrayCount9 ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return 0; // TODO: Write arrayCount9 (Delete comment & replace code) } // Source: http://codingbat.com/prob/p186031 (answer available) /* * Given an array of ints, return true if one of the first 4 elements in the * array is a 9. The array length may be less than 4. */ public static boolean arrayFront9(int[] nums) { // TODO: Delete debugging statements from arrayFront9 System.out.println("****** arrayFront9 ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return true; // TODO: Write arrayFront9 (Delete comment & replace code) } // Source: http://codingbat.com/prob/p136041 (answer available) /* * * Given an array of ints, return true if .. 1, 2, 3, .. appears in the * array somewhere. */ public static boolean array123(int[] nums) { // TODO: Delete debugging statements from array123 System.out.println("****** array123 ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return true; // TODO: Write array123 (Delete comment & replace code) } // Source: http://codingbat.com/prob/p110019 (answer available) /* * Given an array of ints, return the number of times that two 6's are next * to each other in the array plus the number of times where a 6 is followed * by a 7. */ public static int array667(int[] nums) { // TODO: Delete debugging statements from array667 System.out.println("****** array667 ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return 0; // TODO: Write array667 (Delete comment & replace code) } // Source: http://codingbat.com/prob/p170221 (answer available) /* * Given an array of ints, we'll say that a triple is a value appearing 3 * times in a row in the array. Return true if the array does not contain * any triples. */ public static boolean noTriples(int[] nums) { // TODO: Delete debugging statements from noTriples System.out.println("****** noTriples ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return true; // TODO: Write noTriples (Delete comment & replace code) } // Source: http://codingbat.com/prob/p185685 (hint available) /* * Given an array of ints, return true if 6 appears as either the first or * last element in the array. The array will be length 1 or more. */ public static boolean firstLast6(int[] nums) { // TODO: Delete debugging statements from firstLast6 System.out.println("****** firstLast6 ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return true; // TODO: Write firstLast6 (Delete comment & replace code) } // Source: http://codingbat.com/prob/p118976 (hint available) /* * * Given an array of ints, return true if the array is length 1 or more, and * the first element and the last element are equal. */ public static boolean sameFirstLast(int[] nums) { // TODO: Delete debugging statements from sameFirstLast System.out.println("****** sameFirstLast ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return true; // TODO: Write sameFirstLast (Delete comment & replace // code) } // Source: http://codingbat.com/prob/p167011 /* * Return an int array length 3 containing the first 3 digits of pi, {3, 1, * 4}. */ public static int[] makePi() { // TODO: Delete debugging statements from makePi System.out.println("****** makePi ******"); // debug help System.out.println("Some number: " + 0); // debug help int[] retVal = new int[3]; return retVal; // TODO: Write makePi (Delete comment & replace code) } // Source: http://codingbat.com/prob/p191991 /* * Given 2 arrays of ints, a and b, return true if they have the same first * element or they have the same last element. Both arrays will be length 1 * or more. */ public static boolean commonEnd(int[] a, int[] b) { // TODO: Delete debugging statements from commonEnd System.out.println("****** commonEnd ******"); // debug help System.out.println("a: " + Arrays.toString(a)); // debug help System.out.println("b: " + Arrays.toString(b)); // debug help System.out.println("Some number: " + 0); // debug help return true; // TODO: Write commonEnd (Delete comment & replace code) } // Source: http://codingbat.com/prob/p175763 /* * Given an array of ints length 3, return the sum of all the elements. */ public static int sum3(int[] nums) { // TODO: Delete debugging statements from sum3 System.out.println("****** sum3 ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help int sum = 0; for (int i = 0; i < nums.length; i++) { // TODO: Write sum3 (Delete comment & replace code) } return sum; } // Source: http://codingbat.com/prob/p185139 /* * Given an array of ints length 3, return an array with the elements * "rotated left" so {1, 2, 3} yields {2, 3, 1}. */ public static int[] rotateLeft3(int[] nums) { // TODO: Delete debugging statements from rotateLeft3 System.out.println("****** rotateLeft3 ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return null; // TODO: Write rotateLeft3 (Delete comment & replace code) } // Source: http://codingbat.com/prob/p162010 /* * Return the number of even ints in the given array. Note: the % "mod" * operator computes the remainder, e.g. 5 % 2 is 1. */ public static int countEvens(int[] nums) { // TODO: Delete debugging statements from countEvens System.out.println("****** countEvens ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return 0; // TODO: Write countEvens (Delete comment & replace code) } // Source: http://codingbat.com/prob/p196640 /* * Given an array length 1 or more of ints, return the difference between * the largest and smallest values in the array. Note: the built-in * Math.min(v1, v2) and Math.max(v1, v2) methods return the smaller or * larger of two values. */ public static int bigDiff(int[] nums) { // TODO: Delete debugging statements from bigDiff System.out.println("****** bigDiff ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return 0; // TODO: Write bigDiff (Delete comment & replace code) } // Source: http://codingbat.com/prob/p136585 /* * Return the "centered" average of an array of ints, which we'll say is the * mean average of the values, except ignoring the largest and smallest * values in the array. If there are multiple copies of the smallest value, * ignore just one copy, and likewise for the largest value. Use int * division to produce the final average. You may assume that the array is * length 3 or more. */ public static int centeredAverage(int[] nums) { // TODO: Delete debugging statements from centeredAverage System.out.println("****** centeredAverage ******"); // debug help System.out.println("input: " + Arrays.toString(nums)); // debug help System.out.println("Some number: " + 0); // debug help return 0; // TODO: Write centeredAverage (Delete comment & replace code) } // Source: http://codingbat.com/prob/p197888 (solution available) // note - the solution will return the map, we're just modifying it. /* * Modify the given map as follows: if the key "a" has a value, set the key * "b" to have that value, and set the key "a" to have the value "". * Basically "b" is a bully, taking the value and replacing it with the * empty string. */ public static void mapBully(HashMap map) { if (map.containsKey("a")) { map.put("b", map.get("a")); map.put("a", ""); } } // Source: http://codingbat.com/prob/p182712 (hint available) /* * Given a map of food keys and topping values, modify and return the map as * follows: if the key "ice cream" is present, set its value to "cherry". In * all cases, set the key "bread" to have the value "butter". */ public static void topping1(HashMap map) { map.put("bread", "butter"); if (map.containsKey("ice cream")){ map.put("ice cream", "cherry"); } } public static void main(String[] args) { // TODO: Delete debugging statements from main // You can add debugging statements here to test your methods! } }