Extract middle element from a single linked list [closed] - algorithm

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

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?

how to count arrays within an array in ruby and return this 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 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

Ruby: how to make an array of words, with the length of the array being random [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 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

Interpreting "takes as input a sorted list of N integers output will be sorted squared integer list" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
how would you interpret that sentence in order to start coding. do you take it as the user inputs the number into an array or does it come from a text document for example and just scan it.
I already did it for both but I only have one submission so I have to make it count
I think the requirement is very clear - you need to write a function that receives a sorted list of integers, and returns a list of their squares.
E.g., in Java:
public List<Integer> squareList (List<Integer list) {
List<Integer> retVal = new ArrayList<>(list.size());
for (int item : list) {
retVal.add(item * item);
}
Collections.sort(retVal);
return retVal;
}

Add element to array and generate Hash with two arrays in ruby [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
Need to add two arrays before that need to add some value at starting of the first array. Look at the following:
#conunty_format = [ "country", "imps", "revenue","network_revenue"]
final_ca = [2000,55.62,88.69]
I need to add "Canada" to final_ca and generate hash with corresponding county_format.
Hash[#conunty_format.zip(final_ca.unshift('canada'))]
=> {"country"=>"canada", "imps"=>2000, "revenue"=>55.62, "network_revenue"=>88.69}
You can use Array Zip and some properties of Array to achieve it in a single line. see the below code.
resulted_hash = #country_format.zip(final_ca.unshift("Canada")).inject({}) do |r, s| r.merge!({s[0] => s[1]}) end

Resources