20
\$\begingroup\$

Given a sorted list of unique positive floats (none of which are integers) such as:

[0.1, 0.2, 1.4, 2.3, 2.4, 2.5, 3.2]

Evenly spread out the values that fall between two consecutive integers. So in this case you would get:

[0.333, 0.666, 1.5, 2.25, 2.5, 2.75, 3.5]

(I have truncated the values 1/3 and 2/3 for ease of presentation.)

\$\endgroup\$
3
  • \$\begingroup\$ Can the input have repeats? \$\endgroup\$
    – xnor
    Aug 9, 2023 at 6:35
  • 1
    \$\begingroup\$ @xnor No, I edited the question \$\endgroup\$
    – Simd
    Aug 9, 2023 at 6:36
  • 5
    \$\begingroup\$ I think there should be more than one test case. \$\endgroup\$
    – Lynn
    Aug 10, 2023 at 16:54

16 Answers 16

8
\$\begingroup\$

Nekomata, 8 bytes

kŢᵉR→/+j

Attempt This Online!

Input and output are both in fractions, because Nekomata doesn't have floating point numbers.

kŢᵉR→/+j
k           Floor
                e.g. [1/10,2/10,14/10,23/10,24/10,25/10,32/10] -> [0,0,1,2,2,2,3]
 Ţ          Tally; returns a list of unique elements and a list of their counts
                e.g. [0,0,1,2,2,2,3] -> [0,1,2,3], [2,1,3,1]
  ᵉR        Range each count, and push the original list
                e.g. [2,1,3,1] -> [[1,2],[1],[1,2,3],[1]], [2,1,3,1]
    →       Increment
                e.g. [2,1,3,1] -> [3,2,4,2]
     /      Divide
                e.g. [[1,2],[1],[1,2,3],[1]], [3,2,4,2] -> [[1/3,2/3],[1/2],[1/4,1/2,3/4],[1/2]]
      +     Add
                e.g. [0,1,2,3], [[1/3,2/3],[1/2],[1/4,1/2,3/4],[1/2]] -> [[1/3,2/3],[3/2],[9/4,5/2,11/4],[7/2]]
       j    Join
                e.g. [[1/3,2/3],[3/2],[9/4,5/2,11/4],[7/2]] -> [1/3,2/3,3/2,9/4,5/2,11/4,7/2]
\$\endgroup\$
8
\$\begingroup\$

R, 46 45 bytes

Edit: -1 byte thanks to pajonk

\(x)sequence(y<-rle(t<-x%/%1)$l)/rep(y+1,y)+t

Attempt This Online!

A rare use for R's obscure sequence function: "For each element of x the sequence 1...x is created. These are concatenated and the result returned."

The documentation also notes: "sequence mainly exists in reverence to the very early history of R."

\$\endgroup\$
1
  • \$\begingroup\$ -1 byte \$\endgroup\$
    – pajonk
    Aug 10, 2023 at 6:22
6
\$\begingroup\$

APL (Dyalog Unicode), 18 16 bytes

∊⌊(⊂⊣+⍋⍤⊢÷≢⍤,)⌸⊢

Try it online! (Using Extended because TIO is out of date)

⌊()⌸⊢ on each unique floor () and its corresponding argument elements ():

 enclose the

 left argument, i.e. the floor in question

+ added to

⍋⍤⊢ the grade ( sorting permutation: 1…length) of () the right argument ( the elements)

÷ divided by

≢⍤, the count () of () the concatenation (,) of the floor and the elements

enlist (flatten)

\$\endgroup\$
6
\$\begingroup\$

05AB1E, 10 bytes

ïγεā¤>/+}˜

Try it online or try it online with step-by-step debug-lines.

Explanation:

ï         # Floor all values in the (implicit) input-list
 γ        # Group it into adjacent equal values
  ε       # Map over each group:
   ā      #  Push a list in the range [1,length] (without popping the list)
    ¤     #  Push its last item - the length (without popping the list)
     >    #  Increase this length by 1
      /   #  Divide all values in the [1,length]-ranged list by this (length+1)
       +  #  Add it to each floored value of the current group
  }˜      # After the map: flatten the list of lists
          # (after which the result is output implicitly)
\$\endgroup\$
1
5
\$\begingroup\$

Octave, 45 bytes

