Library

Video Player is loading.
 
Current Time 0:00
Duration 8:26
Loaded: 0.00%
 

x1.00


Back

Games & Quizzes

Training Mode - Typing
Fill the gaps to the Lyric - Best method
Training Mode - Picking
Pick the correct word to fill in the gap
Fill In The Blank
Find the missing words in a sentence Requires 5 vocabulary annotations
Vocabulary Match
Match the words to the definitions Requires 10 vocabulary annotations

You may need to watch a part of the video to unlock quizzes

Don't forget to Sign In to save your points

Challenge Accomplished

PERFECT HITS +NaN
HITS +NaN
LONGEST STREAK +NaN
TOTAL +
- //

We couldn't find definitions for the word you were looking for.
Or maybe the current language is not supported

  • 00:00

    >> Let's get greedy.
    >> Let's get greedy.

  • 00:02

    In greedy, our job is to play the role of a greedy cashier.
    In greedy, our job is to play the role of a greedy cashier.

  • 00:06

    The user will tell us how much change we owe them,
    The user will tell us how much change we owe them,

  • 00:09

    and then our job is to calculate the minimum number of coins
    and then our job is to calculate the minimum number of coins

  • 00:13

    that we can use to make that amount of change.
    that we can use to make that amount of change.

  • 00:17

    >> Let's start with an example.
    >> Let's start with an example.

  • 00:19

    Say the user requires $0.32 back.
    Say the user requires $0.32 back.

  • 00:23

    We could do this by giving them 32 pennies, one cent each.
    We could do this by giving them 32 pennies, one cent each.

  • 00:28

    Or I could also use five coins-- by giving them three dimes, $0.10 each,
    Or I could also use five coins-- by giving them three dimes, $0.10 each,

  • 00:35

    and two pennies, $0.02 each.
    and two pennies, $0.02 each.

  • 00:38

    But could we use even fewer coins to make that?
    But could we use even fewer coins to make that?

  • 00:42

    >> The whole tactic in greedy-- to be a greedy cashier--
    >> The whole tactic in greedy-- to be a greedy cashier--

  • 00:45

    is to use the largest coin possible.
    is to use the largest coin possible.

  • 00:47

    So whenever we have quarters we'll use them.
    So whenever we have quarters we'll use them.

  • 00:50

    And then once those run out, we'll use dimes, $0.10 each.
    And then once those run out, we'll use dimes, $0.10 each.

  • 00:54

    Then nickels, 5 cents each, and then down to pennies, one cent each.
    Then nickels, 5 cents each, and then down to pennies, one cent each.

  • 00:58

    By using the largest coin possible whenever we can,
    By using the largest coin possible whenever we can,

  • 01:01

    we ensure that we use the fewest number of coins possible to make the change.
    we ensure that we use the fewest number of coins possible to make the change.

  • 01:07

    >> So let's walk this through.
    >> So let's walk this through.

  • 01:09

    The user needs $0.32.
    The user needs $0.32.

  • 01:11

    So we ask ourselves, can we use a quarter?
    So we ask ourselves, can we use a quarter?

  • 01:14

    Well, yes we can.
    Well, yes we can.

  • 01:15

    So now we only know them $0.07, and we used one coin.
    So now we only know them $0.07, and we used one coin.

  • 01:19

    >> Can we use another quarter?
    >> Can we use another quarter?

  • 01:20

    Well, no.
    Well, no.

  • 01:21

    $0.07 is less than $0.25, so we proceed to the next largest coin available.
    $0.07 is less than $0.25, so we proceed to the next largest coin available.

  • 01:27

    Dimes are $0.10, and again, we can't use dimes.
    Dimes are $0.10, and again, we can't use dimes.

  • 01:30

    Because dimes are worth $0.10, which is more than the amount of change owed.
    Because dimes are worth $0.10, which is more than the amount of change owed.

  • 01:35

    >> We go to nickels.
    >> We go to nickels.

  • 01:36

    And, yes indeed, $0.05 is less than $0.10-- so we can use a nickel.
    And, yes indeed, $0.05 is less than $0.10-- so we can use a nickel.

  • 01:40

    So now we only owe the user $0.02, and we've so far used two coins.
    So now we only owe the user $0.02, and we've so far used two coins.

  • 01:46

    We can't use another nickel.
    We can't use another nickel.

  • 01:47

    So then we proceed to the last coin at our disposal, which are the pennies.
    So then we proceed to the last coin at our disposal, which are the pennies.

  • 01:51

    >> And can we use penny?
    >> And can we use penny?

  • 01:52

    Well, yes-- and we end up using two pennies for a total of four coins.
    Well, yes-- and we end up using two pennies for a total of four coins.

  • 01:59

    >> Once you're finished, the program will look like this.
    >> Once you're finished, the program will look like this.

  • 02:01

    Once the user runs the greedy program, they'll
    Once the user runs the greedy program, they'll

  • 02:03

    be prompted to give the amount of change in dollars that they're owed.
    be prompted to give the amount of change in dollars that they're owed.

  • 02:07

    And then your program will output the minimum amount of coins
    And then your program will output the minimum amount of coins

  • 02:11

    that the greedy cashier would use to make that amount of change.
    that the greedy cashier would use to make that amount of change.

  • 02:14

    >> So now let's break this down into our subtasks.
    >> So now let's break this down into our subtasks.

  • 02:18

    First we're going to prompt our user for an amount of change.
    First we're going to prompt our user for an amount of change.

  • 02:21

    And, as with any user input, we want to make sure that we validate that input
    And, as with any user input, we want to make sure that we validate that input

  • 02:25

    and ensure that we can use that input for the rest of our program.
    and ensure that we can use that input for the rest of our program.

  • 02:29

    Then we're going to always use the largest point possible
    Then we're going to always use the largest point possible

  • 02:32

    and keep track of the coins used.
    and keep track of the coins used.

  • 02:35

    And finally, print the final number of coins that we used.
    And finally, print the final number of coins that we used.

  • 02:39

    >> So let's talk about prompting.
    >> So let's talk about prompting.

  • 02:40

    The amount must make cents, and this is in dollars.
    The amount must make cents, and this is in dollars.

  • 02:43

    And so for dollars, we're going to use the float variable type.
    And so for dollars, we're going to use the float variable type.

  • 02:48

    Now whenever you ask a user for input, you want to make sure that it's valid.
    Now whenever you ask a user for input, you want to make sure that it's valid.

  • 02:52

    And so here we like to take advantage of the do-while loop construct.
    And so here we like to take advantage of the do-while loop construct.

  • 02:56

    >> A do-while loop will execute the body of the loop at least once.
    >> A do-while loop will execute the body of the loop at least once.

  • 03:00

    So this comes in handy.
    So this comes in handy.

  • 03:01

    We know that we need to prompt the user at least once for a float.
    We know that we need to prompt the user at least once for a float.

  • 03:05

    Now if that float is valid.
    Now if that float is valid.

  • 03:07

    That's great.
    That's great.

  • 03:07

    We move on.
    We move on.

  • 03:08

    But if not, the loop will ensure that we get a proper float
    But if not, the loop will ensure that we get a proper float

  • 03:11

    by repeating continuously until the user gives us a valid value.
    by repeating continuously until the user gives us a valid value.

  • 03:16

    >> Now for the do-while loop condition, we need
    >> Now for the do-while loop condition, we need

  • 03:18

    to consider what it means to have an invalid float.
    to consider what it means to have an invalid float.

  • 03:22

    So for the context of this problem, probably
    So for the context of this problem, probably

  • 03:24

    it makes sense just to accept positive values.
    it makes sense just to accept positive values.

  • 03:27

    >> So moving on-- we've obtained a value in dollars from the user.
    >> So moving on-- we've obtained a value in dollars from the user.

  • 03:32

    But we're dealing with coins, which are entirely in cents.
    But we're dealing with coins, which are entirely in cents.

  • 03:35

    $1 is equivalent to 100 cents.
    $1 is equivalent to 100 cents.

  • 03:38

    So a good thing to do is to convert those values into cents.
    So a good thing to do is to convert those values into cents.

  • 03:43

    >> Now when converting from a float to an integer, so dollars to cents,
    >> Now when converting from a float to an integer, so dollars to cents,

  • 03:48

    we want to make sure that we're careful about floating-point imprecision.
    we want to make sure that we're careful about floating-point imprecision.

  • 03:52

    So that means that-- say my dollar value-- my float
    So that means that-- say my dollar value-- my float

  • 03:55

    value-- was an even $2, there still may be some stray numbers in there.
    value-- was an even $2, there still may be some stray numbers in there.

  • 04:00

    So we want to make sure that not only do we multiply by 100 to get the cents,
    So we want to make sure that not only do we multiply by 100 to get the cents,

  • 04:04

    but we also round it off.
    but we also round it off.

  • 04:06

    >> So now we have the amount of change owed to the user.
    >> So now we have the amount of change owed to the user.

  • 04:09

    We originally obtained it in dollars, and now we've converted it to cents.
    We originally obtained it in dollars, and now we've converted it to cents.

  • 04:13

    So now we can proceed with the heart of the greedy algorithm, which is always
    So now we can proceed with the heart of the greedy algorithm, which is always

  • 04:17

    using the largest coin possible.
    using the largest coin possible.

  • 04:19

    While we're doing this, it's essential that we also
    While we're doing this, it's essential that we also

  • 04:21

    keep track of how many coins are going to be returned to the user
    keep track of how many coins are going to be returned to the user

  • 04:25

    as well as the remaining change owed to the user.
    as well as the remaining change owed to the user.

  • 04:28

    >> The program will look something like this.
    >> The program will look something like this.

  • 04:31

    After you get the amount of dollars and convert that to cents,
    After you get the amount of dollars and convert that to cents,

  • 04:34

    then you'll enter a loop.
    then you'll enter a loop.

  • 04:35

    While quarters can be used-- that is to say
    While quarters can be used-- that is to say

  • 04:38

    while the amount of change owed to the user is greater than or equal to $0.25,
    while the amount of change owed to the user is greater than or equal to $0.25,

  • 04:43

    you'll use a quarter.
    you'll use a quarter.

  • 04:45

    >> Now what does using a quarter entail?
    >> Now what does using a quarter entail?

  • 04:47

    Well, one-- you'll increase the coin count to be returned to the user.
    Well, one-- you'll increase the coin count to be returned to the user.

  • 04:51

    And second you'll decrease the current amount of change owed back to the user
    And second you'll decrease the current amount of change owed back to the user

  • 04:55

    by $0.25.
    by $0.25.

  • 04:57

    >> After repeating that until quarters can no longer be used,
    >> After repeating that until quarters can no longer be used,

  • 05:00

    proceed to the next largest coin-- in this case dimes, $0.10.
    proceed to the next largest coin-- in this case dimes, $0.10.

  • 05:04

    So you'll enter that loop until you can no longer use dimes.
    So you'll enter that loop until you can no longer use dimes.

  • 05:07

    Then proceed to the next largest coin, nickels.
    Then proceed to the next largest coin, nickels.

  • 05:10

    After nickels can no longer be used, use the remaining amount in pennies.
    After nickels can no longer be used, use the remaining amount in pennies.

  • 05:14

    And finally, print the number of coins used.
    And finally, print the number of coins used.

  • 05:17

    >> Another way that you can approach the greedy problem
    >> Another way that you can approach the greedy problem

  • 05:20

    is to use the modulo approach.
    is to use the modulo approach.

  • 05:22

    Modulo is an operator that returns the remainder
    Modulo is an operator that returns the remainder

  • 05:25

    of the division between two numbers.
    of the division between two numbers.

  • 05:27

    Say I had 50 mod 5.
    Say I had 50 mod 5.

  • 05:30

    Well, 5 is a factor of 50, so the remainder will be 0.
    Well, 5 is a factor of 50, so the remainder will be 0.

  • 05:34

    50 mod 10-- well, 10 is also a factor of 50, so the remainder is also 0.
    50 mod 10-- well, 10 is also a factor of 50, so the remainder is also 0.

  • 05:39

    50 mod 50-- well, any number mod itself isn't going to have any remainder.
    50 mod 50-- well, any number mod itself isn't going to have any remainder.

  • 05:43

    >> What about 50 mod 49?
    >> What about 50 mod 49?

  • 05:45

    Well, 49 only goes into 50 once.
    Well, 49 only goes into 50 once.

  • 05:47

    So the remainder is going to be 1.
    So the remainder is going to be 1.

  • 05:50

    53 mod 50 is going to give you a remainder of 3.
    53 mod 50 is going to give you a remainder of 3.

  • 05:55

    >> So how can we use modulo and perhaps some division
    >> So how can we use modulo and perhaps some division

  • 05:59

    to implement our greedy algorithm?
    to implement our greedy algorithm?

  • 06:01

    Well, we still want to stay true to the heart of the greedy algorithm-- that
    Well, we still want to stay true to the heart of the greedy algorithm-- that

  • 06:05

    is using the largest coin possible.
    is using the largest coin possible.

  • 06:07

    >> So let's ask ourselves if we can use any quarters to return $0.32 to the user.
    >> So let's ask ourselves if we can use any quarters to return $0.32 to the user.

  • 06:14

    Well, 32 mod 25 gives us a remainder of $0.07.
    Well, 32 mod 25 gives us a remainder of $0.07.

  • 06:20

    So that tells us that we can definitely use one quarter with $0.07 remaining.
    So that tells us that we can definitely use one quarter with $0.07 remaining.

  • 06:24

    >> Can we then use any dimes?
    >> Can we then use any dimes?

  • 06:26

    Well, no-- because $0.07 mod $0.10 gives us a remainder of 7.
    Well, no-- because $0.07 mod $0.10 gives us a remainder of 7.

  • 06:32

    10 doesn't go into 7 at all.
    10 doesn't go into 7 at all.

  • 06:34

    >> Then can we use nickels?
    >> Then can we use nickels?

  • 06:36

    Well $0.07 mod 5 cents gives us two remaining.
    Well $0.07 mod 5 cents gives us two remaining.

  • 06:40

    And lastly, can we use any pennies?
    And lastly, can we use any pennies?

  • 06:42

    2 mod 1 gives us 0, which is ultimately what
    2 mod 1 gives us 0, which is ultimately what

  • 06:45

    we want because then that means that we've returned
    we want because then that means that we've returned

  • 06:48

    to the user all of the change owed.
    to the user all of the change owed.

  • 06:50

    >> So now you have two possible ways of implementing the greedy algorithm--
    >> So now you have two possible ways of implementing the greedy algorithm--

  • 06:54

    one with loops and one with a combination of modulo and division.
    one with loops and one with a combination of modulo and division.

  • 06:59

    So finally, we just need to print the final number of coins.
    So finally, we just need to print the final number of coins.

  • 07:03

    >> If I wanted to tell you that I had 3 pets and this value was hardcoded,
    >> If I wanted to tell you that I had 3 pets and this value was hardcoded,

  • 07:06

    then I could just use a simple print test statement.
    then I could just use a simple print test statement.

  • 07:09

    But our value is actually stored in a variable.
    But our value is actually stored in a variable.

  • 07:12

    So how do you print the values stored in variables?
    So how do you print the values stored in variables?

  • 07:15

    >> For this we take advantage of placeholders.
    >> For this we take advantage of placeholders.

  • 07:17

    Say I had already declared an initialized integer n.
    Say I had already declared an initialized integer n.

  • 07:21

    Then later on if I wanted to print that value, then I would write the string.
    Then later on if I wanted to print that value, then I would write the string.

  • 07:25

    And instead of that value I would use a placeholder for that integer-- %i.
    And instead of that value I would use a placeholder for that integer-- %i.

  • 07:30

    Then after the string, I have a comma, followed by the variable
    Then after the string, I have a comma, followed by the variable

  • 07:33

    that I want to print.
    that I want to print.

  • 07:34

    And later on, when it prints, it'll print the value of n.
    And later on, when it prints, it'll print the value of n.

  • 07:38

    >> I could also use a placeholder for a float, for instance.
    >> I could also use a placeholder for a float, for instance.

  • 07:41

    If I wanted to tell you how much cash I have in my pocket,
    If I wanted to tell you how much cash I have in my pocket,

  • 07:44

    then I could say I have %f dollars.
    then I could say I have %f dollars.

  • 07:46

    And later on when it prints, then n will take the place of that place holder.
    And later on when it prints, then n will take the place of that place holder.

  • 07:51

    I could also, for instance, use several placeholders for several variables.
    I could also, for instance, use several placeholders for several variables.

  • 07:55

    So as long as I list them in order, then I
    So as long as I list them in order, then I

  • 07:57

    can tell you how many dogs and cats I have.
    can tell you how many dogs and cats I have.

  • 08:00

    >> Now we know how to prompt the user for an amount of change,
    >> Now we know how to prompt the user for an amount of change,

  • 08:03

    ensure that that input is valid, and then we
    ensure that that input is valid, and then we

  • 08:06

    have two possible ways of implementing the greedy algorithm of always using
    have two possible ways of implementing the greedy algorithm of always using

  • 08:10

    the largest coin possible.
    the largest coin possible.

  • 08:12

    Because we've kept track of how many coins we're using,
    Because we've kept track of how many coins we're using,

  • 08:15

    we can then print that value at the end, telling the user how many coins they're
    we can then print that value at the end, telling the user how many coins they're

  • 08:19

    getting back.
    getting back.

  • 08:20

    >> My name is the Amayla, and this is CS50.
    >> My name is the Amayla, and this is CS50.

