Array if size over 2 set array to [] [closed] - ruby

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
#ad = [2, 5, 5]
if #ad.size < 2
#ad = []
end
#ad # => [2, 5, 5]
Why is #ad not []? I want to store the last two records found in an array and if they are the same, it should find a new record.

I guess you got the > wrong.
Don’t worry, that happens to the best.
There is an even more terse way of writing your code, using a trailing if.
#ad = [2, 5, 5]
#ad = [] if #ad.size > 2
#ad # => []

Why is #ad not []?
Why would it be? Your code is extremely basic, and it says "if #ad has less than two items, set it to an empty array". As you clearly show, #ad doesn't have less than two items, it has three items. Does three seem less than two to you?
If you're tying to find the number of unique items, you need ot use .uniq, but it still won't be less than two, it will be exactly two, and so you still won't enter the if condition.

Related

How do I do a simple array to hash conversion? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have an array [1,2,3] and want to create hash from it, so the result is {"kangaroo"=>1, "moose"=>2, "mouse"=>3}. What's the best way to do it?
What about:
Hash[%w(kangaroo moose mouse).zip [1,2,3]]
# => {"kangaroo"=>1, "moose"=>2, "mouse"=>3}
Explanation
Array#zip combines both arrays element-wise:
%w(kangaroo moose mouse).zip [1,2,3]
# => [["kangaroo", 1], ["moose", 2], ["mouse", 3]]
Hash::[] creates a Hash from this array.

Convert a set of unique numbers to another set of unique numbers within range (0 to 25) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Lets say I have a set of unique numbers [5, 123, 49176, 30982, 542] now I want to convert the numbers and get another set of unique numbers (integers) which will be in range of 0 to 25
Can any one suggest any algorithm that can help me solve this problem ?
You are going to normalize them?
raw = [5, 123, 49176, 30982, 542]
normalization = [(i-min(raw))/(max(raw)-min(raw))*25 for i in raw]
print(normalization)
It will give you: (all elements are guaranteed to be unique)
[0.0, 0.059994712330438675, 25.0, 15.749628846271177, 0.2730267840800472]
If the elements of result are limited to be integer:
import random
raw = [5, 1, 23, 49176, 30982, 542]
if len(raw) > 26: #there's no way to get more than 26 unique elements from 0~25 integers
print("mission impossible")
else:
print(random.sample(range(26), len(raw)))
It will give you (for example):
[23, 4, 13, 10, 18, 24]
Those are two compromising way for you. But according to you comment, you actually need a injective function whose domain are all integers and codomain are integers in [0,25]. Unfortunately, it's a mission impossible.
(Assuming integers)
You can sort the elements, and give each element its index (in the sorted list). Assuming you have less then (or exactly) 26 numbers, you will get the desired range. You can also avoid sorting if you can allow not deterministic result for the same set by just setting the numbers as the current order (iteration order, for example) of the set.
Note that if you want some general case hashing, it cannot be done - if your range is greater then 26 - you will have duplicates. This is coming directly from Pigeonhole principle, where the range is the pigeons, and the range [0,25] is the Pigeonholes.
Use each number as a seed for a random function, and then go through and make sure you don't have any duplicates.
This will also break down if you have a set larger than 25, like Amit said.

I need to reduce with average [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
a = [[24, 18.0], [24, 16.0], [25, 15.0]]
I need b = [[24, 17.0], [25, 15.0]]
The are 2 rules I need calculate average value of second item in sub-array i.e. [24, 18.0], [24, 16.0] #=> [24, 17.0] - this I can do separately myself, but I can't understand how to combine reduce with average like "ruby way" solutions in a.collect{...}
You can use group_by to group common keys, and sum/size to get the average:
b = a.group_by(&:first).map do |k,v|
[k, v.map(&:last).inject(:+) / v.size]
end

How to create a multidimensional array from a one dimensional array in Ruby? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Suppose I have an array:
a=['hello','shivam','how','are','you']
... and I want to make it into a multidimensional array like this below:
[['hello','shivam'],'how',['are','you']]
How do I do this?
def transform ar
[ [ar[0], ar[1]], ar[2], [ar[3], ar[4]] ]
end
this does exactly what you want to do, i cant do more if you dont share the plattern you want it to order...
You're probably gonna need to look into using the zip method:
a = [1,2,3]
b = [4,5,6]
a.zip(b)
=> [[1, 4], [2, 5], [3, 6]]
Although this won't help you all the way, as you clearly have some singular array entries in there too.

How to find an odd number in an array? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
In an array, there is exactly one element which repeats odd number of times and rest all other elements repeat even number of the times. Find the element optimally
xor all the elements together.
Example:
In [3]: reduce( lambda x,y : x^y, [1, 2, 2, 1, 4, 5, 5] )
Out[3]: 4

Resources