convert number to combination of letters - c#

I need to convert a number between 1 and 6000000 to a letter combination like ABCDE.
Less letters is better. but i'm guessing i will need 4 or 5.
Can someone point me in the right direction as how to write an algorithm to convert numbers to letters and back? only A-Z. (caps).

You need to convert to base-26 numbering: 0 is A, 1 is B, 25 is Z, 26 is BA, etc.
The Hexavigesimal Wikipedia article has code for conversion to base 26.

There are 26 letters in the alphabet.
TYou have 26^4 < 6 000 000 and 26^5 > 6 000 000
Then you will need 5 letters, for most of your elements
Now you just need to express your number in base 26.
Their is only one way to write an X in 0 ... 6 000 000 as follow:
X = a4*26^4 + a3*26^3+ a2*26^2+ a1*26^1+a0
ai in {0,...25} then you just map ai with a letter from A to Z

The most naive thing to do would be to let A,B,...,Z represent the numbers 0,1,...,25 and the just convert your number to base 26 to get the alphabetic conversion.
For example, there is a C# implementation in this answer to this post.

Well if you want to convert from the decimal representation, then there are 10 digits [0-9] and if you want to have one character per decimal digit in the result, then you will need ten alpha characters. But if you convert from the binary representaion, just replace every 0 with an 'A' and every 1 with a 'B'...
everything depends on how you want to do it... The base you decide to use will determine how many letters you will need.
as an example, to do it from a binary representation,
take the number mod 2. If the result is 0 add an 'A' if its a 1, add a 'B'
Divide the number by 2 (or rightshift it one position.)
repeat until number is zero.
start with value of 57
1. 57 Mod 2 = 1 A
2. 57 / 2 = 28
3. 28 Mod 2 = 0 BA
4. 28 / 2 = 14
5. 14 mod 2 = 0 BBA
6. 14 / 2 = 7
7. 7 mod 2 = 1 ABBA --- A musical group !
8. 7 / 2 = 3
9. 3 mod 2 = 1 AABBA
10. 3/ 2 = 1
11. 1 mod 2 = 1 AAABBA
12. 1 / 2 = 0 --- -done

You should equate A = 0, B = 1 and so on upto Z = 25.
This would become a number system with the base (or radix) 26.
With this in mind, two digits can represent numbers ranging from 0 - 675 (ZZ = 675).
3 Digits would represent 26^3. i.e 0 - 17575.
With 5 digits you can represent from 0 - 11881375 (ZZZZZ).
You can take any standard algorithm that converts between decimal to its own radix to do that.
Conversion between Number bases can be referenced for help.

Related

how to find digits (in min-max range) and all sequence with equal to sum of digits=one number

