left operand must be l-value - c

could some one help. getting error with these 2 lines of code. num_red - count_red = red_pot;// all defined as 0
and
while (count_red = 0 && count_yellow = 0 && count_green = 0 && count_brown = 0 && count_blue = 0 && count_pink = 0)
{
if (count_black = 0)
{
score = score + 7;
printf("Score: %d\n", score);
num_balls = num_balls - 1;
}
}

If that's a C-like language, you need to use == for equality checks, not =. The single = is for assignment so that:
int seven = 7;
int five = 5;
if (seven - five == 2) ...
is okay, but:
int seven = 7;
int five = 5;
if (seven - five = 2) ...
will, even if it compiles, not do what you expect.
You have a classic example in your code. The segment:
if (count_black = 0) blah;
will not execute blah when count_black is zero. It will set count_black to zero and steadfastly refuse to ever execute blah, since the result of count_blah = 0 is 0 (false).
If you want the equality:
num_red - count_red == red_pot
to be true, you need to assign one of those variables (the "unknown" one) based on the other two "known" ones. For example, if num_red and count_red are known, set red_pot with:
red_pot = num_red - count_red;
Alternatively, if red_pot and count_red are known, set num_red with:
num_red = count_red + red_pot;

Related

What's an inductive invariant for this piece of code?

For this piece of code:
// n is a user input that can be any integer
s = 0
i = 0
while i < n:
s = s + 1
i = i + 1
return s
I would like to prove that the post condition is if n > 0 then s = sum(0, n) else s = 0 where sum(s,e) just adds 1 from s to e exclusive, starting from initial value of 0.
I thought an invariant is
if n > 0 and i < n then s = sum(0, i) else s = 0 but I can't get it to be proven in Coq or z3. Any hints?
You seem to imply that this algorithm computes the sum but it doesn't actually do that. Instead, it'll count up to n. Perhaps what you intended is:
i = 0
s = 0
while i < n:
i = i+1
s = s+i
Note that we increment s by i, not by 1 as in your program.
Assuming this is the intended program, then a good invariant would be:
s is the sum of all numbers upto and including i
i is at most n
In more programmatic notation:
s == i*(i+1)/2 && i <= n
To see why, remember that the invariant has to hold before and after each loop iteration; and when the loop condition is false, it needs to imply your post-condition. That's why you need the conjunct i <= n, so that when you exit the loop, s will contain the sum indeed.
How about this solution:
// Function left unimplemented, for simplicity
function sum(s: Int, e: Int): Int
ensures result == e - s
method foo(n: Int) returns (s: Int)
requires 0 <= n
{
var i: Int := 0
s := 0
while (i < n)
invariant s == n - sum(i, n)
{
s := s + 1
i := i + 1
}
}
Language and tool are called Viper. You can try your example online (the web interface is somewhat slow and unstable), or use the VSCode plugin.

Possible string permutations of mixture of multiset and set

