golfing a bit more, just for fun
(but it may improve performance a tiny bit, too)
ones1=: (1(,,<,=){.) +/@:* (0:`$:@.(0<#),X)@}.
where XX…XX is either of
(*10^<:)@#,(10^#),1+10#.]
(,&1*10^(,~<:))@#,1+10#.]
(,&1*10^_1 0&+)@#,1+10.
I thought I would squeeze more than 1 s
Yeah, but I was just waking up and did not read Cliff's message
carefully enough.
Here's what I think he was after:
c1=: {{+/1=10#.^:_1]y}}"0
F=: {{{.N#~y=+/\c1 N=.>:i.10>.5*y}}
F 213
521
This uses his proposed components, achieves what he asked for, and
eliminates the 'n' which would make F
ones1 reads better than the original.
factoring out {. and }. really helps.
even though it means pairs of factors
are “distributed” and one doesn’t
immediately see which one is corresponding.
if it hadn’t been for having it fit on one line,
I had kept the recursive call separate, though.
Am 23.01
Hi Skip,
When using n (or v) as names in a direct definition, it assumes you're
defining a conjunction:
F=.{{{.n#~y=+/\c1 n=.>:i.100}}
F 19
F(19)
type <'F'
┌───┐
│conjunction│
└───┘
FF=:{{{.nn#~y+/\c1 nn=.>:i.100}}
type<'FF'
┌┐
│verb│
└┘
or you can explici
If you are working with a limited range, you can use I. to conduct the search.
For example,
ones1=: (1,1&<,1&=,])@{. +/@:* (0:`$:@.(0<#),(10^#),(1+10#.]),#*10^#-1:)@}.
ones=: 10 ones1@:(#.inv)"0 ]
senO=: ] I.~ [: +/\1+/"1@:=10 #.inv [: i. +:@]^:(> ones)^:_~
Here, I used a golfed version of Hauk
Also,
Also, why doesn't this work:
F=.{{{.n#~y=+/\c1 n=.>:i.100}}
F 19
F(19)
Skip Cave
Cave Consulting LLC
On Sat, Jan 22, 2022 at 11:50 PM Skip Cave wrote:
> Thanks for all the examples. I thought that the solution would be pretty
> straightforward.
>
> c1 gives the number of ones in a
Thanks for all the examples. I thought that the solution would be pretty
straightforward.
c1 gives the number of ones in an integer:
* c1=.{{+/1=10#.^:_1]y}}"0*
Test it:
* c1 211*
*2*
So the number of ones from 1 to 81 is:
* +/c1 i.>:81*
*19*
To develop the inverse verb F, we can get a ru
I should have removed from 'seno' the line that says:
assert. hi<1000
The purpose of that line was to save me from having to use jbrk if I
hit an infinite loop from getting my conditions backwards. It's not a
useful mechanism, and should have been removed before I posted the
implementation.
I didn’t do it much differently, conceptually.
But instead of binary search (bisection)
I split it up in ten buckets (dekasect).
Apologies for the bad naming:
another greek-latin hybrid, oh my …
I split that way because I wanted to evaluate
ones more often on small inputs than large.
Didn’t measu
Your 'ones' is a bit more efficient than the routine I had come up
with, so I'll stick with your implementation here.
Meanwhile, it's perhaps worth noting
I.213=ones i.1000
521 522 523 524 525 526 527 528 529 530
So, building the inverse... it's typical for there to be ten numbers
who had n "
This is a working solution. Not pretty, but it gets the job done.
onesList =: (0<{.)`($:@}. + ((10 ^ <:@#) * 1<{.) + ((1 + 10 #. }.) *
1={.) + <:@# * {. * 10 ^ 2 -~ #)@.(1<#)
ones =: 10 onesList@:(#.^:_1)"0 ]
NB. tests
ones 30 521
13 213
That was easy. Now for the “inverse:”
dl
There are pen and paper approaches, but assuming a brute force, and function
that answers basic first part of counting 1s in a number
c1 =: 10&(1 +/@:= #.inv)"0
and if you almost always want the sum from a list of arguments then
c2 =: +/@:c1
a brute force tacit iterative way of answering yo
Interesting problem.
1s in 1 to 9: 1
1s in 10 to 99: 20 = 10 + 10*1
1s in 100 to 999: 300 = 100 + 10*20
1s in 1000 to : 4000 = 1000 + 10*300
1s in the naturals below 10^p: (p-1)*10^p-1
But I am fairly certain this does not apply for non-integral p. Not sure
if a closed-form solution is po
13 matches
Mail list logo