Module: LilUtils::AlgoTestUtils::DataSets

Defined in:
lib/lilutils/algo_test_utils/data_sets.rb

Constant Summary collapse

ALPHABET =
('a'..'z').to_a + ('0'..'9').to_a
SIZE =
ALPHABET.length

Class Method Summary collapse

Class Method Details

.ascii_strings(howmany, *rest) ⇒ Object

Returns random strings of equal length . The length of each string is determined by the parameter passed. The characters that can appear in returned strings are defined by the #ALPHABET. each string in returned set. If unspecified, this is calculated from first argument, howmany, such that the length is enough to generate strings from the entire ALPHABET.

Parameters:

  • howmany (Integer)

    How many random strings do you need?

  • rest (Integer)

    Optional arguments. First argument should be the length of

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lilutils/algo_test_utils/data_sets.rb', line 19

def self.ascii_strings(howmany, *rest)
  p howmany, rest if $DEBUG
  raise ArgumentError, "Size must be positive integer" if howmany < 1
  srand(Time.now.to_i)
  strings = []
  of_length = rest[0] ? rest[0] : get_string_length_for(howmany)
  1.upto howmany do
    strings << get_ascii_string(of_length)
  end
  strings
end

.random_enumerator(k, n) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lilutils/algo_test_utils/data_sets.rb', line 51

def self.random_enumerator(k, n)
  k <= n ? (select, remaining = k, n) : (select, remaining = n, k)
  raise ArgumentError, "k, n should be non-negative" if k < 0 or n < 0
  Enumerator.new do |yielder|
    big = 1 << 32 # 4B is a big number?
    0.upto(n-1) do |num|
      if ((rand(big) % remaining) < select)
        yielder.yield num
        select -= 1
      end
      remaining -= 1
    end
  end
end

.select_random_array(k, n) ⇒ Array

Returns an array of k randomly chosen numbers between [0, n) where each number will be chosen with equal probability. Note that returned array will be sorted. The algorithm is taken from Jon Bentley’s excellent Programming Pearls. To make the method to what you mean, both select_random(n, k) and select_random(k, n) do the same thing, i.e. return an array of size k, each element of array is between 0 and n-1.

Parameters:

  • k (Integer)

    the number of numbers to choose. May not be > n

  • n (Integer)

    the upper limit, never included in returned array

Returns:

  • (Array)

    Integer array of size k such that 0 <= Array < n for all 0 <=i <= k

See Also:

  • #random_enumerator


39
40
41
42
43
# File 'lib/lilutils/algo_test_utils/data_sets.rb', line 39

def self.select_random_array(k, n)
  a = []
  random_enumerator(k, n).each {|random_selection| a << random_selection}
  a
end

.select_random_array_to_file(k, n, file) ⇒ Object



45
46
47
48
49
# File 'lib/lilutils/algo_test_utils/data_sets.rb', line 45

def self.select_random_array_to_file(k, n, file)
  File.open(file, "w") do |f|
    random_enumerator(k, n).each {|random_selection| f.puts random_selection}
  end
end