prime number test in Ruby [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array a = [1,2,3,4,5]. I want to test which of the numbers are prime and wanted to produce the output {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}.
Any one liner will be appreciated.

I posted this as a comment.
require 'prime'
a = (1..5).to_a
Hash[a.map{ |x| [x, x.prime?] }]
=> {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}

The below will work for you,using Prime#prime?:
require 'prime'
a = [1,2,3,4,5]
Hash[a.zip(a.map(&Prime.method(:prime?)))]
# => {1=>false, 2=>true, 3=>true, 4=>false, 5=>true}

An alternate solution:
a = [1,2,3,4,5]
results = {}
a.each {|i| results[i] = (1..i).map{|x| i/x.to_f % 1 == 0}.count(true) == 2}
puts results.inspect #=> {1 => false, 2 => true, 3 => true, 4 => false, 5 => true}

Related

Search an attribute for a substring [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 7 years ago.
Improve this question
I am trying to remove all whitespaces from an object's attribute that contains a given substring. For example, I have an object event and attributes: 4IP2, 3IP5, 2IP1. I would like to do the following:
event[4IP2].gsub(/\s+/, '')
in a generic manner, i.e.,
event[*IP*].gsub(/\s+/, '')
which should work for all attributes 4IP2, 3IP5, 2IP1. Appreciate any help.
Assuming, that event is a hash, here you go:
▶ event = { '4IP2' => 'a b c', '3GG5' => 'ffff f', '2IP1' => 'ggg ' }
▶ event.map { |k, v| [k, /IP/ =~ k ? v.delete(' ') : v] }.to_h
#⇒ { "2IP1" => "ggg", "3GG5" => "ffff f", "4IP2" => "abc" }
If you want to replace the attributes in-place:
event.each {|k,v| v.gsub!(/\s+/, '') if /IP/ =~ k}
Otherwise, to create a copy:
Hash[event.map {|k,v| [k, /IP/ =~ k ? v.gsub(/\s+/, '') : v]}]

How to iterate through this array within an array to see if all values are equal? [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
Here is my sample code
:key1 => "a"
:key2 => "b"
:key3 => "c"
array1 = [[:key1, :key1, :key1],[:key1, :key2, :key3],[:key2, :key2, :key1]]
array1.each { |x| if x.sym_tos == "a"
puts "All match!"
else
puts "no match"
end
}
Yet when I run it, I get the following error code:
undefined method `sym_to_s' for [:R1C1, :R1C2, :R1C3]:Array (NoMethodError)
You probably wanted to say
if x.uniq.length == 1

How to collect different data from array or hash by ruby language? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example,I have datas:
a,b,c,a,c,d
I want to get different datas: a,b,c,d
I want to get data count: a => 2,b => 1,c => 2,d => 1
There may have other datas,such as e,f,g,etc.
How to do?
a = [:a,:b,:c,:a,:c,:d]
p h = a.each_with_object(Hash.new(0)){|i,h| h[i] += 1}
p h.keys
# >> {:a=>2, :b=>1, :c=>2, :d=>1}
# >> [:a, :b, :c, :d]

extracting non-empty value 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 8 years ago.
Improve this question
pry(main)> s = {:a =>2, :d=>'foo', :x => ' ', :n => true, :z => nil}
=> {:a=>2, :d=>"foo"}
pry(main)> s.each do |k,v| p k unless v.empty? end
NoMethodError: undefined method `length' for 2:Fixnum
I understand it happens because fixnum does not have empty methods. Then how to solve this problem in a slick way, no nasty finding data type first and then check it? I want to print those k where v has some value. Yes true is considered a value, but not bunch of spaces. For me "have value" means non-empty characters and boolean true.
With your updated comments, I think that is what you want.
s = {:a =>2, :d=>'foo', :x => ' ', :n => true, :z => nil}
s.each { |k,v| p(k) if !!v && !v.to_s.strip.empty? }
# :n
# :d
# :a
Quick solution:
s.each {|k,v| p k unless v.to_s.empty?}

Renaming an array created by Array.new [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
When creating a new array in IRB, I can use Array.new to get an empty, unassigned array.
Is there a way to reassign that new array? Can I turn, [] into a variable named my_new_array?
I know I can do this:
my_new_array = Array.new
Or I can do:
my_other_new_array = []
But what about reassigning Array.new?
I'm new to Ruby and I'm curious about this little nuance.
In IRB, the underscore _ method will give you the results of the last expression:
Array.new
# => []
my_new_array = _
# => []
Array.new(2, "foo")
# => ["foo", "foo"]
my_new_array = _
# => ["foo", "foo"]

Resources