Skip to main content
Commonmark migration
Source Link

#Racket 97

Racket 97

97 points (87 +20 for two strings, -10 for sorting, no arrays)

(define(s n <)(string->number(list->string(sort(string->list(number->string n))<))))

This uses lists of chars so you need to give it a char comparison function like char<? or char>?. I feel this also passes as ungolfed since it's not much to do than add spaces and increase variable names. My old version is perhaps more honorable :)

Old version without strings:

110 points (120 bytes (utf-8) - 10 for allowing for changing sort order. It uses no strings and no arrays)

(define(S d <)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())(
d d))(if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))<)))

Ungolfed:

(define (number-digit-sort number <)
  (foldl (λ (x a) (+ (* a 10) x))
         0
         (sort (let loop ((acc '()) (number number))
                 (if (= number 0)
                     acc
                     (loop (cons (modulo number 10) acc)
                           (quotient number 10))))
               <)))

I tested it with the 100,000th fibonacci number:

(number-digit-sort (fibonacci 100000) <)
;==> 1111... basically it's the digits:
; 1 2135 times
; 2 2149 times
; 3 2096 times
; 4 2023 times
; 5 2053 times
; 6 2051 times
; 7 2034 times
; 8 2131 times
; 9 2118 times

And the same in opposite order:

(number-digit-sort (fibonacci 100000) >)
; ==> 999... and the digest is
; 9 2118 times
; 8 2131 times
; 7 2034 times
; 6 2051 times
; 5 2053 times
; 4 2023 times
; 3 2096 times
; 2 2149 times
; 1 2135 times
; 0 2109 times

#Racket 97

97 points (87 +20 for two strings, -10 for sorting, no arrays)

(define(s n <)(string->number(list->string(sort(string->list(number->string n))<))))

This uses lists of chars so you need to give it a char comparison function like char<? or char>?. I feel this also passes as ungolfed since it's not much to do than add spaces and increase variable names. My old version is perhaps more honorable :)

Old version without strings:

110 points (120 bytes (utf-8) - 10 for allowing for changing sort order. It uses no strings and no arrays)

(define(S d <)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())(
d d))(if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))<)))

Ungolfed:

(define (number-digit-sort number <)
  (foldl (λ (x a) (+ (* a 10) x))
         0
         (sort (let loop ((acc '()) (number number))
                 (if (= number 0)
                     acc
                     (loop (cons (modulo number 10) acc)
                           (quotient number 10))))
               <)))

I tested it with the 100,000th fibonacci number:

(number-digit-sort (fibonacci 100000) <)
;==> 1111... basically it's the digits:
; 1 2135 times
; 2 2149 times
; 3 2096 times
; 4 2023 times
; 5 2053 times
; 6 2051 times
; 7 2034 times
; 8 2131 times
; 9 2118 times

And the same in opposite order:

(number-digit-sort (fibonacci 100000) >)
; ==> 999... and the digest is
; 9 2118 times
; 8 2131 times
; 7 2034 times
; 6 2051 times
; 5 2053 times
; 4 2023 times
; 3 2096 times
; 2 2149 times
; 1 2135 times
; 0 2109 times

Racket 97

97 points (87 +20 for two strings, -10 for sorting, no arrays)

(define(s n <)(string->number(list->string(sort(string->list(number->string n))<))))

This uses lists of chars so you need to give it a char comparison function like char<? or char>?. I feel this also passes as ungolfed since it's not much to do than add spaces and increase variable names. My old version is perhaps more honorable :)

Old version without strings:

110 points (120 bytes (utf-8) - 10 for allowing for changing sort order. It uses no strings and no arrays)

(define(S d <)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())(
d d))(if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))<)))

Ungolfed:

(define (number-digit-sort number <)
  (foldl (λ (x a) (+ (* a 10) x))
         0
         (sort (let loop ((acc '()) (number number))
                 (if (= number 0)
                     acc
                     (loop (cons (modulo number 10) acc)
                           (quotient number 10))))
               <)))

I tested it with the 100,000th fibonacci number:

(number-digit-sort (fibonacci 100000) <)
;==> 1111... basically it's the digits:
; 1 2135 times
; 2 2149 times
; 3 2096 times
; 4 2023 times
; 5 2053 times
; 6 2051 times
; 7 2034 times
; 8 2131 times
; 9 2118 times

And the same in opposite order:

(number-digit-sort (fibonacci 100000) >)
; ==> 999... and the digest is
; 9 2118 times
; 8 2131 times
; 7 2034 times
; 6 2051 times
; 5 2053 times
; 4 2023 times
; 3 2096 times
; 2 2149 times
; 1 2135 times
; 0 2109 times
new version using strings, but with better score
Source Link
Sylwester
  • 3.8k
  • 15
  • 28

#Racket 11097

97 points (87 +20 for two strings, -10 for sorting, no arrays)

(define(s n <)(string->number(list->string(sort(string->list(number->string n))<))))

This uses lists of chars so you need to give it a char comparison function like char<? or char>?. I feel this also passes as ungolfed since it's not much to do than add spaces and increase variable names. My old version is perhaps more honorable :)

Old version without strings:

110 points (120 bytes (utf-8) - 10 for allowing for changing sort order. It uses no strings and no arrays)

(define(S d <)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())(
d d))(if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))<)))

Ungolfed:

(define (number-digit-sort number <)
  (foldl (λ (x a) (+ (* a 10) x))
         0
         (sort (let loop ((acc '()) (number number))
                 (if (= number 0)
                     acc
                     (loop (cons (modulo number 10) acc)
                           (quotient number 10))))
               <)))

I tested it with the 100,000th fibonacci number:

(number-digit-sort (fibonacci 100000) <)
;==> 1111... basically it's the digits:
; 1 2135 times
; 2 2149 times
; 3 2096 times
; 4 2023 times
; 5 2053 times
; 6 2051 times
; 7 2034 times
; 8 2131 times
; 9 2118 times

And the same in opposite order:

(number-digit-sort (fibonacci 100000) >)
; ==> 999... and the digest is
; 9 2118 times
; 8 2131 times
; 7 2034 times
; 6 2051 times
; 5 2053 times
; 4 2023 times
; 3 2096 times
; 2 2149 times
; 1 2135 times
; 0 2109 times

#Racket 110

(120 bytes (utf-8) - 10 for allowing for changing sort order. It uses no strings and no arrays)

(define(S d <)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())(
d d))(if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))<)))

Ungolfed:

(define (number-digit-sort number <)
  (foldl (λ (x a) (+ (* a 10) x))
         0
         (sort (let loop ((acc '()) (number number))
                 (if (= number 0)
                     acc
                     (loop (cons (modulo number 10) acc)
                           (quotient number 10))))
               <)))

I tested it with the 100,000th fibonacci number:

(number-digit-sort (fibonacci 100000) <)
;==> 1111... basically it's the digits:
; 1 2135 times
; 2 2149 times
; 3 2096 times
; 4 2023 times
; 5 2053 times
; 6 2051 times
; 7 2034 times
; 8 2131 times
; 9 2118 times

And the same in opposite order:

(number-digit-sort (fibonacci 100000) >)
; ==> 999... and the digest is
; 9 2118 times
; 8 2131 times
; 7 2034 times
; 6 2051 times
; 5 2053 times
; 4 2023 times
; 3 2096 times
; 2 2149 times
; 1 2135 times
; 0 2109 times

#Racket 97

97 points (87 +20 for two strings, -10 for sorting, no arrays)

(define(s n <)(string->number(list->string(sort(string->list(number->string n))<))))

This uses lists of chars so you need to give it a char comparison function like char<? or char>?. I feel this also passes as ungolfed since it's not much to do than add spaces and increase variable names. My old version is perhaps more honorable :)

Old version without strings:

110 points (120 bytes (utf-8) - 10 for allowing for changing sort order. It uses no strings and no arrays)

(define(S d <)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())(
d d))(if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))<)))

Ungolfed:

(define (number-digit-sort number <)
  (foldl (λ (x a) (+ (* a 10) x))
         0
         (sort (let loop ((acc '()) (number number))
                 (if (= number 0)
                     acc
                     (loop (cons (modulo number 10) acc)
                           (quotient number 10))))
               <)))

I tested it with the 100,000th fibonacci number:

(number-digit-sort (fibonacci 100000) <)
;==> 1111... basically it's the digits:
; 1 2135 times
; 2 2149 times
; 3 2096 times
; 4 2023 times
; 5 2053 times
; 6 2051 times
; 7 2034 times
; 8 2131 times
; 9 2118 times

And the same in opposite order:

(number-digit-sort (fibonacci 100000) >)
; ==> 999... and the digest is
; 9 2118 times
; 8 2131 times
; 7 2034 times
; 6 2051 times
; 5 2053 times
; 4 2023 times
; 3 2096 times
; 2 2149 times
; 1 2135 times
; 0 2109 times
changed argument to take a comparison procedure instead of a boolean (like Common Lisp entry)
Source Link
Sylwester
  • 3.8k
  • 15
  • 28

#Racket 119110

(define(S d)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())(d d))
(if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))<)))

With ability to choose(120 bytes (utf-8) - 10 for allowing for changing sort order: 120. It uses no strings and no arrays)

(define(S d r<)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())( 
d d))
 (if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))(if r > <))))

Ungolfed (with sort order):

(define (number-digit-sort number reverse?<)
  (foldl (λ (x a) (+ (* a 10) x))
         0
         (sort (let loop ((acc '()) (number number))
                 (if (= number 0)
                     acc
                     (loop (cons (modulo number 10) acc)
                           (quotient number 10))))
               (if reverse? > <))))

I tested it with the 100,000th fibonacci number:

(number-digit-sort (fibonacci 100000) #f<)
;==> 1111... basically it's the digits:
; 1 2135 times
; 2 2149 times
; 3 2096 times
; 4 2023 times
; 5 2053 times
; 6 2051 times
; 7 2034 times
; 8 2131 times
; 9 2118 times

And the same in opposite order:

(number-digit-sort (fibonacci 100000) #t>)
; ==> 999... butand the digest is
; 9 2118 times
; 8 2131 times
; 7 2034 times
; 6 2051 times
; 5 2053 times
; 4 2023 times
; 3 2096 times
; 2 2149 times
; 1 2135 times
; 0 2109 times

#Racket 119

(define(S d)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())(d d))
(if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))<)))

With ability to choose sort order: 120

(define(S d r)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())(d d))
 (if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))(if r > <))))

Ungolfed (with sort order):

(define (number-digit-sort number reverse?)
  (foldl (λ (x a) (+ (* a 10) x))
         0
         (sort (let loop ((acc '()) (number number))
                 (if (= number 0)
                     acc
                     (loop (cons (modulo number 10) acc)
                           (quotient number 10))))
               (if reverse? > <))))

I tested it with the 100,000th fibonacci number:

(number-digit-sort (fibonacci 100000) #f)
;==> 1111... basically it's the digits:
; 1 2135 times
; 2 2149 times
; 3 2096 times
; 4 2023 times
; 5 2053 times
; 6 2051 times
; 7 2034 times
; 8 2131 times
; 9 2118 times

And the same in opposite order:

(number-digit-sort (fibonacci 100000) #t)
; ==> 999... but the digest is
; 9 2118 times
; 8 2131 times
; 7 2034 times
; 6 2051 times
; 5 2053 times
; 4 2023 times
; 3 2096 times
; 2 2149 times
; 1 2135 times
; 0 2109 times

#Racket 110

(120 bytes (utf-8) - 10 for allowing for changing sort order. It uses no strings and no arrays)

(define(S d <)(foldl(λ(x a)(+(* a 10)x))0(sort(let L((l'())( 
d d))(if(= d 0)l(L(cons(modulo d 10)l)(quotient d 10))))<)))

Ungolfed:

(define (number-digit-sort number <)
  (foldl (λ (x a) (+ (* a 10) x))
         0
         (sort (let loop ((acc '()) (number number))
                 (if (= number 0)
                     acc
                     (loop (cons (modulo number 10) acc)
                           (quotient number 10))))
               <)))

I tested it with the 100,000th fibonacci number:

(number-digit-sort (fibonacci 100000) <)
;==> 1111... basically it's the digits:
; 1 2135 times
; 2 2149 times
; 3 2096 times
; 4 2023 times
; 5 2053 times
; 6 2051 times
; 7 2034 times
; 8 2131 times
; 9 2118 times

And the same in opposite order:

(number-digit-sort (fibonacci 100000) >)
; ==> 999... and the digest is
; 9 2118 times
; 8 2131 times
; 7 2034 times
; 6 2051 times
; 5 2053 times
; 4 2023 times
; 3 2096 times
; 2 2149 times
; 1 2135 times
; 0 2109 times
Source Link
Sylwester
  • 3.8k
  • 15
  • 28
Loading