SlideShare a Scribd company logo
1 of 47
Download to read offline
Methodology of 
Problem Solving 
Efficiently Solving Computer Programming Problems 
Doncho Minkov 
Technical Trainer 
http://minkov.it/ 
Telerik Software Academy 
academy.telerik.com
Table of Contents 
1. Problem Solving 
2. Understanding the requirements 
3. Analyzing the problem 
4. Brainstorming 
5. Using Pen and Paper 
6. Checking your ideas 
2
Problems Solving 
From Chaotic to Methodological Approach
How to Solve Problems? 
1. Read and analyze the problems 
2. Use a sheet of paper and a pen for sketching 
3. Think up, invent and try ideas 
4. Break the problem into subproblems 
5. Check up your ideas 
6. Choose appropriate data structures 
7. Think about the efficiency 
8. Implement your algorithm step-by-step 
9. Thoroughly test your solution 
4
Understanding the 
Requirements
Read and Analyze the Problems 
Consider you are at traditional computer 
programming exam or contest 
 You have 5 problems to solve in 6 hours 
First read carefully all problems and try to 
estimate how complex each of them is 
 Read the requirements, don't invent them! 
Start solving the most easy problem first 
 Leave the most complex problem last 
Approach the next problem when the previous is 
completely solved and well tested 
6
Analyzing the Problems 
Example: we are given 3 problems: 
1. Set of Numbers 
 Count the occurrences of a number in set of 
numbers 
2. Students 
 Read a set of students and print the name of the 
student with the highest mark 
3. Binary Representation 
 Find the 'ones' and 'zeros' of a set of numbers in 
their binary representation 
7
Analyzing the Problems (2) 
Read carefully the problems and think a bit 
Order the problems from the easiest to the 
most complex: 
1. Set of Numbers 
Whenever find the number increase a count 
variable with one 
2. Students 
 Temporary student with current highest mark 
3. Binary representation 
 If we know the 'ones', we can easily find the zeros 
8
Using a Paper 
and a Pen 
Visualizing and Sketching 
Your Ideas
Use a Sheet of Paper and a Pen 
Never start solving a problem without a sheet 
of paper and a pen 
You need to sketch your ideas 
Paper and pen is the best visualization tool 
 Allows your brain to think efficiently 
Paper works faster 
than keyboard / screen 
Other visualization tool 
could also work well 
10
Paper and Pen 
Consider the "Binary representation" problem 
We can sketch it to start thinking 
Some ideas immediately come, e.g. 
 Divide by 2 and check for the remainder 
 Bitwise shift right and check for the rightmost bit 
 Bitwise shift left and check for the leftmost bit 
 Count only the "zeros" 
 Count only the "ones" 
 Count both "ones" and "zeros" 
11
Invent Ideas 
Think-up, Invent Ideas and Check Them
Think up, Invent and Try Ideas 
First take an example of the problem 
Sketch it on the sheet of paper 
Next try to invent some idea that works for 
your example 
Check if your idea will work for other examples 
Try to find a case that breaks your idea 
Try challenging examples and unusual cases 
If you find your idea incorrect, try to fix it or 
just invent a new idea 
13
Invent and Try Ideas – Example 
Consider the "Binary Representation" problem 
Idea #1: divide by 2 and check the remainder 
How many times to do this? 
Where to check? 
Idea #2: Bitwise shift and check the 
left/rightmost bit 
Left shift or right shift? 
How many times to repeat this? 
 Is this fast enough? 
14
Check-up Your Ideas 
Don't go Ahead before Checking Your Ideas
Check-up Your Ideas 
Check-up your ideas with examples 
 It is better to find a problem before the idea is 
implemented 
When the code is written, changing radically 
your ideas costs a lot of time and effort 
Carefully select examples for check-up 
Examples should be simple enough to be 
checked by hand in a minute 
Examples should be complex enough to cover 
the most general case, not just an isolated case 
16
Invent New Idea If Needed 
What to do when you find your idea is not 
working in all cases? 
Try to fix your idea 
 Sometime a small change could fix the problem 
 Invent new idea and carefully check it 
Iterate 
 It is usual that your first idea is not the best 
 Invent ideas, check them, try various cases, find 
problems, fix them, invent better idea, etc. 
17
Efficiency and Performance 
Is Your Algorithm Fast Enough?
Think About the Efficiency 
Think about efficiency before writing the first 
line of code 
 Estimate the expected running time 
(e.g. using asymptotic complexity) 
Check the requirements 
 Will your algorithm be fast enough to conform 
with them 
You don't want to implement your algorithm 
and find that it is slow when testing 
You will lose your time 
19
Efficiency is not Always Required 
Best solution is sometimes just not needed 
Read carefully your problem statement 
Sometimes ugly solution could work for your 
requirements and it will cost you less time 
 Example: if you need to sort n numbers, any 
algorithm will work when n ∈ [0..500] 
Implement complex algorithms only when the 
problem really needs them! 
20
Implementation 
Coding and Testing Step-by-Step
Start Coding: Check List 
Never start coding before you find correct idea 
that will meet the requirements 
What you will write before you invent a correct 
idea to solve the problem? 
Checklist to follow before start of coding: 
Ensure you understand well the requirements 
Ensure you have invented a good idea 
Ensure your idea is correct 
Ensure you know what data structures to use 
Ensure the performance will be sufficient 
22
Coding Check List – Example 
Checklist before start of coding: 
Ensure you understand well the requirements 
 Yes, counts values of bits 
Ensure you have invented a correct idea 
 Yes, the idea seems correct and is tested 
Ensure the performance will be sufficient 
 Linear running time  good performance 
23
Implement your 
Algorithm Step-by-Step 
"Step-by-step" approach is always better than 
"build all, then test" 
Implement a piece of your program and test it 
Then implement another piece of the program 
and test it 
 Finally put together all pieces and test it 
Small increments (steps) reveal errors early 
 "Big-boom" integration takes more time 
24
Step #1 – Check The Value 
Of The First Bit 
Now we have the value of the first bit 
What happens to the number? 
Do we need the first bit anymore? 
Remove it by right shift 
25 
int number=10; 
int firstBit = number & 1; 
int number=10; 
int firstBit = number & 1; 
number >>= 1;
Step #1 –Test 
Testing if the correct bit is extracted and 
checked 
 Get feedback as early as possible: 
The result is as expected: 
26 
int number = 10; 
int firstBit = number & 1; 
number >>= 1; 
Console.WriteLine(firstBit); 
Console.WriteLine(number); 
0 // the first bit 
5 // the number without the first bit
Step #2 – Get All The Bits 
Of a Number 
How many bits we want to check? 
 All for the number 
 But how many are they 
Example: the number 10 
 10(10) = 8 + 2 = 1010(2) – 4 bits 
How the find out when to stop the checking? 
When right shifting the zeros in the front get 
more and more 
 At some point the number will become zero 
27
Step #2 – Get All The Bits 
Of a Number 
Until the number becomes equal to 0 
1. Check the value of the bit 
2. Right shift the number 
28 
while (number != 0) 
{ 
int firstBit = number & 1; 
number >>= 1; 
Console.Write("{0} ", firstBit); 
}
Step #2 –Test 
Testing with 10 
29 
0 1 0 1 
Testing with 1111 
1 1 1 0 1 0 1 0 0 0 1 
1111 = 1024 + 64 + 16 + 4 + 2 + 1 = 210 + 26 + 24 + 22 + 21 + 20 
Seems correct
Step #3 – Check For The 
Occurrences Of Bit Value 
So far: 
We can get the values of all the bits of a number 
 Lets count the number of ones and zeros 
30 
while (number != 0) 
{ 
int firstBit = number & 1; 
number >>= 1; 
if (firstBit == 1) 
{ 
oneCount++; 
} 
else 
{ 
zeroCount++; 
} 
}
Step #3 –Test 
Test with number 111 (1101111(2)) 
The result is correct 
31 
ones:6 zeros:1 
Test with number 1234 (10011010010(2)) 
ones:5 zeros:6 
The result is correct
Step #4 – Count The Bits Of 
The Whole Set 
Pass through all the numbers 
Count their bits 
32 
int zeroCount = 0, oneCount = 0, n = 5; 
for (int i = 0; i < n; i++) 
{ 
int number = int.Parse(Console.ReadLine()); 
while (number != 0) 
{ 
int firstBit = number & 1; 
number >>= 1; 
if (firstBit == 1) { oneCount++; } 
else { zeroCount++; } 
} 
Console.WriteLine("ones:{0};zeros:{1}", 
oneCount,zeroCount); 
}
Step #4 – Count The Bits Of 
The Whole Set (2) 
The result is surprisingly incorrect: 
Input: 
1 
2 
3 
4 
5 
Output: 
ones:1; zeros:0 
ones:2; zeros:1 
ones:4; zeros:1 
ones:5; zeros:3 
ones:7; zeros:4 
The result is surprisingly incorrect: 
The count of ones and zeros appears as to stack 
for all the numbers of the set! 
We defined the counters outside the iteration of 
the set loop 
 Put them inside 33
Step #4 – Fixing the Bug 
Now it is OK 
34 
int n = 5; 
for (int i = 0; i < n; i++) 
{ 
int zeroCount = 0, oneCount = 0; 
int number = int.Parse(Console.ReadLine()); 
while (number != 0) 
{ 
int firstBit = number & 1; 
number >>= 1; 
if (firstBit == 1) { oneCount++; } 
else { zeroCount++; } 
} 
Console.WriteLine("ones:{0}; zeros:{1}", 
oneCount,zeroCount); 
}
Testing 
Thoroughly Test Your Solution
Thoroughly Test your Solution 
Wise software engineers say that: 
 Inventing a good idea and implementing it is 
half of the solution 
Testing is the second half of the solution 
Always test thoroughly your solution 
 Invest in testing 
One 100% solved problem is better than 2 or 3 
partially solved 
Testing existing problem takes less time than 
solving another problem from scratch 
36
How to Test? 
Testing could not certify absence of defects 
 It just reduces the defects rate 
Well tested solutions are more likely to be 
correct 
Start testing with a good representative of the 
general case 
Not a small isolated case 
Large and complex test, but 
 Small enough to be easily checkable 
37
How to Test? (2) 
Test the border cases 
E.g. if n ∈ [0..500]  try n=0 , n=1, n=2, n=499, 
n=500 
If a bug is found, repeat all tests after fixing it 
to avoid regressions 
Run a load test 
How to be sure that your algorithm is fast 
enough to meet the requirements? 
Use copy-pasting to generate large test data 
38
Read the Problem Statement 
Read carefully the problem statement 
Does your solution print exactly what is 
expected? 
Does your output follow the requested format? 
Did you remove your debug printouts? 
Be sure to solve the requested problem, not 
the problem you think is requested! 
 Example: "Write a program to print the number 
of permutations on n elements" means to print 
a single number, not a set of permutations! 
39
Testing – Example 
Test with a set of 10 numbers 
 Serious error found  change the algorithm 
Change the algorithm 
 The counters never reset to zero 
 Test whether the new algorithm works 
Test with 1 number 
Test with 2 numbers 
Test with 0 numbers 
Load test with 52 000 numbers 
40
Test With 10000 Numbers – 
Example 
41 
int n = 10000, startNumber=111; 
for (int i = startNumber; i < startNumber + n; i++) 
{ 
int zeroCount = 0; 
int oneCount = 0; 
//int number = int.Parse(Console.ReadLine()); 
int number = i; 
int originalNumber = number; 
while (number != 0) 
{ 
int firstBit = number & 1; 
number >>= 1; 
if (firstBit == 1){ oneCount++; } 
else { zeroCount++; } 
} 
} 
Replace the reading from the 
console with a easier type of check
Test With 10000 Numbers – 
Example (2) 
The result is perfect: 
42 
111(10) = 1101111(2) -> ones:6;zeros:1 
… 
10110(10) = 10011101111110(2) -> ones:10;zeros:4
Testing Tips 
You are given the task of the binary 
representation 
The program must read from the console 
 Integer numbers N and B 
 N integer numbers 
