tdd for coding practices - by zhifu ge

19
TDD for Coding Practices A demo by Zhifu Ge

Upload: ottawaruby

Post on 11-Jan-2017

171 views

Category:

Software


0 download

TRANSCRIPT

TDD for Coding PracticesA demo by Zhifu Ge

Why Practice?

Because all professionals do

working is not practicing

–Robert C. Marin

“You should plan on working 60 hours per week. The first 40 are for your employer. The

remaining 20 are for yourself. ”

Why TDD

It gives me the peace of mind

How to TDD

• write enough test for it to fail • write enough code for the test to pass • repeat as necessary

Why Minitest

it’s simple and does everything the other guys do

How to Minitest

require “minitest/autorun”

def MyTest < Minitest::Test def test_meaning_of

life = Life.new assert_equal 42, meaning_of(life); endend

The problem• The definition of equilibrium index

• Sum of 0 elements are 0

• Worst-case time complexity is O(N)

Prefix Sum

Index(i) 0 1 2 3 4 5 6 7 8

a -1 3 -4 5 1 -6 2 1

prefix_sum 0 -1 2 -2 3 4 -2 0 1

the sum of a[p] to a[q] is prefix_sum[q + 1] - prefix_sum[p]

The Demo