Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Last active July 1, 2021 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericnormand/fb8d0356a5ff7d898707012a97975ec7 to your computer and use it in GitHub Desktop.
Save ericnormand/fb8d0356a5ff7d898707012a97975ec7 to your computer and use it in GitHub Desktop.
430 PurelyFunctional.tv Newsletter

New Numbers

A number is new if its digits are not a permutation of a smaller number. For instance, 789 is a new number because its permutations (879, 798, 897, 978, and 987) are all larger than it is. However, 645 is not a new number since 456 and 465 are smaller than it.

Write a function that takes an integer and returns true if it is a new number and false otherwise.

Examples

(new-number? 789) ;=> true
(new-number? 645) ;=> false
(new-number? 444) ;=> true (it's permutations are not smaller than it)

Bonus: You may find a clever way to write new-number?. In addition to that implementation, implement it in such a way that the definition (no permutations are smaller) is clear from the code.

Thanks to this site for the problem idea, where it is rated Very Hard in Ruby. The problem has been modified.

Please submit your solutions as comments on this gist.

To subscribe: https://purelyfunctional.tv/newsletter/

@KubqoA
Copy link

KubqoA commented Jul 1, 2021

A bit late to the party, without handling the cases where there might be a leading 0:

(defn new-number? [x] (->> x str (map int) (apply <=)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment