import static dit948.SimpleIO.*; import static dit948.Random.*; public class Solutions_Sep7 { public static void main(String[] args) { // test the methods here } /** * Write a subroutine takes as argument start and end integers, and returns * a new array containing the sequence of integers from start up to but not * including end. For instance, start=5 and end=10 yields the array {5, 6, * 7, 8, 9}. You can assume the end number is greater or equal to the start * number. Note that an array of length 0 is also a valid array. */ public static int[] fizzArray3(int start, int end) { int len = end - start; int[] res = new int[len]; for (int i = start; i < end; i++) { res[i - start] = i; } return res; } /** * Write a subroutine takes as argument an array of integers and returns an * array that is left shifted by one -- so {6, 2, 5, 3} returns {2, 5, 3, * 6}. You may modify and return the given array, or return a new array. * Remember to check the corner cases! The subroutine should always return a * value and not crash. */ public static int[] shiftLeft(int[] nums) { if (nums.length == 0) { return nums; } int tmp = nums[0]; for (int i = 0; i < nums.length - 1; i++) { nums[i] = nums[i + 1]; } nums[nums.length - 1] = tmp; return nums; } /** * Write a subroutine takes a string as argument and returns the sum of the * digits 0-9 that appear in the string, ignoring all other characters. The * subroutine returns 0 if there are no digits in the string. Note that * Character.isDigit(c) tests if a character c is one of the characters '0', * '1', .. '9' and Integer.parseInt(string) converts a string to an integer. * Remember to check the corner cases! The subroutine should always return a * value and not crash. */ public static int sumDigits(String str) { int result = 0; if (str.length() == 0) return 0; for (int i = 0; i < str.length(); i++) { if (Character.isDigit(str.charAt(i))) { result += Integer.parseInt("" + str.charAt(i)); } } return result; } /** * Write a subroutine taking an array of integers as argument, returning * true if that array contains three consecutive adjacent numbers, such as * 4, 5, 6, or 23, 24, 25. Remember to check the corner cases! The * subroutine should always return a value, and not crash. */ public static boolean threeConsecutive(int[] a) { if (a.length < 3) return false; for (int i = 0; i <= a.length - 3; i++) if (a[i + 1] == a[i] + 1 && a[i + 1] + 1 == a[i + 2]) return true; return false; } }