For nested if, what will be the equivalent operation - rml

For the if...else statement below
if (a > b) {
max = a;
}
else {
max = b;
}
will result shortcut as below?
max = (a > b) ? a : b;
What about this if and nested if statement?
if (a > b) {
max = a;
}
else {
if (c > d)
max = c;
else
max = d;
}

Just do:
max = (a > b) ? a :
(c > d) ? c : d;
And this can be formatted to be arbitrarily long, so the formatting is important because ternary operators can get real confusing real fast. Consider:
max = (a > b) ? a :
(b > c) ? b :
(c ? d) ? c :
(d ? e) ? d : e;
Verses:
max = (a > b) ? a : (b > c) ? b : (c ? d) ? c : (d ? e) ? d : e;

Related

Can not assert the value from Dafny method

This is the code for Bulls and Cows game, simply it just we have 2 array a[] and b[] with the same length, if a[i] == b[i] then Bulls += 1, if a[i] in b && a[i] != b[i] then Cows += 1.
I have written the Bulls and Cows function, but the method BullCows have some problem when calculate it, it make my assert fail.
`
function bullspec(s:seq<nat>, u:seq<nat>): nat
requires |s| > 0
requires |u| > 0
requires |s| == |u|
{
var index:=0;
if |s| == 1 then (
if s[0]==u[0]
then 1 else 0
) else (
if s[index] != u[index]
then bullspec(s[index+1..],u[index+1..])
else 1+bullspec(s[index+1..],u[index+1..])
)
}
function cowspec(s:seq<nat>, u:seq<nat>): nat
requires |s| > 0
requires |u| > 0
requires |s| <= |u|
{
var extra:= |u|-|s|;
var index:=0;
if |s| == 1 then (
if s[0] in u
then 1 else 0
) else(
if s[index] in u && s[index]!=u[extra]
then (1+ cowspec(s[index+1..],u))
else cowspec(s[index+1..],u)
)
}
method BullsCows (s:seq<nat>, u:seq<nat>) returns (b:nat, c:nat)
requires |s|>0 && |u|>0 &&|s|==|u|
// No duplicates in array
requires forall i, j | 0 <= i < |s| && 0 <= j < |s| && i != j :: s[i] != s[j]
requires forall i, j | 0 <= i < |u| && 0 <= j < |u| && i != j :: u[i] != u[j]
ensures forall k :: 0 <= k < |s| && s[k] !in u ==> b == c == 0
ensures forall k :: 0 <= k < |s| && s[k] in u ==> (c + b) > 0
{
var index := 0;
b := 0;
c := 0;
while(index<|s|)
invariant index <= |s|
invariant forall k :: 0 <= k < index && s[k] in u ==> (b + c) > 0
{
if s[index] in u {
if s[index] == u[index]{
b:=b+1;
} else {
c:=c+1;
}
}
index:=index + 1;
}
}
method NotMain()
{
var sys:seq<nat> := [4,2,9,3,1];
var usr:seq<nat> := [1,2,3,4,5];
assert bullspec(sys, usr) == 1; //True
assert cowspec(sys, usr) == 3; //True
var b:nat, c:nat := BullsCows(sys, usr);
assert b == 1; //Not true
assert c == 3; //Not true
}
`
The method NotMain said that assert b == 1; and assert c==3; are not true, this is Dafny language, please could someone help me with this logical, I'm banging my head.
I try put on many ensures in the BullsCows method but there's nothing happen
The problem is that the postcondition on BullsCows() is not strong enough to prove the final two assertions. In particular, there is no connection between the operation of BullsCows() and the specifications bullspec() and cowspec(). You need to connect these things together.
To illustrate I'm going to use a simpler example which is easier to follow. As for your example above, the final assertion fails in the following:
function sumspec(xs: seq<nat>, i: nat) : nat
requires i <= |xs| {
if |xs| == 0 || i == 0 then 0
else sumspec(xs,i-1) + xs[i-1]
}
method sum(s:seq<nat>) returns (r:nat) {
r := 0;
for i := 0 to |s| {
r := r + s[i];
}
return r;
}
method main() {
assert sumspec([1,2,3],3) == 6;
var r := sum([1,2,3]);
assert r == 6;
}
Whilst sumspec() is a valid specification for sum() we have not connected these two things. As such, Dafny assumes that sum() can return any value for r!
To connect the specification (sumspec) with its implementation (sum) we need a stronger postcondition:
method sum(s:seq<nat>) returns (r:nat)
ensures r == sumspec(s,|s|) {
r := 0;
for i := 0 to |s|
invariant r == sumspec(s,i) {
r := r + s[i];
}
return r;
}
Here, r == sumspec(s,|s|) connects the specification with the result of our implementation. We also added a loop invariant to help Dafny show this postcondition holds.

