19
\$\begingroup\$

We will think of a partition as a sequence of non-increasing integers. For a given partition into \$n_1, n_2, \dots, n_k\$ with \$n_1 \geq n_2 \dots n_{k-1} \geq n_k = n\$ we write it out with \$n_i\$ dots on row \$i\$. So for \$6, 1, 1\$ we would write six dots on the first row, one on the second and one on the third

Task

Given a partition, you should output its conjugate. That is you should output how many dots there are in each column of the dot diagram. For the input \$6, 1, 1\$ the output should be \$3, 1, 1, 1, 1, 1\$.

Examples

5, 2, 1 gives output 3, 2, 1, 1, 1
4, 3, 1 gives output 3, 2, 2, 1
4, 2, 2 gives output 3, 3, 1, 1
3, 3, 2 gives output 3, 3, 2
4 gives output 1, 1, 1, 1
\$\endgroup\$
6
  • 4
    \$\begingroup\$ How is the fact that it's a partition relevant? \$\endgroup\$
    – mousetail
    Oct 30 at 14:39
  • 2
    \$\begingroup\$ @Simd The "context" seems to only make the challenge confusing \$\endgroup\$
    – mousetail
    Oct 30 at 14:41
  • 2
    \$\begingroup\$ will input always come in desc sorted order? \$\endgroup\$ Oct 30 at 15:40
  • 1
    \$\begingroup\$ Can the input contain only one number? If so, please add a test case for that \$\endgroup\$
    – Luis Mendo
    Oct 31 at 0:05
  • 1
    \$\begingroup\$ Related \$\endgroup\$
    – att
    Oct 31 at 0:44

35 Answers 35

11
\$\begingroup\$

BQN, 3 bytes

⊒⌾/

Try it here.

Explanation via example:

   / 4‿3‿1         # use each element as a count to replicate its index
⟨ 0 0 0 0 1 1 1 2 ⟩

   ⊒/ 4‿3‿1        # take a running occurrence count
⟨ 0 1 2 3 0 1 2 0 ⟩

   /⁼⊒/ 4‿3‿1      # the inverse of /, applied via ⌾, counts occurrences
⟨ 3 2 2 1 ⟩
\$\endgroup\$
8
\$\begingroup\$

05AB1E, 5 bytes

