Using the Guess Number Game Idea to showcase Algorithm
After watching 10 minutes of an algorithm course (5hrs plus). I thought to myself I have a project. The premise was what was better: Counting up or the halving technique at getting to a random number from a possible range. (1 - 100) in this case.
So why not write the code myself from scratch. Using Python.
I like to split code into a module for each task and in this case, it is easy. main.py - The controller linear.py - Linear technique guessing halver.py - Halfing Technique guessing
Main.py The only requirement is random, although I do use os.system to clear the terminal for a cleaner look at the end result.
Library random: Modules: seed - Used to control the values in testing randint - Use to ensure integers were used
Functions:
Linear: A simply counting up from first value until reaching the target value function.
Halver:
An initial guess is half-way between the bottom and top of the range.
The created function finds if the target value with methods of lower or greater than this guess.
The second and further guesses would be half that of the previous guess, depending on if the value were greater or lower than the previous guess
If the initial sequence of guess were all greater than or lower than to reach the target value all was good,
The problem was when the second and further guesses required the opposite method to the previous method.
Next Step: Creating an active list of guesses. To ensure the guesses cannot go out of the scope of the method. (See guesses list and its use) The list wouldn't be empty at the first time it would be requested preventing an error, but the management of how the list was used was required for the first couple of guesses.
Temp Step: Two temp files ( in archive) were used to work on each method individually. This did require a little bit more work related to the use of the list but I found it a great stepping stone to get the basic functionality working for greater than and lower than.
Incorporate into Halver.py
This was where it was getting tricky. The temp method file only went as far as running enough steps to ensure that method worked. This time is was getting the list to function as expected from either of the two methods.
This proved tricky. Issues I found were: the differential would add/subtract alternately rather than per the requirement of the guess getting closer to the target value.
If the target value was less than the second guess (if the target value were lower than the initial guess) or more than the second guess( if the target value were higher than the initial guess) Thee guesses would lock to the second guess until the number od tries ran out.
The only other points of concern were ensuring values were integers and absolutes and let the method handle this in a controlled manner.
Finally, I got working and the last part was to ensure that the finished code gave a nice console log.
I have left all of the workings and remarks in were possible to help, me in the future remember, and anyone that wants to play about with it.
Code is on my gitHub