Programming Theory

Programming theory deals with the design, implementation, analysis, characterization, and classification of programming languages and their individual features, along with introductory programming concepts like program structure, variable declaration and conditional and looping constructs. Sub-disciplines include the formal semantics of programming languages, type theory, program analysis and transformation, comparative programming language analysis, metaprogramming, domain-specific languages, compiler construction and run-time systems.

Share tech news, updates, or what's on your mind.

Sign up to Post

Hi,

I am working on below challenge

http://codingbat.com/prob/p123837
I wrote my code as below
public int scoresAverage(int[] scores) {
  int result=0;
  int len=scores.length;
  int lenHalf=len/2;
  int sumEven=0;
    int sumOdd=0;
for(int i=0;i<lenHalf;i++){
    sumEven=(scores[i]+scores[i+1])/2;
    //result=sumeEven;
    //return result;
  }
  
  for(int i=lenHalf;i<len;i++){
    sumOdd=(scores[i]+scores[i+1])/2;
   // result=sumeOdd;
    //return result;
  }
  if(sumOdd>sumEven){
    return sumOdd;
  }
  else{
    return sumEven;
  }


}
return result;
  

}

}

Open in new window





I am not passing all tests with below error

Compile problems:


Error:      }
      ^
Syntax error on token "}", { expected after this token


How to improve my design, approach, code? please advise
0
Hi,

I am working on below challenge

http://codingbat.com/prob/p179487

Pseudo Code:
1. loop through given array
2. find elemt a i position and i+1 position.
3. check their equality.
4. if equal return true
5. if not equal reurn false

I wrote my code as below
public boolean scores100(int[] scores) {
  
  boolean result=true;
  int len=scores.length;
for(int i=0;i<len-1;i++){
  if(scores[i]!=100&&scores[i+1]!=100){
   // i=i+1;
    result=false;
    
    return result;
  }
}
return result;
  

}

Open in new window



Expected      Run            
scores100([1, 100, 100]) → true      true      OK      
scores100([1, 100, 99, 100]) → false      true      X      
scores100([100, 1, 100, 100]) → true      true      OK      
scores100([100, 1, 100, 1]) → false      true      X      
scores100([1, 2, 3, 4, 5]) → false      false      OK      
scores100([1, 2, 100, 4, 5]) → false      false      OK      
other tests
OK      

I am not passing all tests

How to improve my psedo code, approach, code? please advise
0
Hi,

I am working on below challenge.
http://codingbat.com/prob/p125819

My psedo code of approach is
1. iterate given arry
2. find position of 4
3. set next position to 5 by keeping orignal value in buffere
4. create other for loop from  index position where we found 4 plus two and if that value is 5 replace with buffere
5. return modified array

public int[] fix45(int[] nums) {
  

	    for (int z = 0; z < nums.length; z++)
	        if (nums[z] == 4) {
	            int buffer = nums[z + 1];
	            nums[z + 1] = 5;
	            for (int j = z + 2; j < nums.length; j++)
	                if (nums[j] == 5) 
	                	nums[j] = buffer;
	        }
	    return nums;
	

}

Open in new window


i am failing below tests. please advise
Expected      Run            
fix45([5, 4, 9, 4, 9, 5]) → [9, 4, 5, 4, 5, 9]      [5, 4, 5, 4, 5, 9]      X      
fix45([1, 4, 1, 5]) → [1, 4, 5, 1]      [1, 4, 5, 1]      OK      
fix45([1, 4, 1, 5, 5, 4, 1]) → [1, 4, 5, 1, 1, 4, 5]      [1, 4, 5, 1, 1, 4, 5]      OK      
fix45([4, 9, 4, 9, 5, 5, 4, 9, 5]) → [4, 5, 4, 5, 9, 9, 4, 5, 9]      [4, 5, 4, 5, 9, 9, 4, 5, 9]      OK      
fix45([5, 5, 4, 1, 4, 1]) → [1, 1, 4, 5, 4, 5]      [5, 5, 4, 5, 4, 5]      X      
fix45([4, 2, 2, 5]) → [4, 5, 2, 2]      [4, 5, 2, 2]      OK      
fix45([4, 2, 4, 2, 5, 5]) → [4, 5, 4, 5, 2, 2]      [4, 5, 4, 5, 2, 2]      OK      
fix45([4, 2, 4, 5, 5]) → [4, 5, 4, 5, 2]      [4, 5, 4, 5, 2]      OK      
fix45([1, 1, 1]) → [1, 1, 1]      [1, 1, 1]      OK      
fix45([4, 5]) → [4, 5]      [4, 5]      OK      
fix45([5, 4, 1]) → [1, 4, 5]      [5, 4, 5]      X      
fix45([]) → []      []      OK      
fix45([5, 4, 5, 4, 1]) → [1, 4, 5, 4, 5]      [5, 4, 5, 4, 5]      X      
fix45([4, 5, 4, 1, 5]) → [4, 5, 4, 5, 1]      [4, 5, 4, 5, 1]      OK      
fix45([3, 4, 5]) → [3, 4, 5]      [3, 4, 5]      OK      
fix45([4, 1, 5]) → [4, 5, 1]      [4, 5, 1]      OK      
fix45([5, 4, 1]) → [1, 4, 5]      [5, 4, 5]      X      
fix45([2, 4, 2, 5]) → [2, 4, 5, 2]      [2, 4, 5, 2]      OK      
other tests
0
Hi,

I would like to know what are differences between factory design pattern vs abstract factoy design pattern.

When to use which one. Advantages, disadvantages, practical uses of each. Please advise
0
Hi,

I created below Person class with 3 constructors. One with all the fields second one with top 3 fields and third one with bottom three fields.

I am getting below error. can you please advise how to resolve it.

"Duplicate method Person(String, String, String) in type Person"
person.png
0
I love the sound of using automation to fix what can get sticky and ugly as an project grows.

Please give me a quick idea how it works, and what some benefits are of the leading CSS pre-compilers.

Thanks
0
Hi,

When we do upcasting down casting and object instance why actual object still remains same but referenece changes? please advise
0
Hi,

How static method overriding vs normal non static method overriding in child class are different?. child Static method overriding hides parent class static method right which is same with non static methods also right? When we use each one. Please advise

public class Parent {
    public static void test() {
        System.out.println("test from Parent class");
    }
}

public class Child extends Parent {
    public static void test() {
        System.out.println("test from Child");
    }
}


now non static case as below

public class Parent {
    public void test() {//non static
        System.out.println("test from Parent class of nonstatic");
    }
}

public class Child extends Parent {
    public  void test() {//non static
        System.out.println("test from Child of nonstatic");
    }
}
0
Hi,

When do we need to declare method argument as final say as below

void addPerson(final Employee theEmployee) {
......
.....

}

please advise
0
Hi,

I am working on below challenge

http://codingbat.com/prob/p193817


Say that a "clump" in an array is a series of 2 or more adjacent elements of the same value. Return the number of clumps in the given array.

countClumps([1, 2, 2, 3, 4, 4]) → 2
countClumps([1, 1, 2, 1, 1]) → 2
countClumps([1, 1, 1, 1, 1]) → 1

i am not clear on the description of the challenge. please advise
0
Hi,

I am working on below challenge

http://codingbat.com/prob/p196409

Array-3 > maxMirror
prev  |  next  |  chance
We'll say that a "mirror" section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array.

maxMirror([1, 2, 3, 8, 9, 3, 2, 1]) → 3
maxMirror([1, 2, 1, 4]) → 3
maxMirror([7, 1, 2, 9, 7, 2, 1]) → 2

i have not completely understood the description.

How below is 3

maxMirror([1, 2, 1, 4]) → 3
0
Hi,

I am working on below challenge

http://codingbat.com/prob/p104090

My psuedo code of the logical approach is
1. create array of given number
2. loop though each element to set each value with given pattern(not sure how to get other previous patterns?)
3. return the modified array




I wrote my code as below
public int[] seriesUp(int n) {
  int[] nums=new int[n];
  for(int i=0;i<nums.length;i++){
    nums[i]=i;
  }
  return nums;
}

Open in new window






I am not passing all tests



How to improve my design, approach, code? please advise
0
Hi,

I am working on below challenge
http://codingbat.com/prob/p155405

Array-3 > squareUp
prev  |  next  |  chance
Given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1,    0, 2, 1,    3, 2, 1} (spaces added to show the 3 groups).

