Skip to content

Instantly share code, notes, and snippets.

@Kukunin
Last active April 9, 2020 10:49
Show Gist options
  • Save Kukunin/cc9c3efe0a9e95f7df28f90e57c1008d to your computer and use it in GitHub Desktop.
Save Kukunin/cc9c3efe0a9e95f7df28f90e57c1008d to your computer and use it in GitHub Desktop.
A test to create a tree of elements
require 'rails_helper'
RSpec.describe 'hierarchy' do
subject(:hierarchy) { create_hierarchy(per_level: 3, levels: 5) }
def create_element(type, children)
{ type: type, children: children }
end
# Take a pen and piece of paper
# Implement here
def create_hierarchy(per_level:, levels:)
return [create_element('category', [])]
end
# End your implementation here
def child(node)
node[:children].first
end
let(:parent) { hierarchy.first }
let(:child_1) { child(parent) }
let(:child_2) { child(child_1) }
let(:child_3) { child(child_2) }
let(:child_4) { child(child_3) }
it { expect(hierarchy).to have_attributes(size: 3) }
it { expect(parent).to include(children: have_attributes(size: 3)) }
it { expect(parent).to include(type: 'category') }
it { expect(child_1).to include(children: have_attributes(size: 3)) }
it { expect(child_1).to include(type: 'category') }
it { expect(child_2).to include(children: have_attributes(size: 3)) }
it { expect(child_2).to include(type: 'topic') }
it { expect(child_3).to include(children: have_attributes(size: 3)) }
it { expect(child_3).to include(type: 'topic') }
it { expect(child_4).to include(children: have_attributes(size: 0)) }
it { expect(child_4).to include(type: 'topic') }
end
@Kukunin
Copy link
Author

Kukunin commented Apr 7, 2020

The goal of this task is to implement the create_hierarchy function, which returns an array that has per_level elements. Each of that contains own list of elements as children. The amount of levels is defined by levels argument.

ADDITIONAL REQUIREMENT: elements in the first two levels should have type category while elements since the third level should have type topic.

You can copy this spec inside any Rails project with RSpec and run it as a regular test. There are no other dependencies other than RSpec.

Example of the output for per_level:3, levels: 5:

[{:type=>"category",
  :children=>
   [{:type=>"category",
     :children=>
      [{:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]}]},
    {:type=>"category",
     :children=>
      [{:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]}]},
    {:type=>"category",
     :children=>
      [{:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]}]}]},
 {:type=>"category",
  :children=>
   [{:type=>"category",
     :children=>
      [{:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]}]},
    {:type=>"category",
     :children=>
      [{:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]}]},
    {:type=>"category",
     :children=>
      [{:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]}]}]},
 {:type=>"category",
  :children=>
   [{:type=>"category",
     :children=>
      [{:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]}]},
    {:type=>"category",
     :children=>
      [{:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]}]},
    {:type=>"category",
     :children=>
      [{:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]},
       {:type=>"topic",
        :children=>
         [{:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]},
          {:type=>"topic",
           :children=>
            [{:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]},
             {:type=>"topic", :children=>[]}]}]}]}]}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment