Тёмный

Puzzles & Programming Problems (Think Like a Programmer) 

V. Anton Spraul
Подписаться 30 тыс.
Просмотров 292 тыс.
50% 1

This episode introduces the idea that problem solving is essentially the same no matter what problem we're trying to solve, and looks at the connection between puzzles and solving programming Your comments and suggestions for future videos are welcome.
"Think Like a Programmer" is a book I've written to help programmers with problem solving. If you've found that you are able to read programs and understand programming language syntax but aren't always confident writing programs from scratch, my book can help.
For more information on the book head to one of these:
Amazon page for the book: amzn.to/1MZlmlY
My site: www.vantonsprau...
My publisher's site: nostarch.com/th...
Connect with me:
My site: vantonspraul.com
Twitter: / vantonspraul
Facebook: / thinklikeaprogproblems .

Опубликовано:

 

4 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 524   
@vantonspraul
@vantonspraul 7 лет назад
Inspired by the talk in this comment section of this video, I've created a new video about performance and efficiency. I discuss what the terms mean, why we should care about this, and why we sometimes shouldn't. Check it out here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-Xs-lY15vEec.html
@JiveDadson
@JiveDadson 6 лет назад
Here's more inspiration: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-YnWhqhNdYyk.html
@abelcasey9416
@abelcasey9416 3 года назад
Sorry to be so off topic but does anybody know of a way to log back into an instagram account?? I was stupid forgot the account password. I love any tricks you can offer me
@vibingcat9541
@vibingcat9541 2 года назад
@@abelcasey9416 no
@ieshamusgrove7883
@ieshamusgrove7883 6 лет назад
This comment section reminds me of something a senior programmer once told me. He said that there is a world of difference between the attitude of an inexperienced vs experienced programmers(with mid-level being the worst). Senior developers are much more nurturing and understanding while inexperienced developers have a tendency to berate others for mistakes/shortcomings/lack of knowledge. Most here didn't understand the point of the first problem and the lesson he was trying to convey. People just wanted to make themselves look smart while in reality looking very cringey.
@B1TruthBeTold1
@B1TruthBeTold1 5 лет назад
I agree
@sush2142
@sush2142 5 лет назад
Perfect
@forestmanification
@forestmanification 5 лет назад
Mid level represent! I thought of the if() solution as the easiest, least sophisticated solution which will 100% work right off the bat. The problem when you are so good at problem solving such as me, is that everyone start depending on you and you prevent them from developing their own problem solving skills. I curse the stars that made me so smart with bitter sorrow...
@cheems08213
@cheems08213 5 лет назад
I agree.
@iansalsich6508
@iansalsich6508 3 года назад
@@forestmanification hahahah
@ss-oq9pc
@ss-oq9pc 7 лет назад
One thing that's helped me a lot is whenever you see a 'for' loop or need one, instantly fill it in, in your mind, with 'for each'.
@charles-y2z6c
@charles-y2z6c 7 лет назад
I have been programming for 35 years. It is always good to go back to basics and review logic. It is like excercising your brain. Especially when you are learning a new language. Good video series
@systemvoid287
@systemvoid287 8 лет назад
I have 8 years of programming experience, but this is a great video. I actually benefited from this.
@vantonspraul
@vantonspraul 8 лет назад
+System Void Cool!
@ibrahimgreab8423
@ibrahimgreab8423 8 лет назад
hey man can i have your Facebook account or email
@vantonspraul
@vantonspraul 8 лет назад
+ibrahim greab Not sure who you meant to reply to...but if you meant me, I'm at facebook.com/vanton.spraul, plus you can contact me from my site at vantonspraul.com/contact.
@ibrahimgreab8423
@ibrahimgreab8423 8 лет назад
+V. Anton Spraul okay man thanks to you I need advice from an expert programmer
@systemvoid287
@systemvoid287 8 лет назад
Nobody ever said it was hard.
@ruskodudesko9679
@ruskodudesko9679 8 лет назад
my first course in my computer science education was just on how to think like this our professor was a master problem solver and sometimes would show off but it was good to see what we could eventually accomplish at first we didnt like it but he showed us the importance of solving the problem on paper before you even type any code out and it has helped tremensdously in the coding courses
@verseve
@verseve 7 лет назад
im a recent grad in programming. and i completely failed my first interview. this video helped me so much! i learned so much cause i am one of those over thinkers that writes complicated code making problem solutions more complicated than they have to be.
@fmaylinch
@fmaylinch Год назад
I think that for problem solving it's also very important to know what actions you can do. For example, in the water problem you can pour from one container to the other. In programming you also need to know what instructions you can do, what commands are available, what APIs you can call, and in general what components/pieces/actions are involved/possible in the problem.
@vantonspraul
@vantonspraul Год назад
Yes! In the book, I use the example of the fox, goose, and corn puzzle, which has the same issue. If you define your available actions as, "the boat can take the farmer and one item from the west side of the river to the east side," the problem isn't solvable. If you define the actions as, "I have a boat that can travel from one side of the river to the other, and at either end I can embark or disembark so long as I am only carrying one item at a time," the problem is solvable.
@vantonspraul
@vantonspraul 11 лет назад
Glad you are enjoying the book. I've got some good puzzles worked out for upcoming videos. Coming soon!
@FrenzyT0004
@FrenzyT0004 7 лет назад
I was so proud of myself when I heard the solution for the permutation problem's solution.... Thank you, this is what I and many other programming learners need.
@harito16
@harito16 5 лет назад
Thank you for the video, it helped me get a clearer picture of how programming needs to be approached. I bought your book and I am going to start reading it RIGHT NOW!
@Dawo0odz
@Dawo0odz 7 лет назад
I had started programming since 3 moths and I found the solution from the first time but I think that what you did in for loop is more more more interesting
@gabecoelho647
@gabecoelho647 6 лет назад
This is GREAT. Thank you!
@vantonspraul
@vantonspraul 6 лет назад
You're welcome!
@ripndipp
@ripndipp 4 года назад
Oh boy did that department problem make my heart skip and beat. I'm watching this to improve my problem solving skills in JavaScript. I am coming from a medical background with JS bring my first language.
@TheDeybith
@TheDeybith 8 лет назад
Thank you for the information, it's a great video. I am learning to program with Python. I consider myself a beginner in programming. Even though, I solved that problem (without looking at the answer) in 5 lines! Now I really understand why so many people prefer Python...
@PvblivsAelivs
@PvblivsAelivs 8 лет назад
That's funny. My mind jumped to generating all permutations and having an IsValid function while you were describing the problem. But I might include looping the "police" number by 2 and determining the "sanitation" number as forced by the other two as refinements to make the program faster. The most important part is always getting the code to work.
@rcoservicerapporter6257
@rcoservicerapporter6257 Год назад
The most important part is writing as many lines as possible for the same thing so the company can get paid per line.. So always do: var one = 1 var two = 2 print(one + two)
@richdadsummit7557
@richdadsummit7557 5 лет назад
Never programmed before but my first thought was to output all permutations, but them I figured maybe it wasn't allowed, then it turned out to be the solution. Maybe this programming thing will work out for me after all. This video will still be very helpful for me, as other problems have tripped me up in the past.
@Xblade45
@Xblade45 8 лет назад
Thank you Anton! Im looking forward to buy your book! This is really great tips you give.
@vantonspraul
@vantonspraul 8 лет назад
+Xblade45 Thanks! I think you'll like the book as well.
@llucllumador380
@llucllumador380 7 лет назад
I just recently started to study programming. I'm a very beginner. As you suggested I paused the video and tried to make the department number program. I came up with a solution: I did the same 3 loops you did for your 0-10 permutations: for (int fire=1;fire
@nitiKT
@nitiKT 7 лет назад
Thank you for the video, I am a struggling software engineer who is trying to crack white board interviews and failing miserably. Looking at your videos make me realize that my thinking is right.. I should just fine tune the way I think and believe more in my abilities.
@sagarkapasi099
@sagarkapasi099 7 лет назад
i love this content so much no other channel does this thanks
@makodoseznamcz
@makodoseznamcz 8 лет назад
Love this series ...it really gets my brain working as it should got all the problems in few seconds Thanks for making this
@mxolisisilabela9803
@mxolisisilabela9803 4 года назад
My Professor has failed me twice in a TDD exam and when I asked what to do in order to pass his exam, he said I am not a programmer. Thank you for this Playlist. I can't wait for a retake!!!
@blackpilledbuddha4944
@blackpilledbuddha4944 4 года назад
There are thousnds of tutorails on learning various languages and frameworks.... but this series , i dont even need to mention it... thank you
@blackicelyriclyamazin7599
@blackicelyriclyamazin7599 9 лет назад
Im a programmer and I solved both puzzles. I did the second one in java just to test it. These were really nice. I remember when I had to write a piece of code that displayed all prime numbers in an interval without using the modulus operator. Its rather easy but back then I was just starting to code. It was frustrating but once i figured it out I felt so special. LOL
@vantonspraul
@vantonspraul 9 лет назад
I define "original" code as "original for the programmer writing it." Figuring out a problem on your own should make you feel good, even if you know many have solved it before.
@hiota45
@hiota45 8 лет назад
This would be a situation where you could avoid brute forcing by just thinking of all possible number triples. If the PD has to have an even number between 1 and 7, it has to have either 2, 4, or 6. Whatever number it takes, the other two have to have two different numbers that are the difference between it and 12, which result in rather few possibilities. Below is a quick javascript solution. //in each sub array the 0th is the PD, 1st is FD, and 2nd is SD. var permutations = [[2,6,4], [2,7,3], [4,7,1], [4,6,2], [4,5,3], [6,5,1], [6,4,2]]; /*because the above are just all possible number triplets, not all possible permutations... we need to account for the possible permutations of the above triplets fortunately, this just means switching the FD and SD numbers, because the PD's number has to be fixed. This can be done with a for loop, and we'll just tack the results on to our array.*/ for(var i = 0; i
@therealapollo6452
@therealapollo6452 7 лет назад
The "lesson" or message from this video means you should approach problems for a different angle and interpret it differently. It was not meant to be a video on how to write efficient code. Dont start comment wars please ^^
@peterbezak5204
@peterbezak5204 6 лет назад
Wohoo! I figured it out in like 5 seconds! Never heard of it before btw.
@FernandoTheBeast28
@FernandoTheBeast28 10 лет назад
For the cup scenario, it only took me like 5 seconds. I feel soooo special right now.
@TheNightFreaks
@TheNightFreaks 5 лет назад
Thanks for giving me the illusion of being a genius by solving this right away. Now let me get back to bagging groceries.
@Blackdiamond929_
@Blackdiamond929_ 2 месяца назад
Becoming a programmer is first recognising you have a great need for this type of thinking . It’s like my brain need to lift som weight before I can flex and solve these problems
@10tmccarthy
@10tmccarthy 8 лет назад
this is saving my life right now thank you!!
@vantonspraul
@vantonspraul 8 лет назад
+Tom McCar Glad I could help!
@Chng30FsCenEry
@Chng30FsCenEry 6 лет назад
Finally, a video that answers the real challenge of programming.
@xeno126
@xeno126 8 лет назад
Damn that's probably the least optimal solution! Try checking the necessary conditions at each loop and break if they are not met.
@terencekreft482
@terencekreft482 8 лет назад
Yes, but as a programmer you get a working solution and then optimise. My first technical directors mantra was KISS (Keep It Simple Stupid). It works as a principle for getting the right code, and for writing maintainable code.
@joshuaentwistle960
@joshuaentwistle960 8 лет назад
Red - Green - Refactor; Know what should happen, make it happen, make it better, repeat.
@jon8796
@jon8796 7 лет назад
New to programming here. Care to write down example in pseudo code. Thanks
@1Mirar1
@1Mirar1 7 лет назад
You negate the condition (what do we do if it is not the case) and break. Example in the inner most loop: if ( fireDeptNum == policeDeptNum) break; //... if( policeDeptNum % 2 != 0) break; // Iff we get this far we know all conditions were met therefore std::cout
@brainstorm9372
@brainstorm9372 7 лет назад
int N = 7; // which number can we use aka range from 1 to N int maxSum = 12; // what is the sum of all three numbers we need for ( int fireDep = 1; fireDep = maxSum ) // If the sum of the first two numbers exceeds maxSum -> break (all sums that follow will be greater than maxSum) break; int sanDep = maxSum - fireDep - polDep; if ( sanDep == fireDep || sanDep == polDep ) // If the third number equals one of the first two numbers -> continue continue; // Output the permutations that coresponds to all three rules std :: cout
@JenniferDawe
@JenniferDawe 11 лет назад
Wow, I've always been good at puzzles and riddles, but programming made me feel like I was a 2 year old trying to jam a square block in a round hole. I had no idea how to turn my solution into code without just doing it by rote. I've been working with programmers in software for over 10 years... I can come up with "code" in my head and when I explain it to programmers I work with they can make it work...but I couldn't do it myself and I really wanted to be better at that! Thank you so much for this, I will definitely be buying your book!
@vantonspraul
@vantonspraul 11 лет назад
Thanks! I hope my videos and book help. Believe me, lots of people with demonstrated smarts in solving problems in other areas are initially stonewalled when it comes to programming, so you're not alone. But if you enjoy programing and have a plan for developing your problem solving skill, the ability will come.
@arantebw
@arantebw 8 лет назад
V. Anton Spraul, thank you!
@vantonspraul
@vantonspraul 8 лет назад
+Billy Wilson Arante You're welcome! Thanks for watching.
@MrStefaneko
@MrStefaneko 7 лет назад
Those were not permutations, but variations. Permutations are something different, like if you have 3 peedifined digits, you can form 3! = 6 different numbers out of them (if you must use all of them exactly once). If you can use any digit and you can use it more than once, then you can form 10^n (in our case 10^3 = 1000) numbers. That is called the variations.
@them4309
@them4309 5 лет назад
he explained the difference between permutations and combinations and he was right. the algorithm *tests every permutation and prints only the valid ones.
@nameStringEmpty
@nameStringEmpty 8 лет назад
Saw the puzzle for fist time and solved it almost immediately. I think 90% CAN solve it not a few!
@JasonLayton
@JasonLayton 3 года назад
So pumped that I was able to solve the last one! Thanks!
@vantonspraul
@vantonspraul 11 лет назад
Thanks for the comment. Problem solving develops with practice, like any other skill. My book has lots of exercises to work through. If you can't get your hands on it, my next video may give you some ideas on developing your own exercises.
@jaynjb7812
@jaynjb7812 8 лет назад
These videos are a great compliment to the book. I am very happy with my purchase. Thanks V. Anton Spraul.
@player95
@player95 8 лет назад
when u showed problem i paused video..opened turboC++ and started codding took approx. 20 min to solve it .. i used 3 vari. ...police vari was an array with all possible even numbers and then i put 3 for loops to travel all possible sequences ..finally in the center i put if condition like (all vari should equal and sum==12).. gave multiple solutions on console..! tx 4 brainteaser :D ...
@meunomejaestavaemuso
@meunomejaestavaemuso 8 лет назад
+Rushikesh Raskar I did the same, but instead I did it in python. Took me less than 5 minutes o.o for f in range(1, 8): for p in range(2, 8, 2): # police is even, we start at 2 and skip even for s in range(1, 8): if f != p and p != s and s != f and f + p + s == 12: print f, p, s
@player95
@player95 8 лет назад
+Fernando Santos cool ...!
@player95
@player95 8 лет назад
+Fernando Santos I used different methods .like I try to use just one for loop ...stuck at various points ...then came up with that array idea ...
@alby7153
@alby7153 5 лет назад
Sir , please continue this.This is really helpful
@joel9909
@joel9909 5 лет назад
I am thinking like a programmer already. Got the water challenge at first glance. Maybe its cos i studied science at school, and did this whenever i did not have the right measuring beaker....... But the second problem. That requires serious programming. Thanks Anton
@cvsshred7
@cvsshred7 8 лет назад
Who solved the watter puzzle imidiatly if you do thumbs up
@Walleggwp
@Walleggwp 8 лет назад
+Carlon van spijker Pretty easy, tbh.
@onofreisk8
@onofreisk8 8 лет назад
+Carlon van spijker as the 2nd container appeared on the screen !
@aidanmartin1860
@aidanmartin1860 8 лет назад
+Carlon van spijker I thought he was joking about it being difficult.
@connor43057
@connor43057 8 лет назад
lmao yeah I solved in under 10 seconds...
@saptarshisahoo5075
@saptarshisahoo5075 8 лет назад
+Carlon van spijker Man both the probs were pieces of cake.
@samguard8419
@samguard8419 8 лет назад
I made the it in small basic and the total outcomes were 14, although that wasn't the problem: For p = 2 To 6 For f = 1 To 7 For s = 1 To 7 If p + s + f = 12 And (p/2)-Math.Round(p/2) = 0 and p f And f s And p s Then TextWindow.WriteLine("police "+p) TextWindow.WriteLine("fire "+f) TextWindow.WriteLine("Sanitation "+s) outComes = outComes + 1 EndIf EndFor EndFor EndFor TextWindow.WriteLine(outComes)
@tommyeagen3763
@tommyeagen3763 4 года назад
Currently, eating the book it's an amazing book I have a bachelor's degree in computer science and informal system but your book is super awesome 🌟
@greyskull6548
@greyskull6548 8 лет назад
okay! thanks for ideas but that water puzzle was common for me and once again thanks a lot Hope you will share more puzzle coming days
@mishaerementchouk
@mishaerementchouk 3 года назад
These are cute little problems illustrating a whole bunch of important points. The measuring problem is fun to follow with an “exercise”: solve the same problem but with a 6 oz instead of 5 oz. The modified problem looks similar but has no solutions (see Diophantine equations). Thus, it is important to distinguish a general case from a particular given situation. Hence, there may be the need for finding out, if we’re in a good or bad situation. The enumeration problem brings about many contexts: from the 3-partition problem, to various linear and nonlinear programs. An important lesson: don’t fall into the trap of premature optimization. What if the next year another departments, say, building and finance, are added. And the whole enumeration thing must be done in literals (county started to comply with state’s recommendations) but no combination of chosen literals may contain a reserved word with the list of the words provided. Good video!
@AttestedFire66
@AttestedFire66 7 лет назад
It's 5am and I haven't slept but the water puzzle was easy as shit. Instantly got it.
@cnorris1193
@cnorris1193 8 лет назад
And I start geeking out over ham and Swiss and Swiss and ham lmao
@ArronSealmoyGuitar
@ArronSealmoyGuitar 5 лет назад
Your book is a life saver
@ultort
@ultort 8 лет назад
I think there is something wrong with the puzzle, the aim is not to get 2oz (seriously there is nothing to think about). The puzzle is to get 4oz with 5oz and 3oz. Much less straight forward. Here is the answer: fill 5 fill 3 with 5 => 2 left in 5 empty 3 => 0 left in 3 put the 2 left from 5 in 3 => 2 in 3 fill 5 => 5 in 5 fill 3 with 5 => 4 in 5
@ThePimp4dawin
@ThePimp4dawin 7 лет назад
tbh that was still just as easy.
@fatalsg92
@fatalsg92 6 лет назад
or you could just fill another 3rd empty cup with the 2oz water left from the 5oz cup and repeat the process. (the rules did not say that there "should" only be 2 cups, so I added another variable.)
@purpletonkadog
@purpletonkadog 5 лет назад
if 5 = 4 stop loop fill 3 and pour in 5 if 5 = 5, empty 5 and fill with 3
@shubhamdeshkar1685
@shubhamdeshkar1685 5 лет назад
That was not the point of this video,
@vibsh625
@vibsh625 5 лет назад
@@shubhamdeshkar1685 Rules didn't say that you need to comment according to the point of the video.
@SceneTurkey
@SceneTurkey 7 лет назад
An if statement checking for the correct answer was the first thing I thought of because I like to debug incorrect answers by seeing why they are incorrect. It's kind of a faster way to do trial and error. Also, for the cup problem, you used the wrong number; you're supposed to make 4 ounces of water, not 2.
@FourDotSkills
@FourDotSkills 4 года назад
My take away is "Take a holistic view of the problem first and keep all options open" and avoid confirmation bias as well
@joediertay2434
@joediertay2434 7 лет назад
I totally had the wrong idea with the measuring cup problem lmao. I though "well if you fill up both measuring cups to the max, place the 5 oz cup inside a bowl, and pour the 3 oz into the 5 oz: the displaced water will be exactly 2 oz"
@andressuarez3079
@andressuarez3079 5 лет назад
That was a great approach. Thank you very much.
@KoshNaranick
@KoshNaranick 3 года назад
Just bought the book.. hope that being from 2012 that it still works.. not for C++ but for understanding the mind set
@yukimori2104
@yukimori2104 6 лет назад
Maybe the first problem with the two cups is considered difficult, because the first example shows that both cups are already filled. If only the 5 oz. cup were filled, and the 3 oz. is shown as being empty, more people should instantly arrive at the solution: 1: 5-3 = 2 2: Pour water from 5 oz. to 3 oz. cup 3: Leaving 2 oz. of water in the 5 oz. cup Also the second example in which the problem is restated, for some people it ends up being difficult. They would remain being unable to see that pouring fluid from the 5 oz cup into a filled 3 oz cup to arrive at 2 oz remaining in the 5 oz. cup. The reason is that, when given a problem such like this, people tend to think that they aren't allowed to empty one cup, or pour liquid from one into the other. Specifically when there is no mention that such things are allowed in the problem description people wouldn't even consider doing it. Which makes an utmost simple problem a big thing. Would the description say, or the problem be given as: "You have a 5 oz. cup filled with water, you have an empty 3 oz. cup. You wish to measure exactly 2 oz. How are you going to do it?" instead of showing an additional example where both cups will be filled? It is simple maths that 5-3 is 2. And if one 5 oz. cup is filled, the other left empty, and you wish to arrive at 2, there is only one simple way to get there. ;-)
@edgarsanmartinjr.4278
@edgarsanmartinjr.4278 4 года назад
The first problem was so simple, i can’t believe I didn’t get it immediately lolol I had identified that the difference between the two cups was exactly what was needed so I had thought just fill an empty 3oz cup with the 5oz cup over a pan, and the spilt over water should be collected which should equal 2oz
@alexandrenarolles7994
@alexandrenarolles7994 Год назад
very instructive! I do really benefit from it!
@Stickimations
@Stickimations 11 лет назад
Great tutorial. I was really suprised at the simple solution to the last problem :) I am reading your book, loving it so far. You should do more tutorials on puzzles like the last one in the video!
@fatimahatef7175
@fatimahatef7175 2 года назад
This video is very helpful and in the same time exciting 👍🏻
@Mareritt
@Mareritt 8 лет назад
It was actually pretty simple. I just did the main loop with the police department at 2, 4 and 6, then I nested two more loops for the fire and sanitation departments. In the second nested loop, I checked to see if both the total equaled 12 and if the fire and sanitation departments didn't equal each other - you don't have to worry about either one being equal to the police department. Then displayed all those that passed the if statement. Mind you, I haven't watched what his solution is yet so I'm not sure what he did in the video to solve the problem.
@Mareritt
@Mareritt 8 лет назад
+Mareritt Here's the code if anyone's curious. int nTotal = 12; // needed total for ( int pDept = 2; pDept < 7; pDept += 2) // pDept = police department { for( int fDept = 1; fDept < 8; fDept++) // fDept = fire department { for ( int sDept = 1; sDept < 8; sDept++) // sDept = sanitation department { int dTotal = pDept + fDept + sDept; // dTotal = department total if ((nTotal == dTotal) && (fDept != sDept)) { std::cout
@EagleTrue
@EagleTrue 7 лет назад
Thanks man !
@swethaanand3088
@swethaanand3088 5 лет назад
i need more of this... thanks a lot
@formulaintuition8756
@formulaintuition8756 6 лет назад
import itertools a = [x for x in range(1, 11)] permutations = itertools.permutations(a, 3) for p in permutations: if sum(p) == 12 and p[1] % 2 == 0: print(p) I did this, not sure if it's right
@emptyFull123
@emptyFull123 4 года назад
I've written last permutuation problem in different approach in python. Please check anyone if I'm right or wrong. Please correct if i'm wrong :) for police in range(2,7,2): for fire in range(1, 8): for sanitation in range(1, 8): if police + fire + sanitation == 12: print(police, fire, sanitation) Here, police should be in even so initially I've looped from 2 to 6 in interval of 2, (So i don't need to worry about even number of police) within that loop I ran another for loop for fire, and within those both loops I ran another for loop for sanitation, and at last I'm printing only those values whose sum is equal to 12 Total permutuations: (Police, fire, sanitation) 2 3 7 2 4 6 2 5 5 2 6 4 2 7 3 4 1 7 4 2 6 4 3 5 4 4 4 4 5 3 4 6 2 4 7 1 6 1 5 6 2 4 6 3 3 6 4 2 6 5 1
@musaddiqali1
@musaddiqali1 3 года назад
All the permutations are valid except 2 5 5, 4 4 4, 6 3 3.
@tytydraco
@tytydraco 8 лет назад
Extremely helpful! Will consider buying your book :)
@musaddiqali1
@musaddiqali1 3 года назад
My program using Python: def main(): for fir in range(1, 8): for pol in range(2, 8, 2): for san in range(1, 8): if fir != pol and pol != san and fir != san and (fir + pol + san == 12): print(f"Fire: {fir} - Police: {pol} - Sanitation: {san}") return 0 main() Not the best-optimized code, but it just works with 14 permutations as the result.
@PhoenixPho
@PhoenixPho 8 лет назад
To me brute-force solutions always feels weird and inefficient what only beginner and bad programmers would do.. And for some reason all the tutorials tries to describe easy logic problems as hard imperative problems to scare people away. Why not use logic programming instead then?
@vantonspraul
@vantonspraul 8 лет назад
+Raiki Thanks for watching and commenting. In this video I'm not trying to promote brute-force solutions, just getting across the general idea that often attacking a problem from a previous unconsidered angle can quickly produce a solution. That said, it's a mistake to think that general-but-less-efficient coding is purely the realm of the inexperienced or unqualified programmer. Depending on the situation, developing a specific-and-more-efficient solution may be more trouble than it is worth. I'm seeing a lot of comments in this vein, though, and so I'm working on a new video on efficiency.
@PhoenixPho
@PhoenixPho 8 лет назад
+V. Anton Spraul I can understand that it's hard to find a good example what programmers do yet still being efficient. The real world problems and the human brain stand closer to declarative languages especially to logic programming where we have to give the goal and the rules and the program will solve us the problem. When we take such a problem as an example and try to solve it with imperative programming, it often yields an inefficient O(n2) solution even though it may have a better O(n) or O(n*logn) solution. The general idea is that the problem should be something that can be efficiently solved in this way and if it's not, than it should be told how to convert it to such a problem. At 4:00 the lock problem in not permutation but variation with repetitions which can be solved with 3 nested loops. The problem at the end is variation without repetition (still not permutation but closer) where you pick a number from the set of 1-7 and remove it so it can't be picked again later. A whole different problem and a different way of thinking. It doesn't matter now but it will matter later and in my opinion it's better to teach it sooner than later. Anyway good job and keep it up!
@mikstratok
@mikstratok 8 лет назад
+Raiki I always find it a good practice to use the brute force solution as the starting point and optimize from there, don't get me wrong... I also get a bad bad feeling when writing inefficient code... BUT, as practicality dictates: unless the code it is going to run billions of times, the brute force solution is the simplest solution and therefore probably the best solution. Also, you can always add a "//is optimizable" flag in the source code for later :D
@Rockem1234
@Rockem1234 8 лет назад
+Raiki Many times, the best solution comes from brute force solutions, sometimes it's easier to see what is wrong and how to improve it from the trivial solution TDD is actual promotes that approach
@PhoenixPho
@PhoenixPho 8 лет назад
What I mean is, that approach doesn't always work. You can't convert an algorithm to a completely different one by simply making improvements on it. And usually those who don't bother thinking it over and starts off with a brute force solution don't really care about efficiency. As soon as it works, they leave it as it is. I was taught that always think/plan first and code afterwards instead of code first and then try to think about it afterwards.
@christophbuhler4513
@christophbuhler4513 8 лет назад
I would recommend to delimit your dataset. It is not only (way) faster, it makes the code more readable and allows you to actually solve complex problems. Programming is not about adding valid data; It's about subtracting invalid data. The code in this video (10:40) smells. JS approach: for (var i=1; i
@terencekreft482
@terencekreft482 8 лет назад
No, programming is primarily about getting the right answer.
@terencekreft482
@terencekreft482 8 лет назад
No, programming is primarily about getting the right answer.
@ikirstenHD
@ikirstenHD 8 лет назад
Been programming for a little lest that 6 months. I did it my own way(little longer than your code) and these where the results: 1-5-6 1-7-4 2-4-6 2-6-4 3-5-4 3-7-2 4-2-6 4-6-2 5-1-6 5-3-4 6-2-4 6-4-2 7-1-4 7-3-2 Oh and I remember the water question, our lecturer gave us that on the first day of class. I never figured it out. Six months latter this video shows up on my homepage and instantly figured out the question when I saw it LOL
@mehboobfazal
@mehboobfazal 7 лет назад
start with this: , then in another loops for other dept. skip that policeNum to reduce iteration count
@codykostyak3890
@codykostyak3890 10 лет назад
my thoughts on the water puzzle was to pour the two full containers into an unmarked container, then scoop it out with the 3 ounce cup, and the remainder is 2... i was close :P
@andrei-ionutgranat9321
@andrei-ionutgranat9321 3 года назад
Your solution is good, but it can get simple optimisation aka you could at least do the police departament to be even always and not generate the sanitary, just check if the sum is < 12 in wich cas us true the sanitary becomes the remaining part and then check the if they are different. Way faster( 2 less checks for each posible combination and not generating a third value and generating 3 police values instead of 4. Further optimisation is posible but imma not go into details.
@missdragon5892
@missdragon5892 6 лет назад
The solution to your problem with the coding was the first one I came up with, the problem is Idk python, so I wasn't sure if you could optimize it more.
@risaverc1217
@risaverc1217 2 года назад
I have no experience with Programming whatsoever so I have no idea how this would be executed however I thought for the Dept problem the police number would always be 2,4 or 6 for each of these the sum of the other two has to be either 10, 8 or 6 I wonder if a loop could be coded only for numbers to add to those quantities and be different from one another
@MakeWithData
@MakeWithData 8 лет назад
Simply statistics would help a lot with the generation algorithm. No repeating numbers means there are 7x6x5 possible arrangements, so that's your for loops right there. The rest is straightforward
@christophbuhler4513
@christophbuhler4513 8 лет назад
The riddle (10:40) would have been more interesting, if the police dept (i) only wanted odd numbers. You wouldn't have to check, whether the fire / sani dept have the same number, because the sum (12) minus any odd number (i) would always result in an odd number, that cannot consist of two times the same number. for (var i=1; i
@lecillefreakwizon7582
@lecillefreakwizon7582 8 лет назад
a great help for a BSIT student like me..😁👍👍 thank you sirrr
@derekbrown5123
@derekbrown5123 9 лет назад
Ordered your book today!
@vantonspraul
@vantonspraul 9 лет назад
Cool, thanks!
@fajle_rayhan
@fajle_rayhan 6 лет назад
Good to see...
@charlesbrown2782
@charlesbrown2782 6 лет назад
I assumed both jars were filled. I poured half of the water out of the 3oz jar leaving only 1.5oz. I then fill the 3oz jar back to 3oz from the 5oz jar of water leaving 3.5oz of water in the 5oz jar. I repeat the process again and that leaves me with 2oz of water in the 5oz jar and 3oz of water in the 3oz jar. This all works if I'm allowed to eyeball what I think to believe is half of the water left in the 3oz jar. But eyeballing half is pretty close in general, so I believe this should work. But if I knew only the 5oz was full and the 3oz jar was empty that would have been a no brainer addition and subtraction problem.
@ervins3910
@ervins3910 9 лет назад
SO FUCKING AWESOME ! THANKS for the book and videos! That is what I were looking for all the time! Greets from Slovenia.
@vantonspraul
@vantonspraul 9 лет назад
+Ervin S YOU'RE VERY WELCOME! :-). Seriously, glad these have helped you out.
@ixuz07
@ixuz07 8 лет назад
I solved the first puzzle in a few seconds, but once you started emphasize the difficulty I had to go back and ensure I understood the problem correctly. My first solution was the right one. The second puzzle was quickly solved too. I'm a programmer.
@lukedeets5016
@lukedeets5016 4 года назад
Very helpful videos! Thank you!
@haraldpost
@haraldpost 7 лет назад
Nice video, but very inefficient way of solving the task. Time complexity is O(n^3). You are doing 7*7*7 iterations. You can reduce number of iterations by 4/7th's if you put policeDeptNum % 2 == 0) in the outer loop... And since you know that (12 - policeDeptNum - fireDeptNum = sanitaryDeptNum) you don't need the third loop. I was able to find all 14 solutions doing no more than 27 iterations.
@ImZenaan
@ImZenaan 8 лет назад
I solved the department program extremely quickly :D I love problems like this, make the computer do the generating and make it check it too, no need to manually check :P
@TheMrmartin21
@TheMrmartin21 8 лет назад
the real hard problem is when 4 oz are needed. I first saw this problem on Lethal Weapon or was it Die Hard.
@toabhijeetsingh
@toabhijeetsingh 8 лет назад
+King Martin : That was die hard. John Mclane solved it :)
@raybbaby
@raybbaby 6 лет назад
Die Hard 3!!
@umair-anwar
@umair-anwar 4 года назад
The Department Number Permutation can be solved with only one loop. O(n). Just try solving it by getting total number of permutations by 7 raise to power 3. You will figure out the rest yourself.
@brahmabull4004
@brahmabull4004 3 года назад
this makes no sense
@heathergray4880
@heathergray4880 6 лет назад
My solution in Python 3 for the problem. It comes back with a solution of 17 permutations: num_list = [] for fire in range(1, 8): for police in range(2, 8, 2): for sani in range(1, 8): if fire + police + sani == 12: num_list.append((fire, police, sani)) print(len(num_list))
@sankeethganeswaran3024
@sankeethganeswaran3024 5 лет назад
the answer should be 14
@robinator18ps3
@robinator18ps3 8 лет назад
I kinda thought backwords on the water problem :D I was thinking that you could take the 3 oz and fill up the 5 oz from the 3 oz twice. The 2nd time you fill the 5 oz 1 oz would pour over (overfill?) and you could do that twice. xD
@edward3105
@edward3105 4 года назад
I answered in like 5 seconds on problem,I want start learn programming but I dont think I am smart enough,I am an emotional guy so big heart and small brain.What do you think should I do?
@anonymoustroll8062
@anonymoustroll8062 4 года назад
I just bought your books how to think like a programmer it is okay to use even though it's 2012. And by the way Imy language was python
@yashwantsingh6703
@yashwantsingh6703 3 года назад
Damn, that was fun.
@Alexander_Meyer
@Alexander_Meyer 8 лет назад
Great video, thank you very much!
@ydela1961
@ydela1961 7 лет назад
Funny, I came up with something like: tot = 12; maxNum= 7; for (p = 2; p
@ankitaharwal5886
@ankitaharwal5886 5 лет назад
love the tutorial but the suddenly stopping of audio made really hard to understand voice : (
@hemantrakesh5010
@hemantrakesh5010 8 лет назад
I'm done with c++ .. what programming language should I start with next and why ?
Далее
The Blank Screen (Think Like a Programmer)
8:00
Просмотров 72 тыс.
Recursion (Think Like a Programmer)
10:42
Просмотров 156 тыс.
Living life on the edge 😳 #wrc
00:17
Просмотров 7 млн
The Harsh Reality of Being a Software Engineer
10:21
Просмотров 2,9 млн
How To Think Like A Programmer
1:00:07
Просмотров 2 млн
WHY IS THE HEAP SO SLOW?
17:53
Просмотров 229 тыс.
Premature Optimization
12:39
Просмотров 814 тыс.
10 Programming Languages in ONLY 15 minutes!
15:14
Просмотров 700 тыс.