While writing and testing the program 
Don't read from the console 
 First hardcode the numbers 
Later make it 
When you are sure it works 
43
Hard-Coded Input – Example 
44 
// Hardcoded input data – for testing purposes 
int n = 5; 
int num1 = 11; 
int num2 = 127; 
int num3 = 0; 
int num4 = 255; 
int num5 = 24; 
// Read the input from the console 
//int n = int.Parse(Console.ReadLine()); 
//for (int i=0; i<n; i++) 
//{ 
// int num = int.Parse(Console.ReadLine()); 
// // Process the number … 
// // … 
//} 
// Now count the bits for these n numbers …
Summary 
Problems solving needs methodology: 
 Understanding and analyzing problems 
 Using a sheet of paper and a pen for sketching 
 Thinking up, inventing and trying ideas 
 Decomposing problems into subproblems 
 Selecting appropriate data structures 
 Thinking about the efficiency and performance 
 Implementing step-by-step 
 Testing the nominal case, border cases and 
efficiency 
45
форум програмиране, форум уеб дизайн 
курсове и уроци по програмиране, уеб дизайн – безплатно 
уроци по програмиране и уеб дизайн за ученици 
програмиране за деца – безплатни курсове и уроци 
безплатен SEO курс - оптимизация за търсачки 
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop 
ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET 
ASP.NET MVC курс –HTML, SQL, C#, .NET, ASP.NET MVC 
безплатен курс "Разработка на софтуер в cloud среда" 
BG Coder - онлайн състезателна система - online judge 
курсове и уроци по програмиране, книги – безплатно от Наков 
безплатен курс "Качествен програмен код" 
алго академия – състезателно програмиране, състезания 
курсове и уроци по програмиране – Телерик академия 
курс мобилни приложения с iPhone, Android, WP7, PhoneGap 
free C# book, безплатна книга C#, книга Java, книга C# 
Дончо Минков - сайт за програмиране 
Николай Костов - блог за програмиране 
C# курс, програмиране, безплатно 
Methodology of 
Problem Solving 
http://academy.telerik.com
Free Trainings @ Telerik Academy 
Fundamentals of C# Programming 
Course 
csharpfundamentals.telerik.com 
Telerik Software Academy 
academy.telerik.com 
Telerik Academy @ Facebook 
facebook.com/TelerikAcademy 
Telerik Software Academy Forums 
 forums.academy.telerik.com

More Related Content

Recently uploaded

Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfkalichargn70th171
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
Data modeling 101 - Basics - Software Domain
Data modeling 101 - Basics - Software DomainData modeling 101 - Basics - Software Domain
Data modeling 101 - Basics - Software DomainAbdul Ahad
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingShane Coughlan
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
Copilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform CopilotCopilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform CopilotEdgard Alejos
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 

Recently uploaded (20)

Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
Data modeling 101 - Basics - Software Domain
Data modeling 101 - Basics - Software DomainData modeling 101 - Basics - Software Domain
Data modeling 101 - Basics - Software Domain
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
Copilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform CopilotCopilot para Microsoft 365 y Power Platform Copilot
Copilot para Microsoft 365 y Power Platform Copilot
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

