Algorithms and DeMorgan’s Laws Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

CS107 Introduction to Computer Science Lecture 2.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Algorithms Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Chapter 2: Algorithm Discovery and Design
CS107 Introduction to Computer Science Lecture 2.
Computer Science 1620 Programming & Problem Solving.
Complexity (Running Time)
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Fundamentals of Python: From First Programs Through Data Structures
Adapted from slides by Marie desJardins
Fundamentals of Python: First Programs
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Invitation to Computer Science, Java Version, Second Edition.
Testing Michael Ernst CSE 140 University of Washington.
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.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
By the end of this session you should be able to...
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Introduction to Algorithms. What is Computer Science? Computer Science is the study of computers (??) This leaves aside the theoretical work in CS, which.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
1 The Practice of Computing Using PYTHON William PunchRichard Enbody Chapter 3 Algorithms & Program Development Copyright © 2011 Pearson Education, Inc.
CS221 Algorithm Basics. What is an algorithm? An algorithm is a list of instructions that transform input information into a desired output. Each instruction.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
1 Running Experiments for Your Term Projects Dana S. Nau CMSC 722, AI Planning University of Maryland Lecture slides for Automated Planning: Theory and.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
The Scientific Method: How to solve just about anything.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
Algorithms and Pseudocode
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Python - Iteration A FOR loop is ideal if we know how many times we want to repeat. Try this code: for loopCounter in range(10): print(loopCounter) There.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
CSC 108H: Introduction to Computer Programming
Lecture 3 of Computer Science II
CS1371 Introduction to Computing for Engineers
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Unit# 9: Computer Program Development
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Intro to Nested Looping
Data Structures – 1D Lists
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Loops CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Algorithms and DeMorgan’s Laws
Intro to Nested Looping
Software Development Techniques
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Algorithms and DeMorgan’s Laws Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Opposites Sometimes we need to find the opposites of conditions An example is error checking to prompt the user for the correct value  We know what kind of value we want  But we have to loop on a condition for the values we don’t want… Other examples, too 2

Thought Puzzle What is the opposite of: if small<middle and middle<large : A:if small<middle or middle<large : B:if small>=middle and middle>=large : C:if small>=middle or middle>=large :

Thought Puzzle What is the opposite of: if small<middle and middle<large : For that whole statement to be true, both  (small < middle) == true  and  (middle < large) == true 4

Thought Puzzle What is the opposite of: if small<middle and middle<large : For that whole statement to be false, at least  (first statement) == false  or  (second statement) == false 5

Thought Puzzle What is the opposite of: if small<middle and middle<large : For that whole statement to be false, at least  not(small<middle)  or  not(middle<large) 6

Thought Puzzle What is the opposite of: if small<middle and middle<large : For that whole statement to be false, at least  (small>=middle) == false  or  (middle>=large) == false 7

DeMorgan’s Laws The opposite of: A and B Is opposite (A and B) = opposite(A) or opposite(B)

So how do you go the other way? The opposite of: A or B Is opposite (A or B) = opposite(A) and opposite(B)

Reverse these num > 0 and num < 10 status==“student” and amountOwed< < inputNum <

What is an Algorithm? Process or a set of rules to be followed in calculations or other problem-solving operations Or, more informally… A recipe for solving a problem

Algorithm vs. Program An algorithm is a description of how to solve a problem A program is an implementation of an algorithm in a particular language to run on a computer (usually a particular kind of computer) Difference between description of what we want to do and what we actually did

Algorithm vs. Program So which do we write first?  Algorithm?  Program?

What’s the Difference, Really? We can analyze the algorithm independent of its implementation.  Is this the most efficient way to solve a problem? We can examine how easily, or with what difficulty, a language allows us to realize an algorithm  Some languages allow us to more easily solve certain types of problems than others

Last Programming Assignment Compute the number of times a given digit D appears in a given number N.  The number of times 5 appears in 1550 is 2.  The number of times 0 appears in 1550 is 1.  The number of times 3 appears in 1550 is 0.

So what was your algorithm? Set a counter to zero Look at each digit in the number, one at a time Increment the counter every time we see a digit that equals our target value Is that sufficient?

