Skip to content

Commit

Permalink
Rename the example turing machine.
Browse files Browse the repository at this point in the history
  • Loading branch information
ealter committed Aug 7, 2017
1 parent abd0ecf commit d20a6c5
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 52 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -23,11 +23,11 @@ clean:

.PHONY: run
run: venv
venv/bin/python -m vim_turing_machine.machines.merge_business_hours.merge_business_hours '[[1,2],[2,3],[5,8]]' 5
venv/bin/python -m vim_turing_machine.machines.merge_overlapping_intervals.merge_overlapping_intervals '[[1,2],[2,3],[5,8]]' 5

.PHONY: run-vim
build-vim: venv
venv/bin/python -m vim_turing_machine.machines.merge_business_hours.vim_merge_business_hours '[[1,2],[2,3],[5,8]]' 5
venv/bin/python -m vim_turing_machine.machines.merge_overlapping_intervals.vim_merge_overlapping_intervals '[[1,2],[2,3],[5,8]]' 5

open-vim-machine: build-vim
vim -u vimrc machine.vim
Expand Down
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -8,22 +8,22 @@ vim_turing_machine: a tool to allow you to run a Turing machine using only
normal mode Vim commands.

And now you might ask, but what can we do on a Turing machine! To demonstrate
its capabilities, we implemented a solution to the Merge Business Hours
interview question and defined all the state transitions needed to solve this
its capabilities, we implemented a solution to the Merge Overlapping Intervals
question and defined all the state transitions needed to solve this
glorious problem. So next time you need to merge some intervals, don't
hand-write a 10-line python program. Instead, take out your favorite editor and
watch it solve the problem in less than a minute with 1500 lines!
watch it solve the problem in less than a minute with 1500 state transitions!

But a simple naysayer may say, 'We already have vimscript! Why in God's name
would I want to use a Turing machine instead?' To that, we retort: our Turing
machine only uses normal mode. So you could theoretically just type in the
program and then execute it without running a single script! No ex mode either!
This project proves that normal mode in Vim is as powerful as any computer!

Merging your favorite business's hours
======================================
Merging your favorite intervals
===============================

Given a set of sorted potentially overlapping open/close hours, merge the
Given a set of sorted potentially overlapping open/close intervals, merge the
overlapping intervals together.

Example:
Expand Down
2 changes: 1 addition & 1 deletion decode_hours.sh
@@ -1,3 +1,3 @@
#! /bin/bash

venv/bin/python -m vim_turing_machine.machines.merge_business_hours.decode_hours "$1" $2
venv/bin/python -m vim_turing_machine.machines.merge_overlapping_intervals.decode_hours "$1" $2
@@ -1,4 +1,4 @@
from vim_turing_machine.machines.merge_business_hours.decode_hours import decode_hours
from vim_turing_machine.machines.merge_overlapping_intervals.decode_hours import decode_hours


def test_encode_hours():
Expand Down
@@ -1,7 +1,7 @@
import pytest

from vim_turing_machine.machines.merge_business_hours.encode_hours import encode_hours
from vim_turing_machine.machines.merge_business_hours.encode_hours import encode_in_x_bits
from vim_turing_machine.machines.merge_overlapping_intervals.encode_hours import encode_hours
from vim_turing_machine.machines.merge_overlapping_intervals.encode_hours import encode_in_x_bits


@pytest.mark.parametrize('number, encoded', [
Expand Down
Expand Up @@ -2,17 +2,17 @@

import pytest

import vim_turing_machine.machines.merge_business_hours.merge_business_hours
import vim_turing_machine.machines.merge_overlapping_intervals.merge_overlapping_intervals
import vim_turing_machine.struct
import vim_turing_machine.turing_machine
from vim_turing_machine.constants import INITIAL_STATE
from vim_turing_machine.constants import NO_FINAL_STATE
from vim_turing_machine.constants import YES_FINAL_STATE
from vim_turing_machine.machines.merge_business_hours.decode_hours import decode_hours
from vim_turing_machine.machines.merge_business_hours.encode_hours import encode_hours
from vim_turing_machine.machines.merge_business_hours.merge_business_hours import invert_bit
from vim_turing_machine.machines.merge_business_hours.merge_business_hours import invert_direction
from vim_turing_machine.machines.merge_business_hours.merge_business_hours import MergeBusinessHoursGenerator
from vim_turing_machine.machines.merge_overlapping_intervals.decode_hours import decode_hours
from vim_turing_machine.machines.merge_overlapping_intervals.encode_hours import encode_hours
from vim_turing_machine.machines.merge_overlapping_intervals.merge_overlapping_intervals import invert_bit
from vim_turing_machine.machines.merge_overlapping_intervals.merge_overlapping_intervals import invert_direction
from vim_turing_machine.machines.merge_overlapping_intervals.merge_overlapping_intervals import MergeBusinessHoursGenerator
from vim_turing_machine.struct import BACKWARDS
from vim_turing_machine.struct import FORWARDS
from vim_turing_machine.turing_machine import TuringMachine
Expand All @@ -32,12 +32,12 @@ def mock_blank_character():
('0', '1', ' '),
):
with mock.patch.object(
vim_turing_machine.machines.merge_business_hours.merge_business_hours,
vim_turing_machine.machines.merge_overlapping_intervals.merge_overlapping_intervals,
'BLANK_CHARACTER',
' ',
):
with mock.patch.object(
vim_turing_machine.machines.merge_business_hours.merge_business_hours,
vim_turing_machine.machines.merge_overlapping_intervals.merge_overlapping_intervals,
'VALID_CHARACTERS',
('0', '1', ' '),
):
Expand Down Expand Up @@ -265,11 +265,11 @@ def test_copy_closing_hour_without_merging(merger):
)
]
)
def test_merge_business_hours(merger, initial_hours, final_hours):
def test_merge_overlapping_intervals(merger, initial_hours, final_hours):
"""The true integration test!"""
tape = encode_hours(initial_hours, num_bits=3)
machine = run_machine(
merger.merge_business_hours_transitions(),
merger.merge_overlapping_intervals_transitions(),
tape=tape,
)

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -21,7 +21,7 @@ envdir = venv
commands =

