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.
Related
I am trying to list down all the prime numbers up till a specific number e.g. 1000. The code gets slower as the number increase. I am pretty sure it is because of the for loop where (number -1) is checked by all the prime_factors. Need some advise how I can decrease the processing time of the code for larger numbers. Thanks
import time
t0 = time.time()
prime_list = [2]
number = 0
is_not_prime = 0
count = 0
while number < 1000:
print(number)
for i in range (2,number):
count = 0
if (number%i) == 0:
is_not_prime = 1
if is_not_prime == 1:
for j in range (0,len(prime_list)):
if(number-1)%prime_list[j] != 0:
count += 1
if count == len(prime_list):
prime_list.append(number-1)
is_not_prime = 0
count = 0
break
number += 1
print(prime_list)
t1 = time.time()
total = t1-t0
print(total)
Your solution, on top of being confusing, is very inefficient - O(n^3). Please, use the Sieve of Eratosthenes. Also, learn how to use booleans.
Something like this (not optimal, just a mock-up). Essentially, you start with a list of all numbers, 1-1000. Then, you remove ones that are the multiple of something.
amount = 1000
numbers = range(1, amount)
i = 1
while i < len(numbers):
n = i + 1
while n < len(numbers):
if numbers[n] % numbers[i] == 0:
numbers.pop(n)
else:
n += 1
i += 1
print(numbers)
Finally, I was able to answer because your question isn't language-specific, but please tag the question with the language you're using in the example.
This function should take two arguments a list and an int. if an element of the list and the number “a” parity is equal then they’d have to be summed, else the two numbers should be subtracted.
The calculation should be done in this order :
At the beginning, the residual value r is the value of a,
Each element e of lst (taken in the order given by the list) affects the residual value: if e and r are of the same parity (both odd or both even) then the new r’ is equal to the sum of r + e, if not then it should be equal to the subtraction of r - e,
The last r is the result expected.
To put this into an example:
par [4;7;3;6] 5
should return -1, it would work as follows :
5 and 4 have a different parity so we subtract -> 5 - 4 = 1
1 and 7 are both odd, so we add them together -> 1 + 7 = 8
8 and 3 have a different parity -> 8 - 3 = 5
Finally, 5 and 6 have different parity -> 5 - 6 = -1
I have thought of something like this below :
let rec par lst a =
match lst with
| [] -> 0
| h::t -> if (h mod 2 == 0 && a mod 2 == 0) || (h mod 2 == 1 && a mod 2 == 1) then a + h
| h::t -> if (h mod 2 == 0 && a mod 2 == 1) || (h mod 2 == 1 && a mod 2 == 0) then a - h :: par t a ;;
EDIT1 : Here is the error I get from the compiler :
Line 4, characters 83-88: Error: This expression has type int but an
expression was expected of type unit because it is in the result of a
conditional with no else branch
The idea is to build this function using no more than the following predefined functions List.hd, List.tl et List.length.
What is disturbing in my proposition above and how to remediate it? Anyone can help me resolve this, please?
EDIT 2:
I was able to do what is needed with if...then... else syntax (not the best I know for OCaml) but I personally have more difficulties sometimes understanding the pattern matching. Anyhow here's what I got :
let rec par lst a = (* Sorry it might hurt some sensible eyes *)
if List.length lst = 0 then a
else
let r = if (List.hd lst + a) mod 2 == 0 then (a + (List.hd lst))
else (a - (List.hd lst)) in
par (List.tl lst) r ;;
val par : int list -> int -> int = <fun>
Suggestions and help to put it into a pattern-matching syntax are welcomed.
Your code doesn't compile. Did you try compiling it? Did you read the errors and warnings produced by the compiler? Could you please add them to your question?
A few comments about your code:
| h::t -> if ... then ... should be | h::t when ... -> ...;
(h mod 2 == 0 && a mod 2 == 0) || (h mod 2 == 1 && a mod 2 == 1) can be simplified to (h - a) mod 2 == 0;
The compiler likes to know that the matching was exhaustive; in particular, you don't need to repeat the test in the third line of the matching (the third line will only be read if the test was false in the second line);
You are missing the recursive call in the second line of the matching;
In the third line of the matching, you are returning a list rather than a number (the compiler should have explicitly told you about that type mismatch!! did you not read the compiler error message?);
In the first line of the matching, in case the list is empty, you return 0. Are you sure that 0 is the value you want to return, when you've reached the end of the list? What about the residual value that you have calculated?
Once you have fixed this version of your code as a recursive function, I recommend trying to write a code solving the same problem using List.fold_left, rather than List.hd and List.tl as you are suggesting.
When I first wrote my answer, I included a fixed version of your code, but I think I'd be doing you a disservice by handing out the solution rather than letting you figure it out.
There is a simple trick to convert a number to 1 or -1.
Just raise it to the power of 0.
So:
4^0 = 1
-4^0 = -1
However, in AS3:
Math.pow( 4, 0); // = 1
Math.pow(-4, 0); // = 1
Is there a way to get the right answer without an if else?
This could be done bitwise.
Given the number n (avg time: 0.0065ms):
1 + 2 * (n >> 31);
Or slightly slower (avg time: 0.0095ms):
(n < 0 && -1) || 1;
However, Marty's solution is the fastest (avg time: 0.0055ms)
n < 0 ? -1 : 1;
Not sure if without an if/else includes the ternary operator in your eyes, but if not:
// Where x is your input.
var r:int = x < 0 ? -1 : 1;
Will be more efficient than Math.pow() anyway.
I know i can do this
if( a<8 , 1, 0) as Value
but can i do this?
if( 2<a<8 , 1, 0) as Value in mysql ?
I have tired, it doesn't work. if i want to get the 2nd result is there a way to do ?
Sure, you can, but it doesn't give you the result you're hoping for. Perfectly valid, however.
Example:
SELECT IF(1 < 2 < 3, 1, 0) AS `value`
This returns '1'. It must work, right? Not really. What happens is the following:
Is 1 < 2 ? Sure! Let's return "1" (an INT because MySQL doesn't have a boolean TRUE)
Is 1 < 3 ? Sure!
So what happens when we do this?
SELECT IF(100 < 200 < 100) AS `value`
Well.. That's TRUE (1) as well. Because this happens:
Is 100 < 200? Sure! So let's return "1".
Is 1 < 100? Yep!
To be more standardized, I'd write this with a CASE statement:
CASE WHEN a > 2 AND a < 8 THEN 1 ELSE 0 END AS Value
I don't think so, but you can IF( 2<a AND a<8, 1, 0)
SELECT IF (a BETWEEN 3 AND 7, 1, 0) AS Value
Technically you could just use SELECT a BETWEEN 3 AND 7 as this will return 1 or 0 anyway.
Using 3 and 7 because you want > 2 and < 8 not >= 2 and <= 8.
Your example will evaluate 2 < a which returns 0 or 1 depending on the value of a. Then it evaluates 0 < 8 or 1 < 8, which will always evaluate to 1.
Trouble with using Max in where clause of LINQ to SQL. Data below:
QID, Question, TypeID, Disable, VersionID, Sequence
1 Who's on 1st 1 False 1 1
2 Who's on 1st 1 False 2 1
3 What's on 2nd 1 False 1 2
4 What's on 2nd 1 False 2 2
5 I don't know 1 False 1 3
6 I don't know 1 False 2 3
I need to return a group of questions based on the Max of the VersionID as noted below. The result I expect from the data above would include rows 2, 4 & 6 ordered by Sequence.
IEnumerable<QUESTION> questions =
(from q in dataContext.QUESTIONs
where q.TypeID == Convert.ToInt16(ddlType.SelectedValue)
&& (q.Disable == null || q.bDisable == false)
&& (q.VersionID == dataContext.QUESTIONs.Max(q.nVersionID))
orderby q.Sequence ascending
select q);
Max() translates properly in linq-to-sql
Try
IEnumerable<QUESTION> questions = (from q in dataContext.QUESTIONs
let maxVersion = dataContext.QUESTIONs.Max(q.nVersionID)
where q.TypeID == Convert.ToInt16(ddlType.SelectedValue)
&& (q.Disable == null || q.bDisable == false)
&& (q.VersionID == maxVersion)
orderby q.Sequence ascending
select q);