SlideShare une entreprise Scribd logo
1  sur  40
Télécharger pour lire hors ligne
Programming
Exercises
Terry Yin
Who am I?

2
Why Programming Exercises?

3
4
Quality vs. Quantity

5
Gamification

6
Question
Does software company need to
train programmers?

7
Leonhard Euler

Dojo

vs.

8
What is ProjectEuler.net?

9
ProjectEuler.net
"Project Euler exists to encourage, challenge, and develop the skills and enjoyment of anyone with
an interest in the fascinating world of mathematics."

• Project Euler is a series of challenging mathematical/computer

programming problems that requires more than just mathematical
insights to solve.

10
ProjectEuler.net

11
ProjectEuler.net

12
Expectations

Mathematics
lution implementing
So
rfor mance co d ing
Hig h pe
So lving problem
ind ivi dually

13
Example: Quick & Dirty
Once you solved a problem, you are entitled to join the forum for this problem.You can
share your solution with the other people who have also solved it. And the post usually
start with this sentence:!

Good coding habit?

14
Another Example

15
int i, j, k, n, sum;!
int factorial[10000];!

"

• It’s too simple for

•
•

Java, Ruby or Python,
for which they can
handle big numbers by
default."
So let’s look at C/C++."
Real code copied from
the forum.

int* getfactorial(int n)!
{!
factorial[0] = 1;!
k = 0;!
for(i = 2; i <= n; i++)!
{!
for(j = 0; j <= k; j++)!
factorial[j] *= i;!
for(j = 0; j <= k; j++)!
{!
if(factorial[j] >= 10)!
{!
factorial[j+1] += (factorial[j] - (factorial[j] % 10)) / 10;!
factorial[j] = factorial[j] % 10;!
if(j == k)!
k++;!
}!
}!
}!
return factorial;!
}!

"

int getsum(int* array, int k)!
{!
sum = 0;!
for(i = 0; i <= k; i++)!
sum += array[i];!
return sum;!
}!

"

int main()!
{!
int* factorial = getfactorial(n);!
sum = getsum(factorial, k);!
cout << "nThe sum of the digits of " << n << "! is " << sum << ".n";!
return 0;!
16
}!
17
Big number again. Ha ha, I did that
before! Let me reuse it...

18
#include<iostream>!

"

using namespace std;!

"

int i, j, k, n, sum;!
int factorial[10000];!

"

Why the big number is named ‘factorial’
in my previous implementation?

int* getfactorial(int n)!
{!
factorial[0] = 1;!
k = 0;!
for(i = 2; i <= n; i++)!
{!
for(j = 0; j <= k; j++)!
factorial[j] *= i;!
for(j = 0; j <= k; j++)!
{!
if(factorial[j] >= 10)!
{!
factorial[j+1] += (factorial[j] - (factorial[j] % 10)) / 10;!
factorial[j] = factorial[j] % 10;!
if(j == k)!
k++;!
}!
}!
}!
return factorial;!
}

Where’s the code for big number?

19
#include <stdio.h>

"

#define NUMB 120
#define SIZE 1000000000

"
OK, too hard to reuse. It’s not
that hard to invent the wheel again.

// 9 * 120 = 1080 total digits.
// 9 digit numbers

int main() {
int i = 0;
int j = 0;
int bigNum1[NUMB];
int bigNum2[NUMB];
int bigNum3[NUMB];
int counter = 0;

"

"
"

"
}

for (i = 0;
bigNum1 =
bigNum2 =
bigNum3 =
}

