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

แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์ แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์ Warittha Nokmeerod
 
Báo cáo môn mã nguồn mở
Báo cáo môn mã nguồn mởBáo cáo môn mã nguồn mở
Báo cáo môn mã nguồn mởThuyet Nguyen
 
Bao cao session hijacking it-slideshares.blogspot.com
Bao cao session hijacking it-slideshares.blogspot.comBao cao session hijacking it-slideshares.blogspot.com
Bao cao session hijacking it-slideshares.blogspot.comphanleson
 
Đề tài: Cơ cấu tổ chức và quản lý trong công ty cổ phần theo luật doanh nghiệ...
Đề tài: Cơ cấu tổ chức và quản lý trong công ty cổ phần theo luật doanh nghiệ...Đề tài: Cơ cấu tổ chức và quản lý trong công ty cổ phần theo luật doanh nghiệ...
Đề tài: Cơ cấu tổ chức và quản lý trong công ty cổ phần theo luật doanh nghiệ...Viết thuê trọn gói ZALO 0934573149
 
Rèn luyện kỹ năng giải toán phương trình lượng giác cho học sinh lớp 11 trung...
Rèn luyện kỹ năng giải toán phương trình lượng giác cho học sinh lớp 11 trung...Rèn luyện kỹ năng giải toán phương trình lượng giác cho học sinh lớp 11 trung...
Rèn luyện kỹ năng giải toán phương trình lượng giác cho học sinh lớp 11 trung...jackjohn45
 
Cam nang quan tri cong ty second edition
Cam nang quan tri cong ty  second editionCam nang quan tri cong ty  second edition
Cam nang quan tri cong ty second editionNguyen Trang
 
Lý thuyết độ phức tạp tính toán.pdf
Lý thuyết độ phức tạp tính toán.pdfLý thuyết độ phức tạp tính toán.pdf
Lý thuyết độ phức tạp tính toán.pdfMan_Ebook
 
Luận án tiến sĩ luật học hoàn thiện pháp luật về kiểm soát giao dịch giữa côn...
Luận án tiến sĩ luật học hoàn thiện pháp luật về kiểm soát giao dịch giữa côn...Luận án tiến sĩ luật học hoàn thiện pháp luật về kiểm soát giao dịch giữa côn...
Luận án tiến sĩ luật học hoàn thiện pháp luật về kiểm soát giao dịch giữa côn...https://www.facebook.com/garmentspace
 
ประว ต ของ Cloud computing
ประว ต ของ Cloud computingประว ต ของ Cloud computing
ประว ต ของ Cloud computingP'ken Cit
 
ChuyenDeANM ung dung he thong IDS securityonion vao giam sat moi truong mang ...
ChuyenDeANM ung dung he thong IDS securityonion vao giam sat moi truong mang ...ChuyenDeANM ung dung he thong IDS securityonion vao giam sat moi truong mang ...
ChuyenDeANM ung dung he thong IDS securityonion vao giam sat moi truong mang ...nataliej4
 

What's hot (20)

แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์ แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
แบบเสนอโครงร่างโครงงานคอมพิวเตอร์
 
Báo cáo môn mã nguồn mở
Báo cáo môn mã nguồn mởBáo cáo môn mã nguồn mở
Báo cáo môn mã nguồn mở
 
Luận văn: Áp dụng tập quán giải quyết các tranh chấp thương mại
Luận văn: Áp dụng tập quán giải quyết các tranh chấp thương mạiLuận văn: Áp dụng tập quán giải quyết các tranh chấp thương mại
Luận văn: Áp dụng tập quán giải quyết các tranh chấp thương mại
 
Bao cao session hijacking it-slideshares.blogspot.com
Bao cao session hijacking it-slideshares.blogspot.comBao cao session hijacking it-slideshares.blogspot.com
Bao cao session hijacking it-slideshares.blogspot.com
 
Giải pháp hoàn thiện quy định của pháp luật về quản trị công ty
Giải pháp hoàn thiện quy định của pháp luật về quản trị công tyGiải pháp hoàn thiện quy định của pháp luật về quản trị công ty
Giải pháp hoàn thiện quy định của pháp luật về quản trị công ty
 
Chế định Ban kiểm soát của công ty theo Luật Doanh nghiệp, HOT
Chế định Ban kiểm soát của công ty theo Luật Doanh nghiệp, HOTChế định Ban kiểm soát của công ty theo Luật Doanh nghiệp, HOT
Chế định Ban kiểm soát của công ty theo Luật Doanh nghiệp, HOT
 
Đề tài: Cơ cấu tổ chức và quản lý trong công ty cổ phần theo luật doanh nghiệ...
Đề tài: Cơ cấu tổ chức và quản lý trong công ty cổ phần theo luật doanh nghiệ...Đề tài: Cơ cấu tổ chức và quản lý trong công ty cổ phần theo luật doanh nghiệ...
Đề tài: Cơ cấu tổ chức và quản lý trong công ty cổ phần theo luật doanh nghiệ...
 
Đề tài: Vi phạm pháp luật về phòng chống bạo lực gia đình, HAY
Đề tài: Vi phạm pháp luật về phòng chống bạo lực gia đình, HAYĐề tài: Vi phạm pháp luật về phòng chống bạo lực gia đình, HAY
Đề tài: Vi phạm pháp luật về phòng chống bạo lực gia đình, HAY
 
