Related
I'm working on a coding challenge to sort the odd elements of an array while keeping all even elements in their initial position.
I can sort the odd elements in groups between even elements while leaving the evens alone, but I want them to be ordered throughout the array. Here is my code:
def sort_array source_array
return [] if source_array.empty?
a = source_array
n = a.length
loop do
swapped = false
(n - 1).times do |i|
if (a[i] > a[i + 1]) && a[i].odd? && a[i + 1].odd?
a[i], a[i + 1] = a[i + 1], a[i]
swapped = true
end
end
break if not swapped
end
a
end
source_array = [7, 5, 35, 8, 12, 17, 47, 47, 37, 64, 22, 55, 13]
p sort_array(source_array)
# => [5, 7, 35, 8, 12, 17, 37, 47, 47, 64, 22, 13, 55]
You could try this approach:
Create an array with all odds (leaving source_array intact). Sort
this array.
Consider source_array element by element. Is the
element even then leave it . If it is odd, then replace it with the
first element on the sorted odds list. Remove the first element of the
sorted list.
Here are a couple of ways to do that.
arr = [7, 5, 35, 8, 12, 17, 47, 47, 37, 64, 22, 55, 13]
Both begin with the following calculation
odd, even = arr.each_with_index.partition { |n,i| n.odd? }
#=> [[[7, 0], [5, 1], [35, 2], [17, 5], [47, 6], [47, 7], [37, 8], [55, 11], [13, 12]],
# [[8, 3], [12, 4], [64, 9], [22, 10]]]
odd
#=> [[7, 0], [5, 1], [35, 2], [17, 5], [47, 6], [47, 7], [37, 8], [55, 11], [13, 12]]
even
#=> [[8, 3], [12, 4], [64, 9], [22, 10]]
See Enumerable#each_with_index, Enumerable#partition and Integer#odd?.
#1
odd_val, odd_idx = odd.transpose
#=> [[7, 5, 35, 17, 47, 47, 37, 55, 13],
# [0, 1, 2, 5, 6, 7, 8, 11, 12]]
a = even.concat(odd_val.sort.zip(odd_idx))
#=> [[8, 3], [12, 4], [64, 9], [22, 10], [5, 0], [7, 1], [13, 2],
# [17, 5], [35, 6], [37, 7], [47, 8], [47, 11], [55, 12]]
h = a.map(&:reverse).to_h
#=> {3=>8, 4=>12, 9=>64, 10=>22, 0=>5, 1=>7, 2=>13, 5=>17, 6=>35, 7=>37,
# 8=>47, 11=>47, 12=>55}
h.values_at(*0..arr.size-1)
#=> [5, 7, 13, 8, 12, 17, 35, 37, 47, 64, 22, 47, 55]
Note:
odd_val.sort.zip(odd_idx)
#=> [5, 7, 13, 17, 35, 37, 47, 47, 55].zip([0, 1, 2, 5, 6, 7, 8, 11, 12])
#=> [[5, 0], [7, 1], [13, 2], [17, 5], [35, 6], [37, 7], [47, 8], [47, 11], [55, 12]]
See Array#sort, Array#zip and Hash#values_at.
#2
a = odd.map(&:first).sort
#=> [5, 7, 13, 17, 35, 37, 47, 47, 55]
even.each { |n,i| a.insert(i,n) }
a #=> [5, 7, 13, 8, 12, 17, 35, 37, 47, 64, 22, 47, 55]
See Array#insert.
source_array = [7, 5, 35, 8, 12, 17, 47, 47, 37, 64, 22, 55, 13]
odd_sorted = source_array.select(&:odd?).sort
source_array.map {|input| input.even? ? input : odd_sorted.shift }
# [5, 7, 13, 8, 12, 17, 35, 37, 47, 64, 22, 47, 55]
I just have a question,
I have been building alexa skill. (the Concept is Randomize Positivity)
but somehow it keep returning the same sentence....
this is my code...
` for (var j = 0; j < index; j++){
var rand = Math.floor(Math.random() * index);
index -= 1;
var temp = indexList[index];
indexList[index] = indexList[rand];
indexList[rand] = temp;
}`// I also swap the words so It would sure not to return the same but somehow it happens..
and the index is about 15 strings for now...
my Question is how do I make it random as possible?
You're just trying to shuffle an array, right? See the Fisher-Yates shuffle. You're already almost doing the same thing.
I tried this and it worked fine.
var indexList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var rand;
var temp;
for (let i = 0; i < indexList.length; i++) {
rand = Math.floor(Math.random() * (i + 1));
temp = indexList[i];
indexList[i] = indexList[rand];
indexList[rand] = temp;
console.log(indexList);
}
It printed the following, showing that it is shuffling the array more and more each step:
> Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
> Array [2, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
> Array [3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
> Array [4, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
> Array [4, 1, 5, 3, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
> Array [4, 6, 5, 3, 2, 1, 7, 8, 9, 10, 11, 12, 13, 14, 15]
> Array [4, 6, 5, 3, 2, 1, 7, 8, 9, 10, 11, 12, 13, 14, 15]
> Array [4, 6, 5, 3, 2, 8, 7, 1, 9, 10, 11, 12, 13, 14, 15]
> Array [4, 6, 5, 3, 2, 8, 9, 1, 7, 10, 11, 12, 13, 14, 15]
> Array [4, 6, 5, 3, 2, 8, 9, 1, 10, 7, 11, 12, 13, 14, 15]
> Array [4, 6, 5, 3, 2, 8, 9, 1, 10, 7, 11, 12, 13, 14, 15]
> Array [4, 6, 5, 3, 2, 8, 9, 1, 12, 7, 11, 10, 13, 14, 15]
> Array [4, 6, 5, 3, 2, 8, 9, 1, 12, 7, 11, 10, 13, 14, 15]
> Array [4, 6, 14, 3, 2, 8, 9, 1, 12, 7, 11, 10, 13, 5, 15]
> Array [4, 6, 15, 3, 2, 8, 9, 1, 12, 7, 11, 10, 13, 5, 14]
I'm using Ruby 2.4. I'm having trouble creating an array of arrays. I have this
data_cols = [[], []]
lines.each do |line|
parts = [0, *shared_space_indexes, line.size].each_cons(2).map { |a, b| line[a...b].strip }
parts.each_with_index do |part, index|
data_cols[index][data_cols[index] ? data_cols[index].size : 0] = part
end
end
But I get a "NoMethodError: undefined method `[]=' for nil:NilClass" error on the "data_cols[index][data_cols[index] ? data_cols[index].size : 0] = part" line. What I'm trying to do is for each parts array, push each item in "parts" onto its own array correspoding to the index of that element in parts. So, for instance, if the first iteration of the loop has parts equal to
[1, 5, 8, 12]
I would have a data_cols array that looks like
[[1], [5], [8], [12]]
and if the next iteration of the loop had a parts array that looked like
[19, 20, 21, 22]
the data_cols array would then be changed to
[[1, 19], [5, 20], [8, 21], [12, 22]]
It's hard to test without knowing what lines and shared_space_indexes look like.
2 columns → a matrix
zip
For the second part of your question you can use :
[1, 5, 8, 12].zip([19, 20, 21, 22])
#=> [[1, 19], [5, 20], [8, 21], [12, 22]]
transpose
[[1, 5, 8, 12],[19, 20, 21, 22]].transpose
#=> [[1, 19], [5, 20], [8, 21], [12, 22]]
With transpose, both arrays need to have the same size.
3 columns → matrix
array1 = [1, 5, 8, 12]
array2 = [19, 20, 21, 22]
array3 = [99, 88, 77, 66]
p [array1, array2, array3].transpose
#=> [[1, 19, 99], [5, 20, 88], [8, 21, 77], [12, 22, 66]]
columns → rows
If you have an array of arrays :
arrays = [
[1, 5, 8, 12],
[19, 20, 21, 22],
[99, 88, 77, 66]
]
p arrays.transpose
#=> [[1, 19, 99], [5, 20, 88], [8, 21, 77], [12, 22, 66]]
So in your case, I'd just concentrate on creating an array of arrays with the same length, and use transpose at the end.
matrix + column → matrix
As you noticed, if you have
data_cols = [[1, 19], [5, 20], [8, 21], [12, 22]]
arr = [99, 88, 77, 66]
data_cols.zip(arr) would produce a weird half-nested array :
[[[1, 19], 99], [[5, 20], 88], [[8, 21], 77], [[12, 22], 66]]
You can use :
p data_cols.zip(arr).map(&:flatten)
#=> [[1, 19, 99], [5, 20, 88], [8, 21, 77], [12, 22, 66]]
I have an array of integers like this:
[1, 1, 1, 2, 2, 4, 4, 5, 6, 11, 11, 12, 15, 22, 23, 23, 23, 31, 32, 32]
I am trying to convert this to a hash, grouping according to ranges at intervals of 10....
So, in this case it would be
{ [1..10] => 9, [11..20] => 4, [21..30] => 4, [31..40] => 3 }
I have tried a few things which haven't come close so it's a bit pointless putting them down here. I can convert the array to ranges
[1, 1, 1, 2, 2, 4, 4, 5, 6, 11, 11, 12, 15, 22, 23, 23, 23, 31, 32, 32].sort.uniq.inject([]) do |spans, n|
if spans.empty? || spans.last.last != n - 1
spans + [n..n]
else
spans[0..-2] + [spans.last.first..n]
end
end
But this is not what I am looking for. Any suggestions?
Hash[
your_array.group_by{|i| i / 10}.map{|k,v|
[(k*10+1..(k+1)*10), v.count]
}
]
#=> {1..10=>9, 11..20=>4, 21..30=>4, 31..40=>3}
arr.each_with_object(Hash.new(0)) do |e, hash|
i = e / 10
hash[i*10+1..i*10+10] += 1
end
#⇒ {
# 1..10 => 9,
# 11..20 => 4,
# 21..30 => 4,
# 31..40 => 3
# }
I've modified the example to have no numbers between 21 and 30, so that the hash should include the key-value pair 21..30=>0.
arr = [1, 1, 1, 2, 2, 4, 4, 5, 6, 11, 11, 12, 20, 32, 33, 33, 33, 41, 42, 42]
intervals = (1..arr.last-1).step(10).each_with_object({}) { |n,h| h[n..n+9] = 0 }
#=> {1..10=>0, 11..20=>0, 21..30=>0, 31..40=>0, 41..50=>0}
arr.each_with_object(intervals) do |n,intervals|
interval_end = 10*((n+9)/10)
intervals[interval_end-9..interval_end] += 1
end
#=> {1..10=>9, 11..20=>4, 21..30=>0, 31..40=>4, 41..50=>3}
I have this array:
Global Const $array[70][8] = [[1, ...], [2, ...], [3, ...], ... , [70, 0.48124164, 0.88451159, -8855, -612, 176, 1, 7]]
If i run the code, it says "Error: Missing right bracked ')' in expression". If i remove the last element it works. I can't get why the last element is producing this error, and i can't find any syntax error.
I also can compile or build it without any error.
Has anyone an idea what can cause this behaviour?
//EDIT
Here is the full array definition:
Global Const $array[70][8] = [[1, 0.46382308, 0.37846267223358, 3476, 20, 4, -1, 4], [2, 0.66173166, 0.35245704650879, 4268, -7333, 7, 3, 4], [3, 0.43819791, 0.73096454143524, -5063, 891, 158, 2, 4], [4, 0.49637758, 0.27708977460861, 6028, -1121, 381, 1, 4], [5, 0.60390532, 0.37993001937866, 3503, -5119, 83, 2, 4], [6, 0.43493705, 0.50274324417114, 415, 1054, 130, 3, 4], [7, 0.5436604, 0.88573443889618, -9015, -2928, 52, 1, 3], [8, 0.51811683, 0.60228180885315, -1968, -2021, 95, 1, 3], [9, 0.55463874, 0.84207928180695, -7663, -3285, 70, 1, 3], [10, 0.48325252, 0.79658991098404, -6724, -746, -272, 1, 3], [11, 0.5183885, 0.8305846452713, -7633, -1877, -272, -1, 3], [12, 0.55923116, 0.68494528532028, -3946, -3505, 36, 2, 3], [13, 0.48257315, 0.8402858376503, -7837, -644, -262, 2, 3], [14, 0.55860614, 0.70989108085632, -4713, -3447, 36, 2, 3], [15, 0.42719244, 0.61063778400421, -2193, 1338, 70, 2, 3], [16, 0.41371411, 0.57615393400192, -1314, 1882, 48, 2, 3], [17, 0.50219285, 0.79606002569199, -6684, -1330, -272, 2, 3], [18, 0.47042638, 0.43658798933029, 2099, -196, 97, 2, 4], [19, 0.42469245, 0.41934603452682, 2529, 1427, 265, 3, 4], [20, 0.43958377, 0.52923792600632, -262, 827, 92, 2, 4], [21, 0.49751889, 0.78456538915634, -6454, -1299, -268, 2, 3], [22, 0.48034489, 0.50571876764297, 363, -592, 21, 4, 3], [23, 0.39637708, 0.53408849239349, -372, 2397, 39, 3, 4], [24, 0.39023572, 0.64112710952759, -2920, 2726, 75, 2, 4], [25, 0.41588801, 0.64752662181854, -3116, 1715, 51, 2, 1], [26, 0.45412194, 0.81338351964951, -7201, 402, 24, 3, 4], [27, 0.50868737, 0.36688649654388, 3793, -1674, 248, 2, 4], [28, 0.5741769, 0.25597554445267, 6584, -4035, 661, 4, 4], [29, 0.52428531, 0.59066486358643, -1700, -2216, 97, 2, 4], [30, 0.43132287, 0.72403514385223, -5038, 1220, 51, 1, 4], [31, 0.51977443, 0.34100317955017, 4459, -2070, 1206, 2, 4], [32, 0.4120565, 0.59812414646149, -1975, 1930, 62, 3, 4], [33, 0.59803569, 0.29261976480484, 5669, -4950, 805, 3, 4], [34, 0.4343664, 0.67442893981934, -3846, 1152, 154, 5, 4], [35, 0.46480131, 0.91842484474182, -9804, -73, 67, 3, 7], [36, 0.56852471, 0.84611463546753, -7977, -3894, 9, 1, 8], [37, 0.47779053, 0.32180470228195, 4813, -401, 349, 2, 4], [38, 0.52241027, 0.30594861507416, 5344, -2175, 1278, 2, 4], [39, 0.52458423, 0.6877578496933, -4022, -2207, 94, 3, 1], [40, 0.55643224, 0.88345181941986, -8904, -3372, 16, -1, 8], [41, 0.43931204, 0.35995709896088, 3888, 772, 5, 3, 4], [42, 0.54311692, 0.80111443996429, -6882, -2920, 22, -1, 8], [43, 0.42798048, 0.70581495761871, -4682, 1328, 94, -1, 4], [44, 0.3859694, 0.54077327251434, -486, 2956, 63, -1, 4], [45, 0.47751879, 0.33729392290115, 4491, -594, 283, -1, 4], [46, 0.43974685, 0.33378851413727, 4621, 945, 53, 1, 4], [47, 0.51083415, 0.31422311067581, 5179, -1736, 1338, 3, 4], [48, 0.45425778, 0.89918559789658, -9241, 309, 262, 3, 7], [49, 0.36626821, 0.71910309791565, -4933, 3595, 10, 3, 4], [50, 0.550481, 0.33325856924057, 4563, -2943, 1062, 4, 4], [51, 0.45588821, 0.8790088891983, -8811, 306.7, 346.9, 3, 7], [52, 0.54849743, 0.403449177742, 2900, -3145, 177, 3, 4], [53, 0.59265524, 0.30664157867432, 5234, -4822, 691, 3, 4], [54, 0.42800766, 0.74604618549347, -5443, 1265, 20, 3, 4], [55, 0.4743666, 0.874076783, -8673, -364, 191, 1, 7], [56, 0.45151323, 0.957066357, -10774, 383, 25, 3, 7], [57, 0.47893184, 0.984906196, -11285, -681, 121, 4, 7], [58, 0.50632327, 0.886182785, -8951, -1578, 94, 3, 7], [59, 0.52273643, 0.937378764, -10108, -2216, 49, 4, 7], [60, 0.52306246, 0.924375951, -9920, -2244, 27, 4, 7], [61, 0.54613327, 0.291233837, 5638, -3031, 1559, 1, 4], [62, 0.54523652, 0.89673995, -9217, -3016, 20, -1, 8], [63, 0.50667655, 0.897432863, -9184, -1735, 73, 2, 7], [64, 0.53928542, 0.9322021, -10176, -2685, 5, 2, 7], [65, 0.5081439, 0.97875124, -11280, -1734, 0, 1, 7], [66, 0.45591539, 0.87778604, -8811, 315, 348, 4, 7], [67, 0.53086143, 0.92669934, -9956, -2405, 37, 2, 7], [68, 0.49053514, 0.938356995, -10244, -1009, 47, 1, 7], [69, 0.43232834, 0.950096189, -10584, 1205, 31, 1, 7], [70, 0.48124164, 0.88451159, -8855, -612, 176, 1, 7]]
The code around looks like
EndFunc
Global Const first[54][8] = ...
Global Const $second[29][8] = ...
Global Const $array[70][8] = [...] // error array
Global Const $last[30][8] = ...
Func function($first)
Look like a fat finger:
Local $arr[3][3] = [[1, 2, 3], [2, 3, 4], [3, (.00, 0.1575818700]]
==> Missing right bracket ')' in expression.: