How to find next character in Golang? - go

I'm just doing some algorithms problems out of interest in golang. I understand that in other languages to find the next character alphabetically I can bitshift the character, as a character (I'm thinking of C) is really a number.
So I tried doing
"a" >> 2
Or something to that effect, but there is a type mismatch.
I'd like to know how I can achieve this.

I am not sure where you get the idea that this gives you the 'next character'. This is not true in any language. What 'a' >> 2 does is this:
'a' is interpreted as int32(97) (example)
>> means 'shift X right by Y bits'. Shifting something right by 2 bits is functionally the same as an integer divide by 4. So (x >> 2) == (x / 4). (example)
97 / 4 == 24. The b character has ASCII value 98. So this doesn't get you anywhere near. (example)
More on the bit shifting
Bit shifting is most obvious when considering a number in its binary notation. For the expression z = x >> y, we can note the following:
x(97): 01100001
y(2): 00000010
-------- >>
z(24): 00011000
Note that all the bits in x have simply been moved to the right by two bits. The 1 that fell off the end is dropped.
Similarly, you can 'shift left' (<<). Just like x >> 1 is the same as x / 2, x << 1 is the same as x * 2.
Expression: 5>>1 == 5/2 == 2:
x(5): 00000101
y(1): 00000001
-------- >>
z(2): 00000010
Expression: 5<<1 == 5*2 == 10:
x(5): 00000101
y(1): 00000001
-------- <<
z(10): 00001010
Actually getting the next character
If you want the character directly following 'a', you simply add 1 to it as evidenced in this example.

You're trying to shift a string, not a byte, like #Not_a_Golfer said 'a'>>2 should work fine.
However to get the next character you can do something like:
func nextChar(ch byte) byte {
if ch += 1; ch > 'z' {
return 'a'
}
return ch
}
func main() {
fmt.Println(string(nextChar('a')))
}
Of course it'd be more complex if you need more than a-z support, take a look at the unicode package and this blog post about go strings.

Related

Generating unique non-similar codes with validation

