4 kyu

Next smaller number with the same digits

4,206 of 11,791LesRamer

Description:

Write a function that takes a positive integer and returns the next smaller positive integer containing the same digits.

For example:

nextSmaller(21) == 12
nextSmaller(531) == 513
nextSmaller(2071) == 2017
nextSmaller(21) == 12
nextSmaller(531) == 513
nextSmaller(2071) == 2017
nextSmaller(21) == 12
nextSmaller(531) == 513
nextSmaller(2071) == 2017
next_smaller(21) == 12
next_smaller(531) == 513
next_smaller(2071) == 2017
next_smaller(21) == 12
next_smaller(531) == 513
next_smaller(2071) == 2017
next_smaller(21) == Some(12)
next_smaller(531) == Some(513)
next_smaller(2071) == Some(2017)

Return -1 (for Haskell: return Nothing, for Rust: return None), when there is no smaller number that contains the same digits. Also return -1 when the next smaller number with the same digits would require the leading digit to be zero.

nextSmaller(9) == -1
nextSmaller(111) == -1
nextSmaller(135) == -1
nextSmaller(1027) == -1 // 0721 is out since we don't write numbers with leading zeros
nextSmaller(9) == -1
nextSmaller(111) == -1
nextSmaller(135) == -1
nextSmaller(1027) == -1 // 0721 is out since we don't write numbers with leading zeros
nextSmaller(9) == Nothing
nextSmaller(135) == Nothing
nextSmaller(1027) == Nothing -- 0721 is out since we don't write numbers with leading zeros
next_smaller(9) == -1
next_smaller(135) == -1
next_smaller(1027) == -1  # 0721 is out since we don't write numbers with leading zeros
next_smaller(9) == -1
next_smaller(135) == -1
next_smaller(1027) == -1  # 0721 is out since we don't write numbers with leading zeros
next_smaller(9) == None
next_smaller(135) == None
next_smaller(1027) == None  // 0721 is out since we don't write numbers with leading zeros
  • some tests will include very large numbers.
  • test data only employs positive integers.

The function you write for this challenge is the inverse of this kata: "Next bigger number with the same digits."

Strings
Mathematics
Algorithms

Stats:

CreatedNov 28, 2015
PublishedDec 3, 2015
Warriors Trained52974
Total Skips12108
Total Code Submissions131973
Total Times Completed11791
JavaScript Completions3538
C# Completions1131
Java Completions1624
Haskell Completions133
Python Completions4206
Ruby Completions250
Rust Completions301
C Completions640
Crystal Completions11
COBOL Completions8
Go Completions169
D Completions5
Scala Completions11
Total Stars2006
% of votes with a positive feedback rating94% of 1526
Total "Very Satisfied" Votes1355
Total "Somewhat Satisfied" Votes149
Total "Not Satisfied" Votes22
Total Rank Assessments8
Average Assessed Rank
4 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • LesRamer Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • user5036852 Avatar
  • Blind4Basics Avatar
  • Firefly2002 Avatar
  • bshore Avatar
  • Voile Avatar
  • Madjosz Avatar
  • KenKamau Avatar
  • hobovsky Avatar
  • chirsz Avatar
  • OpenByte Avatar
  • TeoChrist Avatar
  • akar-0 Avatar
  • dfhwze Avatar
  • QKiryu Avatar
  • KayleighWasTaken Avatar
  • saudiGuy Avatar
Ad