squareUp(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1]
squareUp(2) → [0, 1, 2, 1]
squareUp(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
0
Hi,

I am working on below challenge

Array-3 > linearIn
prev  |  next  |  chance
Given two arrays of ints sorted in increasing order, outer and inner, return true if all of the numbers in inner appear in outer. The best solution makes only a single "linear" pass of both arrays, taking advantage of the fact that both arrays are already in sorted order.

linearIn([1, 2, 4, 6], [2, 4]) → true
linearIn([1, 2, 4, 6], [2, 3, 4]) → false
linearIn([1, 2, 4, 4, 6], [2, 4]) → true

how to verify one array has one other target array or not? is there is built in method for that?
please advise
0
Hi,

I am running my code as below
public class Child extends Parent{
    public Child(int i) {
		// TODO Auto-generated constructor stub
    	System.out.println("in Child constructor");
	}

	public static void main(String[] args) {
		Child ch = new Child(4);
        ch.print();
    }
}

Open in new window


public class Parent{
    protected int number = 9;

    public Parent() {
    	System.out.println("in Parent no arg construcot");
        this.number = 11;
    }

    public Parent(int number) {
    	System.out.println("in Parent arg construcot");
        this.number = number;
    }

    protected void print() {
        System.out.print(number);
    }
}

Open in new window


when i run child class as java application i got below output
in Parent no arg construcot
in Child constructor
11


My question is since i am calling arument construcor of child as below
Child ch = new Child(4);

Am i not supposed to be called parent arg constructor (rather it is calling parent no argument constructor) . please advise.

i expected instead below output


in Parent no arg construcot
in Parent arg construcot
in Child constructor
11
0
Hi,

I am working on below challenge

http://codingbat.com/prob/p199484

Psedo code description of approach :
1. create new array of given array size
2. loop given array in for loop
3. check each element value not greater than 10
4. if yes multiply by 10
5 if no give same value from given array to new array

I wrote my code as below

public int[] tenRun(int[] nums) {
  
  
  int len =nums.length;
  int[] arr=new int[len];
  for(int i=0;i<len;i++){
    if(nums[i]<10){
    arr[i]=nums[i]*10;
    }
    else{
        arr[i]=nums[i];
    }
  }
  return arr;
}

Open in new window





I am  not passing all tests
Expected      Run            
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]      [20, 10, 30, 40, 20, 50]      X      
tenRun([10, 1, 20, 2]) → [10, 10, 20, 20]      [10, 10, 20, 20]      OK      
tenRun([10, 1, 9, 20]) → [10, 10, 10, 20]      [10, 10, 90, 20]      X      
tenRun([1, 2, 50, 1]) → [1, 2, 50, 50]      [10, 20, 50, 10]      X      
tenRun([1, 20, 50, 1]) → [1, 20, 50, 50]      [10, 20, 50, 10]      X      
tenRun([10, 10]) → [10, 10]      [10, 10]      OK      
tenRun([10, 2]) → [10, 10]      [10, 20]      X      
tenRun([0, 2]) → [0, 0]      [0, 20]      X      
tenRun([1, 2]) → [1, 2]      [10, 20]      X      
tenRun([1]) → [1]      [10]      X      
tenRun([]) → []      []      OK      
other tests
X      
How to improve my design, approach, code? please advise
0
Hi,

