22. Methodology of Problems Solving
-
Upload
intro-c-book -
Category
Technology
-
view
27.060 -
download
5
description
Transcript of 22. Methodology of Problems Solving
Methodology of Methodology of Problem SolvingProblem Solving
Efficiently Solving Computer Efficiently Solving Computer Programming Problems Programming Problems
Svetlin NakovSvetlin NakovTelerik Telerik
CorporationCorporationwww.telerik.com
Problems SolvingProblems SolvingFrom Chaotic to Methodological From Chaotic to Methodological
ApproachApproach
How to Solve Problems?How to Solve Problems?
1.1. Read and Analyze the ProblemsRead and Analyze the Problems
2.2. Use a sheet of paper and a pen for Use a sheet of paper and a pen for sketchingsketching
3.3. Think up, invent and try ideasThink up, invent and try ideas
4.4. Break the problem into subproblemsBreak the problem into subproblems
5.5. Check up your ideasCheck up your ideas
6.6. Choose appropriate data structuresChoose appropriate data structures
7.7. Think about the efficiencyThink about the efficiency
8.8. Implement your algorithm step-by-stepImplement your algorithm step-by-step
9.9. Thoroughly test your solutionThoroughly test your solution3
Understanding the Understanding the RequirementsRequirements
Read and Analyze the Read and Analyze the ProblemsProblems
Consider you are at traditional computer Consider you are at traditional computer programming exam or contestprogramming exam or contest
You have 3-4 problems to solve in 4-5 You have 3-4 problems to solve in 4-5 hourshours
First First read carefully all problemsread carefully all problems and try and try to estimate how complex each of them isto estimate how complex each of them is
Read the requirements, don't invent them!Read the requirements, don't invent them! Start solving the most easy problem firstStart solving the most easy problem first
Leave the most complex problem lastLeave the most complex problem last Approach the next problem when the Approach the next problem when the
previous is completely solved and well previous is completely solved and well testedtested 5
Analyzing the ProblemsAnalyzing the Problems Example: we are given 3 problems:Example: we are given 3 problems:
1.1. Shuffle-cardsShuffle-cards
Shuffle a deck of cards in random orderShuffle a deck of cards in random order
2.2. StudentsStudents
Read a set of students and their marks Read a set of students and their marks and print top and print top 1010 students with the best students with the best results (by averaging their marks)results (by averaging their marks)
3.3. Sorting numbersSorting numbers
Sort a set of numbers in increasing orderSort a set of numbers in increasing order
6
Analyzing the Problems Analyzing the Problems (2)(2)
Read carefully the problems and think a Read carefully the problems and think a bit about their possible solutionsbit about their possible solutions
Order the problems from the easiest to Order the problems from the easiest to the most complex:the most complex:
1.1. Sorting numbersSorting numbers Trivial – we can use the built-in sorting Trivial – we can use the built-in sorting
in .NETin .NET
2.2. Shuffle-cardsShuffle-cards Need to randomize the elements of arrayNeed to randomize the elements of array
3.3. StudentsStudents Needs summing, sorting and text file Needs summing, sorting and text file
processingprocessing 7
Using a Using a Paper and Paper and
a Pena PenVisualizing and Visualizing and Sketching your Sketching your
IdeasIdeas
Use a Sheet of Paper Use a Sheet of Paper and a Penand a Pen
Never start solving a problem Never start solving a problem without a sheet of paper and a penwithout a sheet of paper and a pen You need to sketch your ideasYou need to sketch your ideas
Paper and pen is the best Paper and pen is the best visualization toolvisualization tool
Allows your brain to think efficientlyAllows your brain to think efficiently
Paper works fasterPaper works faster than keyboard / screen than keyboard / screen
Other visualization toolOther visualization tool could also work well could also work well
9
Paper and PenPaper and Pen Consider the "cards shuffle" problemConsider the "cards shuffle" problem
We can sketch it to start thinkingWe can sketch it to start thinking
Some ideas immediately come, e.g.Some ideas immediately come, e.g. Split the deck into two parts and swap Split the deck into two parts and swap
them a multiple timesthem a multiple times Swap 2 random cards a random number Swap 2 random cards a random number
of timesof times Swap each card with a random cardSwap each card with a random card 10
Invent IdeasInvent IdeasThink-up, Invent Ideas and Check ThemThink-up, Invent Ideas and Check Them
Think up, Invent and Think up, Invent and Try IdeasTry Ideas
First take an example of the problemFirst take an example of the problem Sketch it on the sheet of paperSketch it on the sheet of paper
Next try to invent some idea that works Next try to invent some idea that works for your examplefor your example
Check if your idea will work for other Check if your idea will work for other examplesexamples Try to find a case that breaks your ideaTry to find a case that breaks your idea
Try challenging examples and unusual Try challenging examples and unusual casescases
If you find your idea incorrect, try to fix If you find your idea incorrect, try to fix it or just invent a new ideait or just invent a new idea
12
Invent and Try Ideas – Invent and Try Ideas – ExampleExample
Consider the "cards shuffle" problemConsider the "cards shuffle" problem Idea #1: random number of times split Idea #1: random number of times split
the deck into left and right part and the deck into left and right part and swap themswap them How to represent the cards?How to represent the cards?
How to chose a random split point?How to chose a random split point?
How to perform the exchange?How to perform the exchange? Idea #2: swap each card with a random Idea #2: swap each card with a random
cardcard How many times to repeat this?How many times to repeat this?
Is this fast enough?Is this fast enough? 13
Invent and Try Invent and Try Ideas – Example Ideas – Example
(2)(2) Idea #3: swap 2 random cards a Idea #3: swap 2 random cards a
random number of timesrandom number of times How to choose two random cards?How to choose two random cards?
How many times repeat this?How many times repeat this? Idea #4: choose a random card and Idea #4: choose a random card and
insert it in front of the deckinsert it in front of the deck How to choose random card?How to choose random card?
How many times repeat this?How many times repeat this?
14
Divide and ConquerDivide and ConquerDecompose Problems into Manageable Decompose Problems into Manageable
PiecesPieces
Decompose the Decompose the ProblemProblem
into Subproblemsinto Subproblems Work decomposition is natural in Work decomposition is natural in
engineeringengineering It happens every day in the industryIt happens every day in the industry
Projects are decomposed into subprojectsProjects are decomposed into subprojects Complex problems could be decomposed Complex problems could be decomposed
into several smaller subproblemsinto several smaller subproblems Technique known as "Divide and Technique known as "Divide and
Conquer"Conquer"
Small problems could easily be solvedSmall problems could easily be solved
Smaller subproblems could be further Smaller subproblems could be further decomposed as welldecomposed as well 16
Divide and Conquer – Divide and Conquer – ExampleExample
Let's try idea #1:Let's try idea #1: Multiple time split the deck into left and right Multiple time split the deck into left and right
part and swap thempart and swap them Divide and conquerDivide and conquer
Subproblem #1 (single exchange) – split the Subproblem #1 (single exchange) – split the deck into two random parts and exchange deck into two random parts and exchange themthem
Subproblem #2 – choosing a random split Subproblem #2 – choosing a random split pointpoint
Subproblem #3 – combining single exchangesSubproblem #3 – combining single exchanges
How many times to perform "single exchange"?How many times to perform "single exchange"?
17
Subproblem #1 (Single Subproblem #1 (Single Exchange)Exchange)
Split the deck into 2 parts at Split the deck into 2 parts at random split point and exchange random split point and exchange these 2 partsthese 2 parts We visualize this by paper and pen:We visualize this by paper and pen:
18
Subproblem #2 (Random Split
Point) Choosing a random split pointChoosing a random split point
Needs to understand the concept of Needs to understand the concept of pseudo-random numbers and how to pseudo-random numbers and how to use ituse it
In Internet lots of examples are In Internet lots of examples are available, some of them incorrectavailable, some of them incorrect
The class The class System.RandomSystem.Random can do the job can do the job Important detail is that the Important detail is that the RandomRandom
class should be instantiated only onceclass should be instantiated only once Not at each random number generationNot at each random number generation
19
Subproblem #3 (Combining Single
Exchanges) Combining a sequence of single Combining a sequence of single
exchanges to solve the initial problemexchanges to solve the initial problem
How many times to perform single How many times to perform single exchanges to reliably randomize the exchanges to reliably randomize the deck?deck?
N times (where N is the number of the N times (where N is the number of the cards) seems enoughcards) seems enough
We have an algorithm:We have an algorithm: N times split at random position and N times split at random position and
exchange the left and right parts of exchange the left and right parts of the deckthe deck 20
Check-up Your IdeasCheck-up Your IdeasDon't go Ahead before Checking Your Don't go Ahead before Checking Your
IdeasIdeas
Check-up Your IdeasCheck-up Your Ideas Check-up your ideas with examplesCheck-up your ideas with examples
It is better to find a problem before the It is better to find a problem before the idea is implementedidea is implemented
When the code is written, changing When the code is written, changing radically your ideas costs a lot of time radically your ideas costs a lot of time and effortand effort
Carefully select examples for check-upCarefully select examples for check-up Examples should be simple enough to be Examples should be simple enough to be
checked by hand in a minutechecked by hand in a minute
Examples should be complex enough to Examples should be complex enough to cover the most general case, not just an cover the most general case, not just an isolated caseisolated case 22
Check-up Your Ideas – Check-up Your Ideas – ExampleExample
Let's check the idea:Let's check the idea:
After 3 random splits and swaps we After 3 random splits and swaps we obtain the start position obtain the start position seems seems like a bug!like a bug! 23
Invent New Idea If Invent New Idea If NeededNeeded
What to do when you find your idea is not What to do when you find your idea is not working in all cases?working in all cases? Try to fix your ideaTry to fix your idea
Sometime a small change could fix the Sometime a small change could fix the problemproblem
Invent new idea and carefully check itInvent new idea and carefully check it IterateIterate
It is usual that your first idea is not the It is usual that your first idea is not the bestbest
Invent ideas, check them, try various Invent ideas, check them, try various cases, find problems, fix them, invent cases, find problems, fix them, invent better idea, etc.better idea, etc. 24
Invent New Ideas – Invent New Ideas – ExampleExample
Invent few new ideas:Invent few new ideas: New idea #1 – multiple times select 2 New idea #1 – multiple times select 2
random cards and exchange themrandom cards and exchange them
New idea #2 – multiple times select a New idea #2 – multiple times select a random card and exchange it with the random card and exchange it with the first cardfirst card
New idea #3 – multiple times select a New idea #3 – multiple times select a random card and move it to an random card and move it to an external listexternal list
Let's check the new idea #2Let's check the new idea #2 Is it correct?Is it correct? 25
Check-up the New Idea – Check-up the New Idea – ExampleExample
26
The The result result seems seems correctcorrect
Think about Data Think about Data StructuresStructuresSelect Data Structures that Will Work Select Data Structures that Will Work
WellWell
Choosing AppropriateChoosing AppropriateData StructuresData Structures
Choose appropriate data structures Choose appropriate data structures before the start of codingbefore the start of coding Think how to represent input dataThink how to represent input data Think how to represent intermediate Think how to represent intermediate
program stateprogram state Think how to represent the requested Think how to represent the requested
outputoutput You could find that your idea cannot You could find that your idea cannot
be implemented efficientlybe implemented efficiently Or implementation will be very Or implementation will be very
complex or inefficientcomplex or inefficient28
Choose Choose Appropriate Data Appropriate Data
Structures – Structures – ExampleExample How to represent a single card?How to represent a single card?
The best idea is to create a structure The best idea is to create a structure CardCard
FaceFace – could be – could be stringstring, , intint or or enumerationenumeration
SuitSuit – enumeration – enumeration
How to represent a deck of cards?How to represent a deck of cards? Array – Array – Card[]Card[]
Indexed list – Indexed list – List<Card>List<Card>
SetSet / / DictionaryDictionary / / QueueQueue / / StackStack – not a fit – not a fit 29
Efficiency and Efficiency and PerformancePerformance
Is Your Algorithm Fast Enough?Is Your Algorithm Fast Enough?
Think About the Think About the EfficiencyEfficiency
Think about efficiency before Think about efficiency before writing the first line of codewriting the first line of code Estimate the running time Estimate the running time
(asymptotic complexity)(asymptotic complexity) Check the requirementsCheck the requirements
Will your algorithm be fast enough to Will your algorithm be fast enough to conform with themconform with them
You don't want to implement your You don't want to implement your algorithm and find that it is slow algorithm and find that it is slow when testingwhen testing You will lose your timeYou will lose your time 31
Efficiency is not Always Efficiency is not Always RequiredRequired
Best solution is sometimes just not Best solution is sometimes just not neededneeded
Read carefully your problem statementRead carefully your problem statement
Sometimes ugly solution could work for Sometimes ugly solution could work for your requirements and it will cost you your requirements and it will cost you less timeless time
Example: if you need to sort Example: if you need to sort nn numbers, numbers, any algorithm will work when any algorithm will work when nn ∈∈ [0..500][0..500]
Implement complex algorithms only Implement complex algorithms only when the problem really needs themwhen the problem really needs them 32
Efficiency – ExampleEfficiency – Example How much cards we have?How much cards we have?
In a typical deck we have 52 cardsIn a typical deck we have 52 cards
No matter how fast the algorithm is – it No matter how fast the algorithm is – it will work fast enoughwill work fast enough
If we have N cards, we perform N If we have N cards, we perform N swaps swaps the expected running time is the expected running time is O(N)O(N)
O(N) will work fast for millions of O(N) will work fast for millions of cardscards
Conclusion: the efficiency is not an Conclusion: the efficiency is not an issue in this problemissue in this problem 33
ImplementationImplementationCoding and Testing Step-by-StepCoding and Testing Step-by-Step
Start Coding: Check Start Coding: Check ListList
Never start coding before you find correct Never start coding before you find correct idea that will meet the requirementsidea that will meet the requirements What you will write before you invent a What you will write before you invent a
correct idea to solve the problem?correct idea to solve the problem? Checklist to follow before start of coding:Checklist to follow before start of coding:
Ensure you understand well the requirementsEnsure you understand well the requirements Ensure you have invented a good ideaEnsure you have invented a good idea Ensure your idea is correctEnsure your idea is correct Ensure you know what data structures to useEnsure you know what data structures to use Ensure the performance will be sufficientEnsure the performance will be sufficient
35
Coding Check List – Coding Check List – ExampleExample
Checklist before start of coding:Checklist before start of coding: Ensure you understand well the Ensure you understand well the
requirementsrequirements Yes, shuffle given deck of cardsYes, shuffle given deck of cards
Ensure you have invented a correct ideaEnsure you have invented a correct idea Yes, the idea seems correct and is testedYes, the idea seems correct and is tested
Ensure you know what data structures to Ensure you know what data structures to useuse Class Class CardCard, enumeration , enumeration SuitSuit and and List<Card>List<Card>
Ensure the performance will be sufficientEnsure the performance will be sufficient Linear running time Linear running time good performance good performance 36
Implement yourImplement yourAlgorithm Step-by-StepAlgorithm Step-by-Step
"Step-by-step" approach is always "Step-by-step" approach is always better than "build all, then test"better than "build all, then test" Implement a piece of your program and Implement a piece of your program and
test ittest it
Than implement another piece of the Than implement another piece of the program and test itprogram and test it
Finally put together all pieces and test Finally put together all pieces and test itit
Small increments (steps) reveal errors Small increments (steps) reveal errors earlyearly "Big-boom" integration takes more time"Big-boom" integration takes more time 37
Step #1 – Class CardStep #1 – Class Card
38
class Card class Card
{{
public string Face { get; set; }public string Face { get; set; }
public Suit Suit { get; set; }public Suit Suit { get; set; }
public override string ToString() public override string ToString()
{{
String card = "(" + this.Face + " " + this.Suit String card = "(" + this.Face + " " + this.Suit
+")";+")";
return card;return card;
}}
} }
enum Suit enum Suit
{{
CLUB, DIAMOND, HEART, SPADECLUB, DIAMOND, HEART, SPADE
}}
Testing the class Testing the class CardCard to get to get feedback as early as possible:feedback as early as possible:
The result is as expected:The result is as expected:
Step #1 – TestStep #1 – Test
39
static void Main()static void Main()
{{
Card card = new Card() { Face="A", Suit=Suit.CLUB };Card card = new Card() { Face="A", Suit=Suit.CLUB };
Console.WriteLine(card);Console.WriteLine(card);
}}
(A CLUB)(A CLUB)
Step #2 – Create and Step #2 – Create and Print a Deck of CardsPrint a Deck of Cards
40
static void Main()static void Main(){{ List<Card> cards = new List<Card>();List<Card> cards = new List<Card>(); cards.Add(new Card() { Face = "7", Suit = cards.Add(new Card() { Face = "7", Suit = Suit.HEART });Suit.HEART }); cards.Add(new Card() { Face = "A", Suit = cards.Add(new Card() { Face = "A", Suit = Suit.SPADE });Suit.SPADE }); cards.Add(new Card() { Face = "10", Suit = cards.Add(new Card() { Face = "10", Suit = Suit.DIAMOND });Suit.DIAMOND }); cards.Add(new Card() { Face = "2", Suit = cards.Add(new Card() { Face = "2", Suit = Suit.CLUB });Suit.CLUB }); cards.Add(new Card() { Face = "6", Suit = cards.Add(new Card() { Face = "6", Suit = Suit.DIAMOND });Suit.DIAMOND }); cards.Add(new Card() { Face = "J", Suit = cards.Add(new Card() { Face = "J", Suit = Suit.CLUB });Suit.CLUB }); PrintCards(cards);PrintCards(cards);}}static void PrintCards(List<Card> cards) static void PrintCards(List<Card> cards) {{ foreach (Card card in cards) foreach (Card card in cards) Console.Write(card);Console.Write(card); Console.WriteLine();Console.WriteLine();}}
Testing the deck of cards seems to Testing the deck of cards seems to be working correctly:be working correctly:
Step #2 – TestStep #2 – Test
41
(7 HEART)(A SPADE)(10 DIAMOND)(2 CLUB)(6 DIAMOND)(J (7 HEART)(A SPADE)(10 DIAMOND)(2 CLUB)(6 DIAMOND)(J
CLUB)CLUB)
Step #3 – Single Step #3 – Single ExchangeExchange
42
static void PerformSingleExchange(List<Card> cards)static void PerformSingleExchange(List<Card> cards)
{{
Random rand = new Random();Random rand = new Random();
int randomIndex = rand.Next(1, cards.Count - 1);int randomIndex = rand.Next(1, cards.Count - 1);
Card firstCard = cards[1];Card firstCard = cards[1];
Card randomCard = cards[randomIndex];Card randomCard = cards[randomIndex];
cards[1] = randomCard;cards[1] = randomCard;
cards[randomIndex] = firstCard;cards[randomIndex] = firstCard;
}}
To test the single exchange we use To test the single exchange we use the following code:the following code:
The result is unexpected:The result is unexpected:
Step #3 – TestStep #3 – Test
43
static void Main()static void Main(){{ List<Card> cards = new List<Card>();List<Card> cards = new List<Card>(); cards.Add(new Card() { Face = "2", Suit = cards.Add(new Card() { Face = "2", Suit = Suit.CLUB });Suit.CLUB }); cards.Add(new Card() { Face = "3", Suit = Suit.HEART cards.Add(new Card() { Face = "3", Suit = Suit.HEART });}); cards.Add(new Card() { Face = "4", Suit = Suit.SPADE cards.Add(new Card() { Face = "4", Suit = Suit.SPADE });}); PerformSingleExchange(cards);PerformSingleExchange(cards); PrintCards(cards);PrintCards(cards);}}
(2 CLUB)(3 HEART)(4 SPADE)(2 CLUB)(3 HEART)(4 SPADE)
The first element of list is at index The first element of list is at index 0, not 1:0, not 1:
The result is again incorrect (3 The result is again incorrect (3 times the same):times the same):
Step #3 – Fix Bug and Step #3 – Fix Bug and TestTest
44
static void PerformSingleExchange(List<Card> cards)static void PerformSingleExchange(List<Card> cards){{ Random rand = new Random();Random rand = new Random(); int randomIndex = rand.Next(1, cards.Count - 1);int randomIndex = rand.Next(1, cards.Count - 1); Card firstCard = cards[0];Card firstCard = cards[0]; Card randomCard = cards[randomIndex];Card randomCard = cards[randomIndex]; cards[0] = randomCard;cards[0] = randomCard; cards[randomIndex] = firstCard;cards[randomIndex] = firstCard;}}
(3 HEART)(2 CLUB)(4 SPADE)(3 HEART)(2 CLUB)(4 SPADE)(3 HEART)(2 CLUB)(4 SPADE)(3 HEART)(2 CLUB)(4 SPADE)(3 HEART)(2 CLUB)(4 SPADE)(3 HEART)(2 CLUB)(4 SPADE)
Random.Next()Random.Next() has exclusive end has exclusive end range:range:
The result now seems correct:The result now seems correct:
Step #3 – Fix Again Step #3 – Fix Again and Testand Test
45
static void PerformSingleExchange(List<Card> cards)static void PerformSingleExchange(List<Card> cards){{ Random rand = new Random();Random rand = new Random(); int randomIndex = rand.Next(1, cards.Count);int randomIndex = rand.Next(1, cards.Count); Card firstCard = cards[0];Card firstCard = cards[0]; Card randomCard = cards[randomIndex];Card randomCard = cards[randomIndex]; cards[0] = randomCard;cards[0] = randomCard; cards[randomIndex] = firstCard;cards[randomIndex] = firstCard;}}
(3 HEART)(2 CLUB)(4 SPADE)(3 HEART)(2 CLUB)(4 SPADE)(4 SPADE)(3 HEART)(2 CLUB)(4 SPADE)(3 HEART)(2 CLUB)(4 SPADE)(3 HEART)(2 CLUB)(4 SPADE)(3 HEART)(2 CLUB)
Shuffle the entire deck of cards:Shuffle the entire deck of cards:
The result is surprisingly incorrect:The result is surprisingly incorrect:
Step #4 – Shuffle the Step #4 – Shuffle the DeckDeck
46
static void ShuffleCards(List<Card> cards)static void ShuffleCards(List<Card> cards){{ for (int i = 1; i <= cards.Count; i++)for (int i = 1; i <= cards.Count; i++) {{ PerformSingleExchange(cards);PerformSingleExchange(cards); }}}}
Initial deck: (7 HEART)(A SPADE)(10 DIAMOND)(2 CLUB)Initial deck: (7 HEART)(A SPADE)(10 DIAMOND)(2 CLUB)(6 DIAMOND)(J CLUB)(6 DIAMOND)(J CLUB)After shuffle: (7 HEART)(A SPADE)(10 DIAMOND)(2 CLUB)After shuffle: (7 HEART)(A SPADE)(10 DIAMOND)(2 CLUB)(6 DIAMOND)(J CLUB)(6 DIAMOND)(J CLUB)
When we step through the code with When we step through the code with the debugger, the result seems correct:the debugger, the result seems correct:
Without the debugger the result is Without the debugger the result is wrong!wrong!
Step #4 – Strange BugStep #4 – Strange Bug
47
Initial deck: (7 HEART)(A SPADE)(10 DIAMOND)(2 CLUB)Initial deck: (7 HEART)(A SPADE)(10 DIAMOND)(2 CLUB)(6 DIAMOND)(J CLUB)(6 DIAMOND)(J CLUB)After shuffle: (10 DIAMOND)(7 HEART)(A SPADE)(J CLUB)After shuffle: (10 DIAMOND)(7 HEART)(A SPADE)(J CLUB)(2 CLUB)(6 DIAMOND)(2 CLUB)(6 DIAMOND)
RandomRandom should be instantiated only should be instantiated only once:once:
The result finally is correct with The result finally is correct with and without the debuggerand without the debugger
Step #4 – Fix Again Step #4 – Fix Again and Testand Test
48
private static Random rand = new Random();private static Random rand = new Random();static void PerformSingleExchange(List<Card> static void PerformSingleExchange(List<Card> cards)cards){{ int randomIndex = rand.Next(1, cards.Count);int randomIndex = rand.Next(1, cards.Count); Card firstCard = cards[0];Card firstCard = cards[0]; Card randomCard = cards[randomIndex];Card randomCard = cards[randomIndex]; cards[0] = randomCard;cards[0] = randomCard; cards[randomIndex] = firstCard;cards[randomIndex] = firstCard;}}
TestingTestingThoroughly Test Your SolutionThoroughly Test Your Solution
Thoroughly Test your Thoroughly Test your SolutionSolution
Wise software engineers say that:Wise software engineers say that: Inventing a good idea and implementing Inventing a good idea and implementing
it is half of the solutionit is half of the solution Testing is the second half of the Testing is the second half of the
solutionsolution Always test thoroughly your solutionAlways test thoroughly your solution
Invest in testingInvest in testing One 100% solved problem is better than One 100% solved problem is better than
2 or 3 partially solved2 or 3 partially solved Testing existing problem takes less Testing existing problem takes less
time than solving another problem from time than solving another problem from scratchscratch 50
How to Test?How to Test? Testing could not certify absence Testing could not certify absence
of defectsof defects It just reduces the defects rateIt just reduces the defects rate
Well tested solutions are more Well tested solutions are more likely to be correctlikely to be correct
Start testing with a good Start testing with a good representative of the general caserepresentative of the general case Not a small isolated caseNot a small isolated case
Large and complex test, butLarge and complex test, but
Small enough to be easily checkableSmall enough to be easily checkable 51
How to Test? (2)How to Test? (2) Test the border casesTest the border cases
E.g. if E.g. if nn ∈∈ [ [00....500500] ] try try nn==00 , , nn==11, , nn==22, , nn==499499, , nn==500500
If a bug is found, repeat all tests If a bug is found, repeat all tests after fixing it to avoid regressionsafter fixing it to avoid regressions
Run a load testRun a load test How to be sure that your algorithm is How to be sure that your algorithm is
fast enough to meet the fast enough to meet the requirements?requirements?
Use copy-pasting to generate large Use copy-pasting to generate large test datatest data 52
Read the Problem Read the Problem StatementStatement
Read carefully the problem statementRead carefully the problem statement Does your solution print exactly what is Does your solution print exactly what is
expected?expected? Does your output follow the requested Does your output follow the requested
format?format? Did you remove your debug printouts?Did you remove your debug printouts?
Be sure to solve the requested problem, Be sure to solve the requested problem, not the problem you think is requested!not the problem you think is requested! Example: "Write a program to print the Example: "Write a program to print the
number of permutations on n elements" number of permutations on n elements" means to print a single number, not a set means to print a single number, not a set of permutations!of permutations!
53
Testing – ExampleTesting – Example Test with full deck of 52 cardsTest with full deck of 52 cards
Serious error found Serious error found change the change the algorithmalgorithm
Change the algorithmChange the algorithm Exchange the first card with a random card Exchange the first card with a random card
exchange cards 0, 1, …, N-1 with a exchange cards 0, 1, …, N-1 with a random cardrandom card
Test whether the new algorithm worksTest whether the new algorithm works Test with 1 cardTest with 1 card Test with 2 cardsTest with 2 cards Test with 0 cardsTest with 0 cards Load test with 52 000 cardsLoad test with 52 000 cards 54
Test with 52 Cards – Test with 52 Cards – ExampleExample
55
static void TestShuffle52Cards()static void TestShuffle52Cards()
{{
List<Card> cards = new List<Card>();List<Card> cards = new List<Card>();
string[] allFaces = new string[] { "2", "3", "4", "5", string[] allFaces = new string[] { "2", "3", "4", "5",
"6","6",
"7", "8", "9", "10", "J", "Q", "K", "A" };"7", "8", "9", "10", "J", "Q", "K", "A" };
Suit[] allSuits = new Suit[] { Suit.CLUB, Suit[] allSuits = new Suit[] { Suit.CLUB,
Suit.DIAMOND, Suit.DIAMOND,
Suit.HEART, Suit.SPADE };Suit.HEART, Suit.SPADE };
foreach (string face in allFaces)foreach (string face in allFaces)
foreach (Suit suit in allSuits)foreach (Suit suit in allSuits)
{{
Card card = new Card() { Face = face, Suit = Card card = new Card() { Face = face, Suit =
suit };suit };
cards.Add(card);cards.Add(card);
}}
ShuffleCards(cards);ShuffleCards(cards);
PrintCards(cards);PrintCards(cards);
}}
The result is surprising:The result is surprising:
Half of the cards keep their initial Half of the cards keep their initial positionspositions We have serious problem – the We have serious problem – the
randomization algorithm is not reliablerandomization algorithm is not reliable
Test with 52 Cards – Test with 52 Cards – Example (2)Example (2)
56
(4 DIAMOND)(4 DIAMOND)(2 DIAMOND)(2 DIAMOND)(6 HEART)(6 HEART)(2 SPADE)(2 SPADE)(A SPADE)(7 (A SPADE)(7
SPADE)SPADE)(3 DIAMOND)(3 SPADE)(4 SPADE)(4 HEART)(3 DIAMOND)(3 SPADE)(4 SPADE)(4 HEART)(6 CLUB)(K (6 CLUB)(K
HEART)HEART)(5 CLUB)(5 DIAMOND)(5 HEART)(5 CLUB)(5 DIAMOND)(5 HEART)(A HEART)(9 CLUB)(10 (A HEART)(9 CLUB)(10
CLUB)(A CLUB)CLUB)(A CLUB)(6 SPADE)(7 CLUB)(7 DIAMOND)(6 SPADE)(7 CLUB)(7 DIAMOND)(3 CLUB)(9 (3 CLUB)(9
HEART)HEART)(8 CLUB)(8 CLUB)(3 HEART)(3 HEART)(9 SPADE)(9 SPADE)(4 CLUB)(8 HEART)(4 CLUB)(8 HEART)(9 (9
DIAMOND)DIAMOND)(5 SPADE)(8 DIAMOND)(J HEART)(5 SPADE)(8 DIAMOND)(J HEART)(10 DIAMOND)(10 (10 DIAMOND)(10
HEART)(10 SPADE)HEART)(10 SPADE)(Q HEART)(2 CLUB)(Q HEART)(2 CLUB)(J CLUB)(J SPADE)(Q (J CLUB)(J SPADE)(Q
CLUB)CLUB)(7 HEART)(2 HEART)(Q SPADE)(7 HEART)(2 HEART)(Q SPADE)(K CLUB)(K CLUB)(J DIAMOND)(6 (J DIAMOND)(6
DIAMOND)DIAMOND)(K SPADE)(K SPADE)(8 SPADE)(8 SPADE)(A DIAMOND)(A DIAMOND)(Q DIAMOND)(K (Q DIAMOND)(K
DIAMOND)DIAMOND)
Fixing the AlgorithmFixing the Algorithm New idea that slightly changes the algorithm:New idea that slightly changes the algorithm:
Exchange the first card with a random card Exchange the first card with a random card exchange cards 0, 1, …, N-1 with a random cardexchange cards 0, 1, …, N-1 with a random card
57
static void PerformSingleExchange(List<Card> cards, int static void PerformSingleExchange(List<Card> cards, int index)index){{ int randomIndex = rand.Next(1, cards.Count);int randomIndex = rand.Next(1, cards.Count); Card firstCard = cards[index];Card firstCard = cards[index]; cards[index] = cards[randomIndex];cards[index] = cards[randomIndex]; cards[randomIndex] = firstCard;cards[randomIndex] = firstCard;}}static void ShuffleCards(List<Card> cards)static void ShuffleCards(List<Card> cards){{ for (int i = 0; i < cards.Count; i++)for (int i = 0; i < cards.Count; i++) PerformSingleExchange(cards, i);PerformSingleExchange(cards, i);}}
The result now seems correct:The result now seems correct:
Cards are completely randomizedCards are completely randomized
Test with 52 Cards Test with 52 Cards (Again)(Again)
58
(9 HEART)(5 CLUB)(3 CLUB)(7 SPADE)(6 CLUB)(5 SPADE)(6 (9 HEART)(5 CLUB)(3 CLUB)(7 SPADE)(6 CLUB)(5 SPADE)(6
HEART) (4 CLUB)(10 CLUB)(3 SPADE)(K DIAMOND)(10 HEART)HEART) (4 CLUB)(10 CLUB)(3 SPADE)(K DIAMOND)(10 HEART)
(8 CLUB)(A CLUB)(J DIAMOND)(K SPADE)(9 SPADE)(7 CLUB)(8 CLUB)(A CLUB)(J DIAMOND)(K SPADE)(9 SPADE)(7 CLUB)
(10 DIAMOND)(9 DIAMOND)(8 HEART)(6 DIAMOND)(8 SPADE)(5 (10 DIAMOND)(9 DIAMOND)(8 HEART)(6 DIAMOND)(8 SPADE)(5
DIAMOND)(4 HEART) (10 SPADE)(J CLUB)(Q SPADE)(9 CLUB)DIAMOND)(4 HEART) (10 SPADE)(J CLUB)(Q SPADE)(9 CLUB)
(J HEART)(K CLUB)(2 HEART) (7 HEART)(A HEART)(3 (J HEART)(K CLUB)(2 HEART) (7 HEART)(A HEART)(3
DIAMOND)(K HEART)(A SPADE)(8 DIAMOND)(4 SPADE)(3 HEART)DIAMOND)(K HEART)(A SPADE)(8 DIAMOND)(4 SPADE)(3 HEART)
(5 HEART)(Q HEART)(4 DIAMOND)(2 SPADE)(A DIAMOND)(2 (5 HEART)(Q HEART)(4 DIAMOND)(2 SPADE)(A DIAMOND)(2
DIAMOND)(J SPADE)(7 DIAMOND)(Q DIAMOND)(2 CLUB)DIAMOND)(J SPADE)(7 DIAMOND)(Q DIAMOND)(2 CLUB)
(6 SPADE)(Q CLUB)(6 SPADE)(Q CLUB)
Test with 1 CardTest with 1 Card
59
static void TestShuffleOneCard()static void TestShuffleOneCard()
{{
List<Card> cards = new List<Card>();List<Card> cards = new List<Card>();
cards.Add(new Card() { Face = "A", Suit = cards.Add(new Card() { Face = "A", Suit =
Suit.CLUB });Suit.CLUB });
CardsShuffle.ShuffleCards(cards);CardsShuffle.ShuffleCards(cards);
CardsShuffle.PrintCards(cards);CardsShuffle.PrintCards(cards);
}}
Unhandled Exception: Unhandled Exception:
System.ArgumentOutOfRangeException: Index was out of System.ArgumentOutOfRangeException: Index was out of
range. Must be non-negative and less than the size of range. Must be non-negative and less than the size of
the collection. Parameter name: indexthe collection. Parameter name: index
……
Create a method to test with 1 card:Create a method to test with 1 card:
We found a bug:We found a bug:
Test with 1 Card – Bug Test with 1 Card – Bug FixingFixing
60
static void ShuffleCards(List<Card> cards)static void ShuffleCards(List<Card> cards)
{{
if (cards.Count > 1)if (cards.Count > 1)
{{
for (int i = 0; i < cards.Count; i++)for (int i = 0; i < cards.Count; i++)
{{
PerformSingleExchange(cards, i);PerformSingleExchange(cards, i);
}}
}}
}}
We take 1 card are special case:We take 1 card are special case:
Test shows that the problem is fixedTest shows that the problem is fixed
Test with 2 CardsTest with 2 Cards
61
static void TestShuffleTwoCards()static void TestShuffleTwoCards(){{ List<Card> cards = new List<Card>();List<Card> cards = new List<Card>(); cards.Add(new Card() { Face = "A", Suit = cards.Add(new Card() { Face = "A", Suit = Suit.CLUB });Suit.CLUB }); cards.Add(new Card() { Face = "3", Suit = cards.Add(new Card() { Face = "3", Suit = Suit.DIAMOND });Suit.DIAMOND }); CardsShuffle.ShuffleCards(cards);CardsShuffle.ShuffleCards(cards); CardsShuffle.PrintCards(cards);CardsShuffle.PrintCards(cards);}}
(3 DIAMOND)(A CLUB)(3 DIAMOND)(A CLUB)
Create a method to test with 2 cards:Create a method to test with 2 cards:
Bug: sequential executions get the Bug: sequential executions get the same result:same result:
The problem: the first and the second The problem: the first and the second cards always exchange each other cards always exchange each other exactly onceexactly once
Test with 2 Cards – Bug Test with 2 Cards – Bug FixingFixing
62
static void PerformSingleExchange(List<Card> cards, int static void PerformSingleExchange(List<Card> cards, int
index)index)
{{
int randomIndex = rand.Next(0, cards.Count);int randomIndex = rand.Next(0, cards.Count);
Card firstCard = cards[index];Card firstCard = cards[index];
Card randomCard = cards[randomIndex];Card randomCard = cards[randomIndex];
cards[index] = randomCard;cards[index] = randomCard;
cards[randomIndex] = firstCard;cards[randomIndex] = firstCard;
}}
We allow each card to be exchanged with We allow each card to be exchanged with any other random card, including itselfany other random card, including itself
Test shows that the problem is fixedTest shows that the problem is fixed
Test with Test with 00 Cards; Cards; Regression TestsRegression Tests
63
Testing with Testing with 00 cards (empty list) cards (empty list) generates an empty list generates an empty list correct correct resultresult
Seems like the cards shuffle algorithm Seems like the cards shuffle algorithm works correctly after the last few fixesworks correctly after the last few fixes
Needs a regression testNeeds a regression test Test again that new changes did not Test again that new changes did not
break all previously working casesbreak all previously working cases Test with full deck of 52 cards; with 1 Test with full deck of 52 cards; with 1
card; with 2 cards with card; with 2 cards with 00 cards cards everything workseverything works
Load Test – 52 000 Load Test – 52 000 CardsCards
Finally we need a load test with 52 Finally we need a load test with 52 000 cards:000 cards:
64
static void TestShuffle52000Cards()static void TestShuffle52000Cards()
{{
List<Card> cards = new List<Card>();List<Card> cards = new List<Card>();
string[] allFaces = new string[] {"2", "3", "4", "5",string[] allFaces = new string[] {"2", "3", "4", "5",
"6", "7", "8", "9", "10", "J", "Q", "K", "A"};"6", "7", "8", "9", "10", "J", "Q", "K", "A"};
Suit[] allSuits = new Suit[] { Suit.CLUB, Suit[] allSuits = new Suit[] { Suit.CLUB,
Suit.DIAMOND, Suit.DIAMOND,
Suit.HEART, Suit.SPADE};Suit.HEART, Suit.SPADE};
for (int i = 0; i < 1000; i++)for (int i = 0; i < 1000; i++)
foreach (string face in allFaces)foreach (string face in allFaces)
foreach (Suit suit in allSuits)foreach (Suit suit in allSuits)
cards.Add(new Card() { Face = face, Suit = suit cards.Add(new Card() { Face = face, Suit = suit
});});
ShuffleCards(cards);ShuffleCards(cards);
PrintCards(cards);PrintCards(cards);
}}
SummarySummary1.1. Problems solving needs methodology:Problems solving needs methodology:
Understanding and analyzing problemsUnderstanding and analyzing problems Using a sheet of paper and a pen for Using a sheet of paper and a pen for
sketchingsketching Thinking up, inventing and trying ideasThinking up, inventing and trying ideas Decomposing problems into subproblemsDecomposing problems into subproblems Selecting appropriate data structuresSelecting appropriate data structures Thinking about the efficiency and Thinking about the efficiency and
performanceperformance Implementing step-by-stepImplementing step-by-step Testing the nominal case, border cases Testing the nominal case, border cases
and efficiencyand efficiency65
Methodology of Methodology of Problem Problem SolvingSolving
Questions?Questions?
http://academy.telerik.com
ExercisesExercises1.1. Following the problems Following the problems
solving methodology try solving methodology try to approach the following to approach the following problem: We are given N problem: We are given N points (N < points (N < 100100 000000) in the ) in the plane (represented as plane (represented as integer coordinates integer coordinates xxii and and yyii). Write a program to ). Write a program to find all possible horizontal find all possible horizontal or vertical straight lines or vertical straight lines that split the plane into that split the plane into two parts, each holding two parts, each holding equal number of points equal number of points (points at the line are not (points at the line are not counted).counted).
67
Exercises (2)Exercises (2)
2.2. Following the problems solving methodology Following the problems solving methodology solve the following problem: we are given a set solve the following problem: we are given a set SS of of nn integer numbers and a positive integer integer numbers and a positive integer kk ((kk ≤ ≤ nn ≤ 10). Alternating sequence of numbers ≤ 10). Alternating sequence of numbers is a sequence that alternatively changes from is a sequence that alternatively changes from increasing to decreasing of vice versa after increasing to decreasing of vice versa after each of its elements. Write a program to each of its elements. Write a program to generate all alternating sequences generate all alternating sequences ss11, , ss22, …, , …, sskk
consisting of consisting of kk distinct elements from distinct elements from SS..
Example: S = { 2, 5, 3, 4 } Example: S = { 2, 5, 3, 4 } {2, 4, 3}, {2, 5, {2, 4, 3}, {2, 5, 3}, {2, 5, 4}, {3, 2, 4}, {3, 2, 5}, {3, 4, 2}, {3, 3}, {2, 5, 4}, {3, 2, 4}, {3, 2, 5}, {3, 4, 2}, {3, 5, 2}, {3, 5, 4}, {4, 2, 3}, {4, 2, 5}, {4, 3, 5}, 5, 2}, {3, 5, 4}, {4, 2, 3}, {4, 2, 5}, {4, 3, 5}, {5, 2, 3}, {5, 2, 4}, {5, 3, 4} {5, 2, 3}, {5, 2, 4}, {5, 3, 4}
68