Open Side Menu Go to the Top

01-06-2013 , 08:36 PM
i'll take #2 but probably the real answer is:

Code:
{ "foo1": value1,
   "foo2": value2,
   ...
}
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
01-06-2013 , 08:39 PM
i don't really love the question but if i had to choose between those 2 options i'd go with #2
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 08:42 PM
after seeing tyler's post I feel I should clarify that these are instance variables of a class
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 08:46 PM
Quote:
Originally Posted by Xhad
after seeing tyler's post I feel I should clarify that these are instance variables of a class
in that case you definitely need more semantic context to answer it. how are the foo's related? because both answers could be 100% correct depending on context.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 08:50 PM
ok, fair enough. This is a tutorial example where I think I disagree with the authors and am polling you guys to test my own intuitions on this. It's an hypothetical Triangle class and the variables are the angles.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 08:50 PM
I think #2 almost regardless of context if those are our only 2 choices. The root of the problem is at what point do you stop making multiple variables and just put it into an array or some other structure.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:10 PM
Quote:
Originally Posted by Xhad
ok, fair enough. This is a tutorial example where I think I disagree with the authors and am polling you guys to test my own intuitions on this. It's an hypothetical Triangle class and the variables are the angles.
most likely #2 in that case.

however, if you were trying to model a triangle in some domain where each point (and angle) had a name, i could see an argument for #1. eg, the triangle was always oriented the same way with a left, right, and top point/angle, and that's how domain users like to think about and use it.

Then it would make more sense to have leftAngle, rightAngle, and topAngle. Although even in this case a dictionary might make more sense, as tyler suggested.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:18 PM
So, here's the full story: Yesterday I got bored and did a bunch of Codecademy tutorials (including the entire Python section). The one on python classes has you make a Triangle class. The expected answer is:

Code:
class Triangle:
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
While my natural answer was:

Code:
class Triangle:
    def __init__(self, angle1, angle2, angle3):
        self.angles = [angle1, angle2, angle3]
I checked the forums on the section, found someone else complaining that the text of the exercise didn't give any hint that the second was not acceptable, and a moderator's response was "well the first one is correct practice anyway". I don't trust that statement, especially since the very next exercise is made slightly harder by doing it their way.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:20 PM
Quote:
Originally Posted by gaming_mouse
i disagree with this actually. in the hands of a very good teacher, the vast majority of programming concepts are not that hard. the complexity can be broken down. similarly, if the code is hard to read, it most likely means it wasn't written well imo. now the programmer who wrote it might be very smart and very experienced and accomplished, but i'd consider readability to others a condition of well-written code.

so i think the answer to OP's question is, It's very likely the fault of the teacher. ofc, that only matters if you can find an explanation of the same material by a better teacher.
I want a Rich Hickey book on designing simple software. NOW. KICKSTART

Edit: I'd go with a hash/dict there keys like "alpha","beta","gamma" or smth.
Prob tuple>list as well not sure if we'd ever want to add angles at least not in the abstraction I'd use there are obviously a ton more angles possible but then again that would be easier to id with a dict

Last edited by clowntable; 01-06-2013 at 09:32 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:26 PM
xhad,

YOU SHOULD USE A TUPLE ANYWAY TO PREVENT MUTATION DO YOU WANT 200 DEGREE TRIANGLES??!1 /STACKOVERFLOWPEDANT

xoxo,
t

Last edited by tyler_cracker; 01-06-2013 at 09:27 PM. Reason: i'm with g_m it depends entirely on how the class is used. when you need something else, refactor.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:28 PM
Quote:
Originally Posted by Xhad
So, here's the full story: Yesterday I got bored and did a bunch of Codecademy tutorials (including the entire Python section). The one on python classes has you make a Triangle class. The expected answer is:

Code:
class Triangle:
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
While my natural answer was:

Code:
class Triangle:
    def __init__(self, angle1, angle2, angle3):
        self.angles = [angle1, angle2, angle3]
I checked the forums on the section, found someone else complaining that the text of the exercise didn't give any hint that the second was not acceptable, and a moderator's response was "well the first one is correct practice anyway". I don't trust that statement, especially since the very next exercise is made slightly harder by doing it their way.
You really, really cannot answer these questions without having some idea of how the class is going to be used. Also these internal implementation details can often be the equivalent of grammar nittery in writing -- missing the real point. The design of the public interface of the Triangle class, and how well it achieves the goals of the clients likely to use it, is the more important problem.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:34 PM
I agree that the interface is the key part but I think a dict is still the best internal structure for this because it's more expressive and easier to read later. Assigning some value to alpha makes a ton more sense than remembering which angle was what in a tuple/list imo

In a sense by using a dict with good key names we transport some of the geometry domain into the code.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:41 PM
Quote:
Originally Posted by tyler_cracker
xhad,

YOU SHOULD USE A TUPLE ANYWAY TO PREVENT MUTATION DO YOU WANT 200 DEGREE TRIANGLES??!1 /STACKOVERFLOWPEDANT

xoxo,
t
btw, the next exercise? "write a function that checks for a valid triangle" lol. I just want to write 'assert sum(self.angles) == 180' and I can't!

And ok, I get what you guys are saying re: could be better either way. I was more wtf'ing at saying the second one was so definitively wrong that the exercise instructions didn't even need to hint at the first one. I guess we all at least agree that part is ridic, right?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:42 PM
Absolutes are always wrong.

Oh wait...
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:46 PM
assert sum([eval("self.angle%d" % i) for i in range(1, 3)]) == 180

is almost as concise.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:54 PM
Here's something else that bugged me from yesterday by the way. so in the section on lists they have you write a mean function:

Code:
def average(grades):
    return sum(grades)/len(grades)
So far so good. Then they have you write a variance function. And require that it takes the mean as a parameter! So instead of:

Code:
def variance(grades):
    mean = average(grades)
    return sum((g-mean)**2 for g in grades)/len(grades)
And calling variance, you have to write:

Code:
def variance(grades, average):
    return sum((g-mean)**2 for g in grades)/len(grades)
(actually they probably expected a for loop rather than a generator but the grader's not that much of a jerk, hah)

Thereby, allowing us to, um...make bad inputs possible and be more verbose? I don't get it.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 09:54 PM
Quote:
Originally Posted by Xhad
I guess we all at least agree that part is ridic, right?
y
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 10:14 PM
It would also be helpful if this wasn't a purely stupid class. A good triangle program wouldn't need angle input like this. You can have scalar and polar triangle classes instead, or at least if you knew which type of triangles you were using, this class of initialized angles can be better used, but I still wouldn't make angles the init function.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 10:14 PM
Quote:
Originally Posted by tyler_cracker
assert sum([eval("self.angle%d" % i) for i in range(1, 3)]) == 180

is almost as concise.
getattr > eval imo
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-06-2013 , 10:15 PM
Quote:
Originally Posted by daveT
It would also be helpful if this wasn't a purely stupid class. A good triangle program wouldn't need angle input like this. You can have scalar and polar triangle classes instead, or at least if you knew which type of triangles you were using, this class of initialized angles can be better used, but I still wouldn't make angles the init function.
+1
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-07-2013 , 04:32 AM
irt to the code academy program, it reminds of the Circle-Eclipse Problem: http://en.wikipedia.org/wiki/Circle-ellipse_problem
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-07-2013 , 05:17 AM
Quote:
Originally Posted by e i pi
Code:
#include <stdio.h>
#define N 10000
main()
  { int i, p, q, t, id[N];
    for (i = 0; i < N; i++) id[i] = i;
    while (scanf("%d %d\n", &p, &q) == 2)
      {
        for (i = p; i != id[i]; i = id[i]) ;
        for (j = p; j != id[j]; j = id[j]) ;
        if (i == j) continue;
        id[i] = j;
        printf(" %d %d\n", p, q) ;
      }
  }
yea took me a while to work out what was going on. also kind of tilts me that this is literally the 2nd code example in the book. in his other algorithms book it shows up on like page 250 something,

Code:
public class QuickUnion
{
  private int[] id;

  public QuickUnion (int N)
  {
    id = new int[N];
    for (int i = 0; i < N; i++) id[i] = i;
  }

  private int root(int i)
  {
    while (i != id[i]) i = id[i];
    return i;
  }

  public boolean find(int p, int q)
  {
    return root(p) == root(q);
  }

  public void unite(int p, int q)
  {
    int i = root(p);
    int j = root(q);
    id[i] = j;
  }
}
I don't even know java but that is more readable to me. Its kind of frustrating being in a spot where you're not sure if the book is unclear or if the material is just difficult.
seems absolutely ridiculous to define N as a preprocessor directive
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-07-2013 , 05:18 AM
Quote:
Originally Posted by Xhad
Quick survey: Absent any other information, which is preferable? (pseudocode)

Code:
foo1 = value1
foo2 = value2
foo3 = value3
Code:
foo = [value1, value2, value3]
if they're all the same data type i'd go with 2. otherwise i'd go with 1.



edit: misread the question. obv they'd all have to be the same data type. gotta go with 2.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-07-2013 , 08:34 AM
Random note: We should probably indicate that we expect angles in degrees not radians via variable naming convention.

Or I guess an angle class.

That's pretty much the stuff we should think of. What is a triangle, how are triangles used etc.
I mean it's pretty obvious that the constructor for a triangle will not always be based on three angles for example.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-07-2013 , 08:47 AM
Quote:
Originally Posted by Xhad
So far so good. Then they have you write a variance function. And require that it takes the mean as a parameter! So instead of:

Code:
def variance(grades):
    mean = average(grades)
    return sum((g-mean)**2 for g in grades)/len(grades)
And calling variance, you have to write:

Code:
def variance(grades, average):
    return sum((g-mean)**2 for g in grades)/len(grades)
Gah, this tilts me like crazy. We had an intern a couple of months ago and my most common feedback in code reviews was to rewrite method signatures to be more sensible. Writing good method signatures is a major part of writing good interfaces which is in turn a major part of writing good code.

It's especially frustrating because this is something that can be taught and explained really easily.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

      
m