import java.util.ArrayList; import java.util.Arrays; import java.math.BigInteger; import java.math.BigDecimal; /** * Glorious Lab Practical on Java */ public class PracticeLP1 { /** * This computes the sum of a list of BigIntegers * @param a an array list of BigIntegers * @return the sum of the BigIntegers in a */ public static BigInteger sum(ArrayList a) { BigInteger total = BigInteger.ZERO; for(BigInteger item: a) //for item i a: { total = total.add(item); } return total; } /** * This plucks out entries of a string at indices divisible by p. * in a string. * @param s a string * @param p a nonnegative integer. * @return a string that has entries p, 2p, 3p, etc of the * string s. * Examples: * aerate("aardwolf" 2) -> "arwl" * aerate("bacchanalia" 3) -> "bcni" */ public static String aerate(String s, int p) { /*StringBuffer sb = new StringBuffer(); for(int k = 1; k < s.length(); k += p) { sb.append(s.charAt(k)); } return sb.toString();*/ String out = ""; for(int k = 1; k < s.length(); k += p) { out += s.charAt(k); } return out; } /** * This makes a string echoy. See the example * @param s is a string * @return a string with the nth character repeated n times. * example: echoy("cowpie") → coowwwppppiiiiieeeeee * if the string passed is empty, return an empty string */ public static String echoy(String s) { StringBuffer sb = new StringBuffer(); for(int k = 0; k < s.length(); k++) { sb.append(("" + s.charAt(k)).repeat(k + 1)); } return sb.toString(); } /** * This computes the product of the non-zero elements of * a and returns the sum of its digits. * @param a an array list * @return the sum of the digits in the product of the * non-zero entries in a */ public static int finger(ArrayList a) { //compute product of nonzero factors BigInteger prod = BigInteger.ONE; for(BigInteger item: a) { if(!item.equals(BigInteger.ZERO)) { prod = prod.multiply(item); } } //we now have the product stored in prod. int total = 0; while(!prod.equals(BigInteger.ZERO)) { total += prod.mod(BigInteger.TEN).intValue(); prod = prod.divide(BigInteger.TEN); } return total; //sum digits of the product } /** * This filters strings for a specified substring * @param al is an array list of strings. * @param s is a search string * @return an array list of striings containing all those strings * in the array list al having s as a substring. */ public static ArrayList pseudoGrep(ArrayList al, String s) { ArrayList out = new ArrayList<>(); for(String item: al) { if(item.contains(s)) { out.add(item); } } return out; } /** * @param num is a nonnegative integer * @param denom is a nonnegative integer whose only prime factors * are 2 or 5 * @return a string reprsesentation of the exact decimal * expansion of num/denom. * The BigDecimal class can help you with this! * You will need to scrach around on its API page. */ public static String exactQuotient(int num, int denom) { BigDecimal top = new BigDecimal(num); BigDecimal bottom = new BigDecimal(denom); return top.divide(bottom).toPlainString(); } /** * Here is your testing ground. * @param args command-line arguments. You won't have any. */ public static void main(String[] args) { System.out.println(exactQuotient(1, 268435456).equals("0.0000000037252902984619140625")); } }