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?
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 4 months ago.
Improve this question
how can I use for loop to delete maximum and minimum values from a dictionary. for instance if you have a dictionary grades = {sam: [23,43,55]}, peter: [66,55,44], sarah: [99,55,77]}. how can I remove only the maximum and minimum values?
im new to coding and cannot figure it out. Language is python
Hi you can try something like this:
# Create dictionary
dic = {'sam': [23,43,55], 'peter': [66,55,44], 'sarah': [99,55,77]}
# Use for loop to iterate
for key, value in dic.items():
# Find maximum value and it's index
max_value = max(value)
max_index = value.index(max_value)
# Delete maximum value
del dic[key][max_index]
# Find minimum value and it's index
min_value = min(value)
min_index = value.index(min_value)
# Delete miniumum value
del dic[key][min_index]
# Print output
print(dic)
Let me know if you don't understand anything. Cheers
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 needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to write a script on Bash that does:
I have a file, that consists of string like this:
2002-02-15 00:01:19 217.21.43.21 RES company_name
2002-02-15 00:01:19 217.21.43.21 RES company_name
2002-02-15 00:01:19 217.21.43.21 DEL company_name
2002-02-13 00:01:19 217.21.43.21 RES company_name
I need to calculate the number of requests with parameter RES for each day.
Output of script should be:
2002-02-15 2
2002-02-13 1
This should be enough:
awk '/RES/ { N[$1] += 1; }; END { for (day in N) { print day, N[day] } }' your_input
It creates an associative array N whose indices are the days of the first field, and whose values are incremented by one for every line matching RES.
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)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a question:
Find the middle element from a single linked list.
I need to know the way/method of this problem.
You can use two pointers to iterate through the list - one which iterates twice as fast as the other. When the fast pointer reaches the end of the list then the slow pointer will be pointing at the mid-point.
Algorithm:
init slow_pointer = head
init fast_pointer = head
repeat
fast_pointer = fast_pointer->next;
if fast_pointer == NULL
break;
fast_pointer = fast_pointer->next;
if fast_pointer == NULL
break;
slow_pointer = slow_pointer->next;
until false
// slow_pointer now points at the middle node