Base 36 counter without I or O - counter

we have a requirement to make our serial numbers Base 36 (0-9,A-Z). My initial thought was store the counter in decimal and convert to hex only when required for display. This makes the counting simple, however there is another requirement to not use I or O because it'll be confused with 1 and 0 on the barcodes human readable portion. This makes it a bit of a nightmare.
Language is unimportant, but the counter itself will be held in SQL Server 2012+.
Anyone have any experiences with this problem?
Edit:
I've rewritten a method I found to test in C#. It allows any string of base characters to be passed in.
ie. string baseChars = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
It's not pretty but its a start!
private string GetCustomBase(int iValue, string baseChars)
{
int baseNum = baseChars.Length;
int value= iValue;
string result = "";
while( value > 0 )
{
result = baseChars[ 0 + (value % baseNum)] + result;
value = value / baseNum;
}
return result;
}
private int GetDecimal(string strValue, string baseChars)
{
int baseNum = baseChars.Length;
string strAmendedValue = strValue;
int iResult = 0;
//Each char one at a time (from right)
for (int i = 0; i < strValue.Length; i++)
{
string c = strValue.Substring(strValue.Length - i -1, 1);
int iPos = baseChars.IndexOf(c); //get actual value (0 = 0, A = 10 etc.)
int iPowerVal = (int)Math.Pow((double)baseNum, (double)(i));
iResult = iResult + (iPowerVal * iPos);
}
return iResult;
}

An implementation of the suggestion in the question comments. As language is unimportant, here's a Ruby version:
class Integer
def to_34_IO_shifted
to_s(34).upcase.tr("IJKLMNOPQRSTUVWX", "JKLMNPQRSTUVWXYZ")
end
end
class String
def from_34_IO_shifted
upcase.tr("JKLMNPQRSTUVWXYZIO", "IJKLMNOPQRSTUVWX10").to_i(34)
end
end
puts 170.times.map { |x| x.to_34_IO_shifted }.join(' ')
x = 73644
x34 = x.to_34_IO_shifted
x10 = x34.from_34_IO_shifted
puts "\n#{x} -> '#{x34}' -> #{x10}"
puts "'10' -> #{'10'.from_34_IO_shifted}"
puts "'IO' -> #{'IO'.from_34_IO_shifted}"
Output:
0 1 2 3 4 5 6 7 8 9 A B C D E F G H J K L M N P Q R S T U V W X Y Z 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 1G 1H 1J 1K 1L 1M 1N 1P 1Q 1R 1S 1T 1U 1V 1W 1X 1Y 1Z 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 2G 2H 2J 2K 2L 2M 2N 2P 2Q 2R 2S 2T 2U 2V 2W 2X 2Y 2Z 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 3G 3H 3J 3K 3L 3M 3N 3P 3Q 3R 3S 3T 3U 3V 3W 3X 3Y 3Z 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 4G 4H 4J 4K 4L 4M 4N 4P 4Q 4R 4S 4T 4U 4V 4W 4X 4Y 4Z
73644 -> '1VQ0' -> 73644
'10' -> 34
'IO' -> 34
EDIT: made it so that I and O are interpreted as 1 and 0, in case someone does misread it.

Related

Shifting Index values in matlab

Suppose I have two rows as follows with the data
R1 = 12 13 15 17 200 23
R2 = 32 22 43 67 21 74
I would like to know how to shift the values of the 2nd index and 3rd index of R1 (e.g, 13 15) into the second row of R2 so it becomes
R2 = 32 13 15 67 21 74
It's very simple: R2(2:3) = R1(2:3);
Code sample:
R1 = [12 13 15 17 200 23];
R2 = [32 22 43 67 21 74];
R2(2:3) = R1(2:3);
You can also use the following: R2([2,3]) = R1([2,3]);, in case indexes are not sequential.
In case R1 and R2 are two rows in a matrix, you can use the following sample:
% Create the input matrix A:
R1 = [12 13 15 17 200 23];
R2 = [32 22 43 67 21 74];
A = [R1; R2];
%Copy values from index 2 and 3 of first row to index 2 and 3 of second row:
A(2, [2,3]) = A(1, [2,3]);
In case there are more rows, and you need to "shift" all down, you can use the following example:
%Create sample matrix A (6x6 elements).
A = magic(6);
%"Shift" values of index 2,3 of all rows, one row down:
A(2:end, [2,3]) = A(1:end-1, [2,3]);
Refer here: http://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html

Scala for loop yield

I'm new to Scala so I'm trying to mess around with an example in Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition
// Returns a row as a sequence
def makeRowSeq(row: Int) =
for (col <- 1 to 10) yield {
val prod = (row * col).toString
val padding = " " * (4 - prod.length)
padding + prod
}
// Returns a row as a string
def makeRow(row: Int) = makeRowSeq(row).mkString
// Returns table as a string with one row per line
def multiTable() = {
val tableSeq = // a sequence of row strings
for (row <- 1 to 10)
yield makeRow(row)
tableSeq.mkString("\n")
}
When calling multiTable() the above code outputs:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
This makes sense but if I try to change the code in multiTable() to be something like:
def multiTable() = {
val tableSeq = // a sequence of row strings
for (row <- 1 to 10)
yield makeRow(row) {
2
}
tableSeq.mkString("\n")
}
The 2 is being returned and changing the output. I'm not sure where it's being used though to manipulate the output and can't seem to find a similar example searching around here or Google. Any input would be appreciated!
makeRow(row) {2}
and
makeRow(row)(2)
and
makeRow(row).apply(2)
are all equivalent.
makeRow(row) is of type List[String], each String representing one row. So effectively, you are picking character at index 2 from each row. That is why you are seeing 9 spaces and one 1 in your output.
def multiTable() = {
val tableSeq = // a sequence of row strings
for (row <- 1 to 10)
yield makeRow(row) {2}
tableSeq.mkString("\n")
}
is equivalent to applying a map on each row like
def multiTable() = {
val tableSeq = // a sequence of row strings
for (row <- 1 to 10)
yield makeRow(row)
tableSeq.map(_(2)).mkString("\n")
}

