Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation www.telerik.com Technical Trainer.

Slides:



Advertisements
Similar presentations
Chapter 6 Writing a Program
Advertisements

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Chapter 1 - An Introduction to Computers and Problem Solving
CS0004: Introduction to Programming Repetition – Do Loops.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Infinite Horizon Problems
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Computer Science 1620 Programming & Problem Solving.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
Programming Fundamentals (750113) Ch1. Problem Solving
Elementary Data Structures and Algorithms
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 5: Control Structures II (Repetition)
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
General Programming Introduction to Computing Science and Programming I.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Efficiently Solving Computer Programming Problems Svetlin Nakov Telerik Corporation
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Developing an Algorithm
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 22 Developer testing Peter J. Lane. Testing can be difficult for developers to follow  Testing’s goal runs counter to the goals of the other.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
1 Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
1 Markov Decision Processes Infinite Horizon Problems Alan Fern * * Based in part on slides by Craig Boutilier and Daniel Weld.
The Software Development Process
Methodology of Problem Solving Efficiently Solving Computer Programming Problems SoftUni Team Technical Trainers Software University
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Intermediate 2 Computing Unit 2 - Software Development.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
HKOI Programming HKOI Training Team (Intermediate) Alan, Tam Siu Lung Unu, Tse Chi Yung.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Fundamental Programming Fundamental Programming More on Repetition.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
C++ for Engineers and Scientists, Second Edition 1 Problem Solution and Software Development Software development procedure: method for solving problems.
Introduction to Computing Systems and Programming Programming.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Low-Level Programming Languages, Pseudocode and Testing Chapter 6.
Introduction to Computing Science and Programming I
Testing and Debugging.
Lecture 2 Introduction to Programming
Algorithm Analysis CSE 2011 Winter September 2018.
Programming Fundamentals (750113) Ch1. Problem Solving
Presentation transcript:

Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer

From Chaotic to Methodological Approach

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 3

 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 5

 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 6

 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 7

Visualizing and Sketching Your Ideas

 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 9

 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" 10

Think-up, Invent Ideas and Check Them

 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 12

 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? 13

Don't go Ahead before Checking 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 15

 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. 16

Is Your Algorithm Fast Enough?

 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 18

 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! 19

Coding and Testing Step-by-Step

 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 21

 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 22

 "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 23

 What happens to the number?  Do we need the first bit anymore?  Remove it by right shift 24 int number=10; int firstBit = number & 1;  Now we have the value of the first bit int number=10; int firstBit = number & 1; number >>= 1;

 Testing if the correct bit is extracted and checked  Get feedback as early as possible:  The result is as expected: 25 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

 How many bits we want to check?  All for the number  But how many are they  Example: the number 10  10 (10) = = 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 26

 Until the number becomes equal to 0 1.Check the value of the bit 2.Right shift the number 27 while (number != 0) { int firstBit = number & 1; int firstBit = number & 1; number >>= 1; number >>= 1; Console.Write("{0} ", firstBit); Console.Write("{0} ", firstBit);}

 Testing with  Testing with  Seems correct 1111 = =

 So far:  We can get the values of all the bits of a number  Lets count the number of ones and zeros 29 while (number != 0) { int firstBit = number & 1; number >>= 1; if (firstBit == 1) {oneCount++;}else{zeroCount++;}}

 Test with number 111 ( (2) )  The result is correct 30 ones:6 zeros:1  Test with number 1234 ( (2) )  The result is correct ones:5 zeros:6

 Pass through all the numbers  Count their bits 31 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); oneCount,zeroCount);}

 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 32 Input: 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:

 Now it is OK 33 int n = 5; for (int i = 0; i < n; i++) { int zeroCount = 0, oneCount = 0; int zeroCount = 0, oneCount = 0; int number = int.Parse(Console.ReadLine()); int number = int.Parse(Console.ReadLine()); while (number != 0) while (number != 0) { int firstBit = number & 1; int firstBit = number & 1; number >>= 1; number >>= 1; if (firstBit == 1) { oneCount++; } if (firstBit == 1) { oneCount++; } else { zeroCount++; } else { zeroCount++; } } Console.WriteLine("ones:{0}; zeros:{1}", oneCount,zeroCount); Console.WriteLine("ones:{0}; zeros:{1}", oneCount,zeroCount);}

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 35

 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 36

 Test the border cases  E.g. if n ∈ [ ]  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 37

 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! 38

 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 numbers 39

40 int n = 10000, startNumber=111; for (int i = startNumber; i < startNumber + n; i++) { int zeroCount = 0; int zeroCount = 0; int oneCount = 0; int oneCount = 0; //int number = int.Parse(Console.ReadLine()); //int number = int.Parse(Console.ReadLine()); int number = i; int number = i; int originalNumber = number; int originalNumber = number; while (number != 0) while (number != 0) { int firstBit = number & 1; int firstBit = number & 1; number >>= 1; number >>= 1; if (firstBit == 1){ oneCount++; } if (firstBit == 1){ oneCount++; } else { zeroCount++; } else { zeroCount++; } }} Replace the reading from the console with a easier type of check

 The result is perfect: (10) = (2) -> ones:6;zeros:1 … (10) = (2) -> ones:10;zeros:4

 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 42

43 // 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 …

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 44

Questions?