Solved

Help with a relatively simple algorithm

Posted on 2004-08-10
9
224 Views
Last Modified: 2010-03-31
Hi,

I've been stuck trying to make this simple algorithm. I'm hoping someone out there can help me with this.

Basically I need to write a function:

/**
 * @param row 1-based row number
 * @param numOriginalRows number of original rows
 * @param numOutputRows number of output rows
 * @return the resolved row (1-based)
 */
public static int resolveRow(int row, int numOriginalRows, int numOutputRows) {
  // return the result here
}

(defintion: "rectangle", I just mean a bunch of rows and cols - like m x n array)

- numOriginalRows represents an original rectangle
- numOutputRows represents a rectangle that could possibly have more rows in it than numOriginalRows

but I need to access information associated with the original Rectangle. The "row" value is relative to the output rectangle. Therefore I cannot just do getOriginalRectangleRow(row). Instead I need to do getOriginalRectangleRow(resolveRow(row)).

hopefully this is enough information. I can't really explain it more without making it more confusing.

Here is my junit code. It shows the input values and the corresponding output values.

  public void testRowResolver() {

    // 4 Original Rows
   
    assertEquals(resolveRow(1,4,4), 1);    
    assertEquals(resolveRow(2,4,4), 2);
    assertEquals(resolveRow(3,4,4), 3);
    assertEquals(resolveRow(4,4,4), 4);    

    assertEquals(resolveRow(1,4,5), 1);    
    assertEquals(resolveRow(2,4,5), 2);
    assertEquals(resolveRow(3,4,5), 2);
    assertEquals(resolveRow(4,4,5), 3);
    assertEquals(resolveRow(5,4,5), 4);    

    assertEquals(resolveRow(1,4,6), 1);    
    assertEquals(resolveRow(2,4,6), 2);
    assertEquals(resolveRow(3,4,6), 2);
    assertEquals(resolveRow(4,4,6), 3);
    assertEquals(resolveRow(5,4,6), 3);
    assertEquals(resolveRow(6,4,6), 4);    
   
    assertEquals(resolveRow(1,4,7), 1);    
    assertEquals(resolveRow(2,4,7), 2);
    assertEquals(resolveRow(3,4,7), 2);
    assertEquals(resolveRow(4,4,7), 2);
    assertEquals(resolveRow(5,4,7), 3);
    assertEquals(resolveRow(6,4,7), 3);
    assertEquals(resolveRow(7,4,7), 4);    
   
    assertEquals(resolveRow(1,4,8), 1);    
    assertEquals(resolveRow(2,4,8), 2);
    assertEquals(resolveRow(3,4,8), 2);
    assertEquals(resolveRow(4,4,8), 2);
    assertEquals(resolveRow(5,4,8), 3);
    assertEquals(resolveRow(6,4,8), 3);
    assertEquals(resolveRow(7,4,8), 3);
    assertEquals(resolveRow(8,4,8), 4);        
   
    // 5 Original Rows
   
    assertEquals(resolveRow(1,5,5), 1);    
    assertEquals(resolveRow(2,5,5), 2);
    assertEquals(resolveRow(3,5,5), 3);
    assertEquals(resolveRow(4,5,5), 4);
    assertEquals(resolveRow(5,5,5), 5);    
   
    assertEquals(resolveRow(1,5,6), 1);    
    assertEquals(resolveRow(2,5,6), 2);
    assertEquals(resolveRow(3,5,6), 3);
    assertEquals(resolveRow(4,5,6), 3);
    assertEquals(resolveRow(5,5,6), 4);
    assertEquals(resolveRow(6,5,6), 5);    
   
    assertEquals(resolveRow(1,5,7), 1);    
    assertEquals(resolveRow(2,5,7), 2);
    assertEquals(resolveRow(3,5,7), 3);
    assertEquals(resolveRow(4,5,7), 3);
    assertEquals(resolveRow(5,5,7), 3);
    assertEquals(resolveRow(6,5,7), 4);
    assertEquals(resolveRow(7,5,7), 5);    
   
    assertEquals(resolveRow(1,5,8), 1);    
    assertEquals(resolveRow(2,5,8), 2);
    assertEquals(resolveRow(3,5,8), 3);
    assertEquals(resolveRow(4,5,8), 3);
    assertEquals(resolveRow(5,5,8), 3);
    assertEquals(resolveRow(6,5,8), 3);
    assertEquals(resolveRow(7,5,8), 4);
    assertEquals(resolveRow(8,5,8), 5);    
   
  }