Why I'm getting 'return' expression required in a function with a block body error in Kotlin?

open class Test1 {
fun name(a: Int, b: Int): Int {
if (a % 2 == 0 && b % 2 == 0) return (a * b)
if (a % 2 == 1 && b % 2 == 1) return (a + b)
if ((a % 2 == 0).xor(b % 2 == 0)) return if (a > b) (a - b) else (b - a)
}
}
class Test2 : Test1() {
}
val obj1 = Test2()
print(obj1.name(7 , 8))
I'm trying to learn inheritance in Kotlin, but when I call name function from father class and try to print the result I get error below.
error: a return expression required in a function with a block body ('{...}') }
If the first and the second condition is false, the third one must be true so you can remove the last condition. The compiler does not seem to be able to draw this conclusion, so it suspects a case where there is no return.
fun name(a: Int, b: Int): Int {
if (a % 2 == 0 && b % 2 == 0) return (a * b)
if (a % 2 == 1 && b % 2 == 1) return (a + b)
return if (a > b) (a - b) else (b - a)
}
You can also simplify this method as follows
fun name(a: Int, b: Int): Int {
if (a % 2 != b % 2) return if (a > b) (a - b) else (b - a);
return if (a % 2 == 0) (a * b) else (a + b)
}
Your last line of the function
if ((a % 2 == 0).xor(b % 2 == 0)) return if (a > b) (a - b) else (b - a)
if you add brackets for clarity, it would look like:
if ((a % 2 == 0).xor(b % 2 == 0)) {
return if (a > b) (a - b) else (b - a)
}
So if the if test is false, the code won't run, which means you've reached the end of the function and still haven't returned something. You have to return an Int.
As already answered, in order for your code to compile your function must return an int from all execution paths, your function doesn't satisfy that condition. what happens if all the if conditions eveluate to false?
And second thing is that in kotlin return is an expression, so you can lift it out like so
fun name(a: Int, b: Int): Int {
return if (a % 2 == 0 && b % 2 == 0) { (a * b) }
else if (a % 2 == 1 && b % 2 == 1) { (a + b) }
else if ((a % 2 == 0).xor(b % 2 == 0)) { if (a > b) (a - b) else (b - a) }
else SOME_DEFAULT_INT
}

How do you count the variable changing in iteration of 1's

"value" can only either be 0, 60, 120, 180, 240 or 300.
When I try to count each change that "value" has using iterations I'm not getting values of +1. It gives values which are stepping by 10000. Also I do not wish to iterate when a value is reached, I only wish to iterate when it has changed one of the 6 values.
#include <stdio.h>
int angles;
int counter[6] = {0, 60, 120, 180, 240, 300};
if (a == 1 && b == 1 && c == 0)
{
value = 0;
}
if (a == 1 && b == 0 && c == 0)
{
value = 60;
}
if (a == 1 && b == 0 && c == 1)
{
value = 120;
}
if (a == 0 && b == 0 && c == 1)
{
value = 180;
}
if (a== 0 && b == 1 && c == 1)
{
value= 240;
}
if (a == 0 && b == 1 && c == 0)
{
value = 300;
}
2**3 == 8
You are missing conditions for some of the possible combinations of values.
I would put the conditions into a function and return the desired value as soon as it has been determined because that allows to sort out the conditions. Doing that, you will inevitably notice that you do not know what to return in some cases.
Other than that, it´s not clear to me what you are trying to achieve.
Something like this:
#define ERROR -1
int get_value(int a, int b, int c)
{
if(a)
{
if(b)
{
return c ? ERROR : 0;
}
else
{
return c ? 60 : 120;
}
}
else
{
if(b)
{
return c ? 240 : 300;
}
else
{
return c ? 180 : ERROR;
}
}
return ERROR;
}
I would probably avoid all this, consider each of the three values as one bit and use bit operators to get to the desired value.

