Test your skills (2005)
Author |
Message |
Andy
|
Posted: Wed Jan 12, 2005 10:36 pm Post subject: (No subject) |
|
|
since when did u conform to the norm wtd? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Wed Jan 12, 2005 10:40 pm Post subject: (No subject) |
|
|
Andy wrote: since when did u conform to the norm wtd?
Since I started wanting my code to work right.
Let me just reduce it to this: any time you spend learning nonstandard C++ or java, or whatever is time you've lost and you'll never get back. It's time you've not only not spent learning something useful, but it's work you have to undo later on, meaning even less time to learn useful stuff. |
|
|
|
|
|
wtd
|
Posted: Tue Jan 25, 2005 11:57 pm Post subject: (No subject) |
|
|
The answer to this one is way overdue.
wtd wrote: For 30 bits, just to shake things up.
I have the following class in Ruby:
code: | class Foo
def initialize(bar, baz)
@bar, @baz = bar, baz
end
end |
Now, @bar and @baz are private, as all instance variables are in Ruby, but I can write methods which let me access those variables.
With only two additional lines of code add the ability to read and write to @bar, and read @baz.
If you use more than two lines, there will be no bits.
code: | class Foo
attr_accessor :bar
attr_reader :baz
def initialize(bar, baz)
@bar, @baz = bar, baz
end
end |
wtd wrote: Next question!
I have a Haskell function for multiplying an integer by two. Its type is:
code: | double :: Int -> Int |
Without changing the name "double", write the shortest possible function definition. This is a 20 bit question.
We start with the obvious:
But we realize that n is redundant, so we use a partial application of the * operator, which is really just a function itself.
And that, folks, is as short as it gets. |
|
|
|
|
|
wtd
|
Posted: Wed Apr 13, 2005 1:21 pm Post subject: (No subject) |
|
|
The following will not compile. Make the minimal necessary change such that it does compile, and PM me with your answer.
c++: | class A
{
private:
const int& b;
public:
A(const int& init_b)
{
b = init_b;
}
};
int main()
{
int foo(42);
A bar(foo);
return 0;
} |
|
|
|
|
|
|
wtd
|
Posted: Wed Apr 13, 2005 2:16 pm Post subject: (No subject) |
|
|
Sorry, rizzix, that's incorrect. |
|
|
|
|
|
rizzix
|
Posted: Wed Apr 13, 2005 2:19 pm Post subject: (No subject) |
|
|
yea i deleted the post.. did't read it correctly try adding an & |
|
|
|
|
|
wtd
|
Posted: Wed Apr 13, 2005 2:24 pm Post subject: (No subject) |
|
|
Nope. Nowhere do you add an & in the solution. |
|
|
|
|
|
rizzix
|
Posted: Wed Apr 13, 2005 2:26 pm Post subject: (No subject) |
|
|
then it has to be the const.. but.. well i'm not fimiliar with the &.. anyhow.. then isint int & b = &k; the way to go.. and not int &b = k; |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Martin
|
Posted: Wed Apr 13, 2005 2:32 pm Post subject: (No subject) |
|
|
Here's a hint: what if you wanted to do this?
c++: |
#include <iostream>
class A
{
private:
const int& b;
public:
A(const int& init_b)
{
std::cout << b << std::endl;
b = init_b;
}
};
int main()
{
int foo(42);
A bar(foo);
return 0;
} |
|
|
|
|
|
|
rizzix
|
Posted: Wed Apr 13, 2005 2:34 pm Post subject: (No subject) |
|
|
huh? howz that a hint.. i got it already.. gee.. the alias is defined const. pfft. i shouldn't.. point is.. the referencing is wrong.. now isin't it?
as for my second question.. constructors cannot initialise const values? |
|
|
|
|
|
Martin
|
Posted: Wed Apr 13, 2005 2:38 pm Post subject: (No subject) |
|
|
the const is fine the way it is. |
|
|
|
|
|
wtd
|
Posted: Wed Apr 13, 2005 2:40 pm Post subject: (No subject) |
|
|
In C++, in addition to regular old C style pointers there're also references. A reference is declareed like so:
Whereas a regular int would be declared as:
And a pointer to an int as:
A reference to a constant int would be like so:
If we have an int variable, like so:
We can assign it to an int reference like so:
The & used as a prefix operator returns the address in memory of the variable. This is useful when dealing with pointers, but not so much references.
For instance, we might have:
code: | int foo(42);
int * bar(&foo); |
|
|
|
|
|
|
rizzix
|
Posted: Wed Apr 13, 2005 2:41 pm Post subject: (No subject) |
|
|
martin:i dont think so. i compiled it.. and apparently contstuctors cannot initialise const values. |
|
|
|
|
|
Martin
|
Posted: Wed Apr 13, 2005 2:43 pm Post subject: (No subject) |
|
|
Yes they can. I already got this one. |
|
|
|
|
|
wtd
|
Posted: Wed Apr 13, 2005 2:43 pm Post subject: (No subject) |
|
|
rizzix wrote: martin:i dont think so. i compiled it.. and apparently contstuctors cannot initialise const values.
Oh, I assure you they can. Are you sure you understand C++ constructors as well as you think? What you think is initialization might not be initialization at all. |
|
|
|
|
|
|
|