i < NUMB; i++) {
0;
0;
0;

bigNum1[0] = 1;
bigNum2[0] = 1;
counter = 2;
i = 0;
while (i == 0) {
counter++;
for (j = 0; j < NUMB; j++) {
bigNum3[j] = bigNum2[j] + bigNum1[j];
}
for (j = 0; j < NUMB-1; j++) {
while (bigNum3[j] >= SIZE) {
bigNum3[j] -= SIZE;
bigNum3[j+1]++;
}
}
if (bigNum3[111] >= 1)
break;
for (j = 0; j < NUMB; j++) {
bigNum1[j] = bigNum2[j];
bigNum2[j] = bigNum3[j];
}
}
printf("n");
printf("P025 answer = %u", counter);

20
s
ng exercise
i
oblem so lv
Pr
tant skills
ver y impor
✓ Train
inter view
s
ten use d a
Of

✓

ques tions
n skills in
i
ly also tra
✓ Probab
able co de,
n
ng maintai
maki
a little.

•Don’t do it before bed time...

21
What is Cyber Dojo?

22
23
24
25
RED - GREEN - REFACTOR

26
Functional Test

Integration Test

Unit Test
27
28
29
30
Refactoring Dojo

31
Code Retreat
A day-long practice-intensive event for programmers, !
popularized by Corey Haines.

✓
✓
✓
✓
✓
✓
✓
32
Which One Do You Prefer?
33
Poker Hands

34
Poker Hands Kata

35
Poker Hands in ProjectEuler
• This is not a very hard problem to solve,
•

comparing to most of the problems listed in
ProjectEuler.net"
But it’s hard to solve it without bugs if you don’t
have unit tests."

• Problem 54 in projectEuler.net

36
Example: Complicated
Transaction

37
Why Did I Do It Right
With Only One Try?

38
TEST(poker_hand, comparing){
CHECK(pokerHand("4H 5C 6S 7S TD")< pokerHand("2C 3S 7S 8D KD"));
CHECK(!(pokerHand("4H 5C 6S 7S KD")< pokerHand("2C 3S 7S 8D TD")));
CHECK(pokerHand("4H 5C 6S 7S KD")> pokerHand("2C 3S 7S 8D TD"));
}
TEST(poker_hand, compare_cards){
CHECK(pokerHand("3H 5C 6S 7S 8D")< pokerHand("4H 5C 6S 7S 9D"));
CHECK(pokerHand("3H 5C 6S 7S 9D")< pokerHand("4H 5C 6S 7S TD"));
CHECK(pokerHand("3H 5C 6S 7S TD")< pokerHand("4H 5C 6S 7S JD"));
CHECK(pokerHand("3H 5C 6S 7S JD")< pokerHand("4H 5C 6S 7S QD"));
CHECK(pokerHand("3H 5C 6S 7S QD")< pokerHand("4H 5C 6S 7S KD"));
CHECK(pokerHand("3H 5C 6S 7S KD")< pokerHand("4H 5C 6S 7S AD"));
}
TEST(poker_hand, compare_high_cards_2nd){
CHECK(pokerHand("3H 5C 6S 7S 9D")< pokerHand("3H 5C 6S 8S 9D"));
}
TEST(poker_hand, compare_high_cards_3nd_4th_5th){
CHECK(pokerHand("3H 5C 6S 8S 9D")< pokerHand("3H 5C 7S 8S 9D"));
CHECK(pokerHand("3H 4C 7S 8S 9D")< pokerHand("3H 5C 7S 8S 9D"));
CHECK(pokerHand("2H 5C 7S 8S 9D")< pokerHand("3H 5C 7S 8S 9D"));
}
TEST(poker_hand, compare_high_card_and_one_pair){
CHECK(pokerHand("3H 5C 6S 8S 9D")< pokerHand("3H 5C 7S 8S 8D"));
}
TEST(poker_hand, compare_one_pairs){
CHECK(pokerHand("3H 5C 6S 9S 9D")> pokerHand("3H 5C 7S 8S 8D"));
CHECK(pokerHand("5C 6S 9S 9D KD")> pokerHand("5C 7S 8S 8D AD"));
CHECK(pokerHand("5C 6S 9S 9D KD")< pokerHand("5C 7S 9S 9D AD"));
}

"

...
39
Do deliberate exercises.
Alone and with the others.
Always use good practices.
Have fun.

40

Contenu connexe

Similaire à Programming exercises

Learn Python 3 for absolute beginners
Learn Python 3 for absolute beginnersLearn Python 3 for absolute beginners
Learn Python 3 for absolute beginnersKingsleyAmankwa
 
Python in 30 minutes!
Python in 30 minutes!Python in 30 minutes!
Python in 30 minutes!Fariz Darari
 
Lyon jug-how-to-fail-at-benchmarking
Lyon jug-how-to-fail-at-benchmarkingLyon jug-how-to-fail-at-benchmarking
Lyon jug-how-to-fail-at-benchmarkingPierre Laporte
 
Embedded SW Interview Questions
Embedded SW Interview Questions Embedded SW Interview Questions
Embedded SW Interview Questions PiTechnologies
 
Back To The Future.Key 2
Back To The Future.Key 2Back To The Future.Key 2
Back To The Future.Key 2gueste8cc560
 
Class[1][23ed may] [algorithms]
Class[1][23ed may] [algorithms]Class[1][23ed may] [algorithms]
Class[1][23ed may] [algorithms]Saajid Akram
 
Fast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDBFast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDBMongoDB
 
Unit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of PurityUnit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of PurityVictor Rentea
 
Progressive transpilation and the road to ES2015 in production
Progressive transpilation and the road to ES2015 in productionProgressive transpilation and the road to ES2015 in production
Progressive transpilation and the road to ES2015 in productionJacques Favreau
 
FITC '14 Toronto - Technology, a means to an end
FITC '14 Toronto - Technology, a means to an endFITC '14 Toronto - Technology, a means to an end
FITC '14 Toronto - Technology, a means to an endThibault Imbert
 
Technology: A Means to an End with Thibault Imbert
Technology: A Means to an End with Thibault ImbertTechnology: A Means to an End with Thibault Imbert
Technology: A Means to an End with Thibault ImbertFITC
 
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Victor Rentea
 
Refactor legacy code through pure functions
Refactor legacy code through pure functionsRefactor legacy code through pure functions
Refactor legacy code through pure functionsAlexandru Bolboaca
 
Clean Code Development
Clean Code DevelopmentClean Code Development
Clean Code DevelopmentPeter Gfader
 
Mutation Testing with PIT
Mutation Testing with PITMutation Testing with PIT
Mutation Testing with PITRafał Leszko
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean codeVictor Rentea
 

Similaire à Programming exercises (20)

Learn Python 3 for absolute beginners
Learn Python 3 for absolute beginnersLearn Python 3 for absolute beginners
Learn Python 3 for absolute beginners
 
Python in 30 minutes!
Python in 30 minutes!Python in 30 minutes!
Python in 30 minutes!
 
Lyon jug-how-to-fail-at-benchmarking
Lyon jug-how-to-fail-at-benchmarkingLyon jug-how-to-fail-at-benchmarking
Lyon jug-how-to-fail-at-benchmarking
 
Embedded SW Interview Questions
Embedded SW Interview Questions Embedded SW Interview Questions
Embedded SW Interview Questions
 
Back To The Future.Key 2
Back To The Future.Key 2Back To The Future.Key 2
Back To The Future.Key 2
 
Class[1][23ed may] [algorithms]
Class[1][23ed may] [algorithms]Class[1][23ed may] [algorithms]
Class[1][23ed may] [algorithms]
 
Fast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDBFast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDB
 
Unit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of PurityUnit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of Purity
 
Progressive transpilation and the road to ES2015 in production
Progressive transpilation and the road to ES2015 in productionProgressive transpilation and the road to ES2015 in production
Progressive transpilation and the road to ES2015 in production
 
FITC '14 Toronto - Technology, a means to an end
FITC '14 Toronto - Technology, a means to an endFITC '14 Toronto - Technology, a means to an end
FITC '14 Toronto - Technology, a means to an end
 
Technology: A Means to an End with Thibault Imbert
Technology: A Means to an End with Thibault ImbertTechnology: A Means to an End with Thibault Imbert
Technology: A Means to an End with Thibault Imbert
 
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
 
Refactor legacy code through pure functions
Refactor legacy code through pure functionsRefactor legacy code through pure functions
Refactor legacy code through pure functions
 
Test Automation Day 2018
Test Automation Day 2018Test Automation Day 2018
Test Automation Day 2018
 
Clean Code Development
Clean Code DevelopmentClean Code Development
Clean Code Development
 
Perfect Code
Perfect CodePerfect Code
Perfect Code
 
Mutation Testing with PIT
Mutation Testing with PITMutation Testing with PIT
Mutation Testing with PIT
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean code
 
C#, What Is Next?
C#, What Is Next?C#, What Is Next?
C#, What Is Next?
 
ES3-2020-05 Testing
ES3-2020-05 TestingES3-2020-05 Testing
ES3-2020-05 Testing
 

Plus de Terry Yin

Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTerry Yin
 
Six Years of Teaching Certified Scrum Developers
Six Years of Teaching Certified Scrum DevelopersSix Years of Teaching Certified Scrum Developers
Six Years of Teaching Certified Scrum DevelopersTerry Yin
 
Abstraction is a communication tool
Abstraction is a communication toolAbstraction is a communication tool
Abstraction is a communication toolTerry Yin
 
No Reuse Before Use
No Reuse Before UseNo Reuse Before Use
No Reuse Before UseTerry Yin
 
Misconceptions Of Unit Testing
Misconceptions Of Unit TestingMisconceptions Of Unit Testing
Misconceptions Of Unit TestingTerry Yin
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeTerry Yin
 

Plus de Terry Yin (6)

Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code Analyzer
 
Six Years of Teaching Certified Scrum Developers
Six Years of Teaching Certified Scrum DevelopersSix Years of Teaching Certified Scrum Developers
Six Years of Teaching Certified Scrum Developers
 
Abstraction is a communication tool
Abstraction is a communication toolAbstraction is a communication tool
Abstraction is a communication tool
 
No Reuse Before Use
No Reuse Before UseNo Reuse Before Use
No Reuse Before Use
 
Misconceptions Of Unit Testing
Misconceptions Of Unit TestingMisconceptions Of Unit Testing
Misconceptions Of Unit Testing
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy Code
 

Dernier

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 

Dernier (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 

Programming exercises

  • 4. 4
  • 7. Question Does software company need to train programmers? 7
  • 10. ProjectEuler.net "Project Euler exists to encourage, challenge, and develop the skills and enjoyment of anyone with an interest in the fascinating world of mathematics." • Project Euler is a series of challenging mathematical/computer programming problems that requires more than just mathematical insights to solve. 10
  • 13. Expectations Mathematics lution implementing So rfor mance co d ing Hig h pe So lving problem ind ivi dually 13
  • 14. Example: Quick & Dirty Once you solved a problem, you are entitled to join the forum for this problem.You can share your solution with the other people who have also solved it. And the post usually start with this sentence:! Good coding habit? 14
  • 16. int i, j, k, n, sum;! int factorial[10000];! " • It’s too simple for • • Java, Ruby or Python, for which they can handle big numbers by default." So let’s look at C/C++." Real code copied from the forum. int* getfactorial(int n)! {! factorial[0] = 1;! k = 0;! for(i = 2; i <= n; i++)! {! for(j = 0; j <= k; j++)! factorial[j] *= i;! for(j = 0; j <= k; j++)! {! if(factorial[j] >= 10)! {! factorial[j+1] += (factorial[j] - (factorial[j] % 10)) / 10;! factorial[j] = factorial[j] % 10;! if(j == k)! k++;! }! }! }! return factorial;! }! " int getsum(int* array, int k)! {! sum = 0;! for(i = 0; i <= k; i++)! sum += array[i];! return sum;! }! " int main()! {! int* factorial = getfactorial(n);! sum = getsum(factorial, k);! cout << "nThe sum of the digits of " << n << "! is " << sum << ".n";! return 0;! 16 }!
  • 17. 17
  • 18. Big number again. Ha ha, I did that before! Let me reuse it... 18
  • 19. #include<iostream>! " using namespace std;! " int i, j, k, n, sum;! int factorial[10000];! " Why the big number is named ‘factorial’ in my previous implementation? int* getfactorial(int n)! {! factorial[0] = 1;! k = 0;! for(i = 2; i <= n; i++)! {! for(j = 0; j <= k; j++)! factorial[j] *= i;! for(j = 0; j <= k; j++)! {! if(factorial[j] >= 10)! {! factorial[j+1] += (factorial[j] - (factorial[j] % 10)) / 10;! factorial[j] = factorial[j] % 10;! if(j == k)! k++;! }! }! }! return factorial;! } Where’s the code for big number? 19
  • 20. #include <stdio.h> " #define NUMB 120 #define SIZE 1000000000 " OK, too hard to reuse. It’s not that hard to invent the wheel again. // 9 * 120 = 1080 total digits. // 9 digit numbers int main() { int i = 0; int j = 0; int bigNum1[NUMB]; int bigNum2[NUMB]; int bigNum3[NUMB]; int counter = 0; " " " " } for (i = 0; bigNum1 = bigNum2 = bigNum3 = } i < NUMB; i++) { 0; 0; 0; bigNum1[0] = 1; bigNum2[0] = 1; counter = 2; i = 0; while (i == 0) { counter++; for (j = 0; j < NUMB; j++) { bigNum3[j] = bigNum2[j] + bigNum1[j]; } for (j = 0; j < NUMB-1; j++) { while (bigNum3[j] >= SIZE) { bigNum3[j] -= SIZE; bigNum3[j+1]++; } } if (bigNum3[111] >= 1) break; for (j = 0; j < NUMB; j++) { bigNum1[j] = bigNum2[j]; bigNum2[j] = bigNum3[j]; } } printf("n"); printf("P025 answer = %u", counter); 20
  • 21. s ng exercise i oblem so lv Pr tant skills ver y impor ✓ Train inter view s ten use d a Of ✓ ques tions n skills in i ly also tra ✓ Probab able co de, n ng maintai maki a little. •Don’t do it before bed time... 21
  • 22. What is Cyber Dojo? 22
  • 23. 23
  • 24. 24
  • 25. 25
  • 26. RED - GREEN - REFACTOR 26
  • 28. 28
  • 29. 29
  • 30. 30
  • 32. Code Retreat A day-long practice-intensive event for programmers, ! popularized by Corey Haines. ✓ ✓ ✓ ✓ ✓ ✓ ✓ 32
  • 33. Which One Do You Prefer? 33
  • 36. Poker Hands in ProjectEuler • This is not a very hard problem to solve, • comparing to most of the problems listed in ProjectEuler.net" But it’s hard to solve it without bugs if you don’t have unit tests." • Problem 54 in projectEuler.net 36
  • 38. Why Did I Do It Right With Only One Try? 38
  • 39. TEST(poker_hand, comparing){ CHECK(pokerHand("4H 5C 6S 7S TD")< pokerHand("2C 3S 7S 8D KD")); CHECK(!(pokerHand("4H 5C 6S 7S KD")< pokerHand("2C 3S 7S 8D TD"))); CHECK(pokerHand("4H 5C 6S 7S KD")> pokerHand("2C 3S 7S 8D TD")); } TEST(poker_hand, compare_cards){ CHECK(pokerHand("3H 5C 6S 7S 8D")< pokerHand("4H 5C 6S 7S 9D")); CHECK(pokerHand("3H 5C 6S 7S 9D")< pokerHand("4H 5C 6S 7S TD")); CHECK(pokerHand("3H 5C 6S 7S TD")< pokerHand("4H 5C 6S 7S JD")); CHECK(pokerHand("3H 5C 6S 7S JD")< pokerHand("4H 5C 6S 7S QD")); CHECK(pokerHand("3H 5C 6S 7S QD")< pokerHand("4H 5C 6S 7S KD")); CHECK(pokerHand("3H 5C 6S 7S KD")< pokerHand("4H 5C 6S 7S AD")); } TEST(poker_hand, compare_high_cards_2nd){ CHECK(pokerHand("3H 5C 6S 7S 9D")< pokerHand("3H 5C 6S 8S 9D")); } TEST(poker_hand, compare_high_cards_3nd_4th_5th){ CHECK(pokerHand("3H 5C 6S 8S 9D")< pokerHand("3H 5C 7S 8S 9D")); CHECK(pokerHand("3H 4C 7S 8S 9D")< pokerHand("3H 5C 7S 8S 9D")); CHECK(pokerHand("2H 5C 7S 8S 9D")< pokerHand("3H 5C 7S 8S 9D")); } TEST(poker_hand, compare_high_card_and_one_pair){ CHECK(pokerHand("3H 5C 6S 8S 9D")< pokerHand("3H 5C 7S 8S 8D")); } TEST(poker_hand, compare_one_pairs){ CHECK(pokerHand("3H 5C 6S 9S 9D")> pokerHand("3H 5C 7S 8S 8D")); CHECK(pokerHand("5C 6S 9S 9D KD")> pokerHand("5C 7S 8S 8D AD")); CHECK(pokerHand("5C 6S 9S 9D KD")< pokerHand("5C 7S 9S 9D AD")); } " ... 39
  • 40. Do deliberate exercises. Alone and with the others. Always use good practices. Have fun. 40