6
$\begingroup$

I want to achieve this effect

enter image description here

I've tried

With[{n=5},Manipulate[Table[If[0<=i-j<=1,Mod[i,1],1],{j,0,If[i<=n-1,i,n-1]}],{i,0,n}]]

That last one is always wrong, and I feel there must be a neat way to do it. Do you have a better way?
enter image description here

$\endgroup$

5 Answers 5

3
$\begingroup$
With[{n = 5}
 , Manipulate[
  ConstantArray[1, IntegerPart[i]]~Join~
    {If[i > 0, FractionalPart[i], 0]} /. {a__, 0 | 0.} :> {a}
  , {{i, 1}, 0, n}
  ]
 ]

enter image description here

$\endgroup$
5
$\begingroup$

Is this what you are after?

With[{n = 5},
 Manipulate[
  Table[
   If[0 <= i - j <= 1, Mod[i, 1], 1],
   {j, 0,
    If[i <= n - 1, i, n - 1]}
   ],
  {i, 0, n + 0.1}
  ]
 ]

Blockquote

$\endgroup$
4
$\begingroup$

IntegerPartand FractionalPart

After the correction by @cvgmt (Thanks!)

Manipulate[
    Append[
        ConstantArray[1,IntegerPart[k]],
        If[ #>0, #, Nothing ]& @ FractionalPart[k]
    ] 
    , {k, 0, 5}
]

enter image description here


Old answer

My better or slightly wrong original answer, depending on the interpretation. It fails because when k==5, the result should be {1,1,1,1,1} instead of {1, 1, 1, 1, 1, 0.}, as commented by @cvgmt

Manipulate[
    Append[
        ConstantArray[1,IntegerPart[k]],
        FractionalPart[k]
    ] 
    , {k, 0, 5}
]

One could argue that the sequence that makes most sense should always end in a float, but that is subject to discussion and the OP's example doesn't behave like that.

$\endgroup$
2
  • 3
    $\begingroup$ When k==5, the result should be {1,1,1,1,1} instead of {1, 1, 1, 1, 1, 0.} $\endgroup$
    – cvgmt
    Feb 13, 2023 at 11:11
  • $\begingroup$ @cvgmt Thanks, corrected! $\endgroup$
    – rhermans
    Feb 13, 2023 at 11:17
4
$\begingroup$
Manipulate[
 If[i == 0, {N@i}, 
  If[0 < FractionalPart[i] < 1, 
   Join[ConstantArray[1, IntegerPart[i]], {FractionalPart[i]}], 
   ConstantArray[1, IntegerPart[i]]]], {i, 0, 5}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Same problem as mine? For k==4 you get {1,1,1,1,0} $\endgroup$
    – rhermans
    Feb 13, 2023 at 11:59
  • $\begingroup$ @rhermans Thanks, fixed now. $\endgroup$
    – cvgmt
    Feb 13, 2023 at 12:06
2
$\begingroup$

Using QuotientRemainder and ReplaceAll

Manipulate[
    ReplaceAll[
        QuotientRemainder[k, 1],
        RuleDelayed[
            n_Integer,
            Sequence@@ConstantArray[1,n]
        ]
    ]
    , {k, 0, 5}
]

or the same in compact form

Manipulate[
    QuotientRemainder[k, 1] /. n_Integer :> Sequence@@ConstantArray[1,n]  
    , {k, 0, 5}
]

Notice how n_Integer :> Sequence@@ConstantArray[1,n] removes the zeros too, because it replaces the zero with Sequence@@ConstantArray[1,0] which evaluates to Sequence[] which effectively removes it.

{4,0} /. n_Integer :> Sequence@@ConstantArray[1,n]
(* {1,1,1,1} *)

enter image description here

$\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.