Input:
Number of digits in the number
Sum of digits
Range of digits that can be used
Output:
All possible numbers where the sum of digits equals the one defined as an input.
Examples:
(num_digits = 4, sum = 12, range = 0-4)
3 4 4 1
4 4 4 0
4 0 4 4
1 4 4 3
(num_digits = 5, sum = 18, range = 0-7)
5 5 5 2 1
1 2 5 5 5
5 5 2 1 5
0 2 2 7 7
7 7 0 2 2
2 2 7 7 0
(num_digits = 3, sum = 20, range = 0-8)
8 8 4
4 8 8
8 4 8
7 7 6
7 8 5
general method (algorithm) is preferred, however, C# can be a language to chose to resolve
regards
If I were you, I would use recursion, since in 1234 and 1235 sum of 123 is common, so it shouldn't be calculated multiple times.
Your recursive function has two main rules:
The max depth of recursion = number of digits.
The top-level calls the lower level with every possible digit
possible in your case = the range of digits.
Things you need to use in this exercise:
an exit condition for the recursion
loops in a given range
recursive functions
Some type of array/list (be aware that they are copied by memory address and not by value. https://stackoverflow.com/a/4347957/8549369)
This should be enough to write the program, but get used to one thing on StackOverflow: Copy an A-Z solution is not solving an algorithmic task.

Calculate the total amount (number) of combinations of a set with the specified combination length? [duplicate]

This question already has answers here:
Calculating all possible sub-sequences of a given length (C#)
(4 answers)
Closed 5 years ago.
Having a set of elements, which in this case is an Array of 3 characters/elements {A, B, C}:
char[] charSet = "ABC".ToCharArray();
I would like to write a generic usage function to help determine which would be the total amount of combinations that can be generated OF THE SPECIFIED LENGTH and determining too the amount of possible combinations with and without repetition. To avoid possible mistakes: this question is not about combo/perm generation, just calculation.
A simple uncompleted example to understand me:
public static long CalculateCombinations(int setLength, int comboLength, bool allowRepetition)
{
return result;
}
( where setLength is the amount of elements in the set, comboLength is the desired length of each combination, and allowRepetition a deterministic flag to help calculate the amount of combinations when and when not elements repetition is allowed in each combination. )
Then, if I have the same character set specified above, and I want to calculate the amount of possible combinations with repetition, the algorithm should return a value of 9, which would be the equivalent amount to this serie of combinations:
1: AA
2: AB
3: AC
4: BA
5: BB
6: BC
7: CA
8: CB
9: CC
The same algorithm should return me a value of 6 if I dont want repetition, which would be the equivalent amount to this serie of combinations:
1: AB
2: AC
3: BA
4: BC
5: CA
6: CB
Basically I'm trying to reproduce what this online service can do: http://textmechanic.com/text-tools/combination-permutation-tools/combination-generator/ however I tried to investigate and implement different 'nCr' formulas around the WWW (like http://www.vcskicks.com/code-snippet/combination.php ) and StackOverflow threads (like https://stackoverflow.com/a/26312275/1248295 ), but i don't get it how to calculate it when the combination length factor and repetition is involved in the calculation. Maybe this could be too basic than what it appears to me, but maths are not my forte.
My question: how can I write an algorithm that can calculate what I explained?. Would be very grateful if someone could link a formula and its implementation in C# or VB.NET.
Let's try it with three characters, A, B and C (n = 3) and combo length of k = 2, as your example states.
With repetition
We start with two empty spaces.
The first empty space can be filled in 3 possible ways.
For each of three possible ways, the second space can be filled in another three possible ways.
This gives you a total of 3 × 3 possibilities.
In general, there are n ^ k possibilities.
Without repetition
We start with two empty spaces.
The first empty space can be filled in 3 possible ways.
The second empty space can be filled in 2 possible ways, because you don't want to repeat yourself.
This gives you 3 × 2 possibilities in your case.
Let's go with another example. Say, you have five letters (ABCDE) and combo length of four _ _ _ _.
We put any of five letters on the first empty space. This is five possibilities: A, B, C, D, E.
Now for each possibility after the last step, no matter which letter we've chosen, now we have 4 letters left to choose from. If in the previous step we've chosen A, the corpus is now BCDE -- this is four possibilities. For B, we choose from ACDE -- this is again for possibilities. In total, since there were 5 ways to do previous step, and there are 4 ways to go after any of the previous choices, in total this is 20 possibilities: (AB, AC, AD, AE), (BA, BC, BD, BE), (CA, CB, CD, CE), (DA, DB, DC, DE), (EA, EB, EC, ED).
Let's keep going. After picking two letters, we're left with 3. With the same logic as before, for each of the previous 20 possibilities we have another 3 possibilities. This is 60 in total.
And one more space left. We have two letters which we haven't chosen before. From any of the previous 60 possibilities, we now have two possibilities. That's 120 in total.
So we've arrived at this by multiplying 5 × 4 × 3 × 2. Why start from 5? Because we initially had 5 letters: ABCDE. Why have four numbers in our multiplication? Because there were 4 empty spaces: _ _ _ _.
In general, you keep multiplying a decremented value starting from n, and do this k times: n × (n - 1) × ... × (n - k + 1).
The last value is (n - k + 1) because you are multiplying k values in total. From n to (n - k + 1) there are k values in total (inclusive).
We can test this with our n = 5 and k = 4 example. We said that the formula was 5 × 4 × 3 × 2. Now look at the general formula: indeed, we start from n = 5 and keep multiplying until we reach the number 5 - 4 + 1 = 2.
In your function's signature, n is setLength, k is comboLength. The implementation should be trivial with the above formulas, so I'm leaving this to the reader.
These are called permutations with and without repetition.

Dynamic programming - mobile

I have to apply dynamic programming on this problem, but I am not really sure how.
There are 50 buttons with 2000 letters on the mobile phone (sorted alphabetically). Each letter has k-position at the button (letter is typed by k presses). Then we know, how much is the letter used. Program will determine, on which button the letter should be positioned in order to find out minimum number of presses.
Input:
number of buttons
number of letters
frequency of each character in average letter.
Example:
3 // number of buttons
5 // number of letters
1 // frequency of first letter
1 // frequency of second letter
1 // frequency of third letter
1 // frequency of fourth letter
1 // frequency of fifth letter
Solution:
Buttons: 1 2 | 3 4 | 5
1 * 1 + 2 * 1 = 3
1 * 1 + 2 * 1 = 3
1 * 1 = 1
Output is: 3 + 3 + 1 = 7
Program will output: 7
I have made solution to this example. I've created two matrices. One has SS dimension and the other one has KS dimension. In SS matrix each element represent price of the button, which has characters from i coordinate to j coordinate. In KS matrix i represents number of buttons and j represents characters to j.
I have a problem, how to find buttons with minimul cost.
For example:
I want to find in KS table value in [2,3] coordinate. It means, we are spliting three characters in to two buttons. Optimal solution is, that one button will have 2 characters and other one will have one character. In SS solution would be [1,1] + [2,3] or [1,2] + [3,3].
I will appreciate every advice.
table S*S
1 3 6 10 15
0 1 3 6 10
0 0 1 3 6
0 0 0 1 3
0 0 0 0 1
table K*S
1 3 6 10 15
0 2 4 6 9
0 0 3 5 7
You may be familiar with the reasoning behind a Huffman Code, where for optimal usage, or highest efficiency, the least amount of work should be done to get the highest frequency letters. This logic holds for your problem as well. You want to perform the least amount of button presses to reach the highest frequency letters.
Let's say we have numB buttons and numL letters. Let's also assume we have an object called Letter with char letter, and int frequency attributes, and finally, we have LetterCol which is a collection of Letter objects. (My c# is rusty, bear with me).
Step 1:
Sort these letters by frequency.(Any sort function will work). We will use a collection of arraylists of Letters Keypad. It will function like a map.
Each position in Keypad will be an arraylist. Each position will relate to one button. Keypad[0] relates to button 0. Keypad[0] is also an ArrayList of letters. This will be populated with the letters we wish to place on button 0.
List<ArrayList<Letter>> Keypad = new ArrayList<>();
Step 2:
Since LetterCol is sorted by frequency, we just place each letter sequentially on Keypad. We will use the mod function % to ensure we stay within our bounds (i.e., we don't exceed the number of buttons numB).
for(int i = 0; i < numL; i++)
Keypad[i%numB].add(LetterCol[i]);
Now, our placement process is complete. Each letter is placed in it's optimal position. It is time to calculate the output.
Step 3:
We now need to access each position in Keypad and retrieve each letter in those ArrayLists.
int output = 0;
for(int i = 0; i < numB; i++)
for(int j = 0; j < Keypad[i].count; j++){
output += (j+1)*(Keypad[i][j].frequency);
//My syntax may be incorrect, in Java it is: output += (j+1)*(Keypad[i].get[j].frequency);
Now we contain the optimal output. In your numbers example, you go to each button and multiply the number of presses to the frequency of the letter. These for loops perform the same calculation. We go to each button i, multiply the number of presses j+1 to the frequency of the letter Keypad[i][j].frequency.

Understanding hexadecimals and bytes in C#

I seem to lack a fundemental understanding of calculating and using hex and byte values in C# (or programming in general).
I'd like to know how to calculate hex values and bytes (0x--) from sources such as strings and RGB colors (like how do I figure out what the 0x code is for R255 G0 B0 ?)
Why do we use things like FF, is it to compensate for the base 10 system to get a number like 10?
Hexadecimal is base 16, so instead of counting from 0 to 9, we count from 0 to F. And we generally prefix hex constants with 0x. Thus,
Hex Dec
-------------
0x00 = 0
0x09 = 9
0x0A = 10
0x0F = 15
0x10 = 16
0x200 = 512
A byte is the typical unit of storage for values on a computer, and on most all modern systems, a byte contains 8 bits. Note that bit actually means binary digit, so from this, we gather that a byte has a maximum value of 11111111 binary. That is 0xFF hex, or 255 decimal. Thus, one byte can be represented by a minimum of two hexadecimal characters. A typical 4-byte int is then 8 hex characters, like 0xDEADBEEF.
RGB values are typically packed with 3 byte values, in that order, RGB. Thus,
R=255 G=0 B=0 => R=0xFF G=0x00 B=0x00 => 0xFF0000 or #FF0000 (html)
R=66 G=0 B=248 => R=0x42 G=0x00 B=0xF8 => 0x4200F8 or #4200F8 (html)
For my hex calculations, I like to use python as my calculator:
>>> a = 0x427FB
>>> b = 700
>>> a + b
273079
>>>
>>> hex(a + b)
'0x42ab7'
>>>
>>> bin(a + b)
'0b1000010101010110111'
>>>
For the RGB example, I can demonstrate how we could use bit-shifting to easily calculate those values:
>>> R=66
>>> G=0
>>> B=248
>>>
>>> hex( R<<16 | G<<8 | B )
'0x4200f8'
>>>
Base-16 (also known as hex) notation is convenient because you can fit four bits in exactly one hex digit, making conversion to binary very easy, yet not requiring as much space as a full binary notation. This is useful when you need to represent bit-oriented data in a human-readable form.
Learning hex is easy - all you need to do is memorizing a short table of 16 rows defining hex-to-binary conversion:
0 - 0000
1 - 0001
2 - 0010
3 - 0011
4 - 0100
5 - 0101
6 - 0110
7 - 0111
8 - 1000
9 - 1001
A - 1010
B - 1011
C - 1100
D - 1101
E - 1110
F - 1111
With this table in hand, you can easily convert hex strings of arbitrary length to their corresponding bit patterns:
0x478FD105 - 01000111100011111011000100000101
Converting back is easy as well: group your binary digits by four, and use the table to make hex digits
0010 1001 0100 0101 0100 1111 0101 1100 - 0x29454F5C
In decimal, each digit is weighted 10 times more than the one to the right, for example the '3' in 32 is 3 * 10, and the '1' in 102 is 1 * 100. Binary is similar except since there are only two digits (0 and 1) each bit is only weighted twice as much as the one to the right. Hexadecimal uses 16 digits - the 10 decimal digits along with the letters A = 10 to F = 15.
An n-digit decimal number can represent values up to 10^n - 1 and similarly an n-digit binary number can represent values up to 2^n - 1.
Hexadecimal is convenient since you can express a single hex digit in 4 bits since 2^4 = 16 possible values can be represented in 4 bits.
You can convert binary to hex by grouping from the right 4 bits at a time and converting each group to the corresponding hex. For example 1011100 -> (101)(1100) -> 5C
The conversion from hex to binary is even simpler since you can simply expand each hex digit into the corresponding binary, for example 0xA4 -> 1010 0100
The answer to the actual question posted ("Why do we use things like FF, is it to compensate for the base 10 system to get a number like 10?") is this: Computer use bits, that means either 1 or 0.
The essence is similar to what Lee posted and called "positional notation". In a decimal number, each position in the number refers to a power of 10. For example, in the number 123, the last position represents 10^0 -- the ones. The middle position represents 10^1 -- the tens. And the first is 10^2 -- the hundreds. So the number "123" represents 1 * 100 + 2 * 10 + 3 * 1 = 123.
Numbers in binary use the same system. The number 10 (base 2) represents 1 * 2^1 + 0 * 2^0 = 2.
If you want to express the decimal number 10 in binary, you get the number 1010. That means, you need four bits to represent a single decimal digit.
But with four bits you can represent up to 16 different values, not just 10 different values. If you need four bits per digit, you might as well use numbers in the base 16 instead of only base 10. That's where hexadecimal comes into play.
Regarding how to convert ARGB values; as been written in other replies, converting between binary and hexadecimal is comparatively easy (4 binary digits = 1 hex digit).
Converting between decimal and hex is more involving and at least to me it's been easier (if i have to do it in my head) to first convert the decimal into binary representation, and then the binary number into hex. Google probably has tons of how-tos and algorithms for that.

Understanding the behavior of a single ampersand operator (&) on integers

I understand that the single ampersand operator is normally used for a 'bitwise AND' operation. However, can anyone help explain the interesting results you get when you use it for comparison between two numbers?
For example;
(6 & 2) = 2
(10 & 5) = 0
(20 & 25) = 16
(123 & 20) = 16
I'm not seeing any logical link between these results and I can only find information on comparing booleans or single bits.
Compare the binary representations of each of those.
110 & 010 = 010
1010 & 0101 = 0000
10100 & 11001 = 10000
1111011 & 0010100 = 0010000
In each case, a digit is 1 in the result only when it is 1 on both the left AND right side of the input.
You need to convert your numbers to binary representation and then you will see the link between results like 6 & 2= 2 is actually 110 & 010 =010 etc
10 & 5 is 1010 & 0101 = 0000
The binary and operation is performed on the integers, represented in binary. For example
110 (6)
010 (2)
--------
010 (2)
The bitwise AND is does exactly that: it does an AND operation on the Bits.
So to anticipate the result you need to look at the bits, not the numbers.
AND gives you 1, only if there's 1 in both number in the same position:
6(110) & 2(010) = 2(010)
10(1010) & 5(0101) = 0(0000)
A bitwise OR will give you 1 if there's 1 in either numbers in the same position:
6(110) | 2(010) = 6(110)
10(1010) | 5(0101) = 15(1111)
6 = 0110
2 = 0010
6 & 2 = 0010
20 = 10100
25 = 11001
20 & 25 = 10000
(looks like you're calculation is wrong for this one)
Etc...
Internally, Integers are stored in binary format. I strongly suggest you read about that. Knowing about the bitwise representation of numbers is very important.
That being said, the bitwise comparison compares the bits of the parameters:
Decimal: 6 & 2 = 2
Binary: 0110 & 0010 = 0010
Bitwize AND matches the bits in binary notation one by one and the result is the bits that are comon between the two numbers.
To convert a number to binary you need to understand the binary system.
For example
6 = 110 binary
The 110 represents 1x4 + 1x2 + 0x1 = 6.
2 then is
0x4 + 1x2 + 0x1 = 2.
Bitwize and only retains the positions where both numbers have the position set, in this case the bit for 2 and the result is then 2.
Every extra bit is double the last so a 4 bit number uses the multipliers 8, 4, 2, 1 and can there fore represent all numbers from 0 to 15 (the sum of the multipliers.)

Categories

Resources