I am trying to get all possible combinations of a char*. This string consits of four values: two numbers and two different letters. For example:
char *text = "01ab";
There should be
so
different combinations for my example string, which seems to be true (done by hand):
Combinations for values: 0, 1, a, b:
0 0 a b 1 1 a b a 0 0 b b 0 0 a
0 0 b a 1 1 b a a 0 1 b b 0 1 a
0 1 a b 1 0 a b a 0 b 0 b 0 a 0
0 1 b a 1 0 b a a 0 b 1 b 0 a 1
0 a 0 b 1 a 1 b a 1 b 0 b 1 0 a
0 a 1 b 1 a 0 b a 1 b 1 b 1 1 a
0 a b 0 1 a b 1 a 1 0 b b 1 a 0
0 a b 1 1 a b 0 a 1 1 b b 1 a 1
0 b 0 a 1 b 1 a a b 0 0 b a 0 0
0 b 1 a 1 b 0 a a b 0 1 b a 0 1
0 b a 0 1 b a 1 a b 1 0 b a 1 0
0 b a 1 1 b a 0 a b 0 0 b a 1 1
My approach would be the same as the one I did by hand:
get all combinations with the first index of text at the start, then all combinations of the second index of text and so on. So something like this:
void printPasswordCombinations()
{
char *all_values = "01ab";
int len = strlen(all_values);
char *tmp_pwd = malloc(sizeof(len) * sizeof(char));
for(int i=0 ; i<len ; i++)
{
tmp_pwd[0] = all_values[i];
/* len-1, since the first index is already set. */
for(int j=0 ; j<len-1 ; j++)
{
}
}
printf("%s\n", tmp_pwd);
free(tmp_pwd);
}
Now I am a bit confused about how to continue after the first index of the combination. There are several examples for all combinations, but my problem seems to be a bit different, since my the numbers in the combination could be the same and only the letters have to be different.
How could I achieve to print all combinations to my console?
I implemented a function which calculates the amount of possible combinations, so just assume this is already done.
It would be nice if the algorithm would work for any amounts of numbers and letters, so for example all combinations of a text of lenght 6 with four different numbers and two different letters could also be calculated.
The language doesn't matter, any advice is appreciated.
Your problem can be solved by backtracking strategy. It will create all
possible combinations.
I know you want to remove duplicate combinations in case the two number are the same, to get rid of them, you can use a hash table to store generated combination, and then, each time you generate a new combination, bring it to the hash table to check if it was generated or not(if not, enter it to the hash table and print it out, ignore printing in vice versa). There for my pseudocode as follow (you can have a better way):
val characters = [/*4 characters*/]
val picked = [false,false,false,false]
val hashtable = empty
function genetate(string aCombin):
if aCombin.size == 4:
if(hashtable.contains(aCombin)):
//do nothing
else:
print(aCombin)
hashtable.add(aCombin)
for i in characters.size:
if(picked[i]==false):
picked[i]=true
aCombin.add(characters[i])
generate(aCombin)
picked[i]=false //backtrack
aCombine.popBack() //remove the last character
I used Javascript because it can run in browser and language doesn't matter. The below method uses recursion. Try it with '0123ab'.
'use strict';
const input = '01ab';
const reLetters = /[^0-9]/g;
const reDigits = /[0-9]/g;
const nLetters = input.replace(reDigits, '').length;
const nDigits = input.replace(reLetters, '').length;
const findComb = cur => {
if (cur.length === input.length)
return console.log(cur);
for (let l of input) {
if (l.match(reDigits)) {
if (cur.replace(reLetters, '').length === nDigits) continue;
} else {
if (cur.match(l) || cur.replace(reDigits, '').length === nLetters) continue;
}
findComb(cur + l);
}
}
findComb('');
Here is a version without "removing letters to count digits". it is about 20% more efficient. I used nodejs and '01234abc' as input to measure.
'use strict';
const input = '01ab';
const reLetters = /[^0-9]/g;
const reDigits = /[0-9]/g;
const maxLetters = input.replace(reDigits, '').length;
const maxDigits = input.replace(reLetters, '').length;
const findComb = (cur = '', nDigits = 0, nLetters = 0) => {
if (cur.length === input.length)
return console.log(cur);
for (let l of input) {
if (l.match(reDigits)) {
if (nDigits < maxDigits)
findComb(cur + l, nDigits + 1, nLetters);
} else {
if (cur.match(l)) continue;
if (nLetters < maxLetters)
findComb(cur + l, nDigits, nLetters + 1);
}
}
}
findComb();
Here it is without recursion. This is slowest of all, but can be improved.
'use strict';
const input = '01ab';
const reLetters = /[^0-9]/g;
const reDigits = /[0-9]/g;
const nLetters = input.replace(reDigits, '').length;
const nDigits = input.replace(reLetters, '').length;
let cur = '', l = undefined;
do {
l = input[input.indexOf(l) + 1];
if (l !== undefined) {
if (l.match(reDigits)) {
if (cur.replace(reLetters, '').length === nDigits) continue;
} else {
if (cur.match(l) ||
cur.replace(reDigits, '').length === nLetters) continue;
}
if (cur.length + 1 === input.length) {
console.log(cur + l);
} else {
cur = cur + l;
l = undefined;
}
} else {
l = cur[cur.length - 1];
cur = cur.slice(0, -1);
}
} while (cur != '' || l != undefined);
A recursive approach would be the easy way here.
Let's consider that you want to generate all strings with m letters, all of them distinct, taken from a letters[m] array, and n numbers, that can be repeated, taken from a numbers[N] array (n can be smaller, of same size of bigger than N, it does not really matter).
You can solve it this way then (pseudo code, C style):
void print_them_all(char *numbers, int nb_numbers_in_result, int n \
char *letters, bool *is_letter_used, int nb_letters_in_result, int m,
char *current_string){
if ((nb_numbers_in_result == n) && (nb_letters_in_result == m)){
// terminal case -> time to print the current string
printf("%s\n", current_string);
} else {
// string not completely built yet
// get the index where the next char will be added
current_index = nb_letters_in_result + nb_numbers_in_result;
if (nb_numbers_in_result < n){ // still possible to add a number
for (int i = 0; i < N; i++){
current_string[current_index] = numbers[i];
print_them_all(numbers, nb_numbers_in_result+1, n, \
letters, is_letter_used, nb_letters_in_result, m, \
current_string);
}
}
if (nb_letters_in_result < m){ // still possible to add a letter
for (int i = 0; i < m; i++) {
if (is_letter_used[i] == false){ // check the letter has not been added yet
// keep track that the letter has been added by 'marking' it
is_letter_used[i] = true;
// add it
current_string[i] = letters[i];
// recursive call
print_them_all(numbers, nb_numbers_in_result, n, \
letters, is_letter_used, nb_letters_in_result+1, m, \
current_string);
// now 'unmark' the letter
is_letter_used[i] = false;
}
}
}
}
}
To solve this kind of problem, the recursive approach is necessary. It works as follows:
if I have a string with k numbers in it already, k<n, then I can add any number to it, and I can continue (now my string will have k+1 numbers in it).
If I have a string with k letters in it already, k<m, then I can add any letter that was not added already (the array of booleans helps to make sure it is the case), and I can continue.
If my string is ready for print, print it.
The first call should be done with the boolean array initialized to false everywhere, and 0 for the values of nb_letters_in_result and nb_numbers_in_result, since you have not added any number or letter in your result string yet.
As for your result string, since you code in C, don't forget to allocate memory for it:
char *current_string = malloc((m+n+1) * sizeof(char));
and to null-terminate it:
current_string[m+n] = '\0';
I also found an interesting solution for my question.
Assume my example string 01ab.
First we want to create all combinations of the numbers 01 and the permutation of ab.
There are plenty examples of how to solves this.
So now we have all combinations of 01 and ab. I will call them producer combinations:
10 ab
01 ba
11
00
Now we want to combine all numbers with all letters but with the rule
The order of the numbers or letters must not be reserved for each combination
So if we combine 10 with ab we get:
10ab
1a0b
a10b
now we move b to the left side until it is about to swap its place with a, which is forbidden because of my rule. We do this for every combination:
10ab produces:
10ab
since b is already next to a.
1a0b produces:
1ab0
so we got one more combination.
a10b produces:
a1b0
ab10
so we got 2 more combinations.
Now we have all possible combinations for 01 and ab:
10ab
1a0b
a10b
1ab0
a1b0
ab10
Since our producer combinations contain 8 elements we have to do this step 8 times with all elements. The resulting combinations will always contain 6 elements like in my example which leads us to 48 elements in total as I calculated in my question.

Adding modulo 128-bit numbers

I have some 2 numbers of 128 bits. Let it be the same number:
A=282434364544378924672110924168367615433
B=282434364544378924672110924168367615433
It is necessary to add them modulo numbers
340282366920938463460374607431768211337
To represent 128-bit numbers, I use two 64-bit arrays
low_A = A.aa[0];
low_B = B.aa[0];
low_M = M.aa[0];
high_A = A.aa[1];
high_B = B.aa[1];
high_M = M.aa[1];
Thus, selecting the lower and upper parts (we can roughly say that in this way the numbers will be presented to the 64th number system).
The problem is that when adding the numbers A and B, an overflow occurs physically. The transfer is performed to a non-existent bit, although the binary representation remains true. If there was a transfer, we certainly already know that the given number is greater than the modulus.
How then do we explain to the machine what the result should be
A+B-M
if (high_A <= ULLONG_MAX - high_B) flag_h = 0; else flag_h = 1;
if (flag_h) {
int car = 0;
high_A = high_A - high_M;
high_B = high_B - high_M;
high_C = high_A + high_B + high_M;
if (low_C <= low_M)
{
low_C = low_M - low_C;
low_C = ULLONG_MAX - low_C + 1;
car = 1;
}
else { low_C = low_C - low_M; }
high_C -= car;
}
I tried to do this in the above manner, but still the program finds it wrong.
I explain what I wanted to do. I tried to make a mathematical formula (A-M) + (B-M) + M = (A + B-M). I'm trying to subtract from the senior and junior level.
Let's show on the numbers
_ 51
38
1) 8-1 = 7, 7 more will have to be subtracted
2) We simulate a loan at the senior level
3) 10-7 = 9 + 1 - 7 = 3
4) Set loan flag in the unit
5) 5 - 3 - flag = 1
6) 13
I found the solution to the problem, began to subtract from the higher order, here is the code
if (flag_h)
{
int car = 0;
unsigned __int64 temp1,temp2;
if (high_C < high_M)
{
temp1 = high_M - high_C;
high_C = ULLONG_MAX - temp1 + 1;
}
else { high_C = high_C - high_M; }
if (low_C < low_M) {
temp2 = low_M - low_C;
low_C = ULLONG_MAX - temp2 + 1;
high_C--;
}
else { low_C = low_C - low_M; }
}

Mapping Array With Logical Array In Matlab

Let's say an array a=[1,2,3,4,5,6,7,8], and a logical array b=[1,0,0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1], how to get [1,1,1,1,2,3,2,1,1,1,2,3,4,5,6,7,8,8,8], where there is ones the array a continues in opposite direction where its was left at zeros, and for zeros it continues in opposite index direction from the index value it was left at ones.
array a=[1,2,3,4,5,6,7,8]
logical array b=[1,0,0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1]
how to get [1,1,1,1,2,3,2,1,1,1,2,3,4,5,6,7,8,8,8]
i don't know if it's the most elegant way, but it works:
a = [1,2,3,4,5,6,7,8];
len = length(a);
b = [1,0,0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1];
% find when b changes from 0 to 1
bb = [0 diff(b)];
c = b; c(c == 0) = -1;
c(bb == 1) = 0;
% cumsum finds initial indexes
d = cumsum(c);
% truncate indexes if exceeds array
while 1
ix = find(d < 1 | d > len,1,'first');
if isempty(ix)
break;
end
if d(ix) < 1
d(ix:end) = d(ix:end) + 1;
else
d(ix:end) = d(ix:end) - 1;
end
end
res = a(d)

How to implement subtraction using only loop and increment

This is an interview question.
We have only two constructs
loop(a) means loop for a times.
increment(a) increments a.
Thus to implement a+b one could write
loop(a) {inc(b)}
return b;
The question is how to implement a-b.
How about;
a = 10
b = 8
result = 0
loop(b) {
last = 0
times = 0;
loop(a) {
last = times
times = inc(times)
}
result = a = last
}
result is 2
Js eg;
var a = 10;
var b = 8;
var result;
for (var _b = 0; _b < b; _b++) {
var last = 0, times = 0, loopa = 0;
for (var _a = 0; _a < a; _a++) {
last = times;
times = inc(times);
}
result = a = last;
}
function inc(i) {
return i + 1;
}
print(result) // 2
I think if break from loop is allowed, a-b can be done in this way:
c=0;
loop(a) {
if (a==b) break;
inc(c);
inc(b);
}
return c;
Ofcourse assuming a>b.
depends if this Numeric architecture is known:
you can take advantage of the "Two Compliment" mechanism of the x86/x64 architecture,
for example, if the signed numbering scheme is cyclic like.
f(0 < x < 32768) = x
f(32769 < x < 65535) = x - 65536
Then you can use:
dec(a)
{
loop(65535 [= 2^16-1]) { inc(a) }
}
.
solving the riddel as
(a-b)
{
loop(b) { dec(a) }
}
Depending on the Signed scheme the addition Constant can change, same for short, long, large integer types.
Hope this is good :)
Best of luck.
We're a looking for x, so that a-b = x. In other words a = b+x
Pseudocode
int x = 0
WHILE (x <= a) do {
if (b+x == a) BREAK // satisfies a-b = x
x++
}
RESET B
INC B
LOOP A
{
INC D
LOOP B
{
RESET D
}
}

Resources