Nested IF condition not working in google spreadsheet - google-sheets

This doesn't seems to be working in Google spreadsheet, can anyone help me out?
=IF(P70 <= 0, "NYS", IF(AND(P70 > 0, P70 <= 99), "WIP", IF(P70 >= 100, "COM")))

IF statement require 3 arguments: IF(condition, true_exp, false_exp).
You forgot the false_exp in the innermost IF.
It could be expressed more clearly like:
=IF(P70 <= 0, "NYS", IF(P70 <= 99, "WIP", "COM"))

Related

Nested If statements in Google sheets not working properly

I am trying to sort some salary expectations to different levels of jobs in google sheets and have created the formula below. I am expecting it to print entry-level, Mid Level, Senior Level and Blank in the ranges I have defined below.
However, it is only printing Senior Level where the cell values are over 50000 or blank.
What's wrong with my formula?
=IFS(
0> $J2 < 30000, "Entry Level",
30000 >= $J2 < 50000 ,"Mid Level",
$J2 >= 50000, "Senior Level",
ISBLANK($J2),"Blank")
You can't write 30000 >= $J2 < 50000, write instead and( $J2 >= 30000 , $J2 < 50000 )
complete formula
=if(isblank($J2),"Blank",if($J2<30000,"Entry level",if($J2<50000,"Mid level","Senior level")))
use:
=IF((J2 > 0) *(J2 < 30000), "Entry Level",
IF((J2 >= 30000)*(J2 < 50000), "Mid Level",
IF( J2 >= 50000), "Senior Level",
IF(ISBLANK(J2), "Blank", ))))
see: https://webapps.stackexchange.com/q/123729/186471
try:
=IF(J2="", "Blank", VLOOKUP(J2,
{0, "Entry Level";
30000, "Mid Level";
50000, "Senior Level"}, 2, 1))

z3py: Retrieve branching conditions from z3 formula

Let's say I have a z3py program like this one:
import z3
a = z3.Int("a")
input_0 = z3.Int("input_0")
output = z3.Int("output")
some_formula = z3.If(a < input_0, 1, z3.If(a > 1, 4, 2))
s = z3.Solver()
s.add(output == some_formula)
s.check()
m = s.model()
print(m)
Is there an elegant way for me to retrieve the branching conditions from some_formula?
So get a list like [a < input_0, a > 1]. It should work for arbitrarily deep nesting of if expressions.
I know there is some way to use cubes, but I am not able to retrieve more than two cube expressions. I am not sure how to configure the solver.
My ultimate goal is to force the solver to give me different outputs based on the constraints I push and pop. The constraints are the set of conditions I have inferred from this formula.
You can print the cubes using:
for cube in s.cube():
print cube
But this isn't going to really help you. For your example, it prints:
[If(a + -1*input_0 >= 0, If(a <= 1, 2, 4), 1) == 1]
[Not(If(a + -1*input_0 >= 0, If(a <= 1, 2, 4), 1) == 1)]
which isn't quite what you were looking for.
The easiest way to go about your problem would be to directly walk down the AST of the formula yourself, and grab the conditions as you walk along the expressions. Of course, Z3 AST is quite a hairy object (pun intended!), so this will require quite a bit of programming. But reading through the constructors (If, Var etc.) in this file can get you started: https://z3prover.github.io/api/html/z3py_8py_source.html
Alright,thanks #alias! I came up with a custom version, which gets the job done. If someone knows a more elegant way to do this, please let me know.
import z3
a = z3.Int("a")
input_0 = z3.Int("input_0")
output = z3.Int("output")
some_formula = z3.If(a < input_0, 1, z3.If(a > 1, 4, 2))
nested_formula = z3.If(some_formula == 1, 20, 10)
s = z3.Solver()
s.add(output == some_formula)
s.check()
m = s.model()
print(m)
def get_branch_conditions(z3_formula):
conditions = []
if z3.is_app_of(z3_formula, z3.Z3_OP_ITE):
# the first child is usually the condition
cond = z3_formula.children()[0]
conditions.append(cond)
for child in z3_formula.children():
conditions.extend(get_branch_conditions(child))
return conditions
conds = get_branch_conditions(some_formula)
print(conds)
conds = get_branch_conditions(nested_formula)
print(conds)

Lookup Array Formula to calculate difference

Hoping to build an ArrayFormula that's clearly beyond what I understand, so please bear with me. I'm using the following formula to grab the value of the Last Non-Empty Cell and subtract the value of the cell immediately above it.
=ArrayFormula((LOOKUP(2,1/(NOT(ISBLANK(Sheet3!A:A))),Sheet3!A:A))-INDEX(Sheet3!A:A, CountA(A:A)-2,1))
I'd like to use a HLOOKUP function to match names from a vertical list, to identify the last non-empty cell in the corresponding column. I'm able to get the correct value from the 'Names' column with the formula below, but not sure how to integrate this into the ArrayFormula.
=HLOOKUP(A4,Sheet3!A1:E30,1,FALSE)
A correct formula should retrieve the value in the last non-blank cell of a column containing the name in 'Data Test'!A:A
Please see sample sheet for reference: Data Test
The way I understand the data it is reasonable to assume that the range in each column is consecutive.
We will also have to calculate the subsidy change for everyone separately because some of these formulae do not work with ArrayFormulae.
This formula finds the last row of the respective column and the second to last row and subtracts the two, if there is an error (because we try to subtract a string for Eric) we use the last and only value.
=IFERROR(
OFFSET(
Sheet3!$A$1,
COUNTA(OFFSET(Sheet3!$A$1, 0, MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1, 1000)) - 1,
MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1) -
OFFSET(
Sheet3!$A$1,
COUNTA(OFFSET(Sheet3!$A$1, 0, MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1, 1000)) - 2,
MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1),
OFFSET(
Sheet3!$A$1,
COUNTA(OFFSET(Sheet3!$A$1, 0, MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1, 1000)) - 1,
MATCH($A2, Sheet3!$A$1:$E$1, 0) - 1))

