Puzzles & Programming Problems (Think Like a Programmer)

Поділитися
Вставка
  • Опубліковано 10 січ 2025

КОМЕНТАРІ • 523

  • @vantonspraul
    @vantonspraul  7 років тому +24

    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: ua-cam.com/video/Xs-lY15vEec/v-deo.html

    • @JiveDadson
      @JiveDadson 7 років тому

      Here's more inspiration: ua-cam.com/video/YnWhqhNdYyk/v-deo.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 років тому +163

    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 років тому +1

      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 4 роки тому

      @@forestmanification hahahah

  • @ss-oq9pc
    @ss-oq9pc 7 років тому +23

    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 8 років тому +9

    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 9 років тому +98

    I have 8 years of programming experience, but this is a great video. I actually benefited from this.

    • @vantonspraul
      @vantonspraul  9 років тому +1

      +System Void Cool!

    • @ibrahimgreab8423
      @ibrahimgreab8423 9 років тому

      hey
      man can i have your Facebook account or email

    • @vantonspraul
      @vantonspraul  9 років тому +1

      +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 9 років тому

      +V. Anton Spraul
      okay man thanks to you
      I need advice from an expert programmer

    • @systemvoid287
      @systemvoid287 8 років тому

      Nobody ever said it was hard.

  • @vantonspraul
    @vantonspraul  11 років тому +4

    Glad you are enjoying the book. I've got some good puzzles worked out for upcoming videos. Coming soon!

  • @ruskodudesko9679
    @ruskodudesko9679 8 років тому +1

    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 8 років тому +2

    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 Рік тому +2

    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  Рік тому +2

      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.

  • @FrenzyT0004
    @FrenzyT0004 8 років тому +2

    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 років тому +3

    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 років тому +2

    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

  • @Xblade45
    @Xblade45 9 років тому +4

    Thank you Anton! Im looking forward to buy your book! This is really great tips you give.

    • @vantonspraul
      @vantonspraul  9 років тому +1

      +Xblade45 Thanks! I think you'll like the book as well.

  • @therealapollo6452
    @therealapollo6452 7 років тому +2

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

  • @TheDeybith
    @TheDeybith 8 років тому +5

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

  • @richdadsummit7557
    @richdadsummit7557 5 років тому +1

    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.

  • @PvblivsAelivs
    @PvblivsAelivs 9 років тому +4

    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)

  • @makodoseznamcz
    @makodoseznamcz 9 років тому +1

    Love this series ...it really gets my brain working as it should got all the problems in few seconds Thanks for making this

  • @gabecoelho647
    @gabecoelho647 6 років тому +12

    This is GREAT. Thank you!

  • @nitiKT
    @nitiKT 8 років тому +1

    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.

  • @ripndipp
    @ripndipp 5 років тому +14

    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.

  • @mxolisisilabela9803
    @mxolisisilabela9803 5 років тому +1

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

  • @cvsshred7
    @cvsshred7 9 років тому +635

    Who solved the watter puzzle imidiatly
    if you do thumbs up

    • @Walleggwp
      @Walleggwp 9 років тому +9

      +Carlon van spijker Pretty easy, tbh.

    • @onofreisk8
      @onofreisk8 9 років тому +3

      +Carlon van spijker as the 2nd container appeared on the screen !

    • @aidanmartin1860
      @aidanmartin1860 9 років тому +20

      +Carlon van spijker I thought he was joking about it being difficult.

    • @connor43057
      @connor43057 9 років тому +2

      lmao yeah I solved in under 10 seconds...

    • @saptarshisahoo5075
      @saptarshisahoo5075 9 років тому +2

      +Carlon van spijker Man both the probs were pieces of cake.

  • @TheNightFreaks
    @TheNightFreaks 5 років тому +17

    Thanks for giving me the illusion of being a genius by solving this right away. Now let me get back to bagging groceries.

  • @someone-x64
    @someone-x64 4 роки тому +1

    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.

  • @llucllumador380
    @llucllumador380 8 років тому +2

    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

  • @hiota45
    @hiota45 9 років тому +1

    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

  • @sagarkapasi099
    @sagarkapasi099 7 років тому +5

    i love this content so much no other channel does this thanks

  • @blackicelyriclyamazin7599
    @blackicelyriclyamazin7599 10 років тому +1

    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  10 років тому

      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.

  • @blackpilledbuddha4944
    @blackpilledbuddha4944 5 років тому

    There are thousnds of tutorails on learning various languages and frameworks.... but this series , i dont even need to mention it... thank you

  • @SoyMagnumOpus
    @SoyMagnumOpus 10 років тому +2

    For the cup scenario, it only took me like 5 seconds. I feel soooo special right now.

  • @peterbezak5204
    @peterbezak5204 6 років тому +3

    Wohoo! I figured it out in like 5 seconds! Never heard of it before btw.

  • @vantonspraul
    @vantonspraul  11 років тому +2

    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.

  • @JenniferDawe
    @JenniferDawe 11 років тому +2

    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 років тому +4

      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.

  • @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

  • @ultort
    @ultort 9 років тому +28

    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 років тому +5

      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 років тому +1

      @@shubhamdeshkar1685 Rules didn't say that you need to comment according to the point of the video.

  • @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!

  • @MrStefaneko
    @MrStefaneko 7 років тому +1

    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 6 років тому

      he explained the difference between permutations and combinations and he was right. the algorithm *tests every permutation and prints only the valid ones.

  • @alby7153
    @alby7153 5 років тому

    Sir , please continue this.This is really helpful

  • @JasonLayton
    @JasonLayton 4 роки тому

    So pumped that I was able to solve the last one! Thanks!

  • @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.

  • @10tmccarthy
    @10tmccarthy 8 років тому +2

    this is saving my life right now thank you!!

  • @Chng30FsCenEry
    @Chng30FsCenEry 6 років тому

    Finally, a video that answers the real challenge of programming.

  • @jaynjb7812
    @jaynjb7812 9 років тому

    These videos are a great compliment to the book. I am very happy with my purchase. Thanks V. Anton Spraul.

  • @Stickimations
    @Stickimations 11 років тому +2

    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!

  • @yukimori2104
    @yukimori2104 7 років тому

    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. ;-)

  • @nameStringEmpty
    @nameStringEmpty 8 років тому +5

    Saw the puzzle for fist time and solved it almost immediately. I think 90% CAN solve it not a few!

  • @Blackdiamond929_
    @Blackdiamond929_ 6 місяців тому

    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

  • @kenhaley4
    @kenhaley4 8 років тому +20

    Speaking as a programmer with over 40 years of experience, I find it too bad that the final solution he came up with was the most inefficient--discarding some of the optimizations that he'd already discovered. For example, it would still be valid to calculate the sanitation number by subtracting the sum of the other two numbers from 12, thus eliminating the innter loop. It would also still be valid to force the police department to be even, as he did in the earlier attempt.
    Part of "thinking like a programmer" is not being satisfied with the first solution that works. It is often worthwhile to re-examine your solution to see how it can be optimized. If you're writing production code that might be used thousands and thousands of times, it's not only worthwhile, it's vitally important. And--don't casually discard your earlier thoughts--they may still have value.

    • @ss-oq9pc
      @ss-oq9pc 7 років тому

      Any interest in making programming tutorial vids on youtube? lol.

    • @sixtycats8652
      @sixtycats8652 7 років тому +3

      Lmao you said “final solution”
      Is this really the right place

  • @player95
    @player95 9 років тому

    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 9 років тому

      +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 9 років тому

      +Fernando Santos cool ...!

    • @player95
      @player95 9 років тому

      +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 ...

  • @greyskull6548
    @greyskull6548 9 років тому

    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

  • @SceneTurkey
    @SceneTurkey 8 років тому

    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 роки тому +1

    My take away is "Take a holistic view of the problem first and keep all options open" and avoid confirmation bias as well

  • @xeno126
    @xeno126 8 років тому +10

    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 років тому +1

      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 років тому +1

      Red - Green - Refactor; Know what should happen, make it happen, make it better, repeat.

    • @jon8796
      @jon8796 8 років тому

      New to programming here.
      Care to write down example in pseudo code. Thanks

    • @1Mirar1
      @1Mirar1 8 років тому +1

      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

  • @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

  • @samguard8419
    @samguard8419 9 років тому

    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)

  • @haraldpost
    @haraldpost 8 років тому +1

    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.

  • @arantebw
    @arantebw 9 років тому +1

    V. Anton Spraul, thank you!

    • @vantonspraul
      @vantonspraul  9 років тому

      +Billy Wilson Arante You're welcome! Thanks for watching.

  • @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.

  • @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

  • @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 🌟

  • @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 9 років тому

    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

  • @andressuarez3079
    @andressuarez3079 5 років тому

    That was a great approach. Thank you very much.

  • @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.

  • @joediertay2434
    @joediertay2434 8 років тому

    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"

  • @mehboobfazal
    @mehboobfazal 7 років тому

    start with this: , then in another loops for other dept. skip that policeNum to reduce iteration count

  • @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

  • @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))

  • @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.

  • @tytydraco
    @tytydraco 8 років тому

    Extremely helpful! Will consider buying your book :)

  • @fatimahatef7175
    @fatimahatef7175 2 роки тому

    This video is very helpful and in the same time exciting 👍🏻

  • @ArronSealmoyGuitar
    @ArronSealmoyGuitar 6 років тому +1

    Your book is a life saver

  • @derekbrown5123
    @derekbrown5123 10 років тому

    Ordered your book today!

  • @Mareritt
    @Mareritt 9 років тому

    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 9 років тому

      +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

  • @alexandrenarolles7994
    @alexandrenarolles7994 2 роки тому

    very instructive! I do really benefit from it!

  • @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.

  • @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.

  • @cnorris1193
    @cnorris1193 8 років тому +5

    And I start geeking out over ham and Swiss and Swiss and ham lmao

  • @hemantrakesh5010
    @hemantrakesh5010 8 років тому

    I'm done with c++ .. what programming language should I start with next and why ?

  • @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

  • @LiteDev
    @LiteDev 9 років тому

    The permutations of the Department numbers are as follows:
    147.
    165.
    246.
    264.
    327.
    345.
    426.
    462.
    507.
    543.
    561.
    624.
    642.
    705.
    723.
    741.

    • @xzywx
      @xzywx 9 років тому

      +Zachary Blasczyk You included 0 for the second number.. It should be 1-7

    • @LiteDev
      @LiteDev 9 років тому +1

      +Raiki I see that you are correct. I will rework the program when I get to a computer.

    • @LiteDev
      @LiteDev 9 років тому

      +Raiki The permutations of the Department numbers are as follows:
      147.
      165.
      246.
      264.
      327.
      345.
      426.
      462.
      543.
      561.
      624.
      642.
      723.
      741.

  • @TheMrmartin21
    @TheMrmartin21 8 років тому +2

    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 років тому +2

      +King Martin : That was die hard. John Mclane solved it :)

    • @raybbaby
      @raybbaby 7 років тому

      Die Hard 3!!

  • @lukedeets5016
    @lukedeets5016 4 роки тому

    Very helpful videos! Thank you!

  • @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

  • @otard7742
    @otard7742 9 років тому

    The question actually doesn't say you can't display invalid permutations either. So in theory you could just display all permutations of 3 numbers ranging from 1-7. However i don't think it would pass on a test.
    puu.sh/nakhE/43d03e1591.jpg

  • @xzywx
    @xzywx 9 років тому +14

    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  9 років тому

      +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.

    • @xzywx
      @xzywx 9 років тому +1

      +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 9 років тому +1

      +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 9 років тому

      +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

    • @xzywx
      @xzywx 9 років тому

      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.

  • @swethaanand3088
    @swethaanand3088 5 років тому

    i need more of this... thanks a lot

  • @renyruth423
    @renyruth423 4 роки тому

    Really helpful... Please make more such videos... 🙂

  • @AttestedFire66
    @AttestedFire66 8 років тому

    It's 5am and I haven't slept but the water puzzle was easy as shit. Instantly got it.

  • @ixuz07
    @ixuz07 9 років тому +2

    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.

  • @mhc4124
    @mhc4124 7 років тому +1

    Programming comment threads and forums always remind me that fundamentalism is everywhere and that just because someone knows how to write code does not mean they understand what it means to be a scientist. I'm not a scientist, but I do think ethics and experimenting are more important than how smart you think you are.
    EDIT:
    My first solution was to simply count by twos and subtract 7 from the counter to get all permutations. Then I just looped through the result to find the one that satisfied the police chief's non-primacy law and the sum-to-twelve requirement.

  • @robinator18ps3
    @robinator18ps3 9 років тому

    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

  • @KingButcher
    @KingButcher 9 років тому

    python approach :)
    for x in range(1,8):
    for y in range(1,8):
    for z in range(1,8):
    #If even number
    if z % 2 == 0:
    #If there aren't any duplicates
    if len(list(set([x,y,z]))) == 3:
    #If all add to 12
    if x + y + z == 12:
    print "Police: {0} Fire: {1} Sanitation: {2}".format(z,y,x)

    • @soupra777
      @soupra777 9 років тому

      +King Butcher Is your program producing 10 permutations? I got 14. I noticed in your program if police == 2 then fire/sanitation is (4, 6). But they can also be (3, 7)

    • @KingButcher
      @KingButcher 9 років тому

      +ansquad0 ohh, its because the "range" function returns a list starting with 0. So I would have to do; *for _ in range(1,8)* instead.
      Ive fixed the mistake. Thanks for the correction :)

  • @supetorus9612
    @supetorus9612 9 років тому

    I did this in Python. I wrote it on paper first to figure it out then did it on the computer. I made a mistake that made it so 4 solutions were cut off because I accidentally didn't include 7 in my possible numbers. Other than that it worked on my second try.
    I think the way I did it might be slightly more optimized because it checks if the number is valid before it goes on to generating the next number so each preceding for loop has to run once less instead of having to run 399 loops. It only has to run 154 loops.

    • @gun123456zak
      @gun123456zak 9 років тому

      +Supetorus in Python the solution is nothing but a simple one liner

    • @gun123456zak
      @gun123456zak 9 років тому

      +Pyramid Head print [(f, p, s) for f in range(1, 8) for p in (2, 4, 6) for s in range(1, 8) if f != p and f != s and p != s and (f + p + s == 12)]

    • @beegieb4210
      @beegieb4210 9 років тому

      +Pyramid Head
      Better yet:
      [(f, p, s) for f in range(1, 8) for p in (2, 4, 6) for s in range(1, 8) if len({f, p, s}) == 3 and f + p + s == 12]
      Or if you want to make it even shorter and memory efficient:
      r = range(1, 8); [(f, p, s) for f in r for p in r[1::2] for s in r if len({f, p, s}) == 3 and f + p + s == 12]

  • @PalmerPaul
    @PalmerPaul 9 років тому

    One-liner in Python:
    print '
    '.join(map(lambda x:'{} - {} - {}'.format(*x),filter(lambda x:(len(x)==len(set(x))and sum(x)==12 and not x[1]&1),((a,b,c)for a in range(1,8)for b in range(1,8)for c in range(1,8)))))

  • @FuzzyDPozzy
    @FuzzyDPozzy 6 років тому

    i had the idea for the water cup but in all others i had no idea how to fix those pff

  • @edoardopona8512
    @edoardopona8512 10 років тому

    I managed to solve the second problem in python, in a slightly different way (probably because of differences in python). The idea is pretty much the same, but I had to add a couple "if " statements, because i couldn't get the loop to go through all the permutations without them,if anybody has suggestions on how to improve the code, they are very welcome to tell me!!
    police = 1
    fire = 1
    san = 1
    while police

    • @vantonspraul
      @vantonspraul  10 років тому

      Nice work. To clean this up, all you need to do is initialize each loop counter right before each loop starts, so then the nested loops work as intended, and you can control everything through indentation.
      police = 1
      while police

  • @tralee1495
    @tralee1495 8 років тому

    Awesome video dude!

  • @lecillefreakwizon7582
    @lecillefreakwizon7582 8 років тому

    a great help for a BSIT student like me..😁👍👍
    thank you sirrr