Here’s a fun little puzzle for you, which lends itself well to a Python coding solution.
Find a two-digit positive integer that becomes 7 times smaller when its leftmost digit is removed.
Have a go at the challenge yourself before reading further.
7 Puzzle in Python – Solution 1
An initial approach is to use brute force to check all possible candidate numbers. We can do this using Python’s range()
function. Since we want to check 2-digit numbers we can initially use a range of (10, 100)
, remembering that 100
for the second argument will only count up to 99
, as required.
But how to remove the first digit? Well one option is to use use modulo operator %
. This gives the remainder on division, and if we divde by 10
we get the second digit. You can read more about the modulo operator here if you are not familiar with it.
This all leads us to the following first solution:
for i in range(10, 100):
if 7 * (i % 10) == i:
print(i)
To learn more about brute-force algorithms, check out my article: ALGORITHMIC THINKING WITH PYTHON PART 1 – BRUTE FORCE ALGORITHMS.
7 Puzzle in Python – Solution 2
Another solution for removing the first digit of a 2-digit number in Python is to convert it to a string and use slicing. If you want to practice type casting and string slicing, have a go at this solution for yourself before looking at the code below.
for n in range(10, 100):
if n == 7 * int(str(n)[1:]):
print(n)
7 Puzzle in Python – Solution 3
Not much new here, but a little thought will reveal that we don’t need to test numbers above 69
, as dividing the required integer by 7 should result in a single digit integer. This would not be the case for 70
and above, so we can modify our range to make a small optimisation.
for i in range(10, 70):
if 7 * (i % 10) == i:
print(i)
7 Puzzle in Python – Solution 4
Another solution to the puzzle is to abandon brute force and instead to reason carefully about the problem. We are looking for a number which when divided by 7 consists of a single digit which is 7 times smaller than the original. So the original number must be a multiple of 7. Knowing this we can examine all the two-digit multiples of 7:
14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98
and see if one meets our criteria. This is in fact a combination of brute-force and pure reasoning. We drastically narrowed the field of potential candidates by thinking about what properties they must have, and then checked a much smaller list of candidates.
Want to improve your algorithmic thinking skills?
Check out my popular course on LinkedIN Learning – Algorithmic Thinking with Python: Foundations
Please note this is a sponsored link, where I receive a commission for sales at no extra cost to yourself.
We have now explored a few different ways to approach the 7 Puzzle. It could be said that the brute force approaches we used are perhaps a little lazy in terms of applying our problem-solving muscle. However, brute force solutions do have their advantages, even if we end up using a different approach. They can help provide insight into a problem as well as allowing us to check other solutions, assuming they can execute in a reasonable time-frame.
This article has explored a puzzle about numbers using Python programming and a brute-force approach. I hope you found it interesting.
Happy computing!