Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class Ex14_Tester here.
- *
- * @author (dror)
- * @version (a version number or a date)
- */
- /*import static org.junit.Assert.*;
- import static org.hamcrest.CoreMatchers.*;
- import java.lang.reflect.Field;
- import java.time.LocalDate;*/
- import java.util.Random;
- /*import java.util.function.Function;
- import org.junit.Test;
- import org.junit.Assert;*/
- public class Ex14_Tester_Q4
- {
- private static boolean matchWithLoop (int[] a, int[] p)
- {
- int a_len = a.length;
- int p_len = p.length;
- boolean found_match = false;
- //no p --> return true
- if(p == null){
- return true;
- }
- //a < p --> return false
- else if(a.length < p.length){
- return false;
- }
- //Iterate a[] with pattern
- for(int i=0; i< a_len - p_len + 1; i++){
- if(found_match == true){
- return true;
- }
- else{//set to true for the inner loop
- found_match = true;
- }
- for (int j=0; (j< p_len)&&found_match; j++){
- int reminder = a[i+j]/10;
- reminder = (reminder < 0) ? -1*reminder : reminder;
- if(reminder >= 10){//three digits or more
- found_match=false;
- }
- else if(reminder >= 1){ // two digits
- if(p[j]==2 || p[j]==0){
- found_match = true;
- }
- else{ //p[j] == 1
- found_match = false;
- }
- }
- else if(reminder < 1){ //single digit
- if(p[j]==1 || p[j]==0){
- found_match = true;
- }
- else{//p[j] == 2
- found_match = false;
- }
- }
- }
- }
- return found_match; //for the case that a.len == p.len
- }
- public static void main( String args[] ){
- final int LOOP_Q1 = 100;
- boolean printouts = true;
- int i;
- for (i = 0;i< LOOP_Q1;i++)
- {
- Random rand = new Random();
- int[] a = new int[rand.nextInt(50)];//define a[] length up to 200 elements
- int[] p = new int[rand.nextInt(10)];//define p[] length up to 20 elements
- for (int t = 0; t < a.length; t++)
- {
- a[t] = rand.nextInt(220) - 100; //set random numbers for a[]: -100 to 120
- }
- for(int s = 0; s < p.length; s++)
- {
- p[s] = rand.nextInt(3); //set random values: 0,1,2
- }
- boolean result = Ex14.match(a,p);
- boolean expected = matchWithLoop(a,p);
- if(printouts){
- System.out.printf("%d) [", i);
- for (int t = 0; t < a.length; t++)
- {
- System.out.printf("%d ",a[t]);
- }
- System.out.printf("]\n");
- System.out.printf(" %d) [", i);
- for (int s = 0; s < p.length; s++)
- {
- System.out.printf("%d ",p[s]);
- }
- System.out.printf("]\n");
- System.out.printf(" Passed: expected: %b, actual: %b\n\n ",expected,result);
- }
- if(expected != result){
- System.out.println("failed: expected: "+ expected + " Actual: "+ result );
- System.out.println();
- System.exit(1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement