I study verification at the university using promela. And as an example, i need to solve algorithmic puzzle about frogs. I tried to solve the problem but something doesn't work right
In how many moves will the frogs exchange places? Frogs jump in turn on an empty cage when it is nearby or through one frog of the opposite color.
enter image description here
bool all_frog_done;
active proctype frog_jump()
{
byte i = 0;
byte position[7];
position[0] = 1;
position[1] = 1;
position[2] = 1;
position[3] = 0;
position[4] = 2;
position[5] = 2;
position[6] = 2;
all_frog_done = false;
printf("MSC: position[0] %d position[1] %d position[2] %d position[3] %d position[4] %d position[5] %d position[6] %d \n", position[0], position[1], position[2], position[3], position[4], position[5], position[6]);
do
:: (position[0] == 2) && (position[1] == 2) && (position[2] == 2) && (position[3] == 0) && (position[4] == 1) && (position[5] == 1) && (position[6] == 1) ->
all_frog_done = true; break;
:: else ->
if
:: (position[i] == 1) && (position[i+1] == 0) && (i != 6) ->
position[i] = 0;
position[i+1] = 1;
:: (position[i] == 2) && (position[i-1] == 0) && (i != 0) ->
position[i] = 0;
position[i-1] = 2;
:: (position[i] == 1) && (position[i+1] == 2) && (position[i+2] == 0) && (i <= 4)->
position[i] = 0;
position[i+2] = 1;
:: (position[i] == 2) && (position[i-1] == 1) && (position[i-2] == 0) && (i >= 2)->
position[i] = 0;
position[i-2] = 2;
fi;
printf("MSC: position[0] %d position[1] %d position[2] %d position[3] %d position[4] %d position[5] %d position[6] %d \n", position[0], position[1], position[2], position[3], position[4], position[5], position[6]);
if
:: (i < 7) -> i++;
:: (i > 0) -> i--;
fi;
od;
}
Related
I have been having this problem for a few hours. I don't know what it is, but I am having a hard time thinking clearly at the moment. This method displays a set of images. The first part of the method is just setting the gridbag constraints, whereas the next part in the if statement is creating jlabels and adding them to an arraylist of jlabels. The exception is being thrown when I try and add mouselisteners to the jlabels after they have been added to the arraylist (this is on line 112, and i have commented this on the code).
public void displayComplexStimulus(BufferedImage[] complexStimulus){
for(int i = 0; i < numberOfElements; i++){
if (i == 0 || i == 1 || i == 2){
c.gridx = i;
c.gridy = 0;
}
else if(i == 3 || i == 4 || i == 5){
c.gridx = i - 3;
c.gridy = 1;
}
else {
c.gridx = i - 6;
c.gridy = 2;
}
if(counter == 1){
if (phase1Trial.getPositionOfCorrectImage()!= i){
phase1IncorrectLabels.add(new JLabel(new ImageIcon(complexStimulus[i])));
phase1IncorrectLabels.get(i).addMouseListener(this); //line 112
add(phase1IncorrectLabels.get(i),c);
}
else if(phase1Trial.getPositionOfCorrectImage() == i){
correctLabel = new JLabel(new ImageIcon(complexStimulus[i]));
add(correctLabel, c);
correctLabel.addMouseListener(this);
}
}
}
}
If i==phase1Trial.getPositionOfCorrectImage() you're not adding an element to phase1IncorrectLabels. So in the next iteration after adding one element to the array it's at position i-1 and not i. You should replace your get(i) by get(phase1IncorrectLabels.size() - 1).
What is the n queens complexity time by Back Tracking Method?
and what is the count of Queens Position?
With below algorithm :
void queens (index i)
{
index j;
if (promising(i))
if (i == n)
cout << col[1] through col[n];
else
for (j = 1; j <= n; j++) {
col[i + 1] = j;
queens(i + 1);
}
}
bool promising (index i)
{
index k;
bool Switch;
k = 1;
Switch = true ;
while (k < i && switch) {
if (col[i] == col[k] || abs(col[i] – col[k] == i - k))
switch = false;
k++;
}
return Switch;
}
Any suggestion?
I have the following zaMap (see full code here: http://rise4fun.com/Dafny/LCaM):
class zaMap {
var busybits :array<bool>;
var keys : array<int>;
var values : array<int>;
predicate wellformed ()
reads busybits, keys, values, this
{
busybits != null && keys != null && values != null &&
keys != values &&
busybits.Length == keys.Length &&
keys.Length == values.Length
}
// ... more predicates and methods follow
method put(k : int, v : int) returns (success : bool)
requires wellformed()
modifies busybits, keys, values
ensures !success ==> full()
ensures success ==> mapsto(k, v)
{
var i := findEmpty();
if (i < 0)
{
success := false;
return;
}
assert !busybits[i];
busybits[i] := true;
keys[i] := k;
values[i] := v;
success := true;
}
//...
Now I want to add more specifications to the put method. For example, I want to ensure, that if the return value is success == true, then a map was !full() before the function call, or equivalently if a map not full(), it is guaranteed to put there.
The problem is that, in the precondition "requires" I don't know yet what it will return, and in the postcondition "ensures" I don't have an original map anymore. What people do about that?
You can use the old keyword. Let's take a look at an example. The following method sets to zero all positions of an array containing the element x and leaving the rest as they are. Here's the code:
method setToZero(a: array<int>, x : int )
requires a != null;
modifies a;
ensures forall i :: 0 <= i < a.Length && old(a[i]) == x ==> a[i] == 0;
ensures forall i :: 0 <= i < a.Length && old(a[i]) != x ==> a[i] == old(a[i]);
{
var j := 0;
while (j < a.Length)
invariant 0 <= j <= a.Length;
invariant forall i :: 0 <= i < j && old(a[i]) == x ==> a[i] == 0;
invariant forall i :: 0 <= i < j && old(a[i]) != x ==> a[i] == old(a[i]);
invariant forall i :: j <= i < a.Length ==> a[i] == old(a[i]);
{
if (a[j] == x) {
a[j] := 0;
}
j := j + 1;
}
}
I am trying to make it so that of the 4 variables, (squareType1, squareType2, squareType3, and squareType4) only one of them has a value of 0 but which variable it is should be random. The coding is off I know that but I just don't know how to fix it.
squareType1 = arc4random() %2;
squareType2 = arc4random() %2;
squareType3 = arc4random() %2;
squareType4 = arc4random() %2;
if (squareType2 == 0 || squareType3 == 0 || squareType4 == 0) {
squareType1 = 1;
}
if (squareType1 == 0 || squareType3 == 0 || squareType4 == 0) {
squareType2 = 1;
}
if (squareType2 == 0 || squareType1 == 0 || squareType4 == 0) {
squareType3 = 1;
}
if (squareType2 == 0 || squareType3 == 0 || squareType1 == 0) {
squareType4 = 1;
}
A simpler way to do this would be to set all 4 values to 1 initially and then randomly reset one of the variables to 0, e.g.
squareType1 = squareType2 = squareType3 = squareType4 = 1;
switch (arc4random() % 4)
{
case 0: squareType1 = 0; break;
case 1: squareType2 = 0; break;
case 2: squareType3 = 0; break;
case 3: squareType4 = 0; break;
}
Note that you will probably find life easier, in this instance and in general, if you refactor the four separate variables into a single array, e.g. int squareType[4];.
I had an interview today. This question was to optimize the code below. if we will see the code below after for loop there are four steps of "if-else" follows. So, interviewer asked me optimize it to 3 if-else line. I have tried a lot. But could not able to find the solution. Even he told me if you know scripting language then, you can use them also.
Please help me in optimizing the same.
int main()
{
int i = 1;
for(i; i <= 100; i++)
{
if((i % 3 == 0 && i % 5 == 0))
{cout << "PR\n";}
else if(i % 3 == 0)
{cout << "P\n";}
else if(i % 5 == 0)
{cout << "R\n";}
else
{cout << i <<"\n";}
}
system("pause");
return 0;
}
This is a well known question... the "FizzBuzz".
You can even solve it without any explicit IFs
const char *messages[] = {"%i\n", "P\n", "R\n", "PR\n"};
for (i=1; i<=100; i++) {
printf(messages[((i % 3)==0) + 2*((i % 5)==0))], i);
}
Here's one way, in Python:
for i in range(1, 101):
s = ''
if i % 3 == 0:
s += 'P'
if i % 5 == 0:
s += 'R'
if i % 3 != 0 and i % 5 != 0:
s = i
print(s)
Equivalently: using a flag, as shown in your own answer:
for i in range(1, 101):
s, flag = '', False
if i % 3 == 0:
flag = True
s += 'P'
if i % 5 == 0:
flag = True
s += 'R'
if not flag:
s = i
print(s)
Just for fun, a Python version of #6502's answer:
messages = ['{}', 'P', 'R', 'PR']
for i in range(1, 101):
print(messages[(i%3 == 0) + 2*(i%5 == 0)].format(i))
And finally, my personal favorite (because it's the shortest) - using the Greatest Common Divisor function and a lookup table:
from fractions import gcd
messages = {3:'P', 5:'R', 15:'PR'}
for i in range(1, 101):
print(messages.get(gcd(i, 15), i))
I found a solution.
Please let me know whether it is good or not?
int main()
{
int i = 1;int stat=0;
for(i; i <= 100; i++)
{
stat=0;
if(i%3 == 0){stat++; cout << "P";}
if(i%5 == 0){stat++; cout << "R";}
if(stat == 0)cout << i;
cout << "\n";
}
system("pause");
return 0;
}
I really like 6502's answer, but here is a simple solution without extra variables:
for(i = 1; i <= 100; i++)
{
if(i % 3 != 0 && i % 5 != 0)
{
printf("%d\n", i);
continue;
}
if(i % 3 == 0)
printf("P");
if(i % 5 == 0)
printf("R");
printf("\n");
}
this way only use 3 if
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i <= 100; ++i)
{
bool fizz = (i % 3) == 0;
bool buzz = (i % 5) == 0;
if (fizz)
cout << "Fizz";
if (buzz)
cout << "Buzz";
if (!fizz && !buzz)
cout << i;
cout << endl;
}
return 0;
}