Guys.
How to sort an array from particular index to a particular index, not full array sort. I am searching a lot but not find any solution so please tell me how to do this.
You can call sort() directly on a slice of the array:
var array = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
array[2...7].sort()
print(array)
// [9, 8, 2, 3, 4, 5, 6, 7, 1, 0]
The simplest way is extract that specific range from array sort it and after that replace that specific range with sorted array.
Ex
var array = [1,50,42,15,3,25,63,7,26,8,10,36,78,12]
let sliceSortedArray = array[5...10].sorted()
array.removeSubrange(5...10)
array.insert(contentsOf: sliceSortedArray, at: 5)
print(sliceSortedArray) // [7, 8, 10, 25, 26, 63]
print(array) // [1, 50, 42, 15, 3, 7, 8, 10, 25, 26, 63, 36, 78, 12]
Edit As #Martin R suggested you can also use replaceSubrange(_:with:).
array.replaceSubrange(5...10, with: array[5...10].sorted())
You can use a combination of filter and sort which will return a new array with sorted elements. Something along the lines of
let newArray = originalArray.filter {
//This is where you filter based on indexes
}.sort {
//This is where you sort your filtered array
}
Related
I'm trying to add new item into two-dimensional array but I'm getting this error Index out of range
Here is my implementation:
var array = [[Int]]()
array[0][0] = 1 // <-- Index out of range error
Any of you knows why I'm getting this error or if there is work around ?
I really appreciate you help
You have declared an Array of Arrays of Ints.
To append something to that array, you have to create a new Array of Int to append to it.
After you have appended one or more Arrays of Int, you can then modify those values, or append additional Int values to the 2nd-level array:
var array = [[Int]]()
print(array)
// output is: []
// append an array of One Int
array.append([1])
print(array)
// output is: [[1]]
// append an array of Three Ints
array.append([1, 2, 3])
// append an array of Six Ints
array.append([4, 5, 6, 7, 8, 9])
print(array)
// output is: [[1], [1, 2, 3], [4, 5, 6, 7, 8, 9]]
// modify the value of the 2nd Int in the 2nd array
array[1][1] = 100
print(array)
// output is: [[1], [1, 100, 3], [4, 5, 6, 7, 8, 9]]
// append a new Int to the 2nd array
array[1].append(777)
print(array)
// output is: [[1], [1, 100, 3, 777], [4, 5, 6, 7, 8, 9]]
I have an array say [1,2,3,4,5,6,7,8]. I need to take an input from the user and remove the last input number of array elements and append it to the front of the array. This is what I have achieved
def test(number, array)
b = array - array[0...(array.length-1) - number]
array = array.unshift(b).flatten.uniq
return array
end
number = gets.chomp_to_i
array = [1,2,3,4,5,7,8,9]
now passing the argument to test gives me the result. However, there are two problems here. first is I want to find a way to do this append on the front without any inbuilt method.(i.e not using unshift).Second, I am using Uniq here, which is wrong since the original array values may repeat. So how do I still ensure to get the correct output? Can some one give me a better solution to this.
The standard way is:
[1, 2, 3, 4, 5, 7, 8, 9].rotate(-3) #=> [7, 8, 9, 1, 2, 3, 4, 5]
Based on the link I supplied in the comments, I threw this together using the answer to that question.
def test(number, array)
reverse_array(array, 0, array.length - 1)
reverse_array(array, 0, number - 1)
reverse_array(array, number, array.length - 1)
array
end
def reverse_array(array, low, high)
while low < high
array[low], array[high] = array[high], array[low]
low += 1
high -= 1
end
end
and then the tests
array = [1,2,3,4,5,7,8,9]
test(2, array)
#=> [8, 9, 1, 2, 3, 4, 5, 7]
array = [3, 4, 5, 2, 3, 1, 4]
test(2, array)
#=> [1, 4, 3, 4, 5, 2, 3]
Which I believe is what you're wanting, and I feel sufficiently avoids ruby built-ins (no matter what way you look at it, you're going to need to get the value at an index and set a value at an index to do this in place)
I want to find a way to do this append on the front without any inbuilt method
You can decompose an array during assignment:
array = [1, 2, 3, 4, 5, 6, 7, 8]
*remaining, last = array
remaining #=> [1, 2, 3, 4, 5, 6, 7]
last #=> 8
The splat operator (*) gathers any remaining elements. The last element will be assigned to last, the remaining elements (all but the last element) are assigned to remaining (as a new array).
Likewise, you can implicitly create an array during assignment:
array = last, *remaining
#=> [8, 1, 2, 3, 4, 5, 6, 7]
Here, the splat operator unpacks the array, so you don't get [8, [1, 2, 3, 4, 5, 6, 7]]
The above moves the last element to the front. To rotate an array n times this way, use a loop:
array = [1, 2, 3, 4, 5, 6, 7, 8]
n = 3
n.times do
*remaining, last = array
array = last, *remaining
end
array
#=> [6, 7, 8, 1, 2, 3, 4, 5]
Aside from times, no methods were called explicitly.
You could create a new Array with the elements at the correct position thanks to modulo:
array = %w[a b c d e f g h i]
shift = 3
n = array.size
p Array.new(n) { |i| array[(i - shift) % n] }
# ["g", "h", "i", "a", "b", "c", "d", "e", "f"]
Array.new() is a builtin method though ;)
I am trying to find out unique elements and avoid duplicates , between the lists (using list comprehension)
`a = [10 , 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,9]`
`b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]`
dup_list = []
This works , but with duplicates
`final_list = [uniq for uniq in a if a not in dup_list if uniq in b ]`
when i try to append the my dup_list in the last line of the comprehension statement it says invalid syntax
i.e this doesn't work.
"final_list = [uniq for uniq in a if a not in dup_list if uniq in b dup_list.append(uniq) "
I am a newbie in python so apologize for any missed out basic facts .
a = [10, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 9]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
nodupes = sorted([item for item in a if item not in b] +
[item for item in b if item not in a])
That yields [4, 6, 7, 11, 12, 21, 34, 55, 89]
Which is the same answer I get using sets:
seta = set(a)
setb = set(b)
nodupes = sorted(list(seta ^ setb))
a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a2 = [2..4, 8..11, 16..17]
Removing one range of values from an array can be done like this:
[1, 2, 3, 4, 5, 6, 7, 8, 9].slice!(2..5)
Iterating over the ranges and apply the same as above (a2.each { |range| a1.slice!(range) }) isn't perfect though. The ranges overlap sometimes and thus destroy the referencing index for the other ranges.
So, any suggestions on how to remove the ranges in a2 from a1 in the most efficient way?
a1 is normally [*0..10080] long. a2 has about 30 ranges, each containing hundreds of values.
If the result of the first operation impacts the second you're either going to have to track the resulting offset implications, which can get crazy complicated, or simply go about doing the reverse operation and instead flag which you want or don't want using the ranges:
require 'set'
a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a2 = [2..4, 8..11, 16..17]
# Convert the ranges to a set of index values to remove
reject = Set.new(a2.flat_map(&:to_a))
# Using value/index pairs, accumulate those values which are
# not being excluded by their index.
a1.each_with_index.each_with_object([ ]) do |(v, i), a|
a << v unless (reject.include?(i))
end
# => [0, 1, 5, 6, 7, 12, 13, 14, 15, 18, 19, 20]
[-1, *a2.flat_map(&:minmax), a1.length].each_slice(2).flat_map{|i,j| a1[i+1...j]}
# => [0, 1, 5, 6, 7, 12, 13, 14, 15, 18, 19, 20]
I'm not sure this is the least naive solution, but it seems simple to convert your ranges into arrays so you're dealing with like-for-like:
a2.each{ |a| a1 = a1 - a.to_a }
a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a2 = [2..4, 8..11, 16..17]
a1 - a2.flat_map(&:to_a)
Given a sorted array of n integers, like the following:
ary = [3, 5, 6, 9, 14]
I need to calculate the difference between each element and the next element in the array. Using the example above, I would end up with:
[2, 1, 3, 5]
The beginning array may have 0, 1 or many elements in it, and the numbers I'll be handling will be much larger (I'll be using epoch timestamps). I've tried the following:
times = #messages.map{|m| m.created_at.to_i}
left = times[1..times.length-1]
right = times[0..times.length-2]
differences = left.zip(right).map { |x| x[0]-x[1]}
But my solution above is both not optimal, and not ideal. Can anyone give me a hand?
>> ary = [3, 5, 6, 9, 14] #=> [3, 5, 6, 9, 14]
>> ary.each_cons(2).map { |a,b| b-a } #=> [2, 1, 3, 5]
Edit:
Replaced inject with map.
Similar but more concise:
[3, 5, 6, 9, 14].each_cons(2).collect { |a,b| b-a }
An alternative:
a.map.with_index{ |v,i| (a[i+1] || 0) - v }[0..-2]
Does not work in Ruby 1.8 where map requires a block instead of returning an Enumerator.