Why settle for just one programming language when you can solve a problem in six different ones?

New post: jcarroll.com.au/2024/10/26/pol

in which I tackle a puzzle with a similar approach in #rstats :rstats:, #APL 🍎, #julialang :julia:, #haskell :haskell:, #python :python:, and #rustlang :rust:, with a bonus solution in J

If you can spot improvements, have a different approach, or would like to add a solution in another language, let me know!

Follow

@jonocarroll
Just came across your blog post, here's my Julia solution:
```
maxmin(n::Integer) = maxxie(n), minnie(n)

function firstdigitswap(n::Integer, f::Function)
n == 0 && return 0

digs = digits(n)
val, i = f(digs)
digs[i] = digs[end]
digs[end] = val

digits2num(digs)
end

digits2num(digs) = mapreduce((i, d)::Tuple -> d*10^(i-1), +, enumerate(digs))

maxxie(n::Integer) = firstdigitswap(n, findmax)

minnie(n::Integer) = firstdigitswap(n, digs -> findmin((d > 0 ? d : 10 for d in digs)))
```

@digital_carver the swapping could be any pair of digits, unless it makes the first digit 0. This just swaps the ends, right?

@jonocarroll
Ah, this is what happens when I read and code with 4am brain 😄 This just swaps the first digit with the max/min (non-zero) digit, which gives the same result in many cases, but fails for numbers like 92484957 (maxxie should return 99484257, returns with no swaps instead).

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.