@(x)(t=fix(x))+sum(triu(u=t==t'))./(sum(u)+1)

Try it online!

Explanation

@(x)(t=fix(x))+sum(triu(u=t==t'))./(sum(u)+1)

@(x)                                           % Anonymuous function
    (t=fix(x))                                 % t: round down x, element-wise
                        u=t==t'                % u: matrix of pairwise comparisons of t
                   triu(       )               % Upper triangular part of u
               sum(            )               % Sum of each column of that
                                    sum(u)+1   % Sum of each column of u, plus 1
                                 ./(        )  % Divide, elemment-wise
              +                                % Add, element-wise
\$\endgroup\$
4
\$\begingroup\$

Jelly, 11 bytes

ḞJ÷L‘$+Ʋƙ`F

Try it online!

\$\endgroup\$
0
4
\$\begingroup\$

Python, 95 bytes

lambda x:[k+~j/~len(i)for k,[*i]in groupby(x,int)for j in range(len(i))]
from itertools import*

Attempt This Online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 97 \$\endgroup\$ Aug 9, 2023 at 6:52
  • \$\begingroup\$ What would a non groupby solution look like? \$\endgroup\$
    – Simd
    Aug 9, 2023 at 21:24
4
\$\begingroup\$

JavaScript (Node.js), 61 bytes

x=>x.map(k=v=>(k[v|=0]=~-k[v])/~x.filter(t=>~~t==v).length+v)

Try it online!

JavaScript (Node.js), 65 bytes

x=>x.map(k=v=>(k[v|=0]=-~k[v],v)).map(m=v=>v+(m[v]=~-m[v])/~k[v])

Try it online!

\$\endgroup\$
1
4
\$\begingroup\$

Thunno 2 S, 9 bytes

Nġıżnl⁺/+

Try it online!

Port of Kevin Cruijssen's 05AB1E answer.

Explanation

Nġıżnl⁺/+  # Implicit input
N          # Floor each value
 ġ         # Group consecutive
  ı        # Map over this list:
   ż       #  Without popping, push [1..length]
    nl     #  Length of the current group
      ⁺    #  Incremented
       /   #  Divide the range by this
        +  #  Add to the group
           # Implicit output, flattened
\$\endgroup\$
4
\$\begingroup\$

Vyxal, 64 bitsv2, 8 bytes

⌊øĖ₌ɾ›/+f

Try it Online!

-2 thanks to emanresu

Explained

⌊Ġ:@₌ɾ›/+f­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌­
⌊Ġ          # ‎⁡Group the floored values by consecutive items. 
  :@        # ‎⁢Push a copy of that where each item is the corresponding length. 
    ₌ɾ›     # ‎⁣Push a list [range 1..n for n in top], [n+1 for n in top]
       /    # ‎⁤And divide those lists.
        +   # ‎⁢⁡Add that to the original grouped values
         f  # ‎⁢⁢Then flatten
💎

Created with the help of Luminespire.

\$\endgroup\$
1
  • \$\begingroup\$ 9 \$\endgroup\$
    – emanresu A
    Aug 9, 2023 at 10:58
3
\$\begingroup\$

JavaScript (ES6), 63 bytes

a=>a.map(p=v=>a.map(V=>v^V||n++,n=1,p!=(p=~~v)?q=1:q++)&&p+q/n)

Try it online!

Commented

a =>                 // a[] = input array
a.map(p =            // initialize p to a NaN'ish value
  v =>               // for each value v in a[]:
  a.map(V =>         //   for each value V in a[]:
    v ^ V || n++,    //     increment n if floor(v) = floor(V)
    n = 1,           //     starting with n = 1
    p != (p = ~~v) ? //     if p is not equal to floor(v)
                     //     (update p to floor(v) afterwards):
      q = 1          //       start a new group with q = 1
    :                //     else:
      q++            //       increment q
  ) &&               //   end of inner map()
  p + q / n          //   the final value is p + q / n
)                    // end of outer map()
\$\endgroup\$
3
\$\begingroup\$

R, 47 bytes

\(x)ave(x,y<-x%/%1,FUN=\(z)seq(z)/sum(1,z^0))+y

Attempt This Online!

Not as short as the answer using sequence, but ave is pretty neat, too.

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 19 bytes

≦⌊θIEθ⁺ι∕⊕№…θκι⊕№θι

Try it online! Link is to verbose version of code. Explanation:

  θ                 Input array
≦                   Apply to all elements
 ⌊                  Floor
     θ              Floored array
    E               Map over floored elements
       ι            Current floored element
      ⁺             Plus
            θ       Floored array
           …        Truncated to length
             κ      Current index
          №         Count of
              ι     Current floored element
         ⊕          Incremented
        ∕           Divided by
                 θ  Floored array
                №   Count of
                  ι Current floored element
               ⊕    Incremented
   I                Cast to string
                    Implicitly print
\$\endgroup\$
2
\$\begingroup\$

Perl 5, 61 bytes

sub{map{//;map{$'+$_/($c+1)}1..($c=grep$'==int,@_)}0..$_[-1]}

Try it online!

sub {
  map {                #loop ints in $_ from 0 to highest input number as int
    //;                #copies current $_ to $'
    $c=grep$'==int,@_; #$c = count of current int in $_ in input array @_
    map {              #loop 1 to current count in $c
      $' + $_ / ($c+1) #return current outer int + inner int / count+1
    }
    1..$c              #inner loop, no loop and no return if count is 0
  }
  0..$_[-1]            #outer loop
}
\$\endgroup\$
1
\$\begingroup\$

Pyth, 16 bytes

=sMQm+c@XdH1d/+Q

Try it online!

Explanation

=sMQm+c@XdH1d/+QdddQ    # implicitly add Q
                        # implicitly assign Q=eval(input()), H=dict()
=  Q                    # assign Q to
 sMQ                    # floor() mapped over Q
    m              Q    # map lambda d over Q
        XdH1            #   H[d]+=1 (or H[d]=1 if d not in H)
       @    d           #   H[d]
      c                 #   divided by
             /   d      #   the count of how many times d appears in
              +Qd       #   Q with d appended
     +            d     #   plus d
\$\endgroup\$
1
\$\begingroup\$

J, 21 bytes

<.+&;<.<@(#\%1+#)/.<.

Try it online!

Repetition of floor <. 3 times was surprisingly shorter than alternatives that DRY it out.

  • <..../.<. For each group of the floor...
  • <@(#\%1+#) Divide 1...g by (1 + g), where g is the group size. Box it <@ because we have to box heterogeneous lists.
  • <.+&; Add the floor elementwise to that unboxed result.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.