7 methodology-of-problem-solving-120712073958-phpapp02

  • 1. Methodology of Problem Solving Efficiently Solving Computer Programming Problems Doncho Minkov Technical Trainer http://minkov.it/ Telerik Software Academy academy.telerik.com
  • 2. Table of Contents 1. Problem Solving 2. Understanding the requirements 3. Analyzing the problem 4. Brainstorming 5. Using Pen and Paper 6. Checking your ideas 2
  • 3. Problems Solving From Chaotic to Methodological Approach
  • 4. How to Solve Problems? 1. Read and analyze the problems 2. Use a sheet of paper and a pen for sketching 3. Think up, invent and try ideas 4. Break the problem into subproblems 5. Check up your ideas 6. Choose appropriate data structures 7. Think about the efficiency 8. Implement your algorithm step-by-step 9. Thoroughly test your solution 4
  • 6. Read and Analyze the Problems Consider you are at traditional computer programming exam or contest  You have 5 problems to solve in 6 hours First read carefully all problems and try to estimate how complex each of them is  Read the requirements, don't invent them! Start solving the most easy problem first  Leave the most complex problem last Approach the next problem when the previous is completely solved and well tested 6
  • 7. Analyzing the Problems Example: we are given 3 problems: 1. Set of Numbers  Count the occurrences of a number in set of numbers 2. Students  Read a set of students and print the name of the student with the highest mark 3. Binary Representation  Find the 'ones' and 'zeros' of a set of numbers in their binary representation 7
  • 8. Analyzing the Problems (2) Read carefully the problems and think a bit Order the problems from the easiest to the most complex: 1. Set of Numbers Whenever find the number increase a count variable with one 2. Students  Temporary student with current highest mark 3. Binary representation  If we know the 'ones', we can easily find the zeros 8
  • 9. Using a Paper and a Pen Visualizing and Sketching Your Ideas
  • 10. Use a Sheet of Paper and a Pen Never start solving a problem without a sheet of paper and a pen You need to sketch your ideas Paper and pen is the best visualization tool  Allows your brain to think efficiently Paper works faster than keyboard / screen Other visualization tool could also work well 10
  • 11. Paper and Pen Consider the "Binary representation" problem We can sketch it to start thinking Some ideas immediately come, e.g.  Divide by 2 and check for the remainder  Bitwise shift right and check for the rightmost bit  Bitwise shift left and check for the leftmost bit  Count only the "zeros"  Count only the "ones"  Count both "ones" and "zeros" 11
  • 12. Invent Ideas Think-up, Invent Ideas and Check Them
  • 13. Think up, Invent and Try Ideas First take an example of the problem Sketch it on the sheet of paper Next try to invent some idea that works for your example Check if your idea will work for other examples Try to find a case that breaks your idea Try challenging examples and unusual cases If you find your idea incorrect, try to fix it or just invent a new idea 13
  • 14. Invent and Try Ideas – Example Consider the "Binary Representation" problem Idea #1: divide by 2 and check the remainder How many times to do this? Where to check? Idea #2: Bitwise shift and check the left/rightmost bit Left shift or right shift? How many times to repeat this?  Is this fast enough? 14
  • 15. Check-up Your Ideas Don't go Ahead before Checking Your Ideas
  • 16. Check-up Your Ideas Check-up your ideas with examples  It is better to find a problem before the idea is implemented When the code is written, changing radically your ideas costs a lot of time and effort Carefully select examples for check-up Examples should be simple enough to be checked by hand in a minute Examples should be complex enough to cover the most general case, not just an isolated case 16
  • 17. Invent New Idea If Needed What to do when you find your idea is not working in all cases? Try to fix your idea  Sometime a small change could fix the problem  Invent new idea and carefully check it Iterate  It is usual that your first idea is not the best  Invent ideas, check them, try various cases, find problems, fix them, invent better idea, etc. 17
  • 18. Efficiency and Performance Is Your Algorithm Fast Enough?
  • 19. Think About the Efficiency Think about efficiency before writing the first line of code  Estimate the expected running time (e.g. using asymptotic complexity) Check the requirements  Will your algorithm be fast enough to conform with them You don't want to implement your algorithm and find that it is slow when testing You will lose your time 19
  • 20. Efficiency is not Always Required Best solution is sometimes just not needed Read carefully your problem statement Sometimes ugly solution could work for your requirements and it will cost you less time  Example: if you need to sort n numbers, any algorithm will work when n ∈ [0..500] Implement complex algorithms only when the problem really needs them! 20
  • 21. Implementation Coding and Testing Step-by-Step
  • 22. Start Coding: Check List Never start coding before you find correct idea that will meet the requirements What you will write before you invent a correct idea to solve the problem? Checklist to follow before start of coding: Ensure you understand well the requirements Ensure you have invented a good idea Ensure your idea is correct Ensure you know what data structures to use Ensure the performance will be sufficient 22
  • 23. Coding Check List – Example Checklist before start of coding: Ensure you understand well the requirements  Yes, counts values of bits Ensure you have invented a correct idea  Yes, the idea seems correct and is tested Ensure the performance will be sufficient  Linear running time  good performance 23
  • 24. Implement your Algorithm Step-by-Step "Step-by-step" approach is always better than "build all, then test" Implement a piece of your program and test it Then implement another piece of the program and test it  Finally put together all pieces and test it Small increments (steps) reveal errors early  "Big-boom" integration takes more time 24
  • 25. Step #1 – Check The Value Of The First Bit Now we have the value of the first bit What happens to the number? Do we need the first bit anymore? Remove it by right shift 25 int number=10; int firstBit = number & 1; int number=10; int firstBit = number & 1; number >>= 1;
  • 26. Step #1 –Test Testing if the correct bit is extracted and checked  Get feedback as early as possible: The result is as expected: 26 int number = 10; int firstBit = number & 1; number >>= 1; Console.WriteLine(firstBit); Console.WriteLine(number); 0 // the first bit 5 // the number without the first bit
  • 27. Step #2 – Get All The Bits Of a Number How many bits we want to check?  All for the number  But how many are they Example: the number 10  10(10) = 8 + 2 = 1010(2) – 4 bits How the find out when to stop the checking? When right shifting the zeros in the front get more and more  At some point the number will become zero 27
  • 28. Step #2 – Get All The Bits Of a Number Until the number becomes equal to 0 1. Check the value of the bit 2. Right shift the number 28 while (number != 0) { int firstBit = number & 1; number >>= 1; Console.Write("{0} ", firstBit); }
  • 29. Step #2 –Test Testing with 10 29 0 1 0 1 Testing with 1111 1 1 1 0 1 0 1 0 0 0 1 1111 = 1024 + 64 + 16 + 4 + 2 + 1 = 210 + 26 + 24 + 22 + 21 + 20 Seems correct
  • 30. Step #3 – Check For The Occurrences Of Bit Value So far: We can get the values of all the bits of a number  Lets count the number of ones and zeros 30 while (number != 0) { int firstBit = number & 1; number >>= 1; if (firstBit == 1) { oneCount++; } else { zeroCount++; } }
  • 31. Step #3 –Test Test with number 111 (1101111(2)) The result is correct 31 ones:6 zeros:1 Test with number 1234 (10011010010(2)) ones:5 zeros:6 The result is correct
  • 32. Step #4 – Count The Bits Of The Whole Set Pass through all the numbers Count their bits 32 int zeroCount = 0, oneCount = 0, n = 5; for (int i = 0; i < n; i++) { int number = int.Parse(Console.ReadLine()); while (number != 0) { int firstBit = number & 1; number >>= 1; if (firstBit == 1) { oneCount++; } else { zeroCount++; } } Console.WriteLine("ones:{0};zeros:{1}", oneCount,zeroCount); }
  • 33. Step #4 – Count The Bits Of The Whole Set (2) The result is surprisingly incorrect: Input: 1 2 3 4 5 Output: ones:1; zeros:0 ones:2; zeros:1 ones:4; zeros:1 ones:5; zeros:3 ones:7; zeros:4 The result is surprisingly incorrect: The count of ones and zeros appears as to stack for all the numbers of the set! We defined the counters outside the iteration of the set loop  Put them inside 33
  • 34. Step #4 – Fixing the Bug Now it is OK 34 int n = 5; for (int i = 0; i < n; i++) { int zeroCount = 0, oneCount = 0; int number = int.Parse(Console.ReadLine()); while (number != 0) { int firstBit = number & 1; number >>= 1; if (firstBit == 1) { oneCount++; } else { zeroCount++; } } Console.WriteLine("ones:{0}; zeros:{1}", oneCount,zeroCount); }
  • 35. Testing Thoroughly Test Your Solution
  • 36. Thoroughly Test your Solution Wise software engineers say that:  Inventing a good idea and implementing it is half of the solution Testing is the second half of the solution Always test thoroughly your solution  Invest in testing One 100% solved problem is better than 2 or 3 partially solved Testing existing problem takes less time than solving another problem from scratch 36
  • 37. How to Test? Testing could not certify absence of defects  It just reduces the defects rate Well tested solutions are more likely to be correct Start testing with a good representative of the general case Not a small isolated case Large and complex test, but  Small enough to be easily checkable 37
  • 38. How to Test? (2) Test the border cases E.g. if n ∈ [0..500]  try n=0 , n=1, n=2, n=499, n=500 If a bug is found, repeat all tests after fixing it to avoid regressions Run a load test How to be sure that your algorithm is fast enough to meet the requirements? Use copy-pasting to generate large test data 38
  • 39. Read the Problem Statement Read carefully the problem statement Does your solution print exactly what is expected? Does your output follow the requested format? Did you remove your debug printouts? Be sure to solve the requested problem, not the problem you think is requested!  Example: "Write a program to print the number of permutations on n elements" means to print a single number, not a set of permutations! 39
  • 40. Testing – Example Test with a set of 10 numbers  Serious error found  change the algorithm Change the algorithm  The counters never reset to zero  Test whether the new algorithm works Test with 1 number Test with 2 numbers Test with 0 numbers Load test with 52 000 numbers 40
  • 41. Test With 10000 Numbers – Example 41 int n = 10000, startNumber=111; for (int i = startNumber; i < startNumber + n; i++) { int zeroCount = 0; int oneCount = 0; //int number = int.Parse(Console.ReadLine()); int number = i; int originalNumber = number; while (number != 0) { int firstBit = number & 1; number >>= 1; if (firstBit == 1){ oneCount++; } else { zeroCount++; } } } Replace the reading from the console with a easier type of check
  • 42. Test With 10000 Numbers – Example (2) The result is perfect: 42 111(10) = 1101111(2) -> ones:6;zeros:1 … 10110(10) = 10011101111110(2) -> ones:10;zeros:4
  • 43. Testing Tips You are given the task of the binary representation The program must read from the console  Integer numbers N and B  N integer numbers While writing and testing the program Don't read from the console  First hardcode the numbers Later make it When you are sure it works 43
  • 44. Hard-Coded Input – Example 44 // Hardcoded input data – for testing purposes int n = 5; int num1 = 11; int num2 = 127; int num3 = 0; int num4 = 255; int num5 = 24; // Read the input from the console //int n = int.Parse(Console.ReadLine()); //for (int i=0; i<n; i++) //{ // int num = int.Parse(Console.ReadLine()); // // Process the number … // // … //} // Now count the bits for these n numbers …
  • 45. Summary Problems solving needs methodology:  Understanding and analyzing problems  Using a sheet of paper and a pen for sketching  Thinking up, inventing and trying ideas  Decomposing problems into subproblems  Selecting appropriate data structures  Thinking about the efficiency and performance  Implementing step-by-step  Testing the nominal case, border cases and efficiency 45
  • 46. форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно уроци по програмиране и уеб дизайн за ученици програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET ASP.NET MVC курс –HTML, SQL, C#, .NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Дончо Минков - сайт за програмиране Николай Костов - блог за програмиране C# курс, програмиране, безплатно Methodology of Problem Solving http://academy.telerik.com
  • 47. Free Trainings @ Telerik Academy Fundamentals of C# Programming Course csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com Telerik Academy @ Facebook facebook.com/TelerikAcademy Telerik Software Academy Forums  forums.academy.telerik.com

Editor's Notes

  1. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  2. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*