I know there are similar questions so please bear with me.
I wish to generate approximately 50K codes for people to place orders - ideally no longer than 10 chars and can include letters and digits. They are not discount codes so I am not worried about people trying to guess codes. What I am worried about is somebody accidentally entering a wrong digit (ie 1 instead of l or 0 instead of O) and then the system will fail if by chance it is also a valid code.
As the codes are constantly being generated, ideally I don't want a table look-up validation, but an formula (eg if it contains an A the number element should be divisable by 13 or some such).
Select some alphabet (made of digits and letters) of size B such that there are no easy confusions. Assign every symbol a value from 0 to B-1, preferably in random order. Now you can use sequential integers, convert them to base B and assign the symbols accordingly.
For improved safety, you can append one or two checksum symbols for error detection.
With N=34 (ten digits and twenty four letters 9ABHC0FVW3YGJKL1N2456XRTS78DMPQEUZ), 50K codes require codes of length only four.
If you don't want the generated codes to be consecutive, you can scramble the bits before the change of base.
Before you start generating random combinations of characters, there are a couple of things you need to bear in mind:
1. Profanity
If your codes include every possible combination of four letters from the alphabet, they will inevitably include every four-letter word. You need to be absolutely sure that you never ask customers to enter anything foul or offensive.
2. Human error
People often make mistakes when entering codes. Confusing similar characters like O and 0 is only part of the problem. Other common mistakes include transposing adjacent characters (e.g. the → teh) and hitting the wrong key on the keyboard (e.g., and → amd)
To avoid these issues, I would recommend that you generate codes from a restricted alphabet that has no possibility of spelling out anything unfortunate, and use the Luhn algorithm or something similar to catch accidental data entry errors.
For example, here's some Python code that generates hexadecimal codes using an alphabet of 16 characters with no vowels. It uses a linear congruential generator step to avoid outputting sequential numbers, and includes a base-16 Luhn checksum to detect input errors. The code2int() function will return −1 if the checksum is incorrect. Otherwise it will return an integer. If this integer is less than your maximum input value (e.g., 50,000), then you can assume the code is correct.
def int2code(n):
# Generates a 7-character code from an integer value (n > 0)
alph = 'BCDFGHJKMNPRTWXZ'
mod = 0xfffffd # Highest 24-bit prime
mul = 0xc36572 # Randomly selected multiplier
add = 0x5d48ca # Randomly selected addend
# Convert the input number `n` into a non-sequential 6-digit
# hexadecimal code by means of a linear congruential generator
c = "%06x" % ((n * mul + add) % mod)
# Replace each hex digit with the corresponding character from alph.
# and generate a base-16 Luhn checksum at the same time
luhn_sum = 0
code = ''
for i in range(6):
d = int(c[i], 16)
code += alph[d]
if i % 2 == 1:
t = d * 15
luhn_sum += (t & 0x0f) + (t >> 4)
else:
luhn_sum += d
# Append the checksum
checksum = (16 - (luhn_sum % 16)) % 16
code += alph[checksum]
return code
def code2int(code):
# Converts a 7-character code back into an integer value
# Returns -1 if the input is invalid
alph = 'BCDFGHJKMNPRTWXZ'
mod = 0xfffffd # Highest 24-bit prime
inv = 0x111548 # Modular multiplicative inverse of 0xc36572
sub = 0xa2b733 # = 0xfffffd - 0x5d48ca
if len(code) != 7:
return -1
# Treating each character as a hex digit, convert the code back into
# an integer value. Also make sure the Luhn checksum is correct
luhn_sum = 0
c = 0
for i in range(7):
if code[i] not in alph:
return -1
d = alph.index(code[i])
c = c * 16 + d
if i % 2 == 1:
t = d * 15
luhn_sum += (t & 0x0f) + (t >> 4)
else:
luhn_sum += d
if luhn_sum % 16 != 0:
return -1
# Discard the last digit (corresponding to the Luhn checksum), and undo
# the LCG calculation to retrieve the original input value
c = (((c >> 4) + sub) * inv) % mod
return c
# Test
>>> print('\n'.join([int2code(i) for i in range(10)]))
HWGMTPX
DBPXFZF
XGCFRCN
PKKNDJB
JPWXNRK
DXGGCBR
ZCPNMDD
RHBXZKN
KMKGJTZ
FRWNXCH
>>> print(all([code2int(int2code(i)) == i for i in range(50000)]))
True

