I'm making a code just to test some things out
print('Hello! What is your name?')
name = io.read()
print('Hello '..name..'!')
repeat
print('What does a mute person say?')
say = io.read()
x = 'Nothing'
z = 'nothing'
if say == x then
print('Correct!')
elseif say == z then
print('Correct!')
else
print('Incorrect! Guess Again!')
end
until say = x or z
Everything works fine when I test it but if the answer is incorrect you cant try to answer again. Probably a stupid question but, is there anyway to make the question repeat when the answer is incorrect?
The loop condition say = x or z shouldn't compile, use == to test equality, not =.
That's not enough, say == x or z is equivalent to say == x because x or z has the value of x if x is not nil or false.
repeat
-- read say
until say == x or say == z
Related
i'm new to lua and programming in general and i'd like to find out if its possible to make sure that all my 6 variables are different every time. why isnt this working, and is there a more efficient way to make sure my 6 variables are different every time?
local x = math.random(1,6)
local y = math.random(1,6)
local z = math.random(1,6)
local a = math.random(1,6)
local b = math.random(1,6)
local c = math.random(1,6)
if x or y or z or a or b or c == x or y or z or a or b or c then
repeat
y = math.random(1,6)
z = math.random(1,6)
a = math.random(1,6)
b = math.random(1,6)
c = math.random(1,6)
until x or y or z or a or b or c ~= x or y or z or a or b or c
print(x.." , "..y.." , "..z.." , "..a.." , "..b.." , "..c)
else
print(x.." , "..y.." , "..z.." , "..a.." , "..b.." , "..c)
end
Since the number of possible results is the same as the number of variables, you could just make a randomly sorted table of those results and store the contents into the variables. The following code takes the numbers 1 through 6 and stores each one at a random index in an array:
local rolls = {}
for i = 1, 6 do
table.insert(rolls, math.random(#rolls + 1), i)
-- See the sorting in action
print(table.unpack(rolls))
end
local x, y, z, a, b, c = table.unpack(rolls)
print(x, y, z, a, b, c)
As for why your code doesn't work, it's hard for me to understand how you intended it to work. You seem to be treating or as something other than a simple binary operator. You can read about it here.
calling
math.randomseed(os.time())
before every math.random call will do te trick.
os.time() is by default different everytime you call it, and by setting the seed differently, it will give different results.
Probably a basic question related to Z3: i am trying to get all solutions of a boolean expression, e.g. for a OR b, i want to get {(true, true),(false,true),(true,false)}
Based on other responses found, e.g. Z3: finding all satisfying models, i have the following code:
a = Bool('a')
b = Bool('b')
f1=Or(a,b)
s=Solver()
s.add(f1)
while s.check() == sat:
print s
s.add(Not(And(a == s.model()[a], b == s.model()[b])))
The issue is that it enters an infinite loop as at the second iteration: the constraint a == s.model()[a] is evaluated to false b/c s.model()[a] does not exist anymore.
Can someone tell what i am doing wrong?
I would advice you to try writing your loop like this instead:
from z3 import *
a = Bool('a')
b = Bool('b')
f1 = Or(a,b)
s = Solver()
s.add(f1)
while s.check() == sat:
m = s.model()
v_a = m.eval(a, model_completion=True)
v_b = m.eval(b, model_completion=True)
print("Model:")
print("a := " + str(v_a))
print("b := " + str(v_b))
bc = Or(a != v_a, b != v_b)
s.add(bc)
The output is:
Model:
a := True
b := False
Model:
a := False
b := True
Model:
a := True
b := True
The argument model_completion=True is necessary because otherwise m.eval(x) behaves like the identity relation for any x Boolean variable with a don't care value in the current model m and it returns x as a result instead of True/False. (See related Q/A)
NOTE: since z3 kindly marks don't care Boolean variables, an alternative option would be to write your own model generator that auto-completes any partial model. This would reduce the number of calls to s.check(). The performance impact of this implementation is hard to gauge, but it might be slightly faster.
I'm experimenting with (and failing at) reducing sets in z3 over operations like addition. The idea is eventually to prove stuff about arbitrary reductions over reasonably-sized fixed-sized sets.
The first of the two examples below seems like it should yield unsat, but it doesn't. The second does work, but I would prefer not to use it as it requires incrementally fiddling with the model.
def test_reduce():
LIM = 5
VARS = 10
poss = [Int('i%d'%x) for x in range(VARS)]
i = Int('i')
s = Solver()
arr = Array('arr', IntSort(), BoolSort())
s.add(arr == Lambda(i, And(i < LIM, i >= 0)))
a = arr
for x in range(len(poss)):
s.add(Implies(a != EmptySet(IntSort()), arr[poss[x]]))
a = SetDel(a, poss[x])
def final_stmt(l):
if len(l) == 0: return 0
return If(Not(arr[l[0]]), 0, l[0] + (0 if len(l) == 1 else final_stmt(l[1:])))
sm = final_stmt(poss)
s.push()
s.add(sm == 1)
assert s.check() == unsat
Interestingly, the example below works much better, but I'm not sure why...
def test_reduce_with_loop_model():
s = Solver()
i = Int('i')
arr = Array('arr', IntSort(), BoolSort())
LIM = 1000
s.add(arr == Lambda(i, And(i < LIM, i >= 0)))
sm = 0
f = Int(str(uuid4()))
while True:
s.push()
s.add(arr[f])
chk = s.check()
if chk == unsat:
s.pop()
break
tmp = s.model()[f]
sm = sm + tmp
s.pop()
s.add(f != tmp)
s.push()
s.add(sm == sum(range(LIM)))
assert s.check() == sat
s.pop()
s.push()
s.add(sm == 11)
assert s.check() == unsat
Note that your call to:
f = Int(str(uuid4()))
Is inside the loop in the first case, and is outside the loop in the second case. So, the second case simply works on one variable, and thus converges quickly. While the first one keeps creating variables and creates a much harder problem for z3. It's not surprising at all that these two behave significantly differently, as they encode entirely different constraints.
As a general note, reducing an array of elements with an operation is just not going to be an easy problem for z3. First, you have to assume an upper bound on the elements. And if that's the case, then why bother with Lambda or Array at all? Simply create a Python list of that many variables, and ignore the array logic completely. That is:
elts = [Int("s%d"%i) for i in range(100)]
And then to access the elements of your 'array', simply use Python accessor notation elts[12].
Note that this only works if all your accesses are with a constant integer; i.e., your index cannot be symbolic. But if you're looking for proving reduction properties, that should suffice; and would be much more efficient.
I have written a program to print a matrix after some computations and I am getting an output of nan for all elements. I want to break a for loop as soon as the matrix's first element becomes nan to understand the problem. How can I do this? In the terminal, I have printed the matrix a containing nan as all elements and typed a[1][1]=="nan" and a[{{1},{1}}]=="nan" both of which return false. Why are they not returning false and what statement should I use instead?
Your test fails because you are comparing a number with a string, "nan".
If you are sure it's a number, the easiest way is:
if a[1][1] ~= a[1][1] then
because according to IEEE 754, a nan value is considered not equal to any value, including itself.
Two solutions:
local n = 0/0 -- nan
-- first solution
if ( tostring(n) == "nan" ) then
print("is nan!!")
end
-- second solution
if (n ~= n) then
print("is nan!!")
end
Try this:
for x = 1, x2 do -- x2 depends on how big you matrix is.
for y = 1, y2 do -- y2 the same as x2
-- some code depending on how your program works
if a[x][y] == nan then
print( "X:" .. x .. ", Y:" .. y )
break
end
end
end
PS: (nan == nan) is true
I am very green when it comes to F#, and I have run across a small issue dealing with recursive functions that I was hoping could help me understand.
I have a function that is supposed to spit out the next even number:
let rec nextEven(x) =
let y = x + 1
if y % 2 = 0 then y
else nextEven y
// This never returns..
nextEven 3;;
I use the 'rec' keyword so that it will be recursive, although when I use it, it will just run in an endless loop for some reason. If I rewrite the function like this:
let nextEven(x) =
let y = x + 1
if y % 2 = 0 then y
else nextEven y
Then everything works fine (no rec keyword). For some reason I though I needed 'rec' since the function is recursive (so why don't I?) and why does the first version of the function run forever ?
EDIT
Turns out this was a total noob mistake. I had created multiple definitions of the function along the way, as is explained in the comments + answers.
I suspect you have multiple definitions of nextEven. That's the only explanation for your second example compiling. Repro:
module A =
let rec nextEven(x) =
let y = x + 1
if y % 2 = 0 then y
else nextEven y
open A //the function below will not compile without this
let nextEven(x) =
let y = x + 1
if y % 2 = 0 then y
else nextEven y //calling A.nextEven
Try resetting your FSI session.