0
Comment
Question by:ycomp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 13

Expert Comment

by:Webstorm
ID: 11768006
Hi ycomp,

public static int resolveRow(int row, int numOriginalRows, int numOutputRows)
{
    return 1+((numOriginalRows*(row-1))/numOutputRows);
}
0
 

Author Comment

by:ycomp
ID: 11768040
Hi Webstorm,

thanks for the quick response!

unfortunately it doesn't work in all cases:

(the commented lines are problem lines)

    // 4 Original Rows
   
    assertEquals(resolveRow(1,4,4), 1);    
    assertEquals(resolveRow(2,4,4), 2);
    assertEquals(resolveRow(3,4,4), 3);
    assertEquals(resolveRow(4,4,4), 4);    

    assertEquals(resolveRow(1,4,5), 1);    
//    assertEquals(resolveRow(2,4,5), 2);
    assertEquals(resolveRow(3,4,5), 2);
    assertEquals(resolveRow(4,4,5), 3);
    assertEquals(resolveRow(5,4,5), 4);    

    assertEquals(resolveRow(1,4,6), 1);    
//    assertEquals(resolveRow(2,4,6), 2);
    assertEquals(resolveRow(3,4,6), 2);
    assertEquals(resolveRow(4,4,6), 3);
    assertEquals(resolveRow(5,4,6), 3);
    assertEquals(resolveRow(6,4,6), 4);    
   
    assertEquals(resolveRow(1,4,7), 1);    
//    assertEquals(resolveRow(2,4,7), 2);
    assertEquals(resolveRow(3,4,7), 2);
    assertEquals(resolveRow(4,4,7), 2);
    assertEquals(resolveRow(5,4,7), 3);
    assertEquals(resolveRow(6,4,7), 3);
    assertEquals(resolveRow(7,4,7), 4);    
   
    assertEquals(resolveRow(1,4,8), 1);    
//    assertEquals(resolveRow(2,4,8), 2);
    assertEquals(resolveRow(3,4,8), 2);
    assertEquals(resolveRow(4,4,8), 2);
    assertEquals(resolveRow(5,4,8), 3);
    assertEquals(resolveRow(6,4,8), 3);
//    assertEquals(resolveRow(7,4,8), 3);
    assertEquals(resolveRow(8,4,8), 4);        
   
    // 5 Original Rows
   
    assertEquals(resolveRow(1,5,5), 1);    
    assertEquals(resolveRow(2,5,5), 2);
    assertEquals(resolveRow(3,5,5), 3);
    assertEquals(resolveRow(4,5,5), 4);
    assertEquals(resolveRow(5,5,5), 5);    
   
    assertEquals(resolveRow(1,5,6), 1);    
//    assertEquals(resolveRow(2,5,6), 2);
//    assertEquals(resolveRow(3,5,6), 3);
    assertEquals(resolveRow(4,5,6), 3);
    assertEquals(resolveRow(5,5,6), 4);
    assertEquals(resolveRow(6,5,6), 5);    
   
    assertEquals(resolveRow(1,5,7), 1);    
//    assertEquals(resolveRow(2,5,7), 2);
//    assertEquals(resolveRow(3,5,7), 3);
    assertEquals(resolveRow(4,5,7), 3);
    assertEquals(resolveRow(5,5,7), 3);
    assertEquals(resolveRow(6,5,7), 4);
    assertEquals(resolveRow(7,5,7), 5);    
   
    assertEquals(resolveRow(1,5,8), 1);    
//    assertEquals(resolveRow(2,5,8), 2);
//    assertEquals(resolveRow(3,5,8), 3);
//    assertEquals(resolveRow(4,5,8), 3);
    assertEquals(resolveRow(5,5,8), 3);
//    assertEquals(resolveRow(6,5,8), 3);
    assertEquals(resolveRow(7,5,8), 4);
    assertEquals(resolveRow(8,5,8), 5);    
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11768059
In fact, to get exactly what you need in your assertEquals statements:

public static int resolveRow(int row, int numOriginalRows, int numOutputRows)
{
    int dh=(numOriginalRows+1)/2;
    if (row<dh) return row;
    else if (row>(numOutputRows-dh)) return row+numOriginalRows-numOutputRows;
    else return dh;
}
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 92

Expert Comment

by:objects
ID: 11768079
So what does your current method look like?
0
 

Author Comment

