Skip to content

Instantly share code, notes, and snippets.

@butchi
Created October 14, 2014 16:06
Show Gist options
  • Save butchi/4a3a8854f1b5e394c364 to your computer and use it in GitHub Desktop.
Save butchi/4a3a8854f1b5e394c364 to your computer and use it in GitHub Desktop.
yukicoder No.39 桁の数字を入れ替え
(* http://yukicoder.me/problems/no/39 *)
replace[li_, {posA_, posB_}] := ReplacePart[li, {posA -> li[[posB]], posB -> li[[posA]]}]
replacePos[li_] := Select[Tuples[Range[Length[li]], 2], OrderedQ]
map[li_] := replace[li, #] & /@ replacePos[li]
f[n_] := Max[FromDigits[#] & /@ map[IntegerDigits[n]]]
In[1]:= f[12]
Out[1]= 21
In[2]:= f[1122]
Out[2]= 2121
In[3]:= f[999999999]
Out[3]= 999999999
In[4]:= f[9978]
Out[4]= 9987
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment