Haskell Instances required for definition - function

I have the following Haskell code, a function that checks how many digits are the same in a number and based on how many of them were the same takes a value. I keep getting the error:
Instances of (Ord (Int -> Int -> Int), Num (Int -> Int -> Int)) required for definition of digits
digits :: Int->Int->Int
digits x y
|(equal < 2) = 0
|(equal == 2) = 1
|(equal == 3) = 5
|(equal == 4) = 20
|(equal == 5) = 300
|(equal == 6) = 8000
|(equal == 7) = 10000
|otherwise = 100000
where
equal :: Int -> Int -> Int
equal 0 0 = 0
equal a b
|(a `mod` 10 == b `mod` 10) = 1 + equal (a `div` 10) (b `div` 10)
|otherwise = 1 + equal (a `div` 10) (b `div` 10)
What am I doing wrong? Also what does the error mean exactly?
EDIT: Did it like this and it works like a charm!
digits :: Int->Int->Int
digits x y
|(c < 2) = 0
|(c == 2) = 1
|(c == 3) = 5
|(c == 4) = 20
|(c == 5) = 300
|(c == 6) = 8000
|(c == 7) = 10000
|otherwise = 100000
where c = equal x y
where
equal :: Int -> Int -> Int
equal 0 0 = 0
equal a b
|(a `mod` 10 == b `mod` 10) = 1 + equal (a `div` 10) (b `div` 10)
|otherwise = 0 + equal (a `div` 10) (b `div` 10)

Related

Can not get variables to pass from function to main

I can not get main to print anything besides the variables are equal to zero which means my greedy1 function is not passing my variable and I do not know what is wrong.
def greedy1(total):
if (total >= 25):
q = total // 25
remainderCentsQ = total % 25
d = remainderCentsQ // 10
remainderCentsD = total % 10
n = remainderCentsD // 5
remainderCentsN = total % 5
p = remainderCentsN // 1
return (q, d, n, p)
elif (total >= 10):
q = 0
d = total // 10
remainderCentsD = total % 10
n = remainderCentsD // 5
remainderCentsN = total % 5
p = remainderCentsN // 1
return (q, d, n, p)
elif (total >= 5):
q = 0
d = 0
n = total // 5
remainderCentsN = total % 5
p = remainderCentsN // 1
return (q, d, n, p)
else:
q = 0
d = 0
n = 0
p = total // 1
return (q, d, n, p)
def printCoins1(total, q, d, n, p):
print("For " + str(total) + " cents change, I give\n" + str(q) + " quarters\n" + str(d) + " dimes\n" + str(n) + " nickels\n" + str(p) + " pennies")
def main():
total = int(input("How many cents change? "))
q = 0
d = 0
n = 0
p = 0
greedy1(total)
printCoins1(total, q, d, n, p)
main()
Inside your main function, you're calling greedy1 function with total that returns the tuple. But you're not storing this return values back to your variables q, d, n, p.
your main() should look like this:
def main():
total = int(input("How many cents change? "))
(q, d, n, p) = greedy1(total)
printCoins1(total, q, d, n, p)
#Sayas has given the correct answer however there is some redundant code , so I will improve it in my answer
def main():
total = int(input("How many cents change? "))
printCoins1(total, *greedy1(total))

mysql logical "if then" on select

I have a table 'A' that look like that :
L
S
A
1
B
1
C
0
D
1
And I want to add a new column U saying 'OK' or 'NOK' depending on following condition :
If A = 0 & B = 1 => Line A = NOK Else OK
If A = 1 & C = 1 => Line B = NOK Else OK
If B = 1 & D = 1 => Line C = NOK Else OK
So it should look like :
L
S
U
A
1
OK
B
1
OK
C
0
NOK
D
1
OK
I have tried with a CASE, but it is not working, I cannot do the "then C = NOK" when I'm in "If B"
SELECT A.*,
(
CASE
WHEN (L = 'A' AND S = 0) AND (L = 'B' AND S = 1) THEN "NOK"
WHEN (L = 'A' AND S = 1) AND (L = 'C' AND S = 1) THEN "NOK"
WHEN (L = 'B' AND S = 1) AND (L = 'D' AND S = 1) THEN "NOK"
ELSE 'OK'
END
) AS `U`
FROM A
How can I achieve that in mysql directly ? Is it possible ?
Thanks

Calculate Complexity of T(n)? [duplicate]