by:ycomp
ID: 11768108
unfortunately that doesn't quite work either WebStorm. But the problems are limited to the 4 rows example:

    // 4 Original Rows
   
    assertEquals(resolveRow(1,4,4), 1);    
    assertEquals(resolveRow(2,4,4), 2);
    assertEquals(resolveRow(3,4,4), 3);
    assertEquals(resolveRow(4,4,4), 4);    

    assertEquals(resolveRow(1,4,5), 1);    
    assertEquals(resolveRow(2,4,5), 2);
    assertEquals(resolveRow(3,4,5), 2);
    assertEquals(resolveRow(4,4,5), 3);
    assertEquals(resolveRow(5,4,5), 4);    

    assertEquals(resolveRow(1,4,6), 1);    
    assertEquals(resolveRow(2,4,6), 2);
    assertEquals(resolveRow(3,4,6), 2);
//    assertEquals(resolveRow(4,4,6), 3);
    assertEquals(resolveRow(5,4,6), 3);
    assertEquals(resolveRow(6,4,6), 4);    
   
    assertEquals(resolveRow(1,4,7), 1);    
    assertEquals(resolveRow(2,4,7), 2);
    assertEquals(resolveRow(3,4,7), 2);
    assertEquals(resolveRow(4,4,7), 2);
//    assertEquals(resolveRow(5,4,7), 3);
    assertEquals(resolveRow(6,4,7), 3);
    assertEquals(resolveRow(7,4,7), 4);    
   
    assertEquals(resolveRow(1,4,8), 1);    
    assertEquals(resolveRow(2,4,8), 2);
    assertEquals(resolveRow(3,4,8), 2);
    assertEquals(resolveRow(4,4,8), 2);
//    assertEquals(resolveRow(5,4,8), 3);
//    assertEquals(resolveRow(6,4,8), 3);
    assertEquals(resolveRow(7,4,8), 3);
    assertEquals(resolveRow(8,4,8), 4);        
   
    // 5 Original Rows
   
    assertEquals(resolveRow(1,5,5), 1);    
    assertEquals(resolveRow(2,5,5), 2);
    assertEquals(resolveRow(3,5,5), 3);
    assertEquals(resolveRow(4,5,5), 4);
    assertEquals(resolveRow(5,5,5), 5);    
   
    assertEquals(resolveRow(1,5,6), 1);    
    assertEquals(resolveRow(2,5,6), 2);
    assertEquals(resolveRow(3,5,6), 3);
    assertEquals(resolveRow(4,5,6), 3);
    assertEquals(resolveRow(5,5,6), 4);
    assertEquals(resolveRow(6,5,6), 5);    
   
    assertEquals(resolveRow(1,5,7), 1);    
    assertEquals(resolveRow(2,5,7), 2);
    assertEquals(resolveRow(3,5,7), 3);
    assertEquals(resolveRow(4,5,7), 3);
    assertEquals(resolveRow(5,5,7), 3);
    assertEquals(resolveRow(6,5,7), 4);
    assertEquals(resolveRow(7,5,7), 5);    
   
    assertEquals(resolveRow(1,5,8), 1);    
    assertEquals(resolveRow(2,5,8), 2);
    assertEquals(resolveRow(3,5,8), 3);
    assertEquals(resolveRow(4,5,8), 3);
    assertEquals(resolveRow(5,5,8), 3);
    assertEquals(resolveRow(6,5,8), 3);
    assertEquals(resolveRow(7,5,8), 4);
    assertEquals(resolveRow(8,5,8), 5);    
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 500 total points
ID: 11768124
public static int resolveRow(int row, int numOriginalRows, int numOutputRows)
{
    int dh=(numOriginalRows+1)/2,
         th=(numOutputRows+1)/2;
    if (row<dh) return row;
    else if (row>(numOutputRows-dh)) return row+numOriginalRows-numOutputRows;
    else return (row<=th)?dh:numOriginalRows+1-dh;
}
0
 

Author Comment

by:ycomp
ID: 11768126
objects, my current method is blank. I tried to write something but couldn't figure it out. Basically what I need to do is to somehow divide the thing so that the rows that are closer to the top correlate to the original rows closer to the top and those closer to the bottom correlate to those original rows closer to the bottom.

But I can't really describe it very well, which is one reason why I can't write the code. However I do know that the junit code does describe what I need done much better than I can explain it.
0
 

Author Comment

by:ycomp
ID: 11768137
Webstorm, you are awesome... thanks! Saved me a lot of scratching my head. It works 100%.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11768148
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 63
ejb on wildfly 5 61
Is there a simpler dropbox system? 10 52
program arguments vs VM arguments 4 56
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question