greedy (Python)
Skills:
AI Pair Programming80%
Key Takeaways
Implements the greedy algorithm in Python to calculate the minimum number of coins for a given amount of change
Full Transcript
let's get greedy again today we're going to recreate the greedy problem that we did in C but this time in Python now the to-dos haven't changed we want to prompt the user for an amount of change Always using the largest coin possible keeping track of how many coins we used and then printing that final number out so how do we prompt the user for the amount of change that they're owed well in C we had the get float function found in the cs50 library so in order to use that function we had to hashtag include the cs50 library now in Python CS5 also provides you with the get float function found in the cs-50 module so instead of hashtag including it you'll have to import it okay so now how do we validate the input well typically we might use a do Loop in C now python doesn't have an equivalent for the do while loop it does however have an equivalent while loop so how do we use the while loop and make it into a do while loop here's how see how my condition for the while loop is true so that means that that will execute at least once so that's where I'm going to prompt the user for the amount of change and then if they've met my condition and if it's valid then I'll just break out of that Loop and then I'll continue with the rest of my program but if they gave me invalid input then I'll just go back to the top of my Loop and prompt them again so the next thing once we have their input which is in dollars is to convert that into cents where $1 is equivalent to 100 cents before we had the round function available in C which we also have in Python now that we have the value in sense let's proceed with our greedy algorithm always using the largest coin possible keeping track of how many coins must be returned and the total amount that remains now some of you may have implemented this problem using a combination of Y loops and that's perfectly acceptable but there's also an alternative way to use a combination of modulo and division so if you did it the while Loops way then I challenge you to try this out with modulo modulo as you remember Returns the remainder of a division between two numbers then in Python if you want to divide integers then you can use the double slash and then for float divis use the single slash so finally you need to print the final number of coins in C we would have to call Print F passing in a placeholder for that variable indicating what type it was and then include the variable afterwards but in Python if I have a variable called coins then all I have to do is call the print statement and pass in coins so there you have your greedy program my name is Zila and this was greedy
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS50 · CS50 · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Hello, World: Hadi Partovi
CS50
Content Distribution and Archival in a Digital Age
CS50
CS50 2014 - Week 1
CS50
CS50 2014 - Week 3
CS50
CS50 2014 - Week 0, continued
CS50
CS50 2014 - Week 4
CS50
Week 3, continued
CS50
Quiz 0 Review
CS50
CS50 2014 - Week 3, continued
CS50
CS50 2014 - Week 7
CS50
CS50 2014 - Week 7, continued
CS50
Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
CS50
Introduction to Amazon Web Services by Leo Zhadanovsky
CS50
CS50 2014 - Week 9
CS50
How to Build Innovative Technologies by Abby Fichtner
CS50
Light Your World (with Hue Bulbs) by Dan Bradley
CS50
Building Dynamic Web Apps with Laravel by Eric Ouyang
CS50
CS50 2014 - CS50 Lecture by Steve Ballmer
CS50
CS50 2014 - Week 10
CS50
This is CS50 with Steve Ballmer?
CS50
Meteor: a better way to build apps by Roger Zurawicki
CS50
Data Analysis in R by Dustin Tran
CS50
Data Visualization and D3 by David Chouinard
CS50
CS50 2014 - Week 6
CS50
Build Tomorrow's Library by Jeffrey Licht
CS50
CS50 2014 - Week 9, continued
CS50
Essential Scale-Out Computing by James Cuff
CS50
iOS App Development with Swift by Dan Armendariz
CS50
Sam Clark Leads Yale Students on Tour to CS50 at Harvard
CS50
3D Modeling and Manufacture by Ansel Duff
CS50
CS50 2014 - Week 5, continued
CS50
hello, world
CS50
CS50 2014 - Deep Thoughts - Hash Table
CS50
CS50 2014 - Deep Thoughts - Binary Tree
CS50
CS50 2014 - Deep Thoughts - Scratch
CS50
CS50 2014 - Deep Thoughts - MySQL
CS50
LaunchCode Visits CS50
CS50
CS50 Live, Episode 100
CS50
CS50 Field Trip to Google
CS50
This is CS50 AP
CS50
Week 4: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2011 - Harvard University
CS50
Week 1: Wednesday - CS50 2011 - Harvard University
CS50
Week 11: Monday - CS50 2011 - Harvard University
CS50
Week 3: Wednesday - CS50 2011 - Harvard University
CS50
Week 12: Monday - CS50 2011 - Harvard University
CS50
Week 1: Friday - CS50 2011 - Harvard University
CS50
Week 3: Monday - CS50 2011 - Harvard University
CS50
Week 10: Wednesday - CS50 2011 - Harvard University
CS50
Week 2: Monday - CS50 2011 - Harvard University
CS50
Week 9: Monday - CS50 2011 - Harvard University
CS50
Week 7: Monday - CS50 2011 - Harvard University
CS50
Week 5: Monday - CS50 2011 - Harvard University
CS50
Week 5: Wednesday - CS50 2011 - Harvard University
CS50
Week 7: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Monday - CS50 2011 - Harvard University
CS50
Week 9: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Wednesday - CS50 2011 - Harvard University
CS50
Week 10: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2010 - Harvard University
CS50
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
Can AI Badger Reduce Local Coding Agent Token Usage?
Dev.to AI
Devin, the "First AI Software Engineer," Failed 86% of Its Benchmark Tasks, and Then What
Dev.to · Alex @ Vibe Agent Making
How I Built Captrix AI with Codex and TestSprite
Dev.to · ayush002jha
The Collapse of ‘Who Understands, Who Maintains’ — A Software Landscape in the AI Age
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI