5
$\begingroup$

I want to create a variable list like this:

{0, 0, ..., 0, 0, 1, 1}

This list would grow list this, starting with no 0's, but then gaining more 0's to the left the larger you want it.

f[0] := {1, 1}

f[1] := {0, 1, 1}

f[2] := {0, 0, 1, 1}

f[3] := {0, 0, 0, 1, 1}

f[4] := {0, 0, 0, 0, 1, 1}

I know that I can use Table to create ten 0 by doing

Table[0, 10]
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

but not an efficient way to have it so that the last two digits are 1.

$\endgroup$
4
  • $\begingroup$ SparseArray? PadRight/Left? $\endgroup$
    – Kuba
    Commented Jul 11, 2020 at 5:57
  • $\begingroup$ Not able to use SparseArray, but I just realised that I can use Flattern[]. $\endgroup$
    – Robjobbob
    Commented Jul 11, 2020 at 5:58
  • 6
    $\begingroup$ PadLeft and PadRight would be better than the solution you found. There’s also Join and Catenate which are used for this, rather than Flatten. $\endgroup$
    – C. E.
    Commented Jul 11, 2020 at 7:23
  • $\begingroup$ list[nzeros_] := PadLeft[{1, 1}, nzeros + 2] usage list[0], list[1], list[6], etc. $\endgroup$
    – flinty
    Commented Jul 11, 2020 at 13:19

6 Answers 6

7
$\begingroup$

A few additional alternatives:

ClearAll[ap, ur, ar, cr, ab, sa]

ap = ArrayPad[{1, 1}, {#, 0}] &;

ur = UnitStep[Range[# + 2] - # - 1/2] &;

ar = Reverse @ ArrayReshape[{1, 1}, # + 2] &;

cr = Clip[Range[# + 2], {#, #} + 1/2, {0, 1}] &;

ab = Array[a \[Function] Boole[a > #], # + 2] &;

sa = Normal @ SparseArray[{# + 1 | # + 2 -> 1}, # + 2] &;

Examples:

Through[{ap, ur, ar, cr, ab, sa} @ 0]
{{1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}, {1, 1}}
Through[{ap, ur, ar, cr, ab, sa} @ 3]
{{0, 0, 0, 1, 1}, {0, 0, 0, 1, 1}, {0, 0, 0, 1, 1}, {0, 0, 0, 1, 1}, 
{0, 0, 0, 1, 1}, {0, 0, 0, 1, 1}}
Through[{ap, ur, ar, cr, ab, sa}@5]
{{0, 0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1, 1}}
$\endgroup$
1
7
$\begingroup$

You are asking for the digits in the base 2 representation of the number 3, with some number, call it $n_0$, of leading zeros.

Clear[f]

f[n0_] := IntegerDigits[3, 2, n0 + 2]

f[0]    (* {1, 1} *)
f[1]    (* {0, 1, 1} *)
f[2]    (* {0, 0, 1, 1} *)
f[10]   (* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1} *)
$\endgroup$
4
$\begingroup$

Using Join[] and Table[] seems to be the easiest way to do it.

fn[n_] := Join[Table[0, n],{1, 1}];

fn[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}
$\endgroup$
2
  • $\begingroup$ alternatively: Flatten[{ConstantArray[0, n], {1,1} }] $\endgroup$
    – bill s
    Commented Jul 11, 2020 at 15:42
  • 5
    $\begingroup$ or Join[ConstantArray[0, n], {1,1}] $\endgroup$
    – user1066
    Commented Jul 11, 2020 at 17:07
2
$\begingroup$

A few alternatives

n = 5;

list = Table[0, n + 2];

Using SubsetMap (new in 12.0)

SubsetMap[{1, 1} &, n + 1 ;;] @ list

Using ReplaceAt (new in 13.1)

ReplaceAt[_ :> 1, n + 1 ;;] @ list

Using MapAt

MapAt[1 &, list, n + 1 ;;]

Using ReplaceAll

list /. {a__, _, _} :> {a, 1, 1}

All return

{0, 0, 0, 0, 0, 1, 1}

$\endgroup$
2
$\begingroup$

Using Table and If with the index in the table running backward:

f[n_] := Table[If[k == 1 || k == 2, 1, 0], {k, n + 2, 1, -1}]

f[10]

(*{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}*)

A faster alternative using Insert:

f[n_] := Insert[Table[0, {n}], Splice@{1, 1}, -1]

f[10]

(*{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}*)
$\endgroup$
2
$\begingroup$

Using SubsetMap:

Clear["Global`*"];
f[n_ /; n > 2] := SubsetMap[Cos, ConstantArray[0, n], {-1, -2}]
f[n_] := ConstantArray[1, n];

f /@ Range[10] // Grid

enter image description here

$\endgroup$
1
  • $\begingroup$ + 1 Very nice, @Syed! :) $\endgroup$ Commented Apr 8 at 19:38

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.