L˜{Åγ

Attempt This Online!

L        # range, so [6,1,1] is [[1,2,3,4,5,6],[1],[1]]
˜        # flatten, [1,2,3,4,5,6,1,1]
{        # sort, [1,1,1,2,3,4,5,6]
Åγ       # run length encode, [3, 1, 1, 1, 1, 1]

If we can have extraneous zeros in the output, then by porting @frasiyav's great BQN answer we have:

05AB1E, 4 bytes

L˜ā¢

Attempt This Online!

L˜        # same as previously
ā         # length range, [1, 2, 3, ..., len(arr)] (without popping)
¢         # count each
\$\endgroup\$
1
7
\$\begingroup\$

Octave / MATLAB, 17 bytes

@(x)sum(1:x<=x,1)

Anonymous function that inputs a column vector and outputs a row vector.

Try it online!

Explanation

@(x)              % Define anynomous function of x: column vector
        1:x       % Range from 1 to (first entry of) x. Gives a row vector
           <=x    % Less than/equal to x? Element-wise with broadcast. Gives a matrix
    sum(      ,1) % Sum of each column

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

Jelly, 3 bytes

b1S

Try it online!

For some reason, I almost feel like I've seen this even shorter as part of a different solution...

b1     Convert each element of the input to base 1.
  S    Sum across columns.
\$\endgroup\$
6
\$\begingroup\$

05AB1E (legacy), 4 bytes

Å1ζO

Try it online or verify all test cases.

Doesn't work in the new version of 05AB1E, unless we add 0 or ¾ before the ζ.

Explanation:

Å1    # Convert each value in the (implicit) input-list into a list of that many 1s
  ζ   # Zip/transpose; swapping rows/columns,
      # implicitly using a space " " as filler for unequal length rows
   O  # Sum each inner list, which ignores the spaces in the legacy version of 05AB1E
      # (after which the resulting list is output implicitly)
\$\endgroup\$
6
\$\begingroup\$

R, 26 bytes

\(x)Map(\(z)sum(x>=z),1:x)

Attempt This Online!

A function taking a vector of integers and returning a list of integers.

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

R, 28 22 bytes

Edit: -6 bytes thanks to @Giuseppe.

\(x)table(sequence(x))

Attempt This Online!

Port of Based on @Command Master's 05AB1E answer.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ table(sequence(x)) is 22 bytes? You get a named vector, but I think that's fair. \$\endgroup\$
    – Giuseppe
    Oct 31 at 18:23
  • \$\begingroup\$ Yeah, me too. Thanks! \$\endgroup\$
    – pajonk
    Oct 31 at 19:02
5
\$\begingroup\$

Python, 52 50 bytes

-2 thanks to @mousetail

lambda a:[sum(i<x for x in a)for i in range(a[0])]

Attempt This Online!

\$\endgroup\$
5
\$\begingroup\$

J, 7 bytes

I.i.@{.

Attempt This Online!

  • I. What is the "insert before" index when inserting into the input...
  • i.@{. Of each of the numbers 0 through n-1, where n is the first input element.

J, straightforward alternate, 9 bytes

[:+/#"0&1

Attempt This Online!

\$\endgroup\$
5
\$\begingroup\$

Python, 45 bytes

def f(a,j=0):
 for c in-1,*a:a[:c]=c*[j];j+=1

Attempt This Online!

Takes a list and modifies it in place.

Python, 34 bytes

lambda a:sum(a[:,1>0]>range(a[0]))

Attempt This Online!

-4 thanks to @att.

Expects a NumPy array.

Ports @Command Master's pure Python.

\$\endgroup\$
1
  • \$\begingroup\$ second just lambda a:sum(a[:,1>0]>range(a[0])) \$\endgroup\$
    – att
    Nov 3 at 6:55
4
\$\begingroup\$

R, 30 bytes

\(l)rowSums(outer(1:l,l,"<="))

Attempt This Online!

Slightly longer compared to pajonk's, based on this other answer of mine.

\$\endgroup\$
1
  • \$\begingroup\$ I had something like this drafted, but with apply instead of (better) rowSums. \$\endgroup\$
    – pajonk
    Oct 30 at 18:37
4
\$\begingroup\$

sclin, 18 bytes

1X>b tpose \+/ map

Try it on scline!

1 repeated by n, transpose, sum each.

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

Uiua, 8 bytes

⍘⊚⊐/⊂⊐∵⇡

Try it!

⍘⊚⊐/⊂⊐∵⇡  # [5 2 1]
     ⊐∵⇡  # [⟦0 1 2 3 4⟧ ⟦0 1⟧ ⟦0⟧] range of each
  ⊐/⊂     # [0 1 2 3 4 0 1 0]       join
⍘⊚        # [3 2 1 1 1]             occurrences
\$\endgroup\$
4
\$\begingroup\$

Haskell, 35 bytes

f a=[sum[1|j<-a,i<=j]|i<-[1..a!!0]]

Attempt This Online!

-1 byte thanks to matteo_c

Basically a port of Command Master's python answer

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

JavaScript (ES6), 44 bytes

a=>a.map(g=v=>v&&g(--v,b[v]=-~b[v]),b=[])&&b

Try it online!

Commented

a =>              // a[] = input array
a.map(g = v =>    // for each value v in a[], using a recursive
                  // callback function g:
  v &&            //   stop if v = 0
  g(              //   otherwise do a recursive call:
    --v,          //     decrement v
    b[v] = -~b[v] //     increment b[v] (set it to 1 if undefined)
  ),              //   end of recursive call
  b = []          //   start with b = []
) && b            // end of map(); return b[]
\$\endgroup\$
3
\$\begingroup\$

Rust, 58 bytes

|a|(0..a[0]).map(move|i|a.iter().filter(|x|i<**x).count())

Attempt This Online!

Basically a port of Command Master's python answer

\$\endgroup\$
3
\$\begingroup\$

Japt v2.0a0 -Q, 5 bytes

-2 bytes from @AZTECCO

mo yl

Try it


Japt v2.0a0 -Q, 7 bytes

There must be a shorter way

co ü ml

Try it

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can use y method.. mo yl for 5 Bytes \$\endgroup\$
    – AZTECCO
    Oct 30 at 18:38
  • 1
    \$\begingroup\$ Note that if the flag you're using is for output visualisation purposes only then it can instead be entered in the input field in my interpreter so that it doesn't change the language when you copy the complete markdown directly from the interpreter. \$\endgroup\$
    – Shaggy
    Nov 1 at 12:32
3
\$\begingroup\$

PARI/GP, 37 bytes

a->Vecrev(vecsum([x^i-1|i<-a])/(x-1))

Attempt This Online!


PARI/GP, 37 bytes

a->[vecsum([t>=n|t<-a])|n<-[1..a[1]]]

Attempt This Online!

\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 26 bytes

Plus@@PadRight[1^Range@#]&

Try it online!

-12 bytes from @att

\$\endgroup\$
2
  • \$\begingroup\$ 28 bytes \$\endgroup\$
    – att
    Oct 31 at 0:45
  • \$\begingroup\$ 26 \$\endgroup\$
    – att
    Oct 31 at 0:47
3
\$\begingroup\$

Google Sheets, 92 86 bytes

=join(",",byrow(map(split(A1,","),lambda(n,sequence(n,1,,))),lambda(r,len(join(,r)))))

Put the input in as a text string in cell A1 and the formula in B1.

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

Vyxal, 3 bytes

ẋ∩@

Try it Online!

ẋ    Repeat (each value in list times value)
 ∩   Transpose list
  @  Length of each item in list
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 10 bytes

IE⌈θΣX⁰÷ιθ

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

   θ        Input list
  ⌈         Maximum
 E          Map over implicit range
      ⁰     Literal integer `0`
     X      Vectorised raise to power
        ι   Current value
       ÷    Vectorised integer divided by
         θ  Input list
    Σ       Take the sum
I           Cast to string
            Implicitly print
\$\endgroup\$
2
\$\begingroup\$

Retina 0.8.2, 44 bytes

\d+
$*_
\G_(?<=(?=(\2_*,?)+)(_+))
$#1,
\D+$

Try it online! Takes I/O as comma-separated integers but test cases removes and reinserts spaces for convenience. Explanation:

\d+
$*_

Convert to unary.

\G_(?<=(?=(\2_*,?)+)(_+))
$#1,

For 1 up to the first array entry, count how many entries are less than or equal the value.

\D+$

Remove the trailing junk.

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

APL(Dyalog Unicode), 6 bytes SBCS

+⌿≤⌸∘⍸

Try it on APLgolf!

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

Ruby, 37 bytes

->l{l[0].times.map{|x|l-=[x];l.size}}

Try it online!

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

Desmos, 36 bytes

f(l)=[l[l>=i].lengthfori=[1...l[1]]]

Try It On Desmos!

Try It On Desmos! - Prettified

This works because we can assume that the input is always in descending order as per OP's comment.

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

K (ngn/k), 9 bytes

.#'=,/!:'

Try it online!

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

Scala 3, 41 bytes

Saved some bytes thanks to the comment of @Kjetil S


a=>(1to a(0)).map{i=>a.count(_>=i)}.toSeq

Attempt This Online!

\$\endgroup\$
1
  • \$\begingroup\$ One less: a=>(1 to a.head).map{i=>a.count(_>=i)}.toSeq \$\endgroup\$
    – Kjetil S
    Nov 1 at 11:54
2
\$\begingroup\$

C (clang), 59 53 bytes

i;f(*a,z){for(i=0;z;i+=i<a[--z]&&printf("%d ",++z));}

Try it online!

  • -1 thanks to @ceilingcat
    i<=*a; -> *a/i

  • Golfed another 5 Bytes by @att.

for(i=1;i<=*a;) loops until we print a[0] times

i>a[z-1]? check i against last value in input

z-- move to previous position in input if greater

:printf("%d ",z else print position in input ,i++) and increment printed count

I think there can be a way to iterate i backwards.. e.g. for(i=*a;i.. but my brains are cooking.

\$\endgroup\$
4
  • \$\begingroup\$ I was hoping for a C answer. Thank you. \$\endgroup\$
    – Simd
    Oct 31 at 12:31
  • 1
    \$\begingroup\$ There you are! We were close to 30 answers without it, I felt sad \$\endgroup\$
    – AZTECCO
    Oct 31 at 12:38
  • \$\begingroup\$ 53 bytes \$\endgroup\$
    – att
    Nov 3 at 7:15
  • \$\begingroup\$ @att that's a great golfing! Thank you! \$\endgroup\$
    – AZTECCO
    Nov 3 at 20:11
2
\$\begingroup\$

Pip, 9 bytes

#*<|ZJ1Xg

Try It Online!

\$\endgroup\$
4
  • \$\begingroup\$ This is really clever! It doesn't quite work as written, since ZJ is intended for use with strings (try an input of 14, for instance; the output should be fourteen 1's, with no 2's). However, ZD should do what you want. You'll have to find a different way to get rid of the dummy values, though. \$\endgroup\$
    – DLosc
    Nov 15 at 18:28
  • \$\begingroup\$ Oh, or here's a better way that lets you still use ZJ: #*<|ZJ1Xg (9 bytes). \$\endgroup\$
    – DLosc
    Nov 15 at 20:41
  • \$\begingroup\$ @DLosc Thanks for the fix! The docs said ZJ operated on iterables, which I thought also included lists lol \$\endgroup\$
    – Aiden Chow
    Nov 16 at 5:52
  • \$\begingroup\$ It does also work with lists and ranges, but the "join each group of values into a string" part is the problem. If the items in each of the iterables are all the same length, it's fine, but if they're different lengths, you might not get the result you were expecting. \$\endgroup\$
    – DLosc
    Nov 16 at 20:05

Your Answer

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

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