Prolog print value as result instead of true

I need to write a program, which returns a new list from a given list with following criteria.
If list member is negative or 0 it should and that value 3 times to new list. If member is positive it should add value 2 times for that list.
For example :
goal: dt([-3,2,0],R).
R = [-3,-3,-3,2,2,0,0,0].
I have written following code and it works fine for me, but it returns true as result instead of R = [some_values]
My code :
dt([],R):- write(R). % end print new list
dt([X|Tail],R):- X =< 0, addNegavite(Tail,X,R). % add 3 negatives or 0
dt([X|Tail],R):- X > 0, addPositive(Tail,X,R). % add 2 positives
addNegavite(Tail,X,R):- append([X,X,X],R,Z), dt(Tail, Z).
addPositive(Tail,X,R):- append([X,X],R,Z), dt(Tail, Z).
Maybe someone know how to make it print R = [] instead of true.
Your code prepares the value of R as it goes down the recursing chain top-to-bottom, treating the value passed in as the initial list. Calling dt/2 with an empty list produces the desired output:
:- dt([-3,2,0],[]).
Demo #1 - Note the reversed order
This is, however, an unusual way of doing things in Prolog: typically, R is your return value, produced in the other way around, when the base case services the "empty list" situation, and the rest of the rules grow the result from that empty list:
dt([],[]). % Base case: empty list produces an empty list
dt([X|Like],R):- X =< 0, addNegavite(Like,X,R).
dt([X|Like],R):- X > 0, addPositive(Like,X,R).
% The two remaining rules do the tail first, then append:
addNegavite(Like,X,R):- dt(Like, Z), append([X,X,X], Z, R).
addPositive(Like,X,R):- dt(Like, Z), append([X,X], Z, R).
Demo #2
Why do you call write inside your clauses?
Better don't have side-effects in your clauses:
dt([], []).
dt([N|NS], [N,N,N|MS]) :-
N =< 0,
dt(NS, MS).
dt([N|NS], [N,N|MS]) :-
N > 0,
dt(NS, MS).
That will work:
?- dt([-3,2,0], R).
R = [-3, -3, -3, 2, 2, 0, 0, 0] .
A further advantage of not invoking functions with side-effects in clauses is that the reverse works, too:
?- dt(R, [-3, -3, -3, 2, 2, 0, 0, 0]).
R = [-3, 2, 0] .
Of cause you can invoke write outside of your clauses:
?- dt([-3,2,0], R), write(R).
[-3,-3,-3,2,2,0,0,0]
R = [-3, -3, -3, 2, 2, 0, 0, 0] .

Speed z3-solver up with right tactics

I create about 20k constraints and on my machine it takes about 3 minutes to solve them. I have different kind of constraints and below I give examples and explain them. I uploaded the assertions to http://filebin.ca/vKcV1gvuGG3.
I'm interested in solving larger constraint systems, thus I'd like to speed the process up. I would like to ask if you have suggestions on how to solve them faster e.g. by using appropriate tactics. From the tutorial on strategies I know the tactics, but I don't seem to get a positive difference by applying tactics...
The li are labels of a tree. The 1st type makes restrictions on the values of the labels. The label-values typically range between 10-20 different values.
Or(l6 == 11, Or(l6 == 0, l6 == 1, l6 == 2, l6 == 3, l6 == 4,
l6 == 5, l6 == 6, l6 == 7, l6 == 8, l6 == 9, l6 == 10))
The 2nd type relates different labels to each other.
Implies(l12 == 0, Or(l10 == 2, l10 == 5, l10 == 7, l10 == 8, l10 == 10,
False, False))
The 3rd type defines and relates functions f: Int --> Int (or f: BitVector --> BitVector, at loss of completeness), where bound_{s, v} is just a function name, and n is an ID of a node. The goal is to find a satisfying assignment to the functions bound.
Implies(And(bound_s_v (18) >= 0, l18 == 0),
And(bound_s_v (19) >= 0,
bound_s_v (19) >=
bound_s_v (18),
bound_s_v (26) >= 0,
bound_s_v (26) >=
bound_s_v (18)))
The last type determines if a bound is required (>= 0) or not (== -1, )
Or(bound_s'_v'(0) >= 0, bound_s'_v'(0) == -1)
Also there is a requirement that for some initial state the bound is required:
`bound_s0_v0(0) >= 0`
Description of the exutable file: In the first 2-3 lines the script initiates the solver, imports z3 and in the last line calls print s.check()
Presumbaly, you could try to use solver 'qflia', since I see no quantifier in your logic.
Edit:
I tried 'qflia', but it didn't make it faster. Maybe, you should try other solvers as well.

Resources