All noun
let
/let/

word

period when property is rented

greedy (C)

68,395 views

Video Language:

  • English

Caption Language:

  • English (en)

Accent:

  • English (US)

Speech Time:

99%
  • 8:23 / 8:26

Speech Rate:

  • 185 wpm - Fast

Category:

  • Education

Intro:

>> Let's get greedy.. In greedy, our job is to play the role of a greedy cashier.
The user will tell us how much change we owe them,. and then our job is to calculate the minimum number of coins
that we can use to make that amount of change.. >> Let's start with an example.. Say the user requires $0.32 back.. We could do this by giving them 32 pennies, one cent each.
Or I could also use five coins-- by giving them three dimes, $0.10 each,
and two pennies, $0.02 each.. But could we use even fewer coins to make that?. >> The whole tactic in greedy-- to be a greedy cashier--
is to use the largest coin possible.. So whenever we have quarters we'll use them.. And then once those run out, we'll use dimes, $0.10 each.
Then nickels, 5 cents each, and then down to pennies, one cent each.
By using the largest coin possible whenever we can,
we ensure that we use the fewest number of coins possible to make the change.
>> So let's walk this through.. The user needs $0.32..

Video Vocabulary

/ˈkwôrdər/

noun other verb

each of four equal or corresponding parts into which something is or can be divided. 3-month period of time for businesses, etc.. divide into four equal or corresponding parts.

/(h)wenˈevər/

adverb conjunction

used for emphasis instead of 'when' in questions. at whatever time.

/əˈnəT͟Hər/

adjective determiner pronoun

One more, but not this. used to refer to additional person or thing of same type as one. One more (thing).

/lärj/

adjective

of considerable size, extent, etc..

/ˈminəməm/

adjective noun

smallest or lowest. least or smallest amount or quantity possible or required.

/ˈkalkyəˌlāt/

verb

determine mathematically.

/ˈpäsəb(ə)l/

adjective noun

able to be done. candidate for job.

/rəˈkwī(ə)r/

verb

need for particular purpose.