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:
input: 15
output:
15 = 1+2+3+4+5
15 = 4+5+6
15 = 7+8
input: 4
output: cannot split
Here's one way:
def natural_numerals(value)
results = []
(1..value-1).each {|i| (i..value-1).each {|j| results << (i..j).to_a.join("+") if (i+j)*(j-i+1)/2 == value}}
if results.empty? then nil else results end
end
output1 = natural_numerals(15)
output2 = natural_numerals(4)
puts output1.inspect #=> ["1+2+3+4+5", "4+5+6", "7+8"]
puts output2.inspect #=> nil
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to write a Fibonacci sequence program in ruby without using the recursion.
can you please help me to write this function.
def fibonacci(n)
final = []
(0..n).each_with_index do |i, _|
next final << i if i.zero? || i == 1
final << final[i - 1] + final[i - 2]
end
final
end
puts fibonacci(10)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am new to ruby and trying out some examples.I have the array below that contains a hashmap.
f = [{"qty"=>"5", "unit"=>"kgs", "item"=>"sugar", "cost"=>"400", "salestax"=>"0.0"}]
I want to print out some thing like this
5 kgs of sugar : 400 at a tax of 0.0(if you notice the content is from the hashset)
I've tried some thing like:
f.each
{
|m| puts m for u in m |qty,unit,item,cost,salestax| puts "#{qty} #{unit} of #{item} : #{cost} #{salestax}"
}
but its not giving me what I want.
f.each do |hash|
puts "#{hash['qty']} #{hash['unit']} of #{hash['item']}: #{hash['cost']} at a tax of #{hash['salestax']}."
end
Seems like that is what you want.
Another way
f.each { |hash| puts "%s %s of %s : %s at a tax of %s" % hash.values }
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 have a 309 digit integer, I want to iterate through its characters.
Currently I am using:
require 'openssl'
e = 116505013962726356794269846667188147473899121100449069443844506823885859211073843523906823741034558875724969276233769835502344452366515593952571468651971447660633083078837371793388842846199643249996094940742465135064478448126948741186882484457847959126808512823416166517945252986434515406363102297514031583117
and I have:
e.times do |i|
...
end
Which, understandably, yields an error:
undefined method `times' for #<OpenSSL::BN:0x007fec05002140>
I attempted to convert the bignum to an integer:
e.to_i.times do |i|
...
end
Which returned:
bignum too big to convert into `long'
I understand why I am receiving these errors, but I am asking how do I iterate through each character of such a large number?
How is this ?
e = 116505013962726356794269846667188147473899121100449069443844506823885859211073843523906823741034558875724969276233769835502344452366515593952571468651971447660633083078837371793388842846199643249996094940742465135064478448126948741186882484457847959126808512823416166517945252986434515406363102297514031583117
e.to_s.each_char do |c|
# code
end
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
Here is my code:
class String
def frequency
chars.each_with_object(Hash.new(0)) do |char, h|
h["#{char.upcase}:"] += 1 if char[/[[:alpha:]]/]
end
end
end
I've tried breaking it down in smaller bit's of code, such as using a .times do loop but I couldn't figure it out
for example:
str = "\*"
h["A:"] = count('a').times do
str
end
Are you trying to do something like:
counts = 'aassssvvvvv'.frequency
counts.each{|key,count| puts key + '*'*count}
# A:**
# S:****
# V:*****
Or if you want to change the key you can do:
counts.each{|key,amount| counts[key] = '*'*amount}
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
Say that I have three arrays arr1, arr1 and arr3.
arr1 = ["apple", "book", "car", "dog"]
arr2 = ["apple", "book"]
arr3 = ["app", "boo"]
How can I check if arr1 includes: arr2, and arr3 with like wildcards.
You could use Enumerable#grep:
arr3.all? { |item| arr2.grep(/#{item}/)[0] }
#=> true
This matches substrings (e.g. ook), to match only prefixes use /^#{item}/.