Coder Social home page Coder Social logo

quester-arihant's Introduction

quester

Aim

Create a software that automatically solves a question with dynamic values with a piece of code.

Example

Consider the following questions and their answers:

  • Is it possible to add the following units: 1 kilometer and 1 mile? Yes.
  • Is it possible to add the following units: 1 kilometer and 1 kilogram? No.

The solution to these questions is fairly simple. If the dimensions of the units are the same, they can be added. Else, they cannot be added.

If we had to write the solution as a solver code, it would simply be:

unit_1.dimension == unit_2.dimension ?Yes’ : ‘No

Input YAML

The input to the system is given as a yaml file.

text: >
  Is it possible to add the following units: 1 kilometer and 1 mile?
variables:
  - unit_1
  - unit_2
  - "yes_or_no : String"
test_cases:
  - 
    -
      name: unit_1
      value: Kilometer
    -
      name: unit_2
      value: Mile
    -
      name: yes_or_no
      value: "\"yes\""

  - 
    -
      name: unit_1
      value: Kilometer
    -
      name: unit_2
      value: Kilogram
    -
      name: yes_or_no
      value: "\"no\""

solvers:
  - 
    id: 1
    variable: yes_or_no
    condition: eq
    code: >
      #
          unit_1.dimension == unit_2.dimension ? ‘Yes’ : ‘No’
source: >
  This question was created by Fazil Basheer for demo purpose.

Output Crystal Code

By running crystal quester/generator.cr, the application will generate the following files based on the above YAML.

Scenario File

require "../../ilm/**"

class Scenario
  property unit_1 : Unit
  property unit_2 : Unit
  property yes_or_no : String
  
  def initialize(
    @unit_1,
    @unit_2,
    @yes_or_no,
    
  );end

  def test
    it "should pass" do
      solver = Solver.new(
        unit_1: unit_1,
        unit_2: unit_2,
        
      )

      answer = solver.solve

      answer.should eq(yes_or_no)
    end
  end
end

Solver File

class Solver
  property unit_1 : Unit
  property unit_2 : Unit
  
  def initialize(
    @unit_1,
    @unit_2,
        
  );end

  def solve
    #
    unit_1.dimension == unit_2.dimension ?Yes’ : ‘Noend
end

Spec File

require "spec"

require "./scenario.cr"
require "./solver.cr"

describe Solver do
  settings = [

    {
      unit_1: Unit::KILOMETER,
      unit_2: Unit::MILE,
      yes_or_no: "yes",
      
    },
    
    {
      unit_1: Unit::KILOMETER,
      unit_2: Unit::KILOGRAM,
      yes_or_no: "no",
      
    },
    
  ]

  settings.each do |setting|
    Scenario.new(
      unit_1: setting[:unit_1],
      unit_2: setting[:unit_2],
      yes_or_no: setting[:yes_or_no],
      
    ).test
  end
end

Ilm

Ilm is a crystal library that encapsulates the concepts of science. To make the above question work, we must add the unit 'Mile' to Ilm.

inside the class definition of Unit at ilm/unit.cr, add the following snipped in the appropriate location.

class Unit
  ...
  MILE = new(symbol: 'm', dimension: Dimension::L)
  ...
end

Spec

By running crystal spec/questions/:id, the crystal code that was generated will be executed and the tests will pass if everything is fine.

quester-arihant's People

Contributors

frayzil avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.