MySQL, how would my function to evalue a number look? - mysql

I need a function in MySQL which will evaluate a status code...
In my program this is how I evaluate it. However, in SQL I'd want to select all the status code which had a specific Reason. You can see below the reasons and that more than one can be set.
Sub InterpretReasonCode(ByVal pintCode As Integer)
If pintCode >= 16 Then
pintCode -= 16
mbooBlacklistedDomain = True
End If
If pintCode >= 8 Then
pintCode -= 8
mbooSneakedURLChanged = True
End If
If pintCode >= 4 Then
pintCode -= 4
mbooRetriedFailedToAccess = True
End If
If pintCode >= 2 Then
pintCode -= 2
mbooRequestedByAuthor = True
End If
If pintCode >= 1 Then
pintCode -= 1
mbooBlackListed = True
End If
End Sub
My SQL statement would look something like this
Select * from MyTable where Eval_Func(StatusCode,8) = true;
In the future I will be expanding the function to include different flags, .e.g 32, 64, 128, 256 etc.

You don't need a function.
You can mask it directly using bitwise operators
For example:
-- Get all BlacklistedDomain
select *
from tab
where reason_bit_mask & 16;
And I advice against this on anything other than really small tables, because you can't index it properly.

Related

Calling a function multiple times with return value

from graphics import *
def draw():
returnStuff = {'again' : 0, '1st' : 1 }
draw.again = False
win = GraphWin("Quadrilateral Maker", 600, 600)
win.setBackground("yellow")
text = Text(Point(150, 15), 'Click 4 points to create a Quadrilateral')
text.draw(win)
#gets the 4 points
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
p4 = win.getMouse()
p4.draw(win)
vertices = [p1, p2, p3, p4]
#draws the shape
quad = Polygon(vertices)
quad.setFill('red')
quad.setOutline('black')
quad.setWidth(3)
quad.draw(win)
text.setText('Click in the appropriate box.')
#Quit box
quitBox = Rectangle(Point(30, 500), Point(100,550))
quitBox.setFill('green')
quitBox.draw(win)
quitorNah = Text(Point(60, 490), 'Quit')
quitorNah.draw(win)
#again box
quitBox = Rectangle(Point(480, 500), Point(550,550))
quitBox.setFill('green')
quitBox.draw(win)
quitorNah = Text(Point(510, 490), 'Draw Again')
quitorNah.draw(win)
click = win.getMouse()
x = click.getX()
y = click.getY()
while True:
if 30 < x < 100 and 500 < y < 550:
returnStuff['again'] = 0
win.close()
break
elif 480 < x < 550 and 500 < y < 550:
returnStuff['again'] = 1
win.close()
break
return returnStuff
count = 1
returnValue = draw()
if returnValue['1st'] == 1:
count = 0
while count == 1 or returnValue['again'] == 1:
return_value = draw()
So I have this simple interactive program using Zelle graphics, it asks the user to click on 4 points in a window and from that it creates a shape. Then, the user is shown 2 boxes, one to quit and one to draw again. My draw again isn't working, and it has something to do with the return value. I am returning a dictionary, as I need access to 2 of the variables within the function. In the 'returnStuff' dictionary, I have a part called 'again', which is initially set to 0. If the user clicks in the run again box, it changes this value to 1, and then outside the function I have an if statement that should call the function again if that again value is 1. It does this properly the FIRST time, but the 2nd time around my program just stops all together, and I don't understand why.
Can anybody explain why this is happening?
I think you need a while...
while count==1 or returnValue['again'] == 1:
returnValue = draw()

What does this recursive function mean?

I've been tasked with implementing a recursive function in MIPS. This function is
function1(n) = n-5 (if n <= 3)
otherwise = 4*function1(n-1) - n*function1(n-3)
One of the test cases is if n = 6, then the result is 200.
How do you get 200 from entering 6 in this function? To me it looks like the answer should be 2. Is there something I am not understanding about recursion, or am I understanding the function wrong? I am so confused
You seem to be misunderstanding the function somewhere. Here are the steps I followed to arrive at 200:
function1(6) = 4*function1(5) - 6*function1(3) (by rule 2)
function1(5) = 4*function1(4) - 5*function1(2) (by rule 2)
function1(4) = 4*function1(3) - 4*function1(1) (by rule 2)
function1(3) = 3-5 = -2 (by rule 1)
function1(2) = 2-5 = -3 (by rule 1)
function1(1) = 1-5 = -4 (by rule 1)
Substituting back...
function1(4) = 4*-2 - 4*-4 = -8 - -16 = 8
function1(5) = 4*8 - 5*-3 = 32 - -15 = 47
function1(6) = 4*47 - 6*-2 = 188 - -12 = 200

Spawn Function in Corona SDK

Okay so I kind of am almost there but need a little push:
My dustbins spawn but there are a few to begin with like I want and then after about 30 seconds of the numbers of dustbins increasing there are 1000's all clumped together.. What can i do to alter my function so that its only ever a few at a time:
function spawnDustbin()
dustbin = {}
for i = 1,800 do
dustbin[i] = display.newImage("dustbin.png")
dustbin[i].xScale = 0.55
dustbin[i].yScale = 0.55
dustbin[i].y = 555
dustbin[i].x = (math.random(1000,1500) * i)
dustbin[i].speed = 4
physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=30,} )
dustbin[i].enterFrame = moveDustbin
Runtime:addEventListener("enterFrame", dustbin[i])
end
end
and the movedustbin function simply moves the dustbin in the negative x direction:
function moveDustbin(self,event)
if self.x < -2560 then
self.x = 2560
else
self.x = self.x - val
end
end
First 20ish seconds:
http://i.stack.imgur.com/7iEeP.png
After 20 seconds:
http://i.stack.imgur.com/aae0D.png
Thank you very much
James
That "spawnDustbin" function above is spawning 800 dustbins every frame. That will really crush your performance, and I am pretty certain you don't want to create that many objects every frame.
You might want to do something like this:
local dustbin = {}
local i = 0
local function spawnDustbin()
i = i+1
dustbin[i] = display.newImage("dustbin.png")
dustbin[i].xScale = 0.55
dustbin[i].yScale = 0.55
dustbin[i].y = 555
dustbin[i].x = (math.random(1000,1500) * i)
dustbin[i].speed = 4
physics.addBody( dustbin[i], "static", { friction=1.0, density=1.0, bounce=0, radius=30,} )
dustbin[i].enterFrame = moveDustbin
Runtime:addEventListener("enterFrame", dustbin[i])
end
timer.performWithDelay(500, spawnDustbin, 10)
YMMV but that will spawn 10 dustbins in 5 seconds.

How to perform a conditional assignment on each element of the vector

I have a function like this:
y=-2 with x<=0
y=-2+3x^2 with 0=1
I need to compute this function on each element of the 1D matrix, without using a loop.
I thought it was possibile defining a function like this one:
function y= foo(x)
if x<=0
y=-2;
elseif x>=1
y=1;
else
y= -2+3*x.^2;
end
end
But this just produces a single result, how to operate on all elements? I know the . operator, but how to access the single element inside an if?
function b = helper(s)
if s<=0
b=-2;
elseif s>=1
b=1;
else
b= -2+3*s^2;
end
end
Then simply call
arrayfun(#helper, x)
to produce the behaviour you want of your function foo.
Another approach which doesn't need arrayfun() would be to multiply by the conditions:
y = -2*(x <= 0) + (-2+3*x.^2).*(x < 1).*(x > 0) + (x >= 1)
which you could also make a function. This will accept vector inputs for x e.g.
x = [1 4 0 -1 0.5];
y = -2*(x <= 0) + (-2+3*x.^2).*(x < 1).*(x > 0) + (x >= 1)
outputs
y =
1.0000 1.0000 -2.0000 -2.0000 -1.2500

Boolean Logic with If case

There are 2 cases given in the question and on that basis we have to answer.
Cases:
if((NOT(value>=1) OR NOT(value<=10))
if((NOT(value>=1) AND NOT(value<=10))
Now the questions are:
which case you are going to use if the given value either is 1 or 10 ?
which case you are going to use if the given value must be 1 or 10 ?
the problem is whether I takes 1 or 10 I am getting same answer in both the cases. That is if(0) and thus if statement is false in both the cases.?
(NOT(value>=1) OR NOT(value<=10)) = (value < 1) OR (value > 10)
This case is true for [-Infinity ... 0] or [11 ... + Infinity]
Is false for 1 or 10
((NOT(value>=1) AND NOT(value<=10)) = (value < 1) AND (value > 10)
This case is always false, as no number can be smaller than 1 and bigger than 10 the same time.