programming exercises
DESCRIPTION
This is a motivational talk about practicing programming skills as a professional programmer. I gave the talk many times in many conferences and companies. You can find the video of me giving the talk on this here (in Chinese): http://v.youku.com/v_show/id_XMzI3OTI1MDQw.html I did the talk in English as well, but there's no video online.TRANSCRIPT
![Page 1: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/1.jpg)
Programming Exercises
Terry Yin
![Page 2: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/2.jpg)
Who am I?
�2
![Page 3: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/3.jpg)
Why Programming Exercises?
�3
![Page 4: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/4.jpg)
�4
![Page 5: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/5.jpg)
Quality vs. Quantity
�5
![Page 6: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/6.jpg)
Gamification
�6
![Page 7: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/7.jpg)
Does software company need to train programmers?
�7
Question
![Page 8: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/8.jpg)
�8
vs.
Leonhard Euler Dojo
![Page 9: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/9.jpg)
What is ProjectEuler.net?
�9
![Page 10: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/10.jpg)
ProjectEuler.net
• Project Euler is a series of challenging mathematical/computer programming problems that requires more than just mathematical insights to solve.
�10
"Project Euler exists to encourage, challenge, and develop the skills and enjoyment of anyone with
an interest in the fascinating world of mathematics."
![Page 11: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/11.jpg)
�11
ProjectEuler.net
![Page 12: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/12.jpg)
�12
ProjectEuler.net
![Page 13: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/13.jpg)
Expectations
�13
Mathematics
Solution implementing
High performance coding
Solving problem individually
![Page 14: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/14.jpg)
Example: Quick & Dirty
�14
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?
![Page 15: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/15.jpg)
Another Example
�15
![Page 16: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/16.jpg)
• 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.
�16
int i, j, k, n, sum;!int factorial[10000];!"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;!}!
![Page 17: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/17.jpg)
�17
![Page 18: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/18.jpg)
�18
Big number again. Ha ha, I did that before! Let me reuse it...
![Page 19: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/19.jpg)
#include<iostream>!"using namespace std;!"int i, j, k, n, sum;!int factorial[10000];!"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;!}
�19
Why the big number is named ‘factorial’ in my previous implementation?
Where’s the code for big number?
![Page 20: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/20.jpg)
�20
#include <stdio.h> "#define NUMB 120 // 9 * 120 = 1080 total digits. #define SIZE 1000000000 // 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; i < NUMB; i++) { bigNum1 = 0; bigNum2 = 0; bigNum3 = 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); }
OK, too hard to reuse. It’s not that hard to invent the wheel again.
![Page 21: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/21.jpg)
�21
Problem solving exercises
✓Train very important skills
✓Often used as interview
questions
✓Probably also train skills in
making maintainable code,
a little.
•Don’t do it before bed time...
![Page 22: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/22.jpg)
What is Cyber Dojo?
�22
![Page 23: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/23.jpg)
�23
![Page 24: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/24.jpg)
�24
![Page 25: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/25.jpg)
�25
![Page 26: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/26.jpg)
RED - GREEN - REFACTOR
�26
![Page 27: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/27.jpg)
�27
Unit Test
Integration Test
Functional Test
![Page 28: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/28.jpg)
�28
![Page 29: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/29.jpg)
�29
![Page 30: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/30.jpg)
�30
![Page 31: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/31.jpg)
Refactoring Dojo
�31
![Page 32: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/32.jpg)
Code Retreat
✓✓✓✓✓✓✓
�32
A day-long practice-intensive event for programmers, !popularized by Corey Haines.
![Page 33: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/33.jpg)
Which One Do You Prefer?
�33
![Page 34: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/34.jpg)
Poker Hands
�34
![Page 35: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/35.jpg)
Poker Hands Kata
�35
![Page 36: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/36.jpg)
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
![Page 37: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/37.jpg)
Example: Complicated Transaction
�37
![Page 38: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/38.jpg)
Why Did I Do It Right With Only One Try?
�38
![Page 39: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/39.jpg)
�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")); } "...
![Page 40: Programming exercises](https://reader033.vdocuments.net/reader033/viewer/2022050919/5459aefbaf795953128b4e94/html5/thumbnails/40.jpg)
Do deliberate exercises. Alone and with the others. Always use good practices. Have fun.
�40