I want to encode a list of numbers according to the element position. For example, two lists are as following:
list1 = {5069, 5165, 5069, 5068}
list2 = {4519}
If the length of list equals to 1
, it should be changed into {4}, i.e.
list2/.len:{single_}/;Length[len]==1:>{4}
(* output: {4} *)
And if the length of list does not equals to 1
, the list should be changed with this rule:
- the begining of the list should be
1
;- the end should be
3
;- all the number in the
middle
should be2
.
So, list1
should be changed into {1, 2, 2, 3}
. But my solution as followed does not produce the expected result:
stateEncodeRule = {len : {single__} /; Length@len == 1 :> 4, {begin_, middle__, end_} :> {1, 2, 3}}
{5069, 5165, 5069, 5068}/.stateEncodeRule
(* output: {1,2,3} *)
I wonder how to produce the expected result, i.e.
{5069, 5165, 5069, 5068} ==> {1, 2, 2, 3}
Thank you.