Cyclops numbers
Tags: Algorithms Math Number Theory Pattern Matching
Credit: Ilkka Kokkarinen
Difficulty: Easy
Type: Text
Category: Practice
Time Allowed: 15 s
Description:
A nonnegative integer is said to be a cyclops number if it consists of an odd number of digits so that the middle (more poetically, the “eye”) digit is a zero, and all the other digits of that number are non zero. This function should determine whether its parameter integer n is a cyclops number, and return either True or False accordingly.
As an extra challenge, you can try to solve this problem using only loops, conditions and integer arithmetic operations, without first converting the integer into a string and working from there. Dividing any integer by 10 using the integer division // effectively chops off its last digit, whereas extracting the remainder of dividing by 10 using the operator % will extract that last digit. These operations allow us to iterate through the digits of an integer one at the time from lowest to highest, almost as if that integer really were some kind of sequence of digits.
Even better, this operation doesn’t work merely for the familiar base ten, but it works for any base by using that base as the divisor. Especially using two as the divisor instead of ten allows you to iterate through the bits of the binary representation of any integer, which will come handy in problems in your later courses that expect you to be able to manipulate these individual bits. (In practice these division and remainder operations are often further condensed into equivalent but faster bit shifts and bitwise and operations.)
Test Case | Input | Expected Output |
---|---|---|
1 | 0 | True |
2 | 101 | True |
3 | 98053 | True |
4 | 777888999 | False |
5 | 1056 | False |
6 | 675409820 | False |
Leave a Comment
Please login to leave a comment.