Đề tài: Pháp luật về công ty TNHH một thành viên ở Việt Nam, HAY
Đề tài: Pháp luật về công ty TNHH một thành viên ở Việt Nam, HAYĐề tài: Pháp luật về công ty TNHH một thành viên ở Việt Nam, HAY
Đề tài: Pháp luật về công ty TNHH một thành viên ở Việt Nam, HAY
 
Rèn luyện kỹ năng giải toán phương trình lượng giác cho học sinh lớp 11 trung...
Rèn luyện kỹ năng giải toán phương trình lượng giác cho học sinh lớp 11 trung...Rèn luyện kỹ năng giải toán phương trình lượng giác cho học sinh lớp 11 trung...
Rèn luyện kỹ năng giải toán phương trình lượng giác cho học sinh lớp 11 trung...
 
Đề tài: Giải quyết tranh chấp thương mại có yếu tố nước ngoài, HOT
Đề tài: Giải quyết tranh chấp thương mại có yếu tố nước ngoài, HOTĐề tài: Giải quyết tranh chấp thương mại có yếu tố nước ngoài, HOT
Đề tài: Giải quyết tranh chấp thương mại có yếu tố nước ngoài, HOT
 
Cam nang quan tri cong ty second edition
Cam nang quan tri cong ty  second editionCam nang quan tri cong ty  second edition
Cam nang quan tri cong ty second edition
 
Lý thuyết độ phức tạp tính toán.pdf
Lý thuyết độ phức tạp tính toán.pdfLý thuyết độ phức tạp tính toán.pdf
Lý thuyết độ phức tạp tính toán.pdf
 
Luận án tiến sĩ luật học hoàn thiện pháp luật về kiểm soát giao dịch giữa côn...
Luận án tiến sĩ luật học hoàn thiện pháp luật về kiểm soát giao dịch giữa côn...Luận án tiến sĩ luật học hoàn thiện pháp luật về kiểm soát giao dịch giữa côn...
Luận án tiến sĩ luật học hoàn thiện pháp luật về kiểm soát giao dịch giữa côn...
 
ประว ต ของ Cloud computing
ประว ต ของ Cloud computingประว ต ของ Cloud computing
ประว ต ของ Cloud computing
 
Luận văn: Pháp luật về khai thác, sử dụng quyền tác giả, HAY
Luận văn: Pháp luật về khai thác, sử dụng quyền tác giả, HAYLuận văn: Pháp luật về khai thác, sử dụng quyền tác giả, HAY
Luận văn: Pháp luật về khai thác, sử dụng quyền tác giả, HAY
 
Unit3 8
Unit3 8Unit3 8
Unit3 8
 
ChuyenDeANM ung dung he thong IDS securityonion vao giam sat moi truong mang ...
ChuyenDeANM ung dung he thong IDS securityonion vao giam sat moi truong mang ...ChuyenDeANM ung dung he thong IDS securityonion vao giam sat moi truong mang ...
ChuyenDeANM ung dung he thong IDS securityonion vao giam sat moi truong mang ...
 
LV: Công tác xã hội cá nhân trong việc hỗ trợ người cao tuổi tại xã Minh Quang.
LV: Công tác xã hội cá nhân trong việc hỗ trợ người cao tuổi tại xã Minh Quang.LV: Công tác xã hội cá nhân trong việc hỗ trợ người cao tuổi tại xã Minh Quang.
LV: Công tác xã hội cá nhân trong việc hỗ trợ người cao tuổi tại xã Minh Quang.
 
Luận án: Pháp luật về đăng ký kinh doanh cho doanh nghiệp, HAY
Luận án: Pháp luật về đăng ký kinh doanh cho doanh nghiệp, HAYLuận án: Pháp luật về đăng ký kinh doanh cho doanh nghiệp, HAY
Luận án: Pháp luật về đăng ký kinh doanh cho doanh nghiệp, HAY
 

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

Future Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted VersionFuture Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted VersionMintel Group
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfpollardmorgan
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCRashishs7044
 
Islamabad Escorts | Call 03070433345 | Escort Service in Islamabad
Islamabad Escorts | Call 03070433345 | Escort Service in IslamabadIslamabad Escorts | Call 03070433345 | Escort Service in Islamabad
Islamabad Escorts | Call 03070433345 | Escort Service in IslamabadAyesha Khan
 
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCRashishs7044
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation SlidesKeppelCorporation
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607dollysharma2066
 
Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfJos Voskuil
 
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCRashishs7044
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...lizamodels9
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?Olivia Kresic
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...ictsugar
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis UsageNeil Kimberley
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessSeta Wicaksana
 
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdfNewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdfKhaled Al Awadi
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchirictsugar
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Servicecallgirls2057
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...lizamodels9
 

Recently uploaded (20)

Future Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted VersionFuture Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted Version
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
 
Islamabad Escorts | Call 03070433345 | Escort Service in Islamabad
Islamabad Escorts | Call 03070433345 | Escort Service in IslamabadIslamabad Escorts | Call 03070433345 | Escort Service in Islamabad
Islamabad Escorts | Call 03070433345 | Escort Service in Islamabad
 
Corporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information TechnologyCorporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information Technology
 
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
8447779800, Low rate Call girls in Uttam Nagar Delhi NCR
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
 
Digital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdfDigital Transformation in the PLM domain - distrib.pdf
Digital Transformation in the PLM domain - distrib.pdf
 
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
 
MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?MAHA Global and IPR: Do Actions Speak Louder Than Words?
MAHA Global and IPR: Do Actions Speak Louder Than Words?
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful Business
 
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdfNewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchir
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
 

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