Google questions

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.

  1. 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: 196 has distinct nonzero digits and its middle digit 9 is not below both neighbours. 1546 fails because 4 is below both 5 and 6, 320 contains a 0, and 34321 repeats 3. Single-digit 7 has 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.
Import a question