Difference between `if` statements [closed] - algorithm

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Am newbie in programming, just curious, what is the difference between
if(a == 1)
{
//condition
}
if(b == 1)
{
//condition
}
and
if(a == 1 || b == 2)
{
//condition
}
I don't have any questions regarding this, all I want is clarification. Don't over think guys, I know you're all pro's. Take note on the "just curious", cause am noob.

The first statement compares the variable 'a' to '1', and if the variable 'a' is equal to '1', then you will execute the block of code enclosed in brackets. Likewise, the first statement also compares the variable 'b' to '1' and if equal will execute a block of code.
The second expression evaluates two conditions, 'a' being equal to 1, and 'b' being equal to '2'. If either expression is true, then the block of code enclosed in brackets will execute. The double pipe symbol '||' is synonymous with 'or'.

Related

Elegant/Best solution to get the last element from an iteration [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm receiving multiple messages over a channel, and after iterating over them, I would like to keep the last element for further usage. My first (probably bad!) approach was to declare some variable, and then assign it every loop.
let last = 0;
for some in rx_from_channel.iter() {
let last = some;
}
let a = last + 5;
I really don't like this solution - is there a to avoid assigning last in each loop?
Further, I would have expected that after using let last inside the for {} loop for the first time, the variable declared above the loop goes out of scope - and ļast shouldn't be available after the for {} loop at all. The compiler suggests otherwise - why?
You can just do:
let last = rx_from_channel.iter().last().unwrap_or_else(|| &0);
let a = last + 5;
See last()
fn last(self) -> Option<Self::Item>
Consumes the iterator, returning the last element.
Doesn't method last() solve your problem?

Calculate difference between two numbers and get the absolute value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to find the difference between two numbers in Go and the result should not be in "-".
Please find my code below:
dollarValue := 240000 - 480000
The result is "-240000". But my expected output is just "240000". Can anybody help on how to calculate the difference between these two numbers.
Your title is misleading. It should be states without negative instead of - operator.
Basically what you want to get is the absolute different between two numbers
You have two options:
Use if/else condition to return the positive result if the result is negative
Use math.Abs (need to convert from/to float)
Just implement your own method
func diff(a, b int) int {
if a < b {
return b - a
}
return a - b
}
and use it like this:
dollarValue := diff(240000, 480000)

For loop inside if/else inside while. Proper syntax [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why doesn't this work? I'm getting unexpected keyword_end.
while(!k && DATA[q])
if DATA[q] == 'aa' && DATA[p] == 'aa'
pl = DATA[r]
for i in 0..pl
PACK[i] = DATA[i+4]
end
k+=1 #end while
else
q++
p++
r++
end
end
You are getting error because ++ does not work in Ruby.
Ruby has no pre/post increment/decrement operator. For instance, x++
or x-- will fail to parse. More importantly, ++x or --x will do
nothing! In fact, they behave as multiple unary prefix operators: -x
== ---x == -----x == ...... To increment a number, simply write x += 1.
source (which indeed are the words of Ruby's author himself.)
You should replace them as follows:
q+=1
p+=1
r+=1

Get all three digit numbers from array and store in new array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need a regular expression that only matches three digit numbers in the following array. I need the result to be a new array.
Input:
my_array = [111,45456,456,74897,787,45466,789,6587,784,234,456,4658,4587,235,456]
Desired output:
new_array = [111,456,787,789,784,234,456,235,456]
Why regular expression on numbers? You can select all numbers less than 1000 and greater than 99.
my_array.select { |n| n<1000 && n>99 }
Just the regexp would look like this: /^\d{3}$/. But if you'd like an expression that would return an array of values that match that expression this would do it: my_array.select{ |num| num.to_s.match(/^\d{3}$/) }.
Take a look at RegExr to learn more about Regular Expressions.

ruby match two strings based 4 different criteria [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm creating a quick search rails app feature. Any help with how to structure the following matching specifications would be appreciated.
Basically, when a user enters a name to search, they should get results based on the following matching criteria.
accept match on first 3 chars (e.g. Jon for Jones)
reject match on less than 3 chars (e.g. Jo for Jones)
accept exact match for 2 char author name (e.g. Li for Li)
reject exact match on 1 char author name
reject mismatch on chars beyond 3 (e.g. reject Jonis for Jones)
Can this be done with a regular expression?
matchto = 'Jones'.downcase
input = 'Jon'.downcase
matchto.start_with?(input) && 1 < input.length &&
( input.length == matchto.length || 2 < input.length )

Resources