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
Related
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 4 years ago.
Improve this question
I have some strange thing with downcase and upcase my string in array. Share my code:
I suspect the issue is that you do not have correctly encoded strings.
foo = ['МеНше', '4.5']
foo.map(&:downcase) #=> ["менше", "4.5"]
foo.each { |el| puts el.downcase }
#>> менше
#>> 4.5
foo.first.encoding #=> #<Encoding:UTF-8>
The first step would be to check your encoding. If it's not UTF-8, you can coerce a downcase by doing:
foo.each { |el| puts el.mb_chars.downcase.to_s }
#>> менше
#>> 4.5
This solution requires Rails, so you'd need to do
require 'active_support/core_ext'
If you're using plain old ruby.
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 5 years ago.
Improve this question
Below there is a code that works:
class LikeAnArray < Array
end
If I run:
$ p object = LikeAnArray.new
It returns:
$ #=> []
If I run:
$ p object.class
It returns:
$ #=> LikeAnArray
How can I achieve this functionality without making my class be a sub class of Array?
With class LikeArray < Array; end you can achieve the behaviour you are looking for. What are you trying to do?
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
I have array of arrays looking something like this :
arr = [[f,f,f,f,f], [f,f,t,f,f], [f,t,f,t,f]]
and am I outputing it formatted on the console like this:
arr.each {|a| puts a.join.gsub('t','<b></b>').gsub('f','<i></i>')}
and it generates something like this:
<i></i><i></i><i></i><i></i><i></i>
<i></i><i></i><b></b><i></i><i></i>
<i></i><b></b><i></i><b></b><i></i>
but it is only in the output. I am wondering how I can assign it to a string? With the new lines and everything, exactly the way it looks,
a= [["f","f","f","f","f"], ["f","f","t","f","f"], ["f","t","f","t","f"]].map do |arr|
arr.join.gsub(/[ft]/) do |x|
if x =~ /f/
'<i></i>'
elsif x =~ /t/
'<b></b>'
end
end
end.join("\n")
puts a
# >> <i></i><i></i><i></i><i></i><i></i>
# >> <i></i><i></i><b></b><i></i><i></i>
# >> <i></i><b></b><i></i><b></b><i></i>
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
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