|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Did my professor just indirectly call my code a piece of crap? It was just a math computation code to figure out the calculation of a bunch of square roots to 625, you'll see what I mean when you see the code (python btw).
My writing
# TN # 5.37
import math
# set variables uno = 1 dos = 2
i = math.sqrt(uno) k = math.sqrt(dos) counter = 625
total = 1 / (i / k)
# loop
while counter > 0 : uno += 1 dos += 1 total += 1 / (i / k) counter -= 1
print('The sum of the computation is','%.01f' % total)
Professor's code
# DZ # Summation
# SETUP import math total = 0
# NO INPUT, JUST COMPUTE for i in range (1, 625): total += 1 / (math.sqrt (i) + math.sqrt (i + 1))
# OUTPUT print (total)
"(5.37) Everything but the loop is incorrect." is the exact e-mail I got from him.
|
There's a difference between writing good code and writing code that works. Python especially tends to be about succinctness and simplicity, whereas your code has many unnecessary variables and lines.
You've over-complicated something that should be a simple bit of code and made it harder to read and maintain than necessary. Additionally, you don't gain any forward-looking functionality/expandability out of your solution that would warrant the maintainability/readability cost.
All of your variables are poorly named, to boot. They all have no meaning about how they're used or what they contain. i and k are typically used as increment variables, but you've halfway used them as temporary storage for calculated values.
Also, while I don't know Python, I don't believe your logic inside the loop would work. The first time you use i and k you call the math.sqrt of the function on them, but every subsequent time inside the loop you're simply using the same i and k (look at your code and tell me where you do anything to i or k inside your loop or where you use uno or dos after incrementing).
Basically, your loop doesn't do what you want.
|
Snuggles:
Variables i and k are never updated within the loop. Therefore the same value is being added to total at every iteration.
Your professor's code has a for i in range (1, 625) loop, where i will increase by one after every iteration. Therefore, the value that will be added to total will be different every iteration.
Those are the main differences between your code and his code.
If you moved
i = math.sqrt(uno) k = math.sqrt(dos)
into the loop it would probably work.
However, you wrote a lot of code to emulate the style of a for loop. Why not just use a for loop directly?
|
Was wondering if anyone could help me with HW on discrete math. My professor is seriously on crack, HW averages are like 55%.
Ok, so first of all if someone could explain what does it mean to use a combinatorial proof to prove something. I googled about it, but honestly it was really hard to understand and see how it applies to my problem.
In first part of her question it is like this, Use binomial theorem to prove: C(n,0)+C(n,1)2+C(n,2)2^2+...+C(n,n)2^n=3^n
So I recognized it as binomial theorem, of a=2,b=1, substitued left side of the equation for (2+1)^n and said (2+1)^n=3^n
How can I prove the same thing using combinatorial counting argument?
Not rly programming but Discrete Math is at the very foundation of computing...
|
This is explicitly a thread where you don't ask "here's my homework, help"
If you want help, figure out things yourself and then ask for help on specific sticking points.
|
On November 26 2012 13:00 Snuggles wrote: Did my professor just indirectly call my code a piece of crap? Yes, and you should write him a thank you note for letting you know in such a friendly manner. Computer programming is really hard, and if you're going to succeed at it, you need a much better developed sense of perfectionism than that function demonstrates. I can see that it's just a beginner project, so it's completely fine that your code is awful, but it's also important that you understand that the code is wrong both logically and stylistically, and demonstrates that you're not thinking about how to write a program the right way.
So the first problem is, your program is wrong. What was your testing process? I'm guessing you didn't test your result for correctness at all, or you would have noticed the error. Testing the actual calculation would be a lot of work, so my testing process would have been something like: Replace the 625 with 5, work out the answer by hand, see if your code produces the right answer for 5. Once you get it working for 5, test it again with another value. Once you know that the function generates the correct values for a set of testable numbers, you can deduce that it should work for larger numbers, as well.
The second problem was already pretty well explained by everyone else: Your code has a whole bunch of extra bits and doodads that are completely unnecessary, and your variable names are meaningless. This sort of criticism is something that beginners in CS tend to think is just crotchety old people being crotchety, because when you're starting out you write programs that are maybe two pages of code, and you have to "maintain" them for the weekend it takes you to write them. I work on projects with lifetimes around ten years and tens to hundreds of thousands of lines of code, and I have been in the unenviable position of having to fire people who thought that writing legible and maintainable code was a waste of time, because I don't have time to figure out what "uno" meant to someone else three years ago, and he probably doesn't remember anymore, either.
Don't get discouraged, though. Everyone wrote shitty code in the beginning. The important thing is that you understand what it is that makes your code bad, and focus on not making the same mistakes in your next project.
|
On November 26 2012 22:09 AmericanUmlaut wrote:
Don't get discouraged, though. Everyone wrote writes shitty code in the beginning. The important thing is that you understand what it is that makes your code bad, and focus on not making the same mistakes in your next project.
I fixed this statement, because I think it's important for a beginner to know that there is probably not a single programmer in the world, no matter how experienced or intelligent, that doesn't write shitty code sometimes for legitimate reasons (e.g. they just can't think of better ways to do it at that specific moment).
Programming is just too complex for that. Arguably it's one, if the not the, most complex engineering field out there. The degrees of freedom of any software application is just larger than those that you'd potentially find in other fields, and to make it worse, math doesn't help to provide clear cut answers most of the time.
|
On November 26 2012 23:06 heishe wrote:Show nested quote +On November 26 2012 22:09 AmericanUmlaut wrote:
Don't get discouraged, though. Everyone wrote writes shitty code in the beginning. The important thing is that you understand what it is that makes your code bad, and focus on not making the same mistakes in your next project. I fixed this statement, because I think it's important for a beginner to know that there is probably not a single programmer in the world, no matter how experienced or intelligent, that doesn't write shitty code sometimes for legitimate reasons (e.g. they just can't think of better ways to do it at that specific moment). Programming is just too complex for that. Arguably it's one, if the not the, most complex engineering field out there. The degrees of freedom of any software application is just larger than those that you'd potentially find in other fields, and to make it worse, math doesn't help to provide clear cut answers most of the time. I salute you, sir, for you are very, very correct. I have, in fact, not yet reached a point where I wasn't embarassed to read my own code half a year or so after I wrote it: You learn so much so quickly in this field that you're basically always discovering what an ignorant fool you are, and how badly the cutting-edge code you thought you were writing yesterday actually sucked.
|
Very true on all counts. People will bash on you even if you get the problem right if it's too inefficient, if it's not maintainable, if it's not portable, if it's not scalable. Getting the right answer isn't even half the problem most of the time.
I remember writing 30 lines of really shitty, simple code in Python for an interview. I got back home, wrote it in 25 lines. 4 months later, I wrote it in 3. As you improve, you actually get to see your improvement, which is one of the best parts of programming really. You get to see yourself solve problems you had no idea how to solve an hour ago, a week ago, a month ago, a couple years ago. It's really really really satisfying.
The real objective is just to always improve yourself, constantly learn, and never think that you're definitely right about something.
When you see your code from 1 year ago, you should go, "oh damn, I'm a dumb ass for writing that." And then you should simultaneously go, "wow, I love how much improvement I'm making and how much I'm learning."
|
Wow thanks guys. I'm very open to criticism, especially for programming. I gotta say I have the hardest time wrapping my head around the programs our professor wants us to do for the week so I appreciate all the input from you guys to see what is wrong with the way writing my code.
So what I got from you guys so far is- 1. Make my variables more reasonable and relevant 2. Make it more readable 3. Keep it simple 4. Test my program more thoroughly
I don't know why I didn't just use a for loop rather than while... I even remember that the Professor said that you should use a for loop if you know exactly how many iterations you need and a while loop if that's a dependent variable.
So to make my code more readable does that mean just reducing the amount of unnecessary variables and simplifying the code in general? Is there any form of given etiquette in the style so that my Professor doesn't squint his eyes or something?
btw it's gonna be embarrassing as hell to face my professor now today... and we're getting exams back so I'm worried as hell lol.
|
Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.
Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?
|
On November 27 2012 02:45 Snuggles wrote: Wow thanks guys. I'm very open to criticism, especially for programming. I gotta say I have the hardest time wrapping my head around the programs our professor wants us to do for the week so I appreciate all the input from you guys to see what is wrong with the way writing my code.
So what I got from you guys so far is- 1. Make my variables more reasonable and relevant 2. Make it more readable 3. Keep it simple 4. Test my program more thoroughly
I don't know why I didn't just use a for loop rather than while... I even remember that the Professor said that you should use a for loop if you know exactly how many iterations you need and a while loop if that's a dependent variable.
So to make my code more readable does that mean just reducing the amount of unnecessary variables and simplifying the code in general? Is there any form of given etiquette in the style so that my Professor doesn't squint his eyes or something?
btw it's gonna be embarrassing as hell to face my professor now today... and we're getting exams back so I'm worried as hell lol.
Indentation (4 spaces usually or 1 'tab' if you use Notepad++) and programming conventions in general. E.g. my Java lecturer wants me to write constants like this:
private static final double PI = 3.14; (notice caps)
Read further: http://en.wikipedia.org/wiki/Coding_conventions
|
On November 27 2012 03:36 Ame wrote: Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.
Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?
I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake.
Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement.
i=0; while(i<10) { //do stuff ++i; }
for(i=0;i<10;++i) { //do stuff }
|
On November 27 2012 04:09 RoyGBiv_13 wrote:Show nested quote +On November 27 2012 03:36 Ame wrote: Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.
Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not? I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake. Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement. i=0; while(i<10) { //do stuff ++i; } for(i=0;i<10;++i) { //do stuff }
I guess I'm looking for... reasoning for why both For and While loops even exist, instead of just one of them. If there is actually something that can be done in one of the loops that the other cannot do without the addition with many lines of code. Maybe For and While loops hit memory in different way (noob here, no idea how heap/stack/whatnot work)?
Otherwise the answer seems to be something vestigial that is just coding conventions now?
|
On November 27 2012 03:36 Ame wrote: Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.
Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not?
For loops require less lines of code for the same thing a while loop does. Also, the counter variable can be automatically deallocated when the loop ends. But no, here is no for loop that can not be written as a while loop that I know of. A for loop is just a while loop with an initialzer and a statement that runs at the end of every iteration.
|
On November 27 2012 04:28 Ame wrote:Show nested quote +On November 27 2012 04:09 RoyGBiv_13 wrote:On November 27 2012 03:36 Ame wrote: Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.
Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not? I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake. Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement. i=0; while(i<10) { //do stuff ++i; } for(i=0;i<10;++i) { //do stuff } I guess I'm looking for... reasoning for why both For and While loops even exist, instead of just one of them. If there is actually something that can be done in one of the loops that the other cannot do without the addition with many lines of code. Maybe For and While loops hit memory in different way (noob here, no idea how heap/stack/whatnot work)? Otherwise the answer seems to be something vestigial that is just coding conventions now?
A for and while loop that does the same thing will in most compilers get the same representation when its compiled to bytecode, machine code etc. Essentially for loops can be viewed as special case syntactic sugar for while loops.
|
for loops are traditionally for things like a defined number of iterations (walking through an array) where while loops are used for indefinit number of iterations (reading lines of a file until there is nothing left)
so you get:
for(int i=0; i<arrayElements;i++){ take element doggy style; }
while(NOT EOF){ read that dirty next line; }
this is why I personally FUCKING HATE endless for loops, it's butt ugly.
|
On November 27 2012 04:28 Ame wrote:Show nested quote +On November 27 2012 04:09 RoyGBiv_13 wrote:On November 27 2012 03:36 Ame wrote: Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.
Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not? I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake. Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement. i=0; while(i<10) { //do stuff ++i; } for(i=0;i<10;++i) { //do stuff } I guess I'm looking for... reasoning for why both For and While loops even exist, instead of just one of them. If there is actually something that can be done in one of the loops that the other cannot do without the addition with many lines of code. Maybe For and While loops hit memory in different way (noob here, no idea how heap/stack/whatnot work)? Otherwise the answer seems to be something vestigial that is just coding conventions now? It's less a question of convention than a question of readability. If you see for(i=0; i<10; i++){...}, you instantly know what's intended without having to give it a lot of thought, whereas it takes a bit more thinking to parse a while loop that does the same thing (you have to go through the body of the loop and see everything that can happen to your i variable). While loops remain useful, though, because there are a lot of times when you don't have a number of loops you want to do, but rather you just want to loop until you encounter some stop condition, for example looping through a string and doing something with each character:
while(null !== curChar) { curChar = myString.getNextChar(); doSomething(curChar); }
You could technically do the same thing with a for loop, but it would be really unreadable. In fact, in terms of making a program work for and while loops could both be replaced by GOTO, which is what they devolve to at the machine code level in any case. We have these control structures not because they work better logically, but because they make code a lot more intuitive to read and write for humans. One of the most important concepts in programming as far as I'm concerned is understanding that writing code that a computer can understand (what 99% of people think we do for a living) is only the first part of the job, and is often relatively easy*. When you're good, you give as much consideration to making sure that you're writing code that a person can understand.
*Except for pointers and the Javascript SelectionRange object.
Edit to add: Another point is that for() gives you a really simple syntax for something that you do over and over and over again as a programmer. A while loop requires several lines of code to do the same thing, which multiplies your opportunites for typos, forgetting to initialize or increment your counter, and so on. One of the reasons that "elegance" is held holy by computer programmers is that if you can figure out a way to solve a problem with 3 lines of code rather than 20, then even if your 3 lines are no faster and no more logically correct than my 20, your code has only 3 things that can be wrong, and mine has 20. At the same time, if a bug in your code does pop up, the guy who gets the job of fixing it only has to understand 3 lines of code, so he'll probably be able to instantly or very quickly grasp what's being done, which might take significantly more time in a function that's much longer.
|
On November 27 2012 04:09 RoyGBiv_13 wrote:Show nested quote +On November 27 2012 03:36 Ame wrote: Does anyone have a for loop off the top of their head that cannot be written as a while loop? Or the opposite? Internet said while loops work with booleans better... but for loops initialized and terminated fine with booleans when I did a test run in Java.
Is it more of a convention thing then? Such that when another programmer comes in and sees a while or for loop they can then they can make a quick assumption on the check being dependent or not? I'm not exactly sure what you're getting at here. A for loop is the same as a while loop with some additional organizational structure for clarity's sake. Back in old programming (BASIC/Pascal) days, they were used for seperate purposes, so convention has followed. For loops are used as loops that iterate a known number of times before the loop is encountered, whereas while loops are more of a "keep doing this until..." kind of statement. i=0; while(i<10) { //do stuff ++i; } for(i=0;i<10;++i) { //do stuff }
Yes, and in Java, you can also do a for each like things on an iterable class (your own wich return an iterator or one from Java API, for instance ArrayList).
for(Element e : myArrayList) { /*I do stuff with my e*/ } while(myArrayList.hasNext()) { Element e = mayArrayList.next() /*GoTo previus comment*/ }
|
Haha, yes, I get the traditional/conventional reason. >_>
Didn't know the auto deallocate variable part, so I guess that's an additional line of code I didn't think of. Pah, noobs like me don't even deallocate anything to begin with. ... ;;
At the 'less lines of code bit', is it pretty much 3 less lines? Initialization, increment/change, deallocation?
Gotta run, thanks for the replies all. o/
*edit
Ahh, I'll probably take 'readability' as the biggest selling point.
I really need to read up on the stuff such as "for(Element e : myArrayList)". I see that : everywhereeee.
~~
@combinatorial problem: Well, despite it being discouraged but since I don't actually see the 'dont ask about homework' bit in the thread rules... here's in return for people having answered my questions. A guess because it's been forever since I've taken Discrete Math, but I'd wager it has something to do with writing out all the C(n,n) in equation forms and then canceling a lot of things out. Or some crafty substitutions.
|
|
|
|