I am working on below challenge
http://codingbat.com/prob/p142539

Psedo code description of approach :
1. find length of new array by subtracting end-start
2. create new array with above lenght
3. assign each element 0f new array using numbers between start(inclusive) and end(exclusive)

I wrote my code as below

public int[] fizzArray3(int start, int end) {
  int len=end-start;
  int[] arr=new int[len];
  for(int i=0;i<len;i++){
    arr[i]=start+i;
    
  }
  return arr;
}

Open in new window




I am passing all tests
Expected      Run            
fizzArray3(5, 10) → [5, 6, 7, 8, 9]      [5, 6, 7, 8, 9]      OK      
fizzArray3(11, 18) → [11, 12, 13, 14, 15, 16, 17]      [11, 12, 13, 14, 15, 16, 17]      OK      
fizzArray3(1, 3) → [1, 2]      [1, 2]      OK      
fizzArray3(1, 2) → [1]      [1]      OK      
fizzArray3(1, 1) → []      []      OK      
fizzArray3(1000, 1005) → [1000, 1001, 1002, 1003, 1004]      [1000, 1001, 1002, 1003, 1004]      OK      
other tests
OK

How to improve my design, approach, code? please advise
0
Hi,

I am trying below challenge

http://codingbat.com/prob/p134300

i read description as below
Array-2 > sameEnds
prev  |  next  |  chance
Return true if the group of N numbers at the start and end of the array are the same. For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3. You may assume that n is in the range 0..nums.length inclusive.

sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false
Go...Save, Compile, Run



public boolean sameEnds(int[] nums, int len) {
  return true;
}
Go
Shorter output
Expected      Run            
sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → false      true      X      
sameEnds([5, 6, 45, 99, 13, 5, 6], 2) → true      true      OK      
sameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 1) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 2) → false      true      X      
sameEnds([1, 2, 5, 2, 1], 0) → true      true      OK      
sameEnds([1, 2, 5, 2, 1], 5) → true      true      OK      
sameEnds([1, 1, 1], 0) → true      true      OK      
sameEnds([1, 1, 1], 1) → true      true      OK      
sameEnds([1, 1, 1], 2) → true      true      OK      
sameEnds([1, 1, 1], 3) → true      true      OK      
sameEnds([1], 1) → true      true      OK      
sameEnds([], 0) → true      true      OK      
sameEnds([4, 2, 4, 5], 1) → false      true      X      
other tests
X      


i have not understood the description of the challenge clearly. Please advise how ends are same and what is n there?
0
Hi,

I am working on below challenge

http://codingbat.com/prob/p102145

Psedo code description of approach :
1. Loop throguh given array
2. check if array element 2 with adjacent one also 2
3. if yes return true
4. if no return false

I wrote my code as below
public boolean twoTwo(int[] nums) {
  boolean result=false;
  for(int i=0;i<nums.length-1;i++){
    if((nums[i]==nums[i+1])||(nums[i]==nums[i-1])){
      result=true;
      
    }
    else{
      result=false;
    }
  }
  return result;
 
}

Open in new window




I am not passing all tests as below and failing edge cases
Expected      Run            
twoTwo([4, 2, 2, 3]) → true      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:4)      X      
twoTwo([2, 2, 4]) → true      true      OK      
twoTwo([2, 2, 4, 2]) → false      false      OK      
twoTwo([1, 3, 4]) → true      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:4)      X      
twoTwo([1, 2, 2, 3, 4]) → true      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:4)      X      
twoTwo([1, 2, 3, 4]) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:4)      X      
twoTwo([2, 2]) → true      true      OK      
twoTwo([2, 2, 7]) → true      true      OK      
twoTwo([2, 2, 7, 2, 1]) → false      false      OK      
twoTwo([4, 2, 2, 2]) → true      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:4)      X      
twoTwo([2, 2, 2]) → true      true      OK      
twoTwo([1, 2]) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:4)      X      
twoTwo([2]) → false      false      OK      
twoTwo([1]) → true      false      X      
twoTwo([]) → true      false      X      
twoTwo([5, 2, 2, 3]) → true      Exception:java.lang.ArrayIndexOutOfBoundsException: -1 (line number:4)      X      
twoTwo([2, 2, 5, 2]) → false      false      OK      
other tests
X      
Your progress graph for this problem
0
Say I have an array of unix timestamps, and I want to draw a graph showing time across the X axis, and frequency on the Y axis, I need to convert that 1D array into a 2D array (time-group-mid-point, frequency)

