In Ruby How to Generate a Long String of Repeated Text

In Ruby how do I generate a long string of repeated text?

str = "0" * 999999

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

Ruby: How to generate strings of variable bits length with only alphanumeric characters?

Use SecureRandom.

First, make sure you require it:

require 'securerandom'

Then you can generate values:

SecureRandom.alphanumeric(10)
=> "hxYolwzk0P"

Change 10 to whatever length you require.

It's worth pointing out that the example you used was returning not alphanumeric but hexadecimal values. If you specifically require hex then you can use:

SecureRandom.hex(10)
=> "470eb1d8daebacd20920"

How to make a repeated string to the left be deleted without using While?

"2229102".sub(/\A(\d)\1*/, "") #=> "9102"`.

The regular expression reads, "match the first digit in the string (\A is the beginning-of-string anchor) in capture group 1 ((\d)), followed by zero or more characters (*) that equal the contents of capture group 1 (\1). String#gsub converts that match to an empty string.

Is there a faster way to generate random string in ruby?

If your target system has /dev/urandom you can read random bytes from it. It should be faster. Here is code with benchmark. Some Ruby libs use this approach to generate random things.

Benchmark.bm do|b|

b.report("Ruby #rand ") do
100000.times do
big_key = rand(36**1024).to_s(36)
end
end

b.report("/dev/urandom") do
File.open("/dev/urandom",File::RDONLY || File::NONBLOCK || File::NOCTTY) do |f|
100000.times do
big_key = f.readpartial(512).unpack("H*")[0]
end
end
end
end

Here you can see benchmark results on 100000, 1024 char long random strings...

       user     system      total        real
Ruby #rand 31.750000 0.080000 31.830000 ( 32.909136)
/dev/urandom 0.810000 5.870000 6.680000 ( 6.974276)

Find the longest substring in a string

This should work:

string = 'aabbccc'
string.chars.chunk {|a| a}.max_by {|_, ary| ary.length}.last.join

Update:

Explanation of |_, ary|: at this point we have array of 2-element arrays. We only need to use the second one and we ignore the first one. If instead we do |char, ary| some IDEs would complain about unused local variable. Placing _ tells ruby to ignore that value.

Using regex:

We can achieve same thing with regex:

string.scan(/([a-z])(\1*)/).map(&:join).max_by(&:length)

How to find the longest repeated sequence of a string

To be precise you are looking for the longest 3-fold substring.

A k-fold substring of a string s is a repeated substring of s that appears at least k times in s

There was a similar python question a few years ago that would give you a lot of good information. Specifically take a look at Finding the Longest Multiple Repeat. There is a solution on GitHub but it is also in Python.



Related Topics



Leave a reply



Submit