how to Use UNION and Order by Clause in my JPQL Query

I am using eclipse link as a JPA in my project. the current jar i am using is eclipselink-2.5.2.Coming to my Question,
I want to use Order by in UNION combination. My JPQL query looks like following
select req from Rqst req
WHERE (req.Applc =:SYSTEM_IDENTIFIER1 AND req.procesTyp =:PROCESS_TYPE1 AND UPPER(req.updtBy) =:UPDATED_BY1)
AND req.stat <> :ignoreDeletedRequests
UNION
select req from Rqst req
WHERE (req.Applc =:SYSTEM_IDENTIFIER2 AND req.procesTyp =:PROCESS_TYPE2 AND UPPER(req.updtBy) =:UPDATED_BY2)
AND req.stat <> :ignoreDeletedRequests
UNION
select req from Rqst req
WHERE (req.Applc =:SYSTEM_IDENTIFIER3 AND req.procesTyp =:PROCESS_TYPE3 AND UPPER(req.updtBy) =:UPDATED_BY3)
AND req.stat <> :ignoreDeletedRequests
ORDER BY req.rqstId ASC
With out Order by it is working for me. But when i use Order by I am getting The query contains a malformed ending.
Can any help me here i am stuck for long time.
I had the problem as you. I solved that problem by using Collection.sort() like that:
Collections.sort(listObjects, new Comparator<Object[]>() {
#Override
public int compare(final Object[] record1, final Object[] record2) {
int c;
c = convertTime(record1[0].toString()).compareTo(convertTime(record2[0].toString()));
if (c == 0) {
c = record1[1].toString().compareTo(record2[1].toString());
}
if (c == 0) {
c = record1[2].toString().compareTo(record2[2].toString());
}
if (c == 0) {
c = new Long(Long.parseLong(record1[3].toString())).compareTo(Long.parseLong(record2[3].toString()));
}
if (c == 0) {
c = ((Date) record1[4]).compareTo(((Date) record2[4]));
}
if (c == 0) {
c = ((Date) record1[5]).compareTo(((Date) record2[5]));
}
if (c == 0) {
c = new Long(Long.parseLong(record1[6].toString())).compareTo(Long.parseLong(record2[6].toString()));
}
return c;
}
});
You can use Collention.sort() to order multiple fields;

greatest common factor on objective c

I'm new to objective c and I would like to know if there is a method for greatest common factor
gcf() for example so you get the idea
There's no out of the box function. Since Objective-C is a superset of C, you can grab an existing library or set of functions and include it where necessary.
Based on http://www.idevelopment.info/data/Programming/data_structures/c/gcd/gcd.c, you might do this:
// gcd.h
int gcd(int m, int n);
// gcd.c
int gcd(int m, int n) {
int t, r;
if (m < n) {
t = m;
m = n;
n = t;
}
r = m % n;
if (r == 0) {
return n;
} else {
return gcd(n, r);
}
}
Include that file whenever you should wish to use the gcd function:
#import "gcd.h"
There is no built in method, but the Euclidean algorithm is easy to implement, and quite efficient.
The binary GCD algorithm can be slightly more efficient. This link has C code implementing it.
By far the most elegant solution I have come across (non-recursive of course):
int gcd (int a, int b){
int c;
while ( a != 0 ) {
c = a; a = b%a; b = c;
}
return b;
}
Source.
Just putting damian86's answer into Objective-C style (reference to self assumes context of an object, modify accordingly, you could make this a category, etc):
-(int)greatestCommonDivisorM:(int)m N:(int)n
{
int t, r;
if (m < n) {
t = m;
m = n;
n = t;
}
r = m % n;
if (r == 0) {
return n;
} else {
return [self greatestCommonDivisorM:n N:r];
}
}