From a7374d2c675e03f4228e42ce9562a88807189e67 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Sun, 13 Oct 2019 14:08:50 +0200 Subject: [PATCH] Init --- find_algos.rb | 88 +++++++++++++++++++ lol.rb | 217 +++++++++++++++++++++++++++++++++++++++++++++++ sorting_algos.rb | 7 ++ 3 files changed, 312 insertions(+) create mode 100644 find_algos.rb create mode 100644 lol.rb create mode 100644 sorting_algos.rb diff --git a/find_algos.rb b/find_algos.rb new file mode 100644 index 0000000..a65c553 --- /dev/null +++ b/find_algos.rb @@ -0,0 +1,88 @@ +def find_best(container_obj, based_on, compare, type) + " + #{type} result = null; + for (var pls : #{container_obj}) { + if (result == null || pls.get#{based_on}() #{compare} result.get#{based_on}()) { + result = pls; + } + } + + return result; +" +end + +def find_best_with_extra_parameter(container_obj, based_on, compare, type, extra_based_on, extra_compare, extra_param, based_on_type) + + if extra_compare == " > " + result_init = "Integer.MIN_VALUE" + else + result_init = "Integer.MAX_VALUE" + end + + if based_on_type == "int" + helper_variable = "int result#{based_on} = #{result_init};" + else + helper_variable = "String result#{based_on};" + end + + + if extra_compare.length > 4 + # TODO: Check if this case is required + " + #{type} result = null; + #{helper_variable} + for (var pls : #{container_obj}) { + if ((pls.get#{extra_based_on}()#{extra_compare}(#{extra_param}) && pls.get#{based_on}() #{compare} result.get#{based_on}())) { + result = pls; + } + } + + return result; + " + else + " + #{type} result = null; + #{helper_variable} + for (var pls : #{container_obj}) { + if ((pls.get#{extra_based_on}()#{extra_compare}#{extra_param} && pls.get#{based_on}() #{compare} #{result_init}) { + result = pls; + } + } + + return result; + " + end +end + +def find_all(container_obj, based_on, compare, type, parameter) + + if compare.length > 4 + + " + ArrayList<#{type}> result = new ArrayList<>(); + for (var pls : #{container_obj}) { + if (pls.get#{based_on}#{compare}(#{parameter}) { + result.add(pls); + } + } + return result; + " + else + " + ArrayList<#{type}> result = new ArrayList<>(); + for (var pls : #{container_obj}) { + if (pls.get#{based_on}() #{compare} #{parameter}) { + result.add(pls); + } + } + return result; + " + end +end + +def add_method(container_name, name) + " + #{container_name}.add(#{name}); + " +end + diff --git a/lol.rb b/lol.rb new file mode 100644 index 0000000..ba3b22a --- /dev/null +++ b/lol.rb @@ -0,0 +1,217 @@ +require_relative 'find_algos' +require_relative 'sorting_algos' + + +# Hi! Steffan was here... + +def construct_class(name_of_class, parameters, to_string_template) + fields = "" + parameters.each do |x| + fields += "private #{x[:type]} #{x[:name]};" + end + + tmptmp = parameters.map { |x| "#{x[:type]} #{x[:name]}"} + constructor_parameters = tmptmp.join "," + + lel = parameters.map do |x| + "public #{x[:type]} get#{x[:name]}(){ + return #{x[:name]}; + }" + end + + getters = lel.join "\n" + + lol = parameters.map do |x| + "this.#{x[:name]} = #{x[:name]};" + end + + (0...parameters.length).each do |x| + pls = '" + ' + pls += parameters[x][:name] + pls += '+ "' + to_string_template.sub! "#{x}", pls + end + + " + public class #{name_of_class} { + #{fields} + + public #{name_of_class}(#{constructor_parameters}) { + + #{lol.join("\n")} + } + + #{getters} + + public String toString() { + return " + '"' + "#{to_string_template}" + '"' + "; + } + } + " + +end + +def construct_driver(class_name, parameters, methods, container_class, container_params) + + def args(parameters) + tmp = parameters.map do |x| + type = x[:type] + + res_string = "" + if type == "int" + res_string += "#{rand(max=100)}" + else + res_string += '"' + (0...10).map { ('a'..'z').to_a[rand(26)] }.join + '"' + end + res_string + end + tmp.join "," + end + + + + methods_to_print = "" + maggie = methods.map do |x| + + " + System.out.println(lol.#{x[:name]}(#{args x[:parameters]})); + " + end + + driver_string = " + public class TestDriver { + + public TestDriver(){} + + public static void test() { + var tmp1 = new #{class_name}(#{args parameters}); + var tmp2 = new #{class_name}(#{args parameters}); + var tmp3 = new #{class_name}(#{args parameters}); + var tmp4 = new #{class_name}(#{args parameters}); + var tmp5 = new #{class_name}(#{args parameters}); + + System.out.println(tmp1); + System.out.println(tmp2); + System.out.println(tmp3); + System.out.println(tmp4); + System.out.println(tmp5); + + #{container_class} lol = new #{container_class}(#{args container_params}); + + lol.add#{class_name}(tmp1); + lol.add#{class_name}(tmp2); + lol.add#{class_name}(tmp3); + lol.add#{class_name}(tmp4); + lol.add#{class_name}(tmp5); + System.out.println(\"\"); + System.out.println(\"Some checkpoint\"); + #{maggie.join "\n System.out.println(\"Some Checkpoint\"); \n"} + + } + + public static void main(String[] args) { + TestDriver.test(); + } + + } + " +end + +def construct_container(name, parameters, given_fields, methods, arraylist_name) + fields = "" + + parameters.each do |x| + fields += "private #{x[:type]} #{x[:name]};" + end + + given_fields.each do |x| + fields += " private #{x[:type]} #{x[:name]};" + end + + maggie = methods.map do |x| + " + public #{x[:return_type]} #{x[:name]} (#{x[:params].map {|y| "#{y[:type]} #{y[:name]}"}.join ","}) { + #{x[:method_type]} + } + " + end + + construtor_params = parameters.map do |x| + "#{x[:type]} #{x[:name]}" + end + + constructor_handlers = parameters.map do |x| + print(x[:name]) + "this.#{x[:name]} = #{x[:name]};" + end + + container_string = " + import java.util.*; + + public class #{name} { + #{fields} + + public #{name}(#{construtor_params.join ","}) { + #{constructor_handlers.join "\n"} + this.#{arraylist_name} = new ArrayList<>(); + } + + #{maggie.join "\n"} + } + " + + container_string + +end + +name = "Ferry" +file = File.open("#{name}.java", "w") +params = [{:type => "String", :name => "Name"}, {:type => "int", :name => "Length"}, {:type => "int", :name => "width"}] +file.puts(construct_class name, params, "0 1 x 2 meter") +file.close + + +container_methods = [ + { + :return_type => "void", + :name => "addFerry", + :params => [{ + :type => "Ferry", + :name => "f" + }], + :method_type => add_method("ferries", "f") + }, + { + :return_type => "ArrayList", + :name => "smallFerries", + :params => [{ + :type => "int", + :name => "maxLength" + }], + :method_type => find_all("ferries", "Length", " < ", "#{name}", "maxLength") + }, + { + :return_type => "#{name}", + :name => "longFerry", + :params => [{ + :type => "String", + :name => "Name" + }], + :method_type => find_best_with_extra_parameter("ferries", "Length", " > ", "#{name}", "Name", ".equals", "Name", "int") + } +] +container_class = "Harbour" +another_another_file = File.open("#{container_class}.java", "w") +another_another_file.puts(construct_container(container_class, [{:type => "String", :name => "Name"}], [ {:type => "ArrayList<#{name}>", :name => "ferries"}], container_methods, "ferries")) +another_another_file.close + +# TODO: When creating find algorithms, remember to add a space or two (lol for consistency) for <, >, <=, >= + +another_file = File.open("TestDriver.java", "w") +another_file.puts(construct_driver name, params, [{:name => "smallFerries", :parameters => [{:type => "int"}], :print => true}, + {:name => "longFerry", :parameters => [{:type => "String"}], :print => true}], "Harbour", [{:type => "String"}]) +another_file.close + + +`javac *.java` +`java TestDriver` diff --git a/sorting_algos.rb b/sorting_algos.rb new file mode 100644 index 0000000..a408ac4 --- /dev/null +++ b/sorting_algos.rb @@ -0,0 +1,7 @@ +def compare_to_two_vars + +end + +def compare_to_three_vars + +end \ No newline at end of file