I often have to take character data and categorize it numerically at work. A common thing I do is to take character type variables and convert them to numeric type characters, keeping same categories according to the level of work I'm doing. (The longer the substring, the more in depth and specific, shorter substrings for broad level). Enough backstory...
The challenge: In as few bytes as possible, convert the input part A, a vector/list of unique strings, into the output, a vector/list of numbers, keeping unique categories within the length of substrings the same length, which is input part B. Feel free to ask questions for clarification.
w, Vector/list of unique strings of equal character length.
n <= 10
- These strings may be any combination of uppercase letters and numbers. Sorry if it seems my examples follow a pattern, I just created them after a similar pattern I see in the data I work with.
- Some random examples of what input could look like: "A1", "7LJ1", "J426SIR", "4AYE28TLSR", or any other random combination of uppercase letters up to 10 characters. To repeat: Each element in the vector/list will be the same length.
- Input may already be ordered by group, or non-contiguous, meaning elements lying within one group may be separated by elements of a different group. (See example 2)
1 <= s <= n
Output should have the indexes of the sorted, de-duplicated, trimmed values. See example 2. (I've included comments in my output to clarify, this is not required) This can be 0-based or 1-based index, as per your language uses.
Example Input 1:
#Input already alphabetized, but this input is not always guaranteed s = 3, w = [ABC01, ABC11, ABC21, ABD01, ABE01, ABE02, ACA10, ACA11, ACB20, ACB21]
Example Output 1:
[1, #ABC 1, 1, 2, #ABD 3, #ABE 3, 4, #ACA 4, 5, #ACB 5]
Example Input 2: s = 4, w =
[X1Z123, X1Z134, X1Y123, X1Y134, X1Y145, X1Y156, X1X123, X1X124, X1X234, X2Z123, X2Z134, X1X255, X1Y124, X2Z222, X2Z223, X2Z224]
Example 2 Output:
#Categorize by order of appearance [1, #X1Z1 1, 2, #X1Y1 2, 2, 2, 3, #X1X1 3, 4, #X1X2 5, #X2Z1 5, 4, #X1X2 again (to show input can be non-contiguous) 2, #X1Y1 again 6, #X2Z2 6, 6] OR #Input not alphabetized, but indexes still match original input indexes. [4, #X1Z1 4, 3, #X1Y1 3, 3, 3, 1, #X1X1 1, 2, #X1X2 5, #X2Z1 5, 2, #X1X2 again (to show input can be non-contiguous) 3, #X1Y1 again 6, #X2Z2 6, 6]