HERE I SOLVE /PRACTICE PROGRAMMING EXERCISES FROM VARIOUS sources ENJOY!
coding bat fix34 my solution with debugging statements
public int[] fix34(int[] nums) {
//int result[] = new int[nums.length] ;
// System.out.println("before we begin, array is :") ;
// printArray(nums) ;
int index_4 = 0 ;
for(int i = 0 ; i < nums.length;i++){
// System.out.println("**************** i:" + i + " ***************") ;
//index_4 = 0 ;
if( nums[i] == 3){
// System.out.println("found 3 o lo lo!") ;
for(int j = index_4 ; j < nums.length;j++){
// System.out.println("inside 2nd loop - and looking for 4, start at index: " + j) ;
if(nums[j] == 4 && j ==(i+1)){
// System.out.println("3 and 4 goes after - so just skip, it is in good order!") ;
continue ;
}
if( nums[j] == 4){
// System.out.println("found 4 yehoo! - so doing swap!") ;
//doing swap
int temp = nums[i+1] ;
nums[i+1] = 4 ;
nums[j] = temp ;
// System.out.println("swapped elements indexes: " + (i+1) + " and " + j + ", values swapped: " + temp + " and " + nums[i+1]) ;
// System.out.println("the array now: ") ;
index_4 = j ;
// printArray(nums) ;
break ;
}
}//end of 2nd loop who looks for 4
}
}//end of loop
return nums ;
}
Posted by
erjan
same Ends exercise, big inefficient debugged solution
public class endOtherClass{
public static void main(String args[]) {
String res = sameEnds("javaXYZjava") ;
System.out.println(" biggest substring at ends is: " + res) ;
}
public static void printArray(String []arr){
System.out.println("////////////printing array///////////////////") ;
for(int i = 0 ; i < arr.length;i++)
System.out.println(arr[i]) ;
}
public static String sameEnds(String string) {
boolean quit = false ;
String biggest_substring = "" ;
for(int i = 0, k = string.length() ; i
Posted by
erjan
Subscribe to:
Comments (Atom)