Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Tuesday, September 26, 2017
Wednesday, June 15, 2016
splitOdd10 - Recursive - Java & C#
Given an array of ints, is it possible to divide the ints into two groups, so that the sum of one group is a multiple of 10, and the sum of the other group is odd. Every int must be in one group or the other. Write a recursive helper method that takes whatever arguments you like, and make the initial call to your recursive helper from splitOdd10(). (No loops needed.)
splitOdd10([5, 5, 5]) → true splitOdd10([5, 5, 6]) → false splitOdd10([5, 5, 6, 1]) → true |
Java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public boolean splitOdd10(int[] nums) { return helper(0, nums, 0); } public boolean helper(int index, int[] nums, int result) { if(index >= nums.length) { if ((result % 10) % 2 == 1) return true; else return false; } return helper(index + 1, nums, result + nums[index]); } |
Test Result:
C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static Boolean splitOdd10(int[] nums) { return helper(0, nums, 0); } public static Boolean helper(int index, int[] nums, int result) { if(index >= nums.Length) { if ((result % 10) % 2 == 1) return true; else return false; } return helper(index + 1, nums, result + nums[index]); } |
Test :
Label:
C#,
eclipse,
Java,
Java Challenge,
Programming,
VS2013
stringClean - Recursive - Java & C#
Given a string, return recursively a "cleaned" string where adjacent chars that are the same have been reduced to a single char. So "yyzzza" yields "yza".
stringClean("yyzzza") → "yza" stringClean("abbbcdd") → "abcd" stringClean("Hello") → "Helo" |
Java :
1 2 3 4 5 6 7 8 9 10 11 12 | public String stringClean(String str) { //Stop Condition if(str.length()<2) return str; //Main recursive process int index = 0; for(index=0; index+1<str.length(); index++) { if(str.charAt(index) != str.charAt(index+1)) break; } return str.charAt(index)+stringClean(str.substring(index+1)); } |
Test Result:
C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static string stringClean(string str) { //kondisi setop if (str.Length < 2) return str; //main rekursif int index = 0; for (index=0; index+1 < str.Length; index++) { if (str[index] != str[index + 1]) break; } return str[index] + stringClean(str.Substring(index+1)); } |
Test Result :
Label:
C#,
eclipse,
Java,
Java Challenge,
Programming,
VS2013
allStar - Recursive - Java & C#
Given a string, compute recursively a new string where all the adjacent chars are now separated by a "*".
allStar("hello") → "h*e*l*l*o"
allStar("abc") → "a*b*c"
allStar("ab") → "a*b"
Java :
1 2 3 4 5 6 7 8 9 10 | public static String allStar(String str) { //Stop Condition if(str.length()<1) return ""; if(str.length() == 1) return str+"*"; //Main recursive process return str.charAt(0)+"*"+allStar(str.substring(1)); } |
Hasil :
C# :
1 2 3 4 5 6 7 8 9 10 | public static string allStar(String str) { //Kondisi stop if (str.Length < 1) return ""; if (str.Length == 1) return str; //Proses rekursif return str[0] + "*" + allStar(str.Substring(1)); } |
Hasil :
Label:
C#,
eclipse,
Java,
Java Challenge,
Programming,
VS2013
array11 - Recursive - Java & C#
Given an array of ints, compute recursively the number of times that the value 11 appears in the array. We'll use the convention of considering only the part of the array that begins at the given index. In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0.
array11([1, 2, 11], 0) → 1 array11([11, 11], 0) → 2 array11([1, 2, 3, 4], 0) → 0 |
Java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static int array11(int[] nums, int index) { //Stop Condition if(nums.length == 0 ) return 0; if(nums.length == 1) { if(nums[0] == 11) return 1; else return 0; } //Main recursive process int[] temp = Arrays.copyOfRange(nums, index+1, nums.length); if(nums[index] == 11) return 1+array11(temp,index); else return array11(temp,index); } |
Test Result :
C# :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static int array11(int[] nums, int index) { //Kondisi stop if (nums.Length == 0) return 0; if (nums.Length == 1) { if (nums[0] == 11) return 1; else return 0; } //Proses rekursif int[] temp = new int[nums.Length - index - 1]; System.Array.Copy(nums, index+1, temp, 0, temp.Length); if (nums[index] == 11) return 1 + array11(temp, index); else return array11(temp, index); } |
Test Result:
Label:
C#,
eclipse,
Java,
Java Challenge,
Programming,
VS2013
array6 - Recursive - Java & C#
Given an array of ints, compute recursively if the array contains a 6. We'll use the convention of considering only the part of the array that begins at the given index. In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0.
array6([1, 6, 4], 0) → true
array6([1, 4], 0) → false
array6([6], 0) → true
Java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static boolean array6(int[] nums, int index) { //Stop condition if(nums.length == 0) return false; if(nums.length == 1) { if(nums[0]== 6) return true; else return false; } //Main process if(nums[index] == 6) return true; int[] temp = Arrays.copyOfRange(nums, index+1, nums.length); return array6(temp,index); } |
Test Result:
C# :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static Boolean array6(int[] nums, int index) { //stop condition if (nums.Length == 0) return false; if (nums.Length == 1) { if (nums[0] == 6) return true; else return false; } if(nums[index] == 6) return true; //Main recursive process int[] temp = new int[nums.Length-index-1]; System.Array.Copy(nums,index+1,temp,0,temp.Length); return array6(temp, index); } |
Test Result:
Label:
eclipse,
Java,
Java Challenge,
Programming,
VS2013
Tuesday, June 14, 2016
changePi - Recursive - Java & C#
Given a string, compute recursively (no loops) a new string where all appearances of "pi" have been replaced by "3.14".
changePi("xpix") → "x3.14x"
changePi("pipi") → "3.143.14"
changePi("pip") → "3.14p"
Java :
1 2 3 4 5 6 7 | public String changePi(String str) { if (str.length() < 1) return ""; int index = str.indexOf("pi"); if (index < 0) return str; String temp = str.substring(0, index); return temp+"3.14"+changePi(str.substring(index+2, str.length())); } |
Test result:
C# :
1 2 3 4 5 6 7 8 9 | public static String changePi(String str) { if (str.Length < 1) return ""; int index = str.IndexOf("pi"); if (index < 0 ) return str; String temp0 = str.Substring(0, index); String temp = str.Substring(index+2, str.Length-(2+index)); return temp0+"3.14" + changePi(temp); } |
Test Result :
Label:
C#,
eclipse,
Java,
Java Challenge,
Programming,
Visual Studio,
VS2013
changeXY - Recursive - Java & C#
Given a string, compute recursively (no loops) a new string where all the lowercase 'x' chars have been changed to 'y' chars.
changeXY("codex") → "codey"
changeXY("xxhixx") → "yyhiyy"
changeXY("xhixhix") → "yhiyhiy"
Java :
1 2 3 4 5 6 | public String changeXY(String str) { if(str.length() < 1) return ""; char temp = str.charAt(str.length()-1); if(temp == 'x') temp = 'y'; return changeXY(str.substring(0,str.length()-1))+temp; } |
Test Result:
C#:
1 2 3 4 5 6 7 | public static String changeXY(String str) { if (str.Length < 1) return ""; Char temp = str[str.Length - 1]; if (temp == 'x') temp = 'y'; return changeXY(str.Substring(0, str.Length - 1)) + temp; } |
Test Result :
Label:
C#,
eclipse,
Java,
Java Challenge,
Programming,
VS2013
countHi - Recursive
Given a string, compute recursively (no loops) the number of times lowercase "hi" appears in the string.
countHi("xxhixx") → 1 countHi("xhixhix") → 2 countHi("hi") → 1 |
C# :
1 2 3 4 5 6 7 8 9 10 11 | public static int countHi(String str) { //stop condition if (str.Length < 2) return 0; //Process int index = str.IndexOf("hi"); if (index < 0) return 0; return 1 + countHi(str.Substring(index + 1, str.Length - (1 + index))); } |
Result :
Label:
eclipse,
Java,
Java Challenge,
Programming,
VS2013
CountX - Recursive
Given a string, compute recursively (no loops) the number of lowercase 'x' chars in the string.
countX("xxhixx") → 4 countX("xhixhix") → 3 countX("hi") → 0 |
Hisoka's Jutsu :
1 2 3 4 5 | public int countX(String str) { if (str.length() == 0) return 0; if(str.charAt(str.length()-1) == 'x') return 1+countX(str.substring(0, str.length()-1)); return countX(str.substring(0, str.length()-1)); } |
Output :
Ooowh... btw... if we want to write countX in C#, it will gonna be like this :
1 2 3 4 5 6 7 | public static int countX(String str) { //stop condition if (str.Length == 0) return 0; if (str[(str.Length - 1)] == 'x') return 1 + countX(str.Substring(0, str.Length - 1)); return countX(str.Substring(0, str.Length - 1)); } |
Label:
eclipse,
Java,
Java Challenge,
Programming
PowerN - Recursion
Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared).
powerN(3, 1) → 3
powerN(3, 2) → 9
powerN(3, 3) → 27
Java :
1 2 3 4 5 | public int powerN(int base, int n) { if(n == 0 ) return 1; if(n == 1) return base; return base*powerN(base,n-1); } |
Output :
Oooowh...btw... We can use DinamycProgramming style here.... It will gonna look like this :
1 2 3 4 5 6 7 8 9 | public int powerN(int base, int n) { int[] hasil = new int[n]; hasil[0] = base; for(int i=1; i<n; i++) { hasil[i] = base*hasil[i-1]; } return hasil[n-1]; } |
The output will be same, but DinamycProgramming style is faster than recursive at a big n... :D
Monday, June 13, 2016
Check Single Swap Array in Java
Here is the code :
Test case :
Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static boolean CheckSingleSwap(int[] A) { int count = 0; int[]B = Arrays.copyOf(A, A.length); Arrays.sort(B); for(int i=0; i<A.length; i++) { if(A[i] != B[i]) count++; } if(count > 2) return false; return true; } |
Test case :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | System.out.println("Test Single Swap for Array..."); int[]A = {1,2}; int[]B = {1,3,6,3,5,5,3,7,7}; int[]C = {1, 4, 5, 6, 7, 2}; int[]D = {1,2,3,4,5,6,7,8,9,10,11,2,45,56,67,78,89,90,123,124,1245566778}; int[]D1 = {1,2,3,4,5,6,7,8,9,10,11,2,45,56,67,78,89,90,2,123,124,1245566778}; int[]E = {1, 5, 3, 3, 7} ; int[]F= {1, 3, 5}; int[]G = {1,5,3}; int[]H = {1, 2, 6, 3, 4, 5}; System.out.println("A : "+CheckSingleSwap(A)); System.out.println("B : "+CheckSingleSwap(B)); System.out.println("C : "+CheckSingleSwap(C)); System.out.println("D : "+CheckSingleSwap(D)); System.out.println("D1 : "+CheckSingleSwap(D1)); System.out.println("E : "+CheckSingleSwap(E)); System.out.println("F : "+CheckSingleSwap(F)); System.out.println("H : "+CheckSingleSwap(H)); |
Output :
1 2 3 4 5 6 7 8 | A : true B : true C : false D : false D1 : false E : true F : true H : false |
Label:
eclipse,
Java,
Java Challenge,
Programming
Thursday, June 9, 2016
ArrayInversionCount Codility - Road to 100% - Java
Task Link : https://codility.com/programmers/task/array_inversion_count/
First solution (72%):
Result : https://codility.com/demo/results/trainingXBUESU-MQG/
Second Solution (100%) :
Result : https://codility.com/demo/results/training57Y8R2-QK9/
First solution (72%):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Solution { public int solution(int[] A) { // write your code in Java SE 8 int count = 0; for(int p=0; p<A.length; p++) { for(int q=p+1; q<A.length; q++) { if(A[p]>A[q]) count++; } } if(count > 1000000000) return -1; return count; } } |
Result : https://codility.com/demo/results/trainingXBUESU-MQG/
Second Solution (100%) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | // you can also use imports, for example: import java.util.*; // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { public int solution(int[] arr) { // write your code in Java SE 8 if (arr.length < 2) return 0; int m = (arr.length + 1) / 2; int left[] = Arrays.copyOfRange(arr, 0, m); int right[] = Arrays.copyOfRange(arr, m, arr.length); int result = solution(left) + solution(right) + merge(arr, left, right); if(result > 1000000000) return -1; return result; } int merge(int[] arr, int[] left, int[] right) { int i = 0, j = 0, count = 0; while (i < left.length || j < right.length) { if (i == left.length) { arr[i+j] = right[j]; j++; } else if (j == right.length) { arr[i+j] = left[i]; i++; } else if (left[i] <= right[j]) { arr[i+j] = left[i]; i++; } else { arr[i+j] = right[j]; count += left.length-i; j++; } } return count; } } |
Result : https://codility.com/demo/results/training57Y8R2-QK9/
Label:
eclipse,
Java,
Java Challenge,
Programming
Subscribe to:
Posts (Atom)