This question already has answers here:
T(n) = T(n/10) + T(an) + n, how to solve this?
(3 answers)
Closed 2 years ago.
Given: T(n) = T(n/10) + T(an) + n for some a (which I know nothing about its value), and that: T(n) = 1 if n < 10.
I want to check if the following is possible (for some a values, and I want to find the smallest possible a):
For every c > 0 there is n0 > 0 such that for every n > n0, T(n) >= c * n Or in other words T(n)=omega(n)
Any help is appreciated.
Suppose that a < 9/10. Let c = max(1/(9/10 - a), 1), so that c ≥ 1 and 1/c ≤ 9/10 - a. Then for 1 ≤ n < 10,
T(n) = 1 ≤ n ≤ cn.
Inductively, for n ≥ 10,
T(n)
= T(n/10) + T(an) + n
≤ cn/10 + can + n
= c(1/10 + a + 1/c)n
≤ c(1/10 + a + 9/10 - a)n
= cn.
Now suppose that a = 9/10. For 1 ≤ n < 10, we know log10 n < 1 and therefore
T(n) = 1 > n log10 n - n.
Inductively, for n ≥ 10,
T(n)
= T(n/10) + T(9n/10) + n
> (n/10) log10 (n/10) - (n/10) + (9n/10) log10 (9n/10) - (9n/10) + n
= (n/10) log10 (n/10) + (9n/10) log10 (9n/10)
> (n/10) log10 (n/10) + (9n/10) log10 ( n/10)
= (n/10) (log10 n - 1) + (9n/10) (log10 n - 1)
= n log10 n - n.
Given c > 0, pick n0 such that log10 n0 - 1 = c, i.e., n0 = exp10(c + 1). Then for all n > n0,
T(n) > n log10 n - n = n(log10 n - 1) > n(log10 n0 - 1) = cn.
For every c > 0 there is n0 > 0 such that for every n > n0, T(n) >= c*n
We assume T(n) >= cn holds for some a and every c > 0.
By substituting the recurrence in the inequality and solving for a, you will get:
T(n) >= c*n
(c*n/10) + (c*a*n) + n >= c*n
a >= (9/10) - (1/c)
Since our desired outcome is to hold for all c (formally apply limit c tends to infinity, which is where RHS will maximize), we get a >= (9/10). Therefore, smallest value of a is (9/10) which will satisfy T(n) >= c*n for all c.
After this, you can prove by induction, that this is indeed the case.
In each function, you need to find the highest impacting variable of the equation. On the big scale, the smallest possible N affecting this function is any Constant.

Solving inequality leaves CRootOf in answer

when I try to solve this inequality x^3 + 3*x > 3 I get this output:
octave:5> eqn = x^3 + 3*x > 3
eqn = (sym)
3
x + 3⋅x > 3
octave:6> solve(eqn, x)
ans = (sym)
⎛ 3 ⎞
x < ∞ ∧ CRootOf⎝x + 3⋅x - 3, 0⎠ < x
But when I try to solve simpler inequality everything works fine:
eqn = abs(x^2-3)>3
eqn = (sym)
│ 2 │
│x - 3│ > 3
octave:4> solve(eqn, x)
ans = (sym) (-∞ < x ∧ x < -√6) ∨ (√6 < x ∧ x < ∞)
What can I do with this CRoot?

Excel VBA UDF Formatting - Change argument values before calculation

I am trying to write a formula in Excel VBA to calculate: RR = ((A / (A + B)) / (C / (C + D)))
When any of the four arguments (A, B, C, D) are 0, I want to change their value to 0.5 in the calculation.
Is there an easy way of doing this? I believe my formatting is wrong or I'm going about it the wrong way. Any helpful tips would be greatly appreciated.
I've tried:
Function RR(A, B, C, D) As Double
If A = 0 And B = 0 Then
A = 0.5
B = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf A = 0 Then
A = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf B = 0 Then
B = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf C = 0 Then
C = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf D = 0 Then
D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
ElseIf A = 0 And D = 0 Then
A = 0.5 And D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
Else: RR = ((A / (A + B)) / (C / (C + D)))
End If
End Function
Try this one:
Function RR(A, B, C, D) As Double
If A = 0 Then A = 0.5
If B = 0 Then B = 0.5
If C = 0 Then C = 0.5
If D = 0 Then D = 0.5
RR = ((A / (A + B)) / (C / (C + D)))
End Function