Matlab: find mode in range

I have a matrix like:
A=
10 31 32 22
32 35 52 77
68 42 84 32
I need a function like mode but with range, for example mymode(A,10) that return 30, find most frequent number in range 0-10, 10-20, 20-30, .... and return most number in range.
You can use histc to bin your data into the ranges of your desire and then find the bin with the most members using max on the output of histc
ranges = 0:10:50; % your desired ranges
[n, bins] = histc(A(:), ranges); % bin the data
[v,i] = max(n); % find the bin with most occurrences
[ranges(i) ranges(i+1)] % edges of the most frequent bin
For your specific example this returns
ans =
30 40
which matches with your required output, as the most values in A lay between 30 and 40.
[M,F] = mode( A((A>=2) & (A<=5)) ) %//only interested in range 2 to 5
...where M will give you the mode and F will give you frequency of occurence
> A = [10 31 32 22; 32 35 52 77; 68 42 84 32]
A =
10 31 32 22
32 35 52 77
68 42 84 32
> min = 10
min = 10
> max = 40
max = 40
> mode(A(A >= min & A <= max))
ans = 32
>
I guess by the number of different answers that we may be missing your goal. Here is my interpretation.
If you want to have many ranges and you want to output most frequent number for every range, create a cell containing all desired ranges (they could overlap) and use cellfun to run mode() for every range. You can also create a cell with desired ranges using arrayfun in a similar manner:
A = [10 31 32 22; 32 35 52 77; 68 42 84 32];
% create ranges
range_step = 10;
range_start=[0:range_step:40];
range=arrayfun(#(r)([r r+range_step]), range_start, 'UniformOutput', false)
% analyze ranges
o = cellfun(#(r)(mode(A(A>=r(1) & A<=r(2)))), range, 'UniformOutput', false)
o =
[10] [10] [22] [32] [42]

Remove the minimum values per each column of a Matrix

If I had a matrix A such as:
63 55 85 21 71
80 65 85 48 53
55 60 93 71 66
21 65 40 33 21
61 90 80 48 50
... and so on how would I find the minimum values of each column and remove those numbers from the matrix completely, meaning essentially I would have one less row overall.
I though about using:
[C,I] = min(A);
A(I) = [];
but that wouldn't remove the necessary numbers, and also reshape would not work either. I would like for this to work with an arbitrary number of rows and columns.
A = [
63 55 85 21 71
80 65 85 48 53
55 60 93 71 66
21 65 40 33 21
61 90 80 48 50
];
B = zeros( size(A,1)-1, size(A,2));
for i=1:size(A,2)
x = A(:,i);
maxIndex = find(x==min(x(:)),1,'first');
x(maxIndex) = [];
B(:,i) = x;
end
disp(B);
Another vectorized solution:
M = mat2cell(A,5,ones(1,size(A,2)));
z = cellfun(#RemoveMin,M);
B = cell2mat(z);
disp(B);
function x = RemoveMin(x)
minIndex = find(x==min(x(:)),1,'first');
x(minIndex) = [];
x = {x};
end
Another solution:
[~,I] = min(A);
indexes = sub2ind(size(A),I,1:size(A,2));
B = A;
B(indexes) = [];
out = reshape(B,size(A)-[1 0]);
disp(out);
Personally I prefer the first because:
For loops aren't evil - many times they are actually faster (By using JIT optimizer)
The algorithm is clearer to the developer who reads your code.
But of course, its up to you.
Your original approach works if you convert the row indices resulting from min into linear indices:
[m, n] = size(A);
[~, row] = min(A,[],1);
A(row + (0:n-1)*m) = [];
A = reshape(A, m-1, n);

MATLAB: create new matrix from existing matrix according to specifications

Assume we have the following data:
H_T = [36 66 21 65 52 67 73; 31 23 19 33 36 39 42]
P = [40 38 39 40 35 32 37]
Using MATLAB 7.0, I want to create three new matrices that have the following properties:
The matrix H (the first part in matrix H_T) will be divided to 3 intervals:
Matrix 1: the 1st interval contains the H values between 20 to 40
Matrix 2: the 2nd interval contains the H values between 40 to 60
Matrix 3: the 3rd interval contains the H values between 60 to 80
The important thing is that the corresponding T and P will also be included in their new matrices meaning that H will control the new matrices depending on the specifications defined above.
So, the resultant matrices will be:
H_T_1 = [36 21; 31 19]
P_1 = [40 39]
H_T_2 = [52; 36]
P_2 = [35]
H_T_3 = [66 65 67 73; 23 33 39 42]
P_3 = [38 40 32 37]
Actually, this is a simple example and it is easy by looking to create the new matrices depending on the specifications, BUT in my values I have thousands of numbers which makes it very difficult to do that.
Here's a quick solution
[~,bins] = histc(H_T(1,:), [20 40 60 80]);
outHT = cell(3,1);
outP = cell(3,1);
for i=1:3
idx = (bins == i);
outHT{i} = H_T(:,idx);
outP{i} = P(idx);
end
then you access the matrices as:
>> outHT{3}
ans =
66 65 67 73
23 33 39 42
>> outP{3}
ans =
38 40 32 37