File size: 1,502 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env ruby

=begin

This script traverses all the programs inside the LODA program rootdir.
It looks for all the LODA assembly programs there are.
Each program_id is appended to the CSV file.

This script outputs a `program_ids.csv` file, with this format:

    program id
    4
    5
    7
    8
    10

=end

require 'csv'
require_relative 'config'

LODA_PROGRAMS_OEIS = Config.instance.loda_programs_oeis

output_filename = 'data/program_ids.csv'

def obtain_files(path)
    Dir.chdir(path) do
        names = Dir.glob('*').select { |f| File.file? f }
        return names.sort
    end
end

def obtain_dirs(path)
    Dir.chdir(path) do
        names = Dir.glob('*').select { |f| File.directory? f }
        return names.sort
    end
end

def obtain_nested_names(rootdir)
    names = []
    root_dirs = obtain_dirs(rootdir)
    root_dirs.each do |dirname|
        path = File.join(rootdir, dirname)
        names += obtain_files(path)
    end
    names
end

def integers_from_filenames(filenames)
    integers = []
    filenames.map do |filename|
        if filename =~ /0*(\d+)/
            integers << $1.to_i
        else
            puts "Mismatch for #{filename}"
        end
    end
    integers
end


filenames = obtain_nested_names(LODA_PROGRAMS_OEIS)
program_ids = integers_from_filenames(filenames)

CSV.open(output_filename, "wb", col_sep: ";") do |csv|
    csv << ["program id"]
    program_ids.each_with_index do |program_id, index|
        csv << [program_id.to_s]
    end
end