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
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
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.
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...
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
+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.
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
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.
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.
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.
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.
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!
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
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 ^^
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...
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.
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.
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)
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.
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.
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!!!
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
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
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
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
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.
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.
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!
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.
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
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
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.)
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!
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.
he explained the difference between permutations and combinations and he was right. the algorithm *tests every permutation and prints only the valid ones.
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
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!
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. ;-)
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
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.
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 ...
+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
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.
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.
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
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
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
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)
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.
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.
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
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
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.
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"
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
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))
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.
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.
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.
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.
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
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
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
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?
+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.
+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!
+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
+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
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.
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.
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.
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
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)
+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)
+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 :)
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.
+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]
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)))))
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
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
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
Here's more inspiration: ua-cam.com/video/YnWhqhNdYyk/v-deo.html
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
@@abelcasey9416 no
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.
I agree
Perfect
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...
I agree.
@@forestmanification hahahah
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'.
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
I have 8 years of programming experience, but this is a great video. I actually benefited from this.
+System Void Cool!
hey
man can i have your Facebook account or email
+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.
+V. Anton Spraul
okay man thanks to you
I need advice from an expert programmer
Nobody ever said it was hard.
Glad you are enjoying the book. I've got some good puzzles worked out for upcoming videos. Coming soon!
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
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.
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.
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.
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.
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!
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
Thank you Anton! Im looking forward to buy your book! This is really great tips you give.
+Xblade45 Thanks! I think you'll like the book as well.
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 ^^
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...
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.
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.
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)
Love this series ...it really gets my brain working as it should got all the problems in few seconds Thanks for making this
This is GREAT. Thank you!
You're welcome!
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.
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.
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!!!
Who solved the watter puzzle imidiatly
if you do thumbs up
+Carlon van spijker Pretty easy, tbh.
+Carlon van spijker as the 2nd container appeared on the screen !
+Carlon van spijker I thought he was joking about it being difficult.
lmao yeah I solved in under 10 seconds...
+Carlon van spijker Man both the probs were pieces of cake.
Thanks for giving me the illusion of being a genius by solving this right away. Now let me get back to bagging groceries.
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
All the permutations are valid except 2 5 5, 4 4 4, 6 3 3.
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
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
i love this content so much no other channel does this thanks
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
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.
There are thousnds of tutorails on learning various languages and frameworks.... but this series , i dont even need to mention it... thank you
For the cup scenario, it only took me like 5 seconds. I feel soooo special right now.
Wohoo! I figured it out in like 5 seconds! Never heard of it before btw.
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.
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!
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.
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
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
tbh that was still just as easy.
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.)
if 5 = 4 stop
loop
fill 3 and pour in 5
if 5 = 5, empty 5 and fill with 3
That was not the point of this video,
@@shubhamdeshkar1685 Rules didn't say that you need to comment according to the point of the video.
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!
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.
he explained the difference between permutations and combinations and he was right. the algorithm *tests every permutation and prints only the valid ones.
Sir , please continue this.This is really helpful
So pumped that I was able to solve the last one! Thanks!
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
No, programming is primarily about getting the right answer.
No, programming is primarily about getting the right answer.
this is saving my life right now thank you!!
+Tom McCar Glad I could help!
Finally, a video that answers the real challenge of programming.
These videos are a great compliment to the book. I am very happy with my purchase. Thanks V. Anton Spraul.
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!
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. ;-)
Saw the puzzle for fist time and solved it almost immediately. I think 90% CAN solve it not a few!
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
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.
Any interest in making programming tutorial vids on youtube? lol.
Lmao you said “final solution”
Is this really the right place
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 ...
+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
+Fernando Santos cool ...!
+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 ...
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
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.
My take away is "Take a holistic view of the problem first and keep all options open" and avoid confirmation bias as well
Damn that's probably the least optimal solution!
Try checking the necessary conditions at each loop and break if they are not met.
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.
Red - Green - Refactor; Know what should happen, make it happen, make it better, repeat.
New to programming here.
Care to write down example in pseudo code. Thanks
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
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
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
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)
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.
V. Anton Spraul, thank you!
+Billy Wilson Arante You're welcome! Thanks for watching.
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.
Just bought the book.. hope that being from 2012 that it still works.. not for C++ but for understanding the mind set
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 🌟
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
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
That was a great approach. Thank you very much.
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.
this makes no sense
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"
start with this: , then in another loops for other dept. skip that policeNum to reduce iteration count
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
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))
the answer should be 14
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.
Extremely helpful! Will consider buying your book :)
This video is very helpful and in the same time exciting 👍🏻
Your book is a life saver
Ordered your book today!
Cool, thanks!
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 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
very instructive! I do really benefit from it!
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.
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.
And I start geeking out over ham and Swiss and Swiss and ham lmao
I'm done with c++ .. what programming language should I start with next and why ?
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
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.
+Zachary Blasczyk You included 0 for the second number.. It should be 1-7
+Raiki I see that you are correct. I will rework the program when I get to a computer.
+Raiki The permutations of the Department numbers are as follows:
147.
165.
246.
264.
327.
345.
426.
462.
543.
561.
624.
642.
723.
741.
the real hard problem is when 4 oz are needed. I first saw this problem on Lethal Weapon or was it Die Hard.
+King Martin : That was die hard. John Mclane solved it :)
Die Hard 3!!
Very helpful videos! Thank you!
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
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
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?
+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.
+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!
+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
+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
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.
i need more of this... thanks a lot
Really helpful... Please make more such videos... 🙂
It's 5am and I haven't slept but the water puzzle was easy as shit. Instantly got it.
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.
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.
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
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)
+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)
+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 :)
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.
+Supetorus in Python the solution is nothing but a simple one liner
+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)]
+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]
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)))))
i had the idea for the water cup but in all others i had no idea how to fix those pff
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
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
Awesome video dude!
a great help for a BSIT student like me..😁👍👍
thank you sirrr