SlideShare a Scribd company logo
1 of 19
Download to read offline
My attempt to solve 100 Doors kata
Steven Mak
Thursday, 6 June, 13
Change history
2
Do you have experience that you think your code is already the best solution, but
after a while you can think of ways improving it? This does happen to me with
this kata. That’s why I decided to add this slide showing history of the changes.
I might not be updating the code here often, especially these changes didn’t
change the overall thinking or analysis of the problem. You can refer to the
source here: https://github.com/tcmak/kata-100-doors
• 2013-06-04: Initial solution, rather procedural
• 2013-06-05: Apply recursion for shouldOpenInRound method
Thursday, 6 June, 13
100 Doors Kata
3
Reference: http://rosettacode.org/wiki/100_doors
To quote how they describe the problem:
“Problem: You have 100 doors in a row that are all initially closed. You make
100 passes by the doors. The first time through, you visit every door and toggle
the door (if the door is closed, you open it; if it is open, you close it). The
second time you only visit every 2nd door (door #2, #4, #6, ...). The third time,
every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door.
Question: What state are the doors in after the last pass? Which are open,
which are closed?”
(Spoiler warning: you might not want to continue if you haven’t done this kata before)
Thursday, 6 June, 13
First test -
all doors are close at the beginning
4
	 it("All doors closed at the beginning", function()
	 	 local doors = doors(0)
	 	 for i,door in ipairs(doors) do
	 	 	 assert.is_false(door)
	 	 end
	 end)
Thursday, 6 June, 13
Simple implementation
5
local function doors(round)
	 local doors = {}
	 for i = 1, NUM_DOORS do doors[i] = false end
	 return doors
end
NUM_DOORS = 100
(my apology for using global variables for now)
Thursday, 6 June, 13
Second test -
all doors opened after the first pass
6
	 it("All doors open after the first round", function()
	 	 local doors = doors(1)
	 	 for i,door in ipairs(doors) do
	 	 	 assert.is_true(door)
	 	 end
	 end)
Thursday, 6 June, 13
Another Simple implementation and
extract doors initialization to its own function
7
local function doors(round)
	 local doors = initializeDoors()	
	 if round == 0 then return doors end	
	 for i = 1, NUM_DOORS do doors[i] = true end	
	 return doors
end
Thursday, 6 June, 13
Third test -
only alternative doors opened
8
	 it("Second round, alternative doors are open", function()
	 	 local doors = doors(2)
	 	 for i= 1,NUM_DOORS, 2 do assert.is_true(doors[i]) end
	 	 for i= 2,NUM_DOORS, 2 do assert.is_false(doors[i]) end
	 end)
Thursday, 6 June, 13
Let’s fake it for now as if I know the solution
9
local function doors(round)
	 local doors = initializeDoors()
	 if round == 0 then return doors end
	 for i = 1, NUM_DOORS do
	 	 if (i+1) % round == 0 then doors[i] = true end
	 end
	 return doors
end
You are right, at this moment I still have
no clue how the complete solution is like,
or any pattern related to the problem
Thursday, 6 June, 13
Fourth test -
what is it???
10
	 it("All doors closed at the beginning", function()
	 	 ... ...
	 end)
	 it("All doors open after the first round", function()
	 	 ... ...
	 end)
	 it("Second round, alternative doors are open", function()
	 	 ... ...
	 end)
It does not seem to have any easy way to
describe the pattern in the next test
Thursday, 6 June, 13
Fourth test -
get a piece of paper and do some math
11
Doors Pass 1 Pass 2 Pass 3
1 Open Open Open
2 Open Close Close
3 Open Open Close
4 Open Close Close
5 Open Open Open
6 Open Close Open
7 Open Open Open
8 Open Close Close
9 Open Open Close
10 Open Close Close
Thursday, 6 June, 13
Fourth test -
think deeper
12
Doors Pass 1 Pass 2 Pass 3 flips#
1 Open Open Open 1
2 Open Close Close 2
3 Open Open Close 2
4 Open Close Close 2
5 Open Open Open 1
6 Open Close Open 3
7 Open Open Open 1
8 Open Close Close 2
9 Open Open Close 2
10 Open Close Close 2
Do you see the pattern:
Whether a door is closed or opened
depends on the number of numbers,
limited by the number of passes, which
can divide this door number.
For example:
9 is divisible by 1 and 3
6 is divisible by 1, 2, and 3
If this number of factors is odd, then it will
be opened, otherwise it is closed
Thursday, 6 June, 13
Put the original kata aside for now
and do this little kata
13
For any integer N, how many integers, which are less than or equal to
another given integer R, that can divide N
(for 100-door kata, we are only concerned if it is odd or even)
For example:
N R result Odd?
1 1 1 TRUE
2 2 2 FALSE
3 3 2 FALSE
4 2 2 FALSE
4 4 3 TRUE
Thursday, 6 June, 13
I will leave the process of solving
this kata for your own pleasure
14
local function shouldOpenInRound(number, round)
	 local shouldOpen = false
	
	 for factor=1,round do
	 	 if number % factor == 0 then shouldOpen = not shouldOpen end
	 end
	
	 return shouldOpen
end
Thursday, 6 June, 13
Fourth test -
Let’s continue
15
	 it("Third round, [1 0 0 0 1 1]", function()
	 	 local originalNumOfDoors = NUM_DOORS
	 	 NUM_DOORS = 6
	
	 	 local doors = doors(3)
	 	 assert.is_true(doors[1])
	 	 assert.is_false(doors[2])
	 	 assert.is_false(doors[3])
	 	 assert.is_false(doors[4])
	 	 assert.is_true(doors[5])
	 	 assert.is_true(doors[6])
	 	
	 	 NUM_DOORS = originalNumOfDoors
	 end)
Thursday, 6 June, 13
Now it’s an easy piece of cake
16
local function doors(round)
	 local doors = initializeDoors()
	 if round == 0 then return doors end
	
	 for i = 1, NUM_DOORS do
	 	 if shouldOpenInRound(i, round) then doors[i] = true end
	 end
	 return doors
end
Thursday, 6 June, 13
Refactor and remove redundant code
17
NUM_DOORS = 100
local function shouldOpenInRound(number, round)
	 local shouldOpen = false
	
	 for factor=1,round do
	 	 if number % factor == 0 then shouldOpen = not shouldOpen end
	 end
	
	 return shouldOpen
end
local function doors(round)
	 local doors = {}
	
	 for i = 1, NUM_DOORS do
	 	 doors[i] = shouldOpenInRound(i, round)
	 end
	
	 return doors
end
Thursday, 6 June, 13
Fifth test -
confirming this is right
18
	 it("100th round, 1,4,9,16...100", function()
	 	 local doors = doors(100)
	 	 for i=1,NUM_DOORS do
	 	 	 if math.sqrt(i) == math.ceil(math.sqrt(i)) then
	 	 	 	 assert.is_true(doors[i])
	 	 	 else
	 	 	 	 assert.is_false(doors[i])
	 	 	 end
	 	 end
	 end)
Yay! things pass as expected. Smile!
Thursday, 6 June, 13
In retrospect...
• What if I follow TPP strictly?
• Is this the most optimal solution?
• Shall this code be more OO or functional?
• Can these functions be shorter/cleaner?
Updates can be found at: https://github.com/tcmak/kata-100-doors
Thank you for spending time on this.
More feedback can be sent to:
19
Steven Mak 麥天志
steven@odd-e.com
http://www.odd-e.com
https://twitter.com/stevenmak
http://weibo.com/odde
Odd-e Hong Kong Ltd.
Steven Mak 麥天志
Agile Coach
Hong Kong
Email: steven@odd-e.com
Web: www.odd-e.com
Twitter: stevenmak
Thursday, 6 June, 13

More Related Content

What's hot

Giới thiệu cách sử dụng Bootstrap CSS Framework
Giới thiệu cách sử dụng Bootstrap CSS FrameworkGiới thiệu cách sử dụng Bootstrap CSS Framework
Giới thiệu cách sử dụng Bootstrap CSS Frameworkhocwebgiare
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introductionteach4uin
 
Xây dựng hiệu xuất cao cho nhóm
Xây dựng hiệu xuất cao cho nhómXây dựng hiệu xuất cao cho nhóm
Xây dựng hiệu xuất cao cho nhómforeman
 
NJ Hadoop Meetup - Apache NiFi Deep Dive
NJ Hadoop Meetup - Apache NiFi Deep DiveNJ Hadoop Meetup - Apache NiFi Deep Dive
NJ Hadoop Meetup - Apache NiFi Deep DiveBryan Bende
 
Phương pháp đặt mục tiêu SMART
Phương pháp đặt mục tiêu SMARTPhương pháp đặt mục tiêu SMART
Phương pháp đặt mục tiêu SMARTDeltaViet
 
Ngon ngu truy van sql
Ngon ngu truy van sqlNgon ngu truy van sql
Ngon ngu truy van sqlPhùng Duy
 
Đào tạo cơ bản 5S
Đào tạo cơ bản 5SĐào tạo cơ bản 5S
Đào tạo cơ bản 5SKristiMarcus
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from insidejulien pauli
 
Bài tiểu luận thuyết trình về kaizen 5s
Bài tiểu luận thuyết trình về kaizen 5sBài tiểu luận thuyết trình về kaizen 5s
Bài tiểu luận thuyết trình về kaizen 5sTÀI LIỆU NGÀNH MAY
 
Coredns nodecache - A highly-available Node-cache DNS server
Coredns nodecache - A highly-available Node-cache DNS serverCoredns nodecache - A highly-available Node-cache DNS server
Coredns nodecache - A highly-available Node-cache DNS serverYann Hamon
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
7 bước xếp thời khóa biểu
7 bước xếp thời khóa biểu7 bước xếp thời khóa biểu
7 bước xếp thời khóa biểuBùi Việt Hà
 
Tài liệu tham khảo về 6 chiếc mũ tư duy.
Tài liệu tham khảo về 6 chiếc mũ tư duy. Tài liệu tham khảo về 6 chiếc mũ tư duy.
Tài liệu tham khảo về 6 chiếc mũ tư duy. Ha Ngo
 
elasticsearchソースコードを読みはじめてみた
elasticsearchソースコードを読みはじめてみたelasticsearchソースコードを読みはじめてみた
elasticsearchソースコードを読みはじめてみたfurandon_pig
 
Kỹ năng quản lý thời gian hiệu quả
Kỹ năng quản lý thời gian hiệu quảKỹ năng quản lý thời gian hiệu quả
Kỹ năng quản lý thời gian hiệu quảLê Tưởng
 

What's hot (20)

Giới thiệu cách sử dụng Bootstrap CSS Framework
Giới thiệu cách sử dụng Bootstrap CSS FrameworkGiới thiệu cách sử dụng Bootstrap CSS Framework
Giới thiệu cách sử dụng Bootstrap CSS Framework
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
 
Xây dựng hiệu xuất cao cho nhóm
Xây dựng hiệu xuất cao cho nhómXây dựng hiệu xuất cao cho nhóm
Xây dựng hiệu xuất cao cho nhóm
 
NJ Hadoop Meetup - Apache NiFi Deep Dive
NJ Hadoop Meetup - Apache NiFi Deep DiveNJ Hadoop Meetup - Apache NiFi Deep Dive
NJ Hadoop Meetup - Apache NiFi Deep Dive
 
Phương pháp đặt mục tiêu SMART
Phương pháp đặt mục tiêu SMARTPhương pháp đặt mục tiêu SMART
Phương pháp đặt mục tiêu SMART
 
Ngon ngu truy van sql
Ngon ngu truy van sqlNgon ngu truy van sql
Ngon ngu truy van sql
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Đào tạo cơ bản 5S
Đào tạo cơ bản 5SĐào tạo cơ bản 5S
Đào tạo cơ bản 5S
 
6 mũ tư duy
6 mũ tư duy6 mũ tư duy
6 mũ tư duy
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 
Bài tiểu luận thuyết trình về kaizen 5s
Bài tiểu luận thuyết trình về kaizen 5sBài tiểu luận thuyết trình về kaizen 5s
Bài tiểu luận thuyết trình về kaizen 5s
 
Node.js căn bản
Node.js căn bảnNode.js căn bản
Node.js căn bản
 
Coredns nodecache - A highly-available Node-cache DNS server
Coredns nodecache - A highly-available Node-cache DNS serverCoredns nodecache - A highly-available Node-cache DNS server
Coredns nodecache - A highly-available Node-cache DNS server
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
7 bước xếp thời khóa biểu
7 bước xếp thời khóa biểu7 bước xếp thời khóa biểu
7 bước xếp thời khóa biểu
 
Tài liệu tham khảo về 6 chiếc mũ tư duy.
Tài liệu tham khảo về 6 chiếc mũ tư duy. Tài liệu tham khảo về 6 chiếc mũ tư duy.
Tài liệu tham khảo về 6 chiếc mũ tư duy.
 
elasticsearchソースコードを読みはじめてみた
elasticsearchソースコードを読みはじめてみたelasticsearchソースコードを読みはじめてみた
elasticsearchソースコードを読みはじめてみた
 
Học python
Học pythonHọc python
Học python
 
Kỹ năng quản lý thời gian hiệu quả
Kỹ năng quản lý thời gian hiệu quảKỹ năng quản lý thời gian hiệu quả
Kỹ năng quản lý thời gian hiệu quả
 
Slide-dao-tao-HORENSO.pdf
Slide-dao-tao-HORENSO.pdfSlide-dao-tao-HORENSO.pdf
Slide-dao-tao-HORENSO.pdf
 

More from Steven Mak

Continuous Security Testing
Continuous Security TestingContinuous Security Testing
Continuous Security TestingSteven Mak
 
Quality comes free with open source testing tools
Quality comes free with open source testing toolsQuality comes free with open source testing tools
Quality comes free with open source testing toolsSteven Mak
 
Adopting technical practices 2013
Adopting technical practices 2013Adopting technical practices 2013
Adopting technical practices 2013Steven Mak
 
Bossless companies
Bossless companiesBossless companies
Bossless companiesSteven Mak
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?Steven Mak
 
Driving Quality with TDD
Driving Quality with TDDDriving Quality with TDD
Driving Quality with TDDSteven Mak
 
Unbearable Test Code Smell
Unbearable Test Code SmellUnbearable Test Code Smell
Unbearable Test Code SmellSteven Mak
 
Sustainable TDD
Sustainable TDDSustainable TDD
Sustainable TDDSteven Mak
 
Introduction to Acceptance Test Driven Development
Introduction to Acceptance Test Driven DevelopmentIntroduction to Acceptance Test Driven Development
Introduction to Acceptance Test Driven DevelopmentSteven Mak
 
Essential practices and thinking tools for Agile Adoption
Essential practices and thinking tools for Agile AdoptionEssential practices and thinking tools for Agile Adoption
Essential practices and thinking tools for Agile AdoptionSteven Mak
 
ATDD in Practice
ATDD in PracticeATDD in Practice
ATDD in PracticeSteven Mak
 

More from Steven Mak (11)

Continuous Security Testing
Continuous Security TestingContinuous Security Testing
Continuous Security Testing
 
Quality comes free with open source testing tools
Quality comes free with open source testing toolsQuality comes free with open source testing tools
Quality comes free with open source testing tools
 
Adopting technical practices 2013
Adopting technical practices 2013Adopting technical practices 2013
Adopting technical practices 2013
 
Bossless companies
Bossless companiesBossless companies
Bossless companies
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?
 
Driving Quality with TDD
Driving Quality with TDDDriving Quality with TDD
Driving Quality with TDD
 
Unbearable Test Code Smell
Unbearable Test Code SmellUnbearable Test Code Smell
Unbearable Test Code Smell
 
Sustainable TDD
Sustainable TDDSustainable TDD
Sustainable TDD
 
Introduction to Acceptance Test Driven Development
Introduction to Acceptance Test Driven DevelopmentIntroduction to Acceptance Test Driven Development
Introduction to Acceptance Test Driven Development
 
Essential practices and thinking tools for Agile Adoption
Essential practices and thinking tools for Agile AdoptionEssential practices and thinking tools for Agile Adoption
Essential practices and thinking tools for Agile Adoption
 
ATDD in Practice
ATDD in PracticeATDD in Practice
ATDD in Practice
 

Recently uploaded

Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with CultureSeta Wicaksana
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...lizamodels9
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyEthan lee
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayNZSG
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfPaul Menig
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756dollysharma2066
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...amitlee9823
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMRavindra Nath Shukla
 
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Delhi Call girls
 
Boost the utilization of your HCL environment by reevaluating use cases and f...
Boost the utilization of your HCL environment by reevaluating use cases and f...Boost the utilization of your HCL environment by reevaluating use cases and f...
Boost the utilization of your HCL environment by reevaluating use cases and f...Roland Driesen
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...anilsa9823
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...rajveerescorts2022
 
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Dave Litwiller
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Servicediscovermytutordmt
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Dipal Arora
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLSeo
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdfRenandantas16
 

Recently uploaded (20)

Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
 
Mifty kit IN Salmiya (+918133066128) Abortion pills IN Salmiyah Cytotec pills
Mifty kit IN Salmiya (+918133066128) Abortion pills IN Salmiyah Cytotec pillsMifty kit IN Salmiya (+918133066128) Abortion pills IN Salmiyah Cytotec pills
Mifty kit IN Salmiya (+918133066128) Abortion pills IN Salmiyah Cytotec pills
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdf
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
Best VIP Call Girls Noida Sector 40 Call Me: 8448380779
 
Boost the utilization of your HCL environment by reevaluating use cases and f...
Boost the utilization of your HCL environment by reevaluating use cases and f...Boost the utilization of your HCL environment by reevaluating use cases and f...
Boost the utilization of your HCL environment by reevaluating use cases and f...
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
 
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Service
 
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
Call Girls Navi Mumbai Just Call 9907093804 Top Class Call Girl Service Avail...
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
 

100 doors kata solution

  • 1. My attempt to solve 100 Doors kata Steven Mak Thursday, 6 June, 13
  • 2. Change history 2 Do you have experience that you think your code is already the best solution, but after a while you can think of ways improving it? This does happen to me with this kata. That’s why I decided to add this slide showing history of the changes. I might not be updating the code here often, especially these changes didn’t change the overall thinking or analysis of the problem. You can refer to the source here: https://github.com/tcmak/kata-100-doors • 2013-06-04: Initial solution, rather procedural • 2013-06-05: Apply recursion for shouldOpenInRound method Thursday, 6 June, 13
  • 3. 100 Doors Kata 3 Reference: http://rosettacode.org/wiki/100_doors To quote how they describe the problem: “Problem: You have 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, you visit every door and toggle the door (if the door is closed, you open it; if it is open, you close it). The second time you only visit every 2nd door (door #2, #4, #6, ...). The third time, every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door. Question: What state are the doors in after the last pass? Which are open, which are closed?” (Spoiler warning: you might not want to continue if you haven’t done this kata before) Thursday, 6 June, 13
  • 4. First test - all doors are close at the beginning 4 it("All doors closed at the beginning", function() local doors = doors(0) for i,door in ipairs(doors) do assert.is_false(door) end end) Thursday, 6 June, 13
  • 5. Simple implementation 5 local function doors(round) local doors = {} for i = 1, NUM_DOORS do doors[i] = false end return doors end NUM_DOORS = 100 (my apology for using global variables for now) Thursday, 6 June, 13
  • 6. Second test - all doors opened after the first pass 6 it("All doors open after the first round", function() local doors = doors(1) for i,door in ipairs(doors) do assert.is_true(door) end end) Thursday, 6 June, 13
  • 7. Another Simple implementation and extract doors initialization to its own function 7 local function doors(round) local doors = initializeDoors() if round == 0 then return doors end for i = 1, NUM_DOORS do doors[i] = true end return doors end Thursday, 6 June, 13
  • 8. Third test - only alternative doors opened 8 it("Second round, alternative doors are open", function() local doors = doors(2) for i= 1,NUM_DOORS, 2 do assert.is_true(doors[i]) end for i= 2,NUM_DOORS, 2 do assert.is_false(doors[i]) end end) Thursday, 6 June, 13
  • 9. Let’s fake it for now as if I know the solution 9 local function doors(round) local doors = initializeDoors() if round == 0 then return doors end for i = 1, NUM_DOORS do if (i+1) % round == 0 then doors[i] = true end end return doors end You are right, at this moment I still have no clue how the complete solution is like, or any pattern related to the problem Thursday, 6 June, 13
  • 10. Fourth test - what is it??? 10 it("All doors closed at the beginning", function() ... ... end) it("All doors open after the first round", function() ... ... end) it("Second round, alternative doors are open", function() ... ... end) It does not seem to have any easy way to describe the pattern in the next test Thursday, 6 June, 13
  • 11. Fourth test - get a piece of paper and do some math 11 Doors Pass 1 Pass 2 Pass 3 1 Open Open Open 2 Open Close Close 3 Open Open Close 4 Open Close Close 5 Open Open Open 6 Open Close Open 7 Open Open Open 8 Open Close Close 9 Open Open Close 10 Open Close Close Thursday, 6 June, 13
  • 12. Fourth test - think deeper 12 Doors Pass 1 Pass 2 Pass 3 flips# 1 Open Open Open 1 2 Open Close Close 2 3 Open Open Close 2 4 Open Close Close 2 5 Open Open Open 1 6 Open Close Open 3 7 Open Open Open 1 8 Open Close Close 2 9 Open Open Close 2 10 Open Close Close 2 Do you see the pattern: Whether a door is closed or opened depends on the number of numbers, limited by the number of passes, which can divide this door number. For example: 9 is divisible by 1 and 3 6 is divisible by 1, 2, and 3 If this number of factors is odd, then it will be opened, otherwise it is closed Thursday, 6 June, 13
  • 13. Put the original kata aside for now and do this little kata 13 For any integer N, how many integers, which are less than or equal to another given integer R, that can divide N (for 100-door kata, we are only concerned if it is odd or even) For example: N R result Odd? 1 1 1 TRUE 2 2 2 FALSE 3 3 2 FALSE 4 2 2 FALSE 4 4 3 TRUE Thursday, 6 June, 13
  • 14. I will leave the process of solving this kata for your own pleasure 14 local function shouldOpenInRound(number, round) local shouldOpen = false for factor=1,round do if number % factor == 0 then shouldOpen = not shouldOpen end end return shouldOpen end Thursday, 6 June, 13
  • 15. Fourth test - Let’s continue 15 it("Third round, [1 0 0 0 1 1]", function() local originalNumOfDoors = NUM_DOORS NUM_DOORS = 6 local doors = doors(3) assert.is_true(doors[1]) assert.is_false(doors[2]) assert.is_false(doors[3]) assert.is_false(doors[4]) assert.is_true(doors[5]) assert.is_true(doors[6]) NUM_DOORS = originalNumOfDoors end) Thursday, 6 June, 13
  • 16. Now it’s an easy piece of cake 16 local function doors(round) local doors = initializeDoors() if round == 0 then return doors end for i = 1, NUM_DOORS do if shouldOpenInRound(i, round) then doors[i] = true end end return doors end Thursday, 6 June, 13
  • 17. Refactor and remove redundant code 17 NUM_DOORS = 100 local function shouldOpenInRound(number, round) local shouldOpen = false for factor=1,round do if number % factor == 0 then shouldOpen = not shouldOpen end end return shouldOpen end local function doors(round) local doors = {} for i = 1, NUM_DOORS do doors[i] = shouldOpenInRound(i, round) end return doors end Thursday, 6 June, 13
  • 18. Fifth test - confirming this is right 18 it("100th round, 1,4,9,16...100", function() local doors = doors(100) for i=1,NUM_DOORS do if math.sqrt(i) == math.ceil(math.sqrt(i)) then assert.is_true(doors[i]) else assert.is_false(doors[i]) end end end) Yay! things pass as expected. Smile! Thursday, 6 June, 13
  • 19. In retrospect... • What if I follow TPP strictly? • Is this the most optimal solution? • Shall this code be more OO or functional? • Can these functions be shorter/cleaner? Updates can be found at: https://github.com/tcmak/kata-100-doors Thank you for spending time on this. More feedback can be sent to: 19 Steven Mak 麥天志 steven@odd-e.com http://www.odd-e.com https://twitter.com/stevenmak http://weibo.com/odde Odd-e Hong Kong Ltd. Steven Mak 麥天志 Agile Coach Hong Kong Email: steven@odd-e.com Web: www.odd-e.com Twitter: stevenmak Thursday, 6 June, 13