• Post Reply Bookmark Topic Watch Topic
  • New Topic

Determine logical pattern for loop  RSS feed

 
bob fissle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write a loop that will print to the buffer based on the following conditions:




The example above is a dumbed down version of what I'm trying to do, I'm mainly interested in the logic. Please assume that variables a and b will have an infinite amount of numbers -- for the sake of this example. The main point that i'm trying to illustrate is the pattern in the variables a and b; a starts with 0 and 1, and then every other two numbers are found in the variable a, and the opposite for b. I'm trying to figure out how to write logic that will print out values a or b based on the pattern described above inside a loop.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob fissle wrote:I'm trying to write a loop that will print to the buffer based on the following conditions:




The example above is a dumbed down version of what I'm trying to do, I'm mainly interested in the logic. Please assume that variables a and b will have an infinite amount of numbers -- for the sake of this example. The main point that i'm trying to illustrate is the pattern in the variables a and b; a starts with 0 and 1, and then every other two numbers are found in the variable a, and the opposite for b. I'm trying to figure out how to write logic that will print out values a or b based on the pattern described above inside a loop.


OHH! Now I think I get it.

What you want is for a and b to be Collections, not ints. For instance, a could be a Set containing 0, 1, 4, 5, etc., and b could be a Set containing the other numbers. Then your loop body is just


Further, if every possible value of i will be in either a or b--that is, no skipped values--then you don't even need the b set.

 
bob fissle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion, I'll try to elaborate a bit more. Here's few examples of what I would want as output within the context of a loop...



I'm hoping the example above illustrates the output that I'm seeking more clearly. As far as logic goes, I'm trying to determine a way to replicate the above. However, I DON'T want to hardcode all possible values for variables a and b. Meaning, I don't want to create an Integer Array List for a and b and than check if the current value of i is in the Array List. I'm wondering if there's a slick way to replicate the pattern shown above. Is this possible?

I DON'T want to do this
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob fissle wrote:
I DON'T want to hardcode all possible values for variables a and b


So, is there a pattern, a set of rules, that determines which numbers are in set A and which are in set B? If so, write code that matches that pattern. If you need help with that, you'll have to spell out the rules clearly and precisely.

And if there's not some pattern, then just read the numbers from a file or user input or whatever is appropriate at runtime.

I it's clear that the only two possibilities are either rules to describe the pattern, or else providing the actual values explicitly. I hope it's also clear that this fact has nothing to do with Java and is simply a universal truth derived from common logic.
 
bob fissle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
bob fissle wrote:
So, is there a pattern, a set of rules, that determines which numbers are in set A and which are in set B? If so, write code that matches that pattern. If you need help with that, you'll have to spell out the rules



That's the main issue that I'm having, determining a way to code the "rule"/pattern.

Here's the pattern again:

a = 0,1,4,5,8,9,12,13,16,17 and son on..
b = 2,3,6,7,10,11,14,15,18,19 and so on..

a starts the pattern with 0 and 1 and includes other next set of consecutive numbers -- i hope that makes sense. Does this pattern make sense? If so, is there a way to place this pattern into code?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob fissle wrote:

That's the main issue that I'm having, determining a way to code the "rule"/pattern.


If you can't describe it clearly and precisely in English, then it's impossible to write code for it in any language.


Here's the pattern again:

a = 0,1,4,5,8,9,12,13,16,17 and son on..
b = 2,3,6,7,10,11,14,15,18,19 and so on..


I don't see any pattern, or any "and so on". I have no idea which set 20, 21, 22, 23 go into, or why.

a starts the pattern with 0 and 1 and includes other next set of consecutive numbers


It's clear that A starts with 0, 1 and includes the numbers that are listed consecutively after A, but that doesn't mean anything.


EDIT: Okay, now I see the pattern. I was just slow to pick up on it.

Yes, that's quite easy to code, but I'm going to leave it up to you to describe the pattern in English and/or algebraic terms first. It will be a good exercise.
 
Jon Swanson
Ranch Hand
Posts: 230
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really looking for the pattern 0,1,4,5,8,9...? or 2,3,6,7,...? One thought that occurs to me is that if you use i/2 you get 0,0,2,2,4,4... and 1,1,3,3,... and you can then test whether (i/2) % 2 is zero or not. Of course, that assumes the exact pattern that you showed.
 
bob fissle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Swanson wrote:Are you really looking for the pattern 0,1,4,5,8,9...? or 2,3,6,7,...? One thought that occurs to me is that if you use i/2 you get 0,0,2,2,4,4... and 1,1,3,3,... and you can then test whether (i/2) % 2 is zero or not. Of course, that assumes the exact pattern that you showed.


Perfect! Thanks Jon!!
 
Sebanti Sanyal
Ranch Hand
Posts: 58
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob fissle wrote:I'm trying to write a loop that will print to the buffer based on the following conditions:




The example above is a dumbed down version of what I'm trying to do, I'm mainly interested in the logic. Please assume that variables a and b will have an infinite amount of numbers -- for the sake of this example. The main point that i'm trying to illustrate is the pattern in the variables a and b; a starts with 0 and 1, and then every other two numbers are found in the variable a, and the opposite for b. I'm trying to figure out how to write logic that will print out values a or b based on the pattern described above inside a loop.


I believe there are four AP(Arithmetic Progression) series, stating from 0,1,2 and 3 respectively. The constant difference is 4 in each case.
Series 1: 0,4,8,12 .. prints a
Series 2: 1,5,9,13 ...prints a
Series 3: 2,6,10,14 ..prints b
Series 4: 3,7,11,15 .. prints b
Please check if the following code works.
 
Runrioter Wung
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebanti Sanyal wrote:
Please check if the following code works.


Sebanti, please remember that this site is NotACodeMill(⇐click), and, as it states clearly at the top of the topics page, We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers. Doing somebody's work for him is not helpful.

Thanks!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runrioter Wung wrote:
... some code...


Runrioter, please remember that this site is NotACodeMill(⇐click), and, as it states clearly at the top of the topics page, We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers. Doing somebody's work for him is not helpful.

Thanks!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for noticing, Jeff. I know it is a bit late in the day, but I have edited the code slightly. The hard part is seeing the pattern.

I challenge you to re-write your solution using the >> and & operators instead of / and %.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The hard part is seeing the pattern.


Actually, I think describing it in English is the hard part.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!