Advertisement
Chockrit

Malleo NiceNumbers

Apr 29th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. def nice_number(n):
  2.     # Local program state to help us determine what to check while looping through each digit
  3.     # In a nice number, you begin by counting up continuously (or not at all)
  4.     # Then at some point we begin counting down continuously (or not at all)
  5.     countUp = true
  6.    
  7.     # Convert n to a string to make array accesses easier/cleaner, as str[i] accesses the ith char in the string
  8.     nStr = str(n)
  9.    
  10.     #Array accesses are to n[x] and n[x+1]. This range prevents out-of-range accesses.
  11.     for x in range(0, len(n) - 2):     
  12.         if countUp = true:
  13.             #Number counts up at current digit
  14.             if int(nStr[x+1]) == int(nStr[x]) + 1:
  15.                 continue
  16.             #Number counts down at current digit
  17.             else if int(nStr[x+1]) == int(nStr[x]) - 1:
  18.                 countUp = false
  19.                 continue
  20.             #If this digit doesn't count up nor down, then n is not Nice.
  21.             else:
  22.                 return false
  23.                
  24.         else:
  25.             #Since countUp == false, we only have to check if current digit counts down.
  26.             if int(nStr[x+1]) == int(nStr[x]) - 1:
  27.                 continue
  28.             #If this digit doesn't count down, then n is not Nice.
  29.             else:
  30.                 return false
  31.     #We made it through the whole loop, therefore n is Nice.
  32.     return true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement