Determine type of triangle - sql

I'm trying the hacker rank type of triangle below where based on the 3 sides it has to be determined if the triangle is equilateral, isosceles, scaelene, or not a triangle.
https://www.hackerrank.com/challenges/what-type-of-triangle/problem
I'm not sure why the code below isn't passing the test case. Unfortunately, I can't download the test case to see why it isn't working.
SELECT CASE WHEN A = B AND B = C AND A = C THEN 'Equilateral'
WHEN (A = B AND B != C AND A != C) OR (B = C AND A != B AND A != C) OR (A = C AND A != B AND B != C) THEN 'Isosceles'
WHEN ((A + B) < C) OR ((B + C) < A) OR ((C + A) < B) THEN 'Not a triangle'
ELSE 'Scalene' END
FROM Triangles

Try something like this:
SELECT
CASE
WHEN A + B > C AND A + C > B AND B + C > A THEN
CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene' END
ELSE 'Not A Triangle' END
FROM TRIANGLES
Only test for the type of triangle when it is a triangle.

Try this :
SELECT CASE WHEN A + B > C AND A+C>B AND B+C>A THEN
CASE WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
WHEN A != B OR B != C OR A != C THEN 'Scalene'
END
ELSE 'Not A Triangle' END FROM TRIANGLES;
The condition for scalene triangle does not include the following condition in your original query : Side 1 + Side 2 <= Side 3

Related

If, Then and Select Case

I'm writing VB Code and I see a question below
There are three positive integers A, B, C
If A is greater than B, C is equal to A+B
If A is less than or equal to B, then C is equal to A-B.
Please use IF...Then and Select/Switch Case to write a program, both of which are used in this program, and additional variables can be added by yourself.
I would like to ask how to write this question, as far as I know, the answer just need only IF Then or Select Case can be realized?
Dim A As Double = 3
Dim B As Double = 2
Dim C As Double = 1
Dim D As Double = 0
D = A - B
Select Case D
Case D > 0
C = A + B
Case D < 0
C = A - B
End Select
If D > 0 Then
C = A + B
ElseIf D < 0 Then
C = A - B
End If
In the real world you wouldn't need to use both an If/Then statement and a Select/Case statement. Since this appears to be homework, I believe the exercise is to see if you can use both conditional check.
I would setup two functions that accept two arguments (a and b) that return the value of c. In your first function use an If/Then and in your second function use a Select/Case.
E.g.
Private Function IfThenOption(a As Integer, b As Integer) As Integer
Dim c As Integer
If (a > b) Then
c = a + b
ElseIf (a < b) Then
c = a - b
Else
c = a
End If
Return c
End Function
Private Function SelectCaseOption(a As Integer, b As Integer) As Integer
Dim c As Integer
Select Case True
Case a > b
c = a + b
Case a < b
c = a - b
Case Else
c = a
End Select
Return c
End Function
Example: https://dotnetfiddle.net/kwcyWc

CASE expression in WHERE clause for diferent and

Can I use case expression to build where like this?
select *
from table
where
case
when x=y then z= j and t=v
when x=k then q= p and s=l
end
;
I need change where clause depending on the value of x variable.
Use or:
select *
from table
where (x = y and z = j and t = v) or (x = k and q = p and s = l);
An alternative to using OR is to use nested CASE statements:
SELECT *
FROM table_name
WHERE CASE
WHEN x = y THEN CASE WHEN z = j AND t = v THEN 1 ELSE 0 END
WHEN x = k THEN CASE WHEN q = p AND s = l THEN 1 ELSE 0 END
ELSE 0
END = 1;
or you could simplify it to:
SELECT *
FROM table_name
WHERE CASE
WHEN x = y AND z = j AND t = v THEN 1
WHEN x = k AND q = p AND s = l THEN 1
ELSE 0
END = 1;
However, you should check whether Oracle can use column indexes or if a separate function-based index is required with these solutions.

SQL Server: update five columns with one condition

In a table, I have a column for currency. I have to calculate a value from two columns (ex: a, b) and update one of the five columns (ex: e, f, g, h, i, j) based on the currency.
I tried with CASE like
CASE when currency = 'INR'
then a+b
else 0
end as e
CASE when currency = 'USD'
then a+b
else 0
end as f
CASE when currency = 'CAN'
then a+b
else 0
end as g
CASE when currency = 'EUR'
then a+b
else 0
end as h
CASE when currency = 'AUD'
then a+b
else 0
end as i
Is there any other way to do this?
Update myTable
set e = CASE when currency = 'INR' then a+b ELSE 0 end,
f = CASE when currency = 'USD' then a+b ELSE 0 end,
g = CASE when currency = 'CAN' then a+b ELSE 0 end,
h = CASE when currency = 'EUR' then a+b ELSE 0 end,
i = CASE when currency = 'AUD' then a+b ELSE 0 end;
Pseudo code would be like following.
NULL / 0 values are not covered.
var result = a+b;
var columnName;
CASE
WHEN currency = 'INR' THEN columnName = e,
WHEN currency = 'USD' THEN columnName = f,
.
.
WHEN currency = 'AUD' THEN columnName = j
END
**your update statement goes here with 'columnName' variable..**

Where Clause Case Statement; ORA-00907: missing parenthesis

I have a query and when I run it I get the error message ORA-00907: missing parenthesis. When I replace the CASE statement with either x = g and or y = g and it runs as expected.
SELECT *
FROM
table1,
table2,
table3,
table4,
table5,
table6,
table7,
table8
WHERE
a = b and
c = d and
e = d and
CASE strfldvar
WHEN 'BROKEN_ARROW' THEN (x = g)
WHEN 'BROKEN_BOX' THEN (y = g)
ELSE -1
end
and
f = h and
i = j
What am I doing wrong here?
case is an expression, not a predicate (i.e condition) : it 'returns' a typed value and can not contain predicate as result (in the then parts). In your case (assuming else -1 means 'no match') :
AND g = CASE strfldvar
WHEN 'BROKEN_ARROW' THEN x
WHEN 'BROKEN_BOX' THEN y
ELSE NULL -- never match, even if g is null
END
although I think it would be simpler to replace it with :
AND (
(strfldvar = 'BROKEN_ARROW' AND x = g)
OR (strfldvar = 'BROKEN_BOX' AND y = g)
)
Instead of
CASE strfldvar
WHEN 'BROKEN_ARROW' THEN (x = g)
WHEN 'BROKEN_BOX' THEN (y = g)
ELSE -1
have this:
x=CASE strfldvar WHEN 'BROKEN_ARROW' THEN g ELSE x END
y=CASE strfldvar WHEN 'BROKEN_ARROW' THEN g ELSE y END
I would replace
CASE strfldvar
WHEN 'BROKEN_ARROW' THEN (x = g)
WHEN 'BROKEN_BOX' THEN (y = g)
ELSE -1
by
(CASE WHEN strfldvar = 'BROKEN_ARROW' and x = g then 1
WHEN strfldvar = 'BROKEN_BOX' and y = g then 1
ELSE -1
END) = 1

You have four numbers, how do you figure out which one is greatest?

Is there a very simple algorithm to figure out which of 4 numbers is the greatest?
var lst = new List<int>() { 1, 7, 3, 4 };
var max = lst.Max();
I got no VB, but you get the idea.
If they are in an array, something like this should work:
VB:
Dim ar As Integer() = {3, 6, 9, 12}
Dim largest As Integer = ar(0)
For i As Integer = 1 To ar.Length - 1
If ar(i) > largest Then
largest = ar(i)
End If
Next
C#:
int[] ar = {3, 6, 9, 12};
int largest = ar[0];
for(int i = 1; i < ar.Length; i++) {
if(ar[i] > largest) {
largest = ar[i];
}
}
If you're using a language that supports some sort of max function or array sorting definitely use those features. Or choose any of the other sane answers in this thread. However, just for fun:
maximum = (var1 > var2 ? var1 : var2) > (var3 > var 4 ? var3 : var 4) ?
(var1 > var2 ? var1 : var2) :
(var3 > var 4 ? var3 : var 4);
Put the numbers into an array, sort the array, then select the one whose index is array length -1.
Or you could put the numbers into an array, sort the array, reverse the array, and then select index 0.
If you need to write your own sorting algorithm, the simplest one to implement is likely to be the bubble sort.
With VB.Net you could the following and it will work for any number of numbers
Public Function Max(ParamArray items As Integer()) As Integer
if items.Length = 0 Then
throw New ArgumentException("need at least 1 number")
End IF
return items.Max()
End Function
Then you can now do
Max(1,2,3,4)
There are plenty of ways you could do this.
A really naive approach would be:
#Pseudocode
If number1 > number2 and number1 > number3 and number1 > number4: return number1
Else if number2 > number3 and number2 > number4: return number2
Else if number3 > number4: return number3
Else: return number4
It's more practical to use arrays but if you're starting that could be more complicated than simple if blocks.
If they're in an array - and doing it explicitly rather than using sort:
int max = int.MinValue; // i.e. the "largest" negative number
int largest = -1;
for (int index = 0; index < array.Length; index++)
{
if (array[index] > max)
{
max = array[index];
largest = index;
}
}
The greatest value will be max and it's index in largest.
Nate's answer is more efficient as it uses the first element of the array as the initial value. So the first three lines of my solution would become:
int max = array[0];
int largest = 0;
for (int index = 0; index < array.Length; index++)
Get A, B, C, D from user;
largest = A;
if B > largest then
Largest = B;
if C > largest then
Largest = C;
if D > largest then
largest = D;
Print largest
In Java, if a is an int[4]:
Math.max(Math.max(a[0], a[1]), Math.max(a[2], a[3]))
Dim MyValues As New List(Of Double)
Dim MaxValue As Double
Dim tValue As Double
MyValues.Add(12.58)
MyValues.Add(3.58)
MyValues.Add(518.6)
MyValues.Add(244)
MyValues.Add(31.25)
For Each tValue In MyValues
If MaxValue < tValue Then
MaxValue = tValue
End If
Next
MsgBox(MaxValue)
My first question would be why? Second would be, if it's only four numbers then it really doesn't matter. Whatever takes your fancy. I personally would go with the fewest lines of code. Which would be to use the built in array.Sort method, then take the last item.
I would also consider using LINQ, just because you can.
Or Math.Max in a nasty nested way so Math.Max(Number1,Math.Max(Number2,Math.Max(Number3,Number4))))
If there could be hundreds of numbers, then I would try and pick a better algorithm. Probably the one suggested by #ChrisF, although it would depend on where the numbers are coming from, EG a database could find the max much easier, or if the numbers are being read from somewhere sequentially then you could store the max as you read the numbers.
this is my own analization. i made this code to display the lowest and highest numbers among the 4 inputted numbers from the textbox,. it will display the lowest and highest to appointed labels. if u input two same lowest or highest numbers, a msgbox appear to notify u somehow that u inputted same highest or lowest numbers and it displays back to its appropriate label. i used labels for the display of lowest and highest. here's my fb: iver saladaga anoos , 2ndyear student of JHCSC tambulig, zamboanga del sur, philippines..
so here it is! it worked for me. im using vb6 enterprise edition. :)
Private Sub Command1_Click()
Dim A, B, C, D As Long
A = Text1.Text
B = Text2.Text
C = Text3.Text
D = Text4.Text
If A < B And A < C And A < D Then
Label9.Caption = A
Else
If A > B And A > C And A > D Then
Label10.Caption = A
End If
End If
If A < B And A < D And A < C Then
Label9.Caption = A
Else
If A > B And A > D And A > C Then
Label10.Caption = A
End If
End If
If A < C And A < B And A < D Then
Label9.Caption = A
Else
If A > C And A > B And A > D Then
Label10.Caption = A
End If
End If
If A < C And A < D And A < B Then
Label9.Caption = A
Else
If A > C And A > D And A > B Then
Label10.Caption = A
End If
End If
If A < D And A < C And A < B Then
Label9.Caption = A
Else
If A > D And A > C And A > B Then
Label10.Caption = A
End If
End If
If A < D And A < B And A < C Then
Label9.Caption = A
Else
If A > D And A > B And A > C Then
Label10.Caption = A
End If
End If
If B < C And B < A And B < D Then
Label9.Caption = B
Else
If B > C And B > A And B > D Then
Label10.Caption = B
End If
End If
If B < C And B < D And B < A Then
Label9.Caption = B
Else
If B > C And B > D And B > A Then
Label10.Caption = B
End If
End If
If B < A And B < C And B < D Then
Label9.Caption = B
Else
If B > A And B > C And B > D Then
Label10.Caption = B
End If
End If
If B < A And B < D And B < C Then
Label9.Caption = B
Else
If B > A And B > D And B > C Then
Label10.Caption = B
End If
End If
If B < D And B < C And B < A Then
Label9.Caption = B
Else
If B > D And B > C And B > A Then
Label10.Caption = B
End If
End If
If B < D And B < A And B < C Then
Label9.Caption = B
Else
If B > D And B > A And B > C Then
Label10.Caption = B
End If
End If
If C < A And C < B And C < D Then
Label9.Caption = C
Else
If C > A And C > B And C > D Then
Label10.Caption = C
End If
End If
If C < A And C < D And C < B Then
Label9.Caption = C
Else
If C > A And C > D And C > B Then
Label10.Caption = C
End If
End If
If C < B And C < A And C < D Then
Label9.Caption = C
Else
If C > B And C > A And C > D Then
Label10.Caption = C
End If
End If
If C < B And C < D And C < A Then
Label9.Caption = C
Else
If C > B And C > D And C > A Then
Label10.Caption = C
End If
End If
If C < D And C < A And C < B Then
Label9.Caption = C
Else
If C > D And C > A And C > B Then
Label10.Caption = C
End If
End If
If C < D And C < B And C < A Then
Label9.Caption = C
Else
If C > D And C > B And C > A Then
Label10.Caption = C
End If
End If
If D < A And D < B And D < C Then
Label9.Caption = D
Else
If D > A And D > B And D > C Then
Label10.Caption = D
End If
End If
If D < A And D < C And D < B Then
Label9.Caption = D
Else
If D > A And D > C And D > B Then
Label10.Caption = D
End If
End If
If D < B And D < A And D < C Then
Label9.Caption = D
Else
If D > B And D > A And D > C Then
Label10.Caption = D
End If
End If
If D < B And D < C And D < A Then
Label9.Caption = D
Else
If D > B And D > C And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < B And D < A Then
Label9.Caption = D
Else
If D > C And D > B And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < A And D < B Then
Label9.Caption = D
Else
If D > C And D > A And D > B Then
Label10.Caption = D
End If
End If
Command2.Enabled = True
If A = D And A > C And A > B Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = D And A < C And A < B Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If A = B And A > C And A > D Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = B And A < C And A < D Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If B = D And B > A And B > C Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = D And B < A And B < C Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If B = C And B > D And B > A Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = C And B < D And B < A Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If C = A And C > D And C > B Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = A And C < D And C < B Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
If C = D And C > B And C > A Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = D And C < B And C < A Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
End Sub
Private Sub Command2_Click()
Text1.Text = Clear
Text2.Text = Clear
Text3.Text = Clear
Text4.Text = Clear
Label9.Caption = Clear
Label10.Caption = Clear
Command2.Enabled = False
Command1.Enabled = False
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Command2.Enabled = False
Command1.Enabled = False
End Sub
Private Sub Text1_Change()
Command1.Enabled = True
End Sub