From the course: Code Clinic: Ruby
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Loops with backtracking
- In the last movie, we took our first stab at this problem by using simple loops. We did okay for our first attempt. In the process we realized that we really needed to gain more control over the looping process. Ruby offers us a nice variation on loops. The conditional loop, in this case, while. So you can see in the example, I've got n equals zero, and then while n is less than eight, keep looping. So inside the loop, I'm going to do some code and some business, and then I'm going to increment n. N is going to now going to increase in value by one, that's what that plus equals means, it means take the existing value and increment it by this amount. So the first time through, n is equal to zero, then it's equal to one, two, three, four, five until finally n is equal to eight. And at that point the loop does not execute because now n is no longer less than eight so it just exits, it drops right down below the end and continues with any code that may be below that. The results of this…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Eight Queens problem1m 46s
-
Hints, tips, and tricks3m 54s
-
Overview of my solution5m 55s
-
Queen and board classes6m 10s
-
Displaying the board7m 1s
-
Determining safe positions6m 48s
-
Using loops7m 8s
-
Loops with backtracking5m 12s
-
Loops with smarter backtracking6m 2s
-
Recursion primer3m 45s
-
Using recursion6m 52s
-
Finding all possible solutions5m 46s
-
Further exercises3m 56s
-
-
-
-