Is there a name for this algorithm? (I've been calling it changeBinary)

Is there a name for this algorithm? (I've been calling it changeBinary)
DESCRIPTION:
You take a binary string as input.
The first bit of the output is the same as the first bit of the input.
Every bit after that is 0 if the bit at that index of the input string is the same as the bit at the previous index in the input string. Otherwise, it's 1.
For example,
Input: 00011000001010100001001000010011
Output: 00010100001111110001101100011010
Here is a simple javascript implementation:
var changeBinary = function(binaryString){
var output = binaryString[0] === '0' ? '0' : 1;
for (var i = 1; i < binaryString.length; i++){
var nextBit = binaryString[i] === binaryString[i - 1] ? '0' : '1';
output += nextBit;
}
return output;
}
OBSERVATIONS:
First, it seems that if you keep applying the algorithm to a string, it eventually returns to its original value. Second, it the number of iterations it takes to do so seems to always be a power of 2 (including 2^0 = 1). For example, if you apply the changeBinary function above 32 times to the string above, it will return to the original value.
Has anyone ever encountered this before, and if so, do you know of any other information about it?
It just seems to me like this is something so simple and basic that someone must have studied it more in depth.
Any feedback would be greatly appreciated.
It may be interesting to know that this is x ^ (x << 1) on a BigInteger (or, if you limit the length of the strings, the same thing but on a fixed-size integer), also describable as clmul(x, 3).
Carryless multiplication, which is essentially just like normal multiplication, but instead of adding the partial products you XOR them, has some fairly nice properties, such as being commutative and associative. The associative property is especially of interest since it allows you to reason easily about what composing your algorithm with itself a couple of times does: for example
changeBinary o changeBinary is clmul(clmul(x, 3), 3) = clmul(x, clmul(3, 3)) = clmul(x, 5)
That it's a carryless multiplication by 3 also explains why it "undoes" itself when applied often enough, as the carryless multiplicative inverse of 3 is the number with all bits set, which with 32 bits is 0xffffffff, which can be formed as 331 (with carryless exponentiation). This also follows from the equivalence of a carryless square to a "bit-spread", so it takes a bit string abcd to a0b0c0d, and thus clpow(3, 32) = 1 - 5 spreads have spread the bits so far apart that only the original lsb is left over, the rest does not fit in a 32bit number.
And that also gives a faster inversion, because the number with all bits set can be decomposed into small number of (carryless) factors:
3 x 5 x 17 x 257 x 65537 ...
With a number of factors that is the base two logarithm of the number of bits (rounded up).
Since x ^ (x >> 1) converts a number to Gray Code, I suppose you might call this a "mirrored" Gray Code. The same trick with the factors is used "in the mirror image" to convert a Gray Code back to binary:
x ^= x >> 1 // this is like a "mirror" of x = clmul(x, 3)
x ^= x >> 2 // 5
x ^= x >> 4 // 17
x ^= x >> 8
x ^= x >> 16
Here we just flip the direction of the shift to get:
x ^= x << 1
x ^= x << 2
x ^= x << 4
x ^= x << 8
x ^= x << 16
Which is clmul(x, 0xffffffff) and has also been called PS-XOR(x)
The algorithm you described is an example of Delta Encoding.

Rational comparison of bits

I have a number of type int. It lies within [0,255]. That is, includes 8 bits. I need to check often say:
2(int) = 00000010(Binary)
1. The bit 6 and bit 7 must be equal to 0 and 1 respectively.
And I check it like this:
if ((!(informationOctet_ & (1 << 6))) && (informationOctet_ & (1 << 7)))
{
...
}
But it is not very readable, whether it is possible - to do something "beautiful"?
I can not use the std::bitset, my head says it's a waste of resources and you can not do without it.
There are two reasonable solutions: either set all don't-care bits to zero, and then test the result, or set the don't-care bits to one and test the result:
(x & 0xC0) == 0x80
(x | ~0xC0) == ~0x40
As harold pointed out in the comment, the first form is far more common. This pattern is so common that the optimizer of your compiler will recognize it.
Other forms exist, but they're obscure: ((x ^ 0x80) & 0xC0 == 0) works just as well but is less clear. Some ISA's cannot load large constants directly, so they use the equivalent of ((x>>6) & 0x3) == 0x2. Don't bother with this, your optimizer will.
You can apply some masking techniques as,
int i = 246; // Lets say any value.
int chk = ( i & 00000110b ); // eliminates all other bits except 6th & 7th bit
if (chk == 2) // because we want to check 6th bit is 0 & 7th is 1, that becomes 2 value in decimal
printf("The 6th bit is 0 & 7th bit is 1");
else
printf("Either 6th bit is not 0 or 7th bit is not 1, or both are not 0 & 1 respectivly");

Innovative way for checking if number has only one on bit in signed int

I'm looking for an innovative way to check if a number has only one on bit in a signed int.
I am well aware that I can simply do a loop with a counter, some modular division, and a bit shift. But I'm curious if there is a better way since we are only looking for ONE bit to be on.
bool HasOnlyOneBit (int numb)
{
//return true if numb has only one bit (I.E. is equal to 1, 2, 4, 8, 16... Int.MinValue)
}
return x == (x & -x);
This answer works because of the way two's complement notation is designed.
First, an example. Assume we have 8-bit signed integers.
00010000 = 16
11110000 = -16
The bitwise and will give you 00010000 as a result, equal to your original value! The reason that this works is because when negating in 2's complement, first invert all the bits, then add 1. You'll have a bunch of zeros and a bunch of carries until a one falls into place. The bitwise and then checks if we have the right bit set.
In the case of a number that isn't a power of two:
00101010 = 42
& 11010110 = -42
----------
00000010 != 42
Your result will still have only a single bit, but it won't match the original value. Therefore your original value had multiple bits set.
Note: This technique returns true for 0, which may or may not be desirable.
This is a famous problem
(x & x-1) == 0
Power of 2 from Wiki : here
64 = 01000000 (x)
63 = 00111111 (x-1)
______________
& = 00000000 == 0
______________
Case when some other bits are ON
18 = 00010010 (x)
17 = 00010001 (x-1)
______________
& = 00010000 != 0
______________
I'd recommend you take a look at the Bit Twiddling Hacks page and choose the most suitable option under "Determining if an integer is a power of 2" or "Counting bits set".
return (x && ((x & x-1) == 0))
return (x && (0x8000000000000000ULL % x));
This is a simplification of the following code:
if (x == 0) {
return false;
} else if (0x8000000000000000ULL % x) {
return false;
} else {
return true;
}
Explanation: 0x8000000000000000 is the highest "1 bit only" value for an 64 bit register. Only a division by an other "1 bit only" value will result in no remainder.
Take the log to base 2 of your number, if it's an integer your number has only 1 1 bit. Not sure that I think this is better than any of your excluded options.
Python3 memory-efficient solution
return n > 0 and (n & (n-1)) == 0

Interview qns...Do the below without any conditional or comparison operator

Do the below without any conditional or comparison operator.
if (Number <= 0)
{
Print '0';
}
else
{
print Number;
}
thanks..
My original simple solution:
1. print( (abs(Number)+Number) / 2 )
That solution would work in most cases, unless Number is very large (more than half the maximum e.g. Number >= MAX_INT/2) in which case the addition may cause overflow.
The following solution solves the overflow problem:
2. print( (abs(Number)/2) + (Number/2) )
However, there may be a case in which Number is and must remain integer, and the division operator (/) is integer division, so that 7/2=3. In this case solution 2 won't work because if Number=7 it will print 6 (for this case solution 1 will work just fine).
So if we need to deal with both large numbers AND integer arithmetic, the following monstrosity comes to the rescue, adding compensation for the 1 that may be lost in the division by 2 in case of odd integer:
3. print(
( (abs(Number)/2)+(Number/2) ) +
((
(Number-(2*(Number/2))) +
(abs(Number)-(2*(abs(Number)/2)))
) / 2)
)
print max(0, number)
Let's say that number is represented by an 8-bit two's complement integer.
Positive numbers including 0 all have the MSB set to 0.
Negative numbers all have the MSB set to 1.
So we take the complement of the MSB, extend it to the full 8 bits, and bitwise AND it with the original number, e.g.
Positive:
00110101 -> MSB is 0
11111111 -> complement of MSB extended
00110101 -> bitwise AND of above
Negative:
10110101 -> MSB is 1
00000000 -> complement of MSB extended
00000000 -> bitwise AND of above
No comparisons needed - I'm kind of assuming that bitwise AND isn't strictly a comparison.
Also, sorry for the lack of code, but you get the idea.
I haven't seen a solution yet that is valid for the complete domain.
An other solution is to call a function that raises an exception if the input value is 0 or below 0. Then catch the exception and print 0.
Or you can use a function Sign, that returns -1 if the input is <0, 0 if it's 0 and 1 otherwise.
print ((sign(x)+1) * sign(x) / 2) * x.
sign can be -1, 0 or 1, so ((sign(x)+1) * sign(x) / 2) can have the following values:
-1 -> ((-1+1)*-1)/2 = 0
0 -> ((0+1)*0)/2 = 0
1 -> ((1+1) * 1)/2 = 1
Another method is to create a lookup table that maps all non negative numbers to themself and the rest to 0.
But, in my opinion, the original function is much clearer. So why violate the KISS principle.
Similar to the accepted answer. Although acceptable where absolute value is implemented using comparisons, but also more prone to overflow:
print( (sqrt(Number * Number) + x) / 2);
Assuming C or C++:
switch ((unsigned long)Number & ~(unsigned long)LONG_MAX) {
case 0:
printf("%d\n", Number);
break;
default:
printf("0\n", Number);
break;
}
If you consider switch to be a conditional operator, then try this:
unsigned long flag = (unsigned long)Number & ~(unsigned long)LONG_MAX;
flag /= (unsigned long)LONG_MAX + 1;
flag = 1 - flag;
printf("%d\n", Number * flag);

Resources