[flake8]
max-line-length = 119
max-line-length = 131

[pep8]
ignore = E265,E309,E501

This file was deleted.

@@ -1,6 +1,6 @@
"""Descodes a binary string to a json representation of the business's hours
after the merge business hours turing machine has processed it. Reads json from
arv1 and outputs the initial tape."""
"""Decodes a binary string to a json representation of the intervals
after the merge overlapping intervals turing machine have processed them. Reads
json from the command line and outputs the initial tape."""
import json
import sys

Expand Down
@@ -1,6 +1,6 @@
"""Encodes a json representation of the business's hours into the 5-bit binary
representation used by the merge business hours turing machine. It takes input
from stdin and outputs the initial tape."""
"""Encodes a json representation of the intervals into the 5-bit binary
representation used by the merge overlapping intervals hours turing machine. It
takes input from stdin and outputs the initial tape."""
import json
import sys

Expand Down
Expand Up @@ -18,8 +18,8 @@
from vim_turing_machine.constants import INITIAL_STATE
from vim_turing_machine.constants import VALID_CHARACTERS
from vim_turing_machine.constants import YES_FINAL_STATE
from vim_turing_machine.machines.merge_business_hours.decode_hours import decode_hours
from vim_turing_machine.machines.merge_business_hours.encode_hours import encode_hours
from vim_turing_machine.machines.merge_overlapping_intervals.decode_hours import decode_hours
from vim_turing_machine.machines.merge_overlapping_intervals.encode_hours import encode_hours
from vim_turing_machine.struct import BACKWARDS
from vim_turing_machine.struct import DO_NOT_MOVE
from vim_turing_machine.struct import FORWARDS
Expand All @@ -31,7 +31,7 @@ class MergeBusinessHoursGenerator(object):
def __init__(self, num_bits=BITS_PER_NUMBER):
self._num_bits = num_bits

def merge_business_hours_transitions(self):
def merge_overlapping_intervals_transitions(self):
"""This is the main orchestration point of the program"""
# This is the beginning of the loop that goes through the rest of the hours.
CHECK_NEXT_SET_OF_HOURS = 'CheckNextSetOfHours'
Expand Down Expand Up @@ -673,7 +673,7 @@ def invert_direction(direction):
initial_tape = encode_hours(input_string, num_bits)

gen = MergeBusinessHoursGenerator(num_bits)
merge_business_hours = TuringMachine(gen.merge_business_hours_transitions(), debug=True)
merge_business_hours.run(initial_tape=initial_tape, max_steps=5000)
merge_overlapping_intervals = TuringMachine(gen.merge_overlapping_intervals_transitions(), debug=True)
merge_overlapping_intervals.run(initial_tape=initial_tape, max_steps=5000)

print(decode_hours(''.join(merge_business_hours.tape), num_bits))
print(decode_hours(''.join(merge_overlapping_intervals.tape), num_bits))
@@ -0,0 +1,17 @@
import json
import sys

from vim_turing_machine.machines.merge_overlapping_intervals.encode_hours import encode_hours
from vim_turing_machine.machines.merge_overlapping_intervals.merge_overlapping_intervals import MergeBusinessHoursGenerator
from vim_turing_machine.vim_machine import VimTuringMachine


if __name__ == '__main__':
input_string = json.loads(sys.argv[1])
num_bits = int(sys.argv[2])

initial_tape = encode_hours(input_string, num_bits)

gen = MergeBusinessHoursGenerator(num_bits)
merge_overlapping_intervals = VimTuringMachine(gen.merge_overlapping_intervals_transitions(), debug=True)
merge_overlapping_intervals.run(initial_tape=initial_tape)

0 comments on commit d20a6c5

Please sign in to comment.