Perfect Numbers
GoogleOnsiteMedium
A positive integer is perfect when its digits are pairwise distinct, none of its digits is 0, and no digit that has a neighbour on each side is smaller than both of them. For example, 196, 23 and 12463 are perfect, while 1546 (the 4 is below both neighbours), 320 (it contains a 0) and 34321 (the 3 repeats) are not.
Implement the class PerfectNumbers.
Part 1: Is it perfect
Implement is_perfect(x) that returns True when x is perfect and False otherwise.
- Example 1
- init
[]- operations
[["is_perfect",[196]],["is_perfect",[23]],["is_perfect",[12463]],["is_perfect",[1546]],["is_perfect",[320]],["is_perfect",[34321]],["is_perfect",[7]]]- Output
[true,true,true,false,false,false,true]
Why:
196has distinct nonzero digits and its middle digit9is not below both neighbours.1546fails because4is below both5and6,320contains a0, and34321repeats3. Single-digit7has no digit with two neighbours, so it is perfect.
Constraints
1 <= x <= 10^18
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.