How can I go from this list [36, 73, 84, 0, 73, 83, 0, 69, 69, 78, 0, 84, 69, 75, 83, 84, 31] to this [0000003673, 8400738300, 6969780084, 6975838431]. Is there also a way to make the elements the exact amount of numbers that I want. thanks in advance
4 Answers
lst = {36, 73, 84, 0, 73, 83, 0, 69, 69, 78, 0, 84, 69, 75, 83, 84, 31};
m = 5;
il = Max @ IntegerLength @ lst;
Map[StringJoin] @
IntegerString[#, 10, il] & @
Partition[lst, m, m, {- Mod[Length @ lst, m, 1], 1}, 0]
{"0000003673", "8400738300", "6969780084", "6975838431"}
Steps:
Partition[lst, m, m, {-Mod[Length@lst, m], 1}, 0]
{{0, 0, 0, 36, 73}, {84, 0, 73, 83, 0}, {69, 69, 78, 0, 84}, {69, 75, 83, 84, 31}}
IntegerString[%, 10, il]
{{"00", "00", "00", "36", "73"}, {"84", "00", "73", "83", "00"}, {"69", "69", "78", "00", "84"}, {"69", "75", "83", "84", "31"}}
Map[StringJoin] @ %
{"0000003673", "8400738300", "6969780084", "6975838431"}
-
$\begingroup$ Thank you so much for your help kglr. You realy made my day. $\endgroup$– JonasDec 8, 2021 at 10:06
Is there also a way to make the elements the exact amount of numbers that I want
Right now, you are partitioning in sets of five starting from the right. Let that number be k
.
k = 5; (* "exact amount of numbers" *)
alist = {36, 73, 84, 0, 73, 83, 0, 69, 69, 78, 0, 84, 69, 75, 83, 84,
31}
blist = PadLeft[alist, k*Ceiling[Length@alist/k]]
{0, 0, 0, 36, 73, 84, 0, 73, 83, 0, 69, 69, 78, 0, 84, 69, 75, 83, \ 84, 31}
clist = Partition[blist, UpTo[k]]
{{0, 0, 0, 36, 73}, {84, 0, 73, 83, 0}, {69, 69, 78, 0, 84}, {69, 75,
83, 84, 31}}
dlist = FromDigits[#, 100] & /@ clist
{3673, 8400738300, 6969780084, 6975838431}
These are numbers, but you want padded strings:
StringPadLeft[#, 2 k, "0"] &@(ToString /@ dlist)
{"0000003673", "8400738300", "6969780084", "6975838431"}
Running this with k=6 yields.
{"003673840073", "830069697800", "846975838431"}
a = {36, 73, 84, 0, 73, 83, 0, 69, 69, 78, 0, 84, 69, 75, 83, 84, 31};
StringJoin /@ Reverse[Reverse /@
Partition[Reverse[a], 5, 5, {1, 1}, 0] /. a_Integer :> ToString[a] /. "0" -> "00"]
lst = {36, 73, 84, 0, 73, 83, 0, 69, 69, 78, 0, 84, 69, 75, 83, 84, 31};
m = 5;
il = Max @ IntegerLength @ lst;
StringPartition[#, il m] & @
StringPadLeft[#, il m Ceiling[Length @ lst / m], "0"] & @
StringJoin @
IntegerString[lst, 10, il]
{"0000003673", "8400738300", "6969780084", "6975838431"}
0000003673
, what is the mapping? And why is the second one8400738300
? i,e. what are the rules used? $\endgroup$