Generate letters to represent number using ruby?
class Numeric
Alph = ("a".."z").to_a
def alph
s, q = "", self
(q, r = (q - 1).divmod(26)); s.prepend(Alph[r]) until q.zero?
s
end
end
3.alph
# => "c"
26.alph
# => "z"
27.alph
# => "aa"
4123.alph
# => "fbo"
Generate array of all letters and digits
[*('a'..'z'), *('0'..'9')] # doesn't work in Ruby 1.8
or
('a'..'z').to_a + ('0'..'9').to_a
or
(0...36).map{ |i| i.to_s 36 }
(the Integer#to_s
method converts a number to a string representing it in a desired numeral system)
Assign number to alphabet in array
Here's one way:
arr.map {|ch| ch.ord - 'A'.ord + 10}
How can I generate random mixed letters and numbers in Ruby
How about this:
def random_tuple(length)
letters_and_numbers = "abcdefghijklmnopqrstuvwxyz0123456789"
answer = ""
length.times { |i| answer << letters_and_numbers[rand(36)] }
answer
end
output = ""
500.times { |i| output << random_tuple(8) + "\n" }
You could also have the function append the newline, but I think this way is more general.
Generate Unique Random String With Letters And Numbers In Lower Case
If you are using ruby 1.9.2 you can use SecureRandom:
irb(main):001:0> require 'securerandom'
=> true
irb(main):002:0> SecureRandom.hex(13)
=> "5bbf194bcf8740ae8c9ce49e97"
irb(main):003:0> SecureRandom.hex(15)
=> "d2413503a9618bacfdb1745eafdb0f"
irb(main):004:0> SecureRandom.hex(32)
=> "432e0a359bbf3669e6da610d57ea5d0cd9e2fceb93e7f7989305d89e31073690"
How to generate a random string in Ruby
(0...8).map { (65 + rand(26)).chr }.join
I spend too much time golfing.
(0...50).map { ('a'..'z').to_a[rand(26)] }.join
And a last one that's even more confusing, but more flexible and wastes fewer cycles:
o = [('a'..'z'), ('A'..'Z')].map(&:to_a).flatten
string = (0...50).map { o[rand(o.length)] }.join
If you want to generate some random text then use the following:
50.times.map { (0...(rand(10))).map { ('a'..'z').to_a[rand(26)] }.join }.join(" ")
this code generates 50 random word string with words length less than 10 characters and then join with space
function that returns the number of letters that repeat in a string
Here is an orthodox way to do it:
'sldhelanlaskjkajksda'.each_char.group_by(&:itself).count{|_, v| v.length > 1}
# => 6
The reason your code does not work is because, (i) once the
i2
loop terminates, you increment i1
, and try for another i2
loop in the next i1
iteration, but since i2
hasn't been touched after failed to satisfy the loop condition, it will not satisfy the condition again, and the i2
loop will never run again, and (ii) you are initializing i2
to a constant.To fix it, initialize i2
within i1
loop at the beginning, and initialize it to i2 = i1 + 1
, not 1
.
ruby - How can I generate an array of every combination of letters and numbers of a given length?
alphanum = [*?a..?z, *?0..?9]
length.times.flat_map { |l|
alphanum.repeated_permutation(l + 1).map(&:join)
}
Note that length > 3
will give you a lot of results.
EDIT: As meagar says, this is very memory-intensive. An Enumerator-based answer (not as pretty, but won't kill your memory):
e = Enumerator.new do |y|
length.times do |l|
alphanum.repeated_permutation(l + 1).each do |p|
y << p.join
end
end
end
Related Topics
Vagrant/Virtualbox Vm Provisioning: Rbenv Installs Successfully But Subsequent Uses in Script Fail
Stream Console Output Through Http (With Ruby)
Why Is Rspec So Slow Under Rails
Is It Right to Assign Multiple Variables Like This a = B = C = D = 5
How to Write Negative Loop in Ruby Like For(I=Index; I >= 0; I --)
How to Check If a Param Is True or False
Rails Model Has_Many , Belongs_To Relations
Find Most Common String in an Array
Generate Letters to Represent Number Using Ruby
Rails 4.0.1 on Heroku, Can't Create Database
Bcrypt Error: Devise Ruby 2.0 and Rails 4.0
What's the Best Way to Deploy a Jruby on Rails Application to Tomcat
Passing Error Messages Through Flash
Installed Rails But the Rails Command Says It's Not Installed