A Stronger Algorithm Set a counter to zero For each digit in the number:  Look at the last digit in the number (using %)  Increment the counter every time we see a digit that equals our target value  Shrink the number by one digit (using //)  Repeat until there are no more digits to look at

Aspects of an Algorithm Detailed: Provide enough detail to be implementable. Can be tricky to define completely, relies on “common sense” Effective: the algorithm should eventually halt, and halt in a “reasonable” amount of time. “reasonable” might change under different circumstances (faster computer, more computers, etc.)

Aspects of an Algorithm (2) Specify Behavior: the algorithm should be specific about the information that goes in (quantity, type, etc.) and the information that comes out. General Purpose: algorithms should be idealized and therefore general purpose. A sorting algorithm should be able to sort anything (numbers, letters, patient records, etc.)

A Lot to Do! That is a lot to do for the burgeoning programmer. Get better as we go along, but good to know what the standards are!

Aspects of a Program Readability Robustness Correctness

Aspects of a Program: Readability We will emphasize, over and over, that a program is an essay on problem solving intended to be read by other people, even if “other people” is you in the future! Write a program so that you can read it, because it is likely that sometime in the future you will have to read it! So what makes a program readable…

Readability(2): Naming The easiest thing to do that affects readability is good naming  use names for the items you create that reflect their purpose  to help keep straight the types used, include that as part of the name. Python does not care about the type stored, but you do!

What Does this Do? a = int(input("give a number: ")) b=1 c = 0 while b <= a : c = c + b b = b + 1 print (a,b,c) print( "Result: ", c/(b – 1))

What Does this Do? # average of a sum of integers in a given range limit_str = input(“range is 1 to input:”) limit_int = int(limit_str) count_int = 1 sum_int = 0 while count_int <= limit_int: sum_int = sum_int + count_int count_int = count_int + 1 average = sum_int/(count_int – 1) print(“Average of sum of integers from 1 to”,\ limit_int,”is”,average)

Readability(3): Comments info at the top, the goal of the code purpose of variables (if not obvious by the name) purpose of other functions being used anything “tricky”. If it took you time to write, it probably is hard to read and needs a comment

Readability(4): Indenting indenting is a visual cue to say what code is “part of” other code. This is not always required as it is in Python, but Python forces you to indent. This aids readability greatly.

Aspects of Programming (2) Robust: As much as possible, the program should account for inputs that are not what is expected. More on this with error handling in Chapter 14

Aspects of Programming (2) Correct: Our programs should produce correct results. Much harder to ensure than it looks!

The Problem is “Problem-Solving” Remember, two parts to our goal:  Understand the problems to be solved  Encode the solution in a programming language, e.g. Python

Mix of Both The goal in each class is to do a little of both: problem solving and Python Terribly important that we impress on you to try and understand how to solve the problem first before you try and code it.  Develop the algorithm first!  Then develop the program.

So which is harder? Writing the algorithm? Writing the program? It’s a lot like French poetry when you don’t know much about French or poetry… 32

Questions about PA04? Latin Squares Use prints to help you troubleshoot but remove before you submit.

Bonus Slides If I don’t get to these in class, please read these on your own! 34

Steps to Problem Solving From “PProblem SSSolving”, DeFranco & Vinsonhaler Be Proactive See it Simplify it Stir it up Pause and Reflect

Steps to Problem Solving Engage/Commit Visualize/See Try it/Experiment Simplify Analyze/Think Relax

Engage You need to commit yourself to addressing the problem. Don’t give up easily Try different approaches Set the “mood” Just putting in time does not mean you put in a real effort!!!

Visualize/See the Problem Find a way that works for you, some way to make the problem tangible. draw pictures layout tables literally “see” the problem somehow Everyone has a different way, find yours!

Try It/Experiment For some reason, people are afraid to just “try” some solution. Perhaps they fear failure, but experiments, done just for you, are the best way to figure out problems. Be willing to try, and fail, to solve a problem. Get started, don’t wait for enlightenment!

Simplify Simplifying the problem so you can get a handle on it is one of the most powerful problem solving tools. Given a hard problem, make is simplier (smaller, clearer, easier), figure that out, then ramp up to the harder problem.

Think it Over/Analyze If your solution isn’t working: stop evaluate how you are doing analyze and keep going, or start over. People can be amazingly “stiff”, banging their heads against the same wall over and over again. Loosen up, find another way!

One More Thing: Relax Take your time. Not getting an answer right away is not the end of the world. Put it away and come back to it. You’d be surprised how easy it is to solve if you let it go for awhile. That’s why starting early is a luxury you should afford yourself.