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)
Related
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 1 year ago.
Improve this question
I have below kind of array,
val mArrayList: ArrayList<String> = ArrayList()
mArrayList.add("10-12 YRS")
mArrayList.add("2-3 YRS")
mArrayList.add("4-5 YRS")
mArrayList.add("7-10 YRS")
mArrayList.add("5-7 YRS")
I have the above kind of ArrayList and I want to sort this ArrayList.
How to sort this kind of array?
Can anyone please help?
In Kotlin, you have the sortedBy/sortBy methods to sort such a list. The first one returns a new list that is sorted, the second sorts the receiver list in-place.
The lambda you pass it allows you to extract the value you want to use for comparison.
For instance, you can use this to sort by the first integer in your elements:
val sorted = mArrayList.sortedBy { it.split("-").first().toInt() }
Or if you want to sort your list in place:
mArrayList.sortBy { it.split("-").first().toInt() }
In both cases, it.split("-").first().toInt() splits the string into 2 parts (before and after the dash), and converts the first part into an integer (which is comparable).
I believe, this is the sort you are looking for:
mArrayList.sortBy { it.split("-", limit = 2)[0].toInt() }
println(mArrayList) //[2-3 YRS, 4-5 YRS, 5-7 YRS, 7-10 YRS, 10-12 YRS]
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?
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.
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 8 years ago.
Improve this question
I am trying to count the arrays themselves not the elements. so say i had the following
arrays = [["1 2"],["3 4"],["5 6"],["6 7"]]
i am then trying to find a way to return only one of these arrays as a set of instructions if asked for?
Your example showed a multi-dimensional array, which is an array of arrays.
To count the number of arrays in this 2 dimensional array, in other words the number of elements, you could use the following methods.
arrays = [["1 2"],["3 4"],["5 6"],["6 7"]]
arrays.length
arrays.size
arrays.count
If you would like to return one of them you simply reference the element
arrays[0]
=> Returns ["1 2"]
If you want to return a random element,
arrays.sample
=> Returns a random array
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 8 years ago.
Improve this question
I use the excellent faker gem to generate random words for my models. Eg. product.name = Faker::Lorem.word
Sometimes I need to generate a sentence, and I want the length of the sentence to
vary each time.
How to achieve this with ruby?
How about:
result = rand(max_size).times.map { produce_word }
Since you have not provided enough information, this is my approach, [*1..100].sample will return a random number between 1 and 100, so looping that times the string which is returned bya method named get_word will get stored in the array word_array
word_array = []
[*1..100].sample.times do
word_array << get_word
end