[for ease of reading, I've used standard time, but in reality, this will be unix timestamps)

so for example:
11:25:00
12:20:00
12:29:00
12:45:00
13:00:00
13:15:00

and if I want the groups to be 60 minutes, but offset by 30 minutes (halfthe interval, each way)
would become:
10:30 to 11:30 (11:00) = 1
11:30 to 12:30  (12:00) = 2
12:30 to 13:30 (13:00) = 3
13:30 to 14:30 (14:00) = 0

Can anyone suggest a function to do this, or even the correct mathematical terminology so I can research it further.

Thanks.
0
I am working on below challenge

http://codingbat.com/prob/p159979

Psedo code description of approach :
1. Loop throguh given array
2. check if array adjacent three elements are even or odd
3. if yes return true
4. if no return false

I wrote my code as below

public boolean modThree(int[] nums) {
  boolean result=false;
  
  for(int i=0;i<nums.length-2;i++){
    if(nums[i]%2==0&&nums[i+1]%2==0&&nums[i+2]%2==0){
      result=true;
      return result;
    }
    
    else if(nums[i+1]%2==0&&nums[i+2]%2==0&&nums[i+3]%2==0){
      result=true;
      return result;
    }
    
     else if(nums[i]%2==1&&nums[i+1]%2==1&&nums[i+2]%2==1){
      result=true;
      return result;
    }
    
    else if(nums[i+1]%2==1&&nums[i+2]%2==1&&nums[i+3]%2==1){
      result=true;
      return result;
    }
    
    
    else{
      result=false;
    }
    //return result;
  }
  return result;
}

Open in new window




I am failing couple of  tests
Expected      Run            
modThree([2, 1, 3, 5]) → true      true      OK      
modThree([2, 1, 2, 5]) → false      false      OK      
modThree([2, 4, 2, 5]) → true      true      OK      
modThree([1, 2, 1, 2, 1]) → false      false      OK      
modThree([9, 9, 9]) → true      true      OK      
modThree([1, 2, 1]) → false      false      OK      
modThree([1, 2]) → false      false      OK      
modThree([1]) → false      false      OK      
modThree([]) → false      false      OK      
modThree([9, 7, 2, 9]) → false      false      OK      
modThree([9, 7, 2, 9, 2, 2]) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: 6 (line number:10)      X      
modThree([9, 7, 2, 9, 2, 2, 6]) → true      true      OK
How to improve my design, approach, code? please advise
0
Hi,

I am trying below code from link
http://www.tutorialspoint.com/java/lang/object_notify.htm
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class ObjectDemo extends Object {

	private List synchedList;

	public ObjectDemo() {
		// create a new synchronized list to be used
		synchedList = Collections.synchronizedList(new LinkedList());
	}

	// method used to remove an element from the list
	public String removeElement() throws InterruptedException {
		synchronized (synchedList) {

			// while the list is empty, wait
			while (synchedList.isEmpty()) {
				System.out.println("List is empty...");
				synchedList.wait();
				System.out.println("Waiting...");
			}
			String element = (String) synchedList.remove(0);

			return element;
		}
	}

	// method to add an element in the list
	public void addElement(String element) {
		System.out.println("Opening...");
		synchronized (synchedList) {

			// add an element and notify all that an element exists
			synchedList.add(element);
			System.out.println("New Element:'" + element + "'");

			synchedList.notifyAll();
			System.out.println("notifyAll called!");
		}
		System.out.println("Closing...");
	}

	public static void main(String[] args) {
		final ObjectDemo demo = new ObjectDemo();

		Runnable runA = new Runnable() {

			public void run() {
				try {
					String item = demo.removeElement();
					System.out.println("" + item);
				} catch (InterruptedException ix) {
					

Open in new window

0
Hi,

Does constructor called while serializing object? please advise
0
hi,
can finally block executed even when there is a return statement inside try? please advise
0
Hi,
I wonder why Map interface has equals() and hashCode() methods again in Entry  method as they are already there before?

[i]boolean
equals(Object
paramObject);
int
hashCode();[/i]
public
static
abstract
interface
[b]Entry[/b]<K,
V>
{
K
getKey();
V
getValue();
V
setValue(V
paramV);
boolean
equals(Object
paramObject);
int
hashCode();
}

Open in new window


please advise
0

Programming Theory

Programming theory deals with the design, implementation, analysis, characterization, and classification of programming languages and their individual features, along with introductory programming concepts like program structure, variable declaration and conditional and looping constructs. Sub-disciplines include the formal semantics of programming languages, type theory, program analysis and transformation, comparative programming language analysis, metaprogramming, domain-specific languages, compiler construction and run-time systems.