Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reduce or Increase looping depending on parameter input  RSS feed

 
Ben Coleman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an ArrayList<String> with a size of 1000. I am looping through that ArrayList and finding words and trying to form a square from them. For example as follows:

C A R D
A R E A
R E A R
D A R T

It is working but it involves a lot of nested looping and I am hard-coding the loops in this instance where I am try to form a square 4x4. So I am using 4 For Loops.

I am looking to have the flexibility to pass in the square size for example 5x5 or 6x6 and so on which will require 5 or 6 For Loops respectively. How can I go about doing that.

If this was just a single loop, I would have just passed in an Integer and looped according to that Integer. But since I am nesting, the number of For loops needed is going to differ thus confused.

When I am forming a square of 4x4, I end up looping 4 times where each loops checks different substrings to derive a correct square. When 5x5 is needed, I would need 5 loops to make the checks. I mean to solve the issue where I increase decrease loops depending on the parameter/ or a way to loop without creating so many For Loops. Can I get some guidance on this please.

 
Campbell Ritchie
Marshal
Posts: 55734
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to the that finding words to make the magic square is much harder than setting the length of a loop. I presume you are using a common or garden for loop. In which case none of the numbers needs to be fixed in advance. You can easily use a formula for the values in the initialisation part of the loop and the continuation part. You can of course use keyboard input to set up those two formulae.
for (int i = formula1; i < formula2; i++) ...
Beware:-
  • The second formula has to be larger than the first, otherwise the loop will never start.
  • If the first formula is negative or the second formula greater than the size of your List, you will go outwith the bounds of the List and not be able to access those non-existent elements.
  •  
    Campbell Ritchie
    Marshal
    Posts: 55734
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are you sure that the number of inner loops differs with the size of words chosen? If your outer loop runs 5×, your inner loops will run 5× too, or will look for 5‑character substrings. You shouldn't need more loops than for 4 characters, surely? Only different repetitions of the same loops.
     
    Piet Souris
    Rancher
    Posts: 1980
    67
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why do you need all those loops?

    Say, you have partitioned your wordlist into a HashMap, with key the first one (or two, three, ...) letters,
    then when you start with your initial word, your second word must start with the second letter of the first word (or start with the second and third letters of the first word), until you either get stuck or you have formed the number of words equal to the number of letters of your initial word. Or am I understanding something wrong?
     
    Campbell Ritchie
    Marshal
    Posts: 55734
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Piet Souris wrote:. . . Or am I understanding something wrong?
    You are probably correct; I didn't think about algorithms to fill the square.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!