Test your skills (2005)
Author |
Message |
wtd
|
Posted: Thu Sep 22, 2005 12:11 pm Post subject: (No subject) |
|
|
A Ruby question.
Write a class Ninja, for which calling a method it doesn't have raises no errors, but rather stores information about the method call (the name of the method and the arguments passed to it). |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Thu Sep 22, 2005 12:14 pm Post subject: (No subject) |
|
|
A C++ question.
Given that we have a file "input_file" open and pointing to a plain text file, and a std::vector of characters "chars_of_input_file", write a single line of code which will slurp all of the chars in the file into the vector.
You may not use "for" or "while", or multiple semi-colons (you're allowed one to terminate the entire line). |
|
|
|
|
|
wtd
|
Posted: Thu Sep 22, 2005 12:58 pm Post subject: (No subject) |
|
|
Objective-Caml question.
I have a module Foo:
code: | module Foo =
struct
let bar = "hello"
let print_bar () = print_endline bar
end;; |
Make it impossible to access "bar" from outside of the module. |
|
|
|
|
|
wtd
|
Posted: Thu Sep 22, 2005 4:48 pm Post subject: (No subject) |
|
|
I have two Objective-C classes:
code: | @interface Foo : Object
- (void) sayHello;
@end
@interface Bar : Object
- (void) sayHello;
@end |
Both have different implementations of sayHello, but the implementations aren't important.
I want to pass objects of these classes to a function.
code: | void sayHello(id greetingObject)
{
[greetingObject sayHello];
} |
Here I've used the "id" type, which for lack of a better explanation essentially includes anything.
But what if I only want to accept objects which have a "- (void) sayHello" method? And I don't want to just get an error at run-time. I want to enforce that limit.
Modify the code to make that possible.
The entire original program:
code: | #import <objc/Object.h>
#include <stdio.h>
@interface Foo : Object
- (void) sayHello;
@end
@interface Bar : Object
- (void) sayHello;
@end
void sayHello(id);
int main()
{
Foo *f = [[Foo alloc] init];
Bar *b = [[Bar alloc] init];
sayHello(f);
sayHello(b);
}
void sayHello(id greetingObject)
{
[greetingObject sayHello];
}
@implementation Foo
- (void) sayHello
{
puts("Hello");
}
@end
@implementation Bar
- (void) sayHello
{
puts("Yo");
}
@end |
|
|
|
|
|
|
Hikaru79
|
Posted: Fri Sep 23, 2005 8:30 pm Post subject: (No subject) |
|
|
wtd wrote: Well, it may be "interesting" by some definition of that word, but these tests are meant to test details, and things that are often overlooked.
I either don't know Turing well enough to myself test others on such things, or haven't seen an opportunity to ask such questions.
With that said, the next question:
Ok, Pythonistas, this one's for you again.
Given the class Foo:
Python: | class Foo(object):
def __init__(self):
self.bar = 42 |
We can easily add attributes to an instance of Foo at run-time.
Python: | baz = Foo()
baz.ninja = 27.3
print baz.ninja |
That's not a problem.
Create a subclass Wooble of Foo which makes all added attributes strings. Make sure it doesn't change the type of "bar".
Do this in one function in the Wooble class.
Python: | class Wooble(Foo):
def __setattr__( self, name, value ):
if name!='bar':
self.__dict__[name] = str(value)
else:
self.__dict__[name] = value |
I feel bad about the 'if name!='bar' thing, but it fits the problem description, as you said |
|
|
|
|
|
Hikaru79
|
Posted: Fri Sep 23, 2005 11:24 pm Post subject: (No subject) |
|
|
wtd wrote: A Haskell question:
Let's have a simple factorial program.
code: | import IO
main =
do
putStr "Enter a number: "
hFlush stdout
inputLine <- hGetLine stdin
let inputNumber = read input
let factorial = product [1..number]
let strFactorial = show factorial
putStrLn strFactorial |
Now... rewrite main as a single line of code with no explicit passing of state (such as: "let inputNumber = read input" and "let factorial = product [1..number]").
Is this OK?
Haskell: | import IO;
main = do { putStr "Enter a number: " ; hFlush stdout ; inputLine <- hGetLine stdin ; putStrLn $ show $ product [1..read inputLine] } |
|
|
|
|
|
|
wtd
|
Posted: Sat Sep 24, 2005 12:26 am Post subject: (No subject) |
|
|
And neither one is correct, since there's explicit passing of state. |
|
|
|
|
|
rizzix
|
Posted: Sat Sep 24, 2005 12:27 am Post subject: (No subject) |
|
|
mb i realized that... |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rizzix
|
Posted: Sat Sep 24, 2005 12:55 am Post subject: (No subject) |
|
|
here's it...
Haskell: | import IO
main = putStr "Enter a number: " >> hFlush stdout >> hGetLine stdin >>= \inputLine -> (putStrLn $ show $ product [1 .. read inputLine]) |
|
|
|
|
|
|
wtd
|
Posted: Sat Sep 24, 2005 3:15 am Post subject: (No subject) |
|
|
Nice try, but you're using an unnecessary eta-expansion. Study function composition, young padawan. |
|
|
|
|
|
rizzix
|
Posted: Sat Sep 24, 2005 10:16 am Post subject: (No subject) |
|
|
No i don't know much about functional composition in haskell.. (actually i understand the concept, but i don;t know how to apply it there)
but i might as well ans ur other 2, they seem too easy
wtd wrote: A C++ question.
Given that we have a file "input_file" open and pointing to a plain text file, and a std::vector of characters "chars_of_input_file", write a single line of code which will slurp all of the chars in the file into the vector.
You may not use "for" or "while", or multiple semi-colons (you're allowed one to terminate the entire line).
c++: | chars_of_input_file.insert(chars_of_input_file.begin(), istream_iterator<char>(input_file), istream_iterator<char>()); |
and for the Obj-C one..
obj-c: | #import <objc/Object.h>
#include <stdio.h>
@protocol SaysHello
- (void) sayHello;
@end
@interface Foo : Object <SaysHello>
- (void) sayHello;
@end
@interface Bar : Object <SaysHello>
- (void) sayHello;
@end
void sayHello(id);
int main()
{
Foo *f = [[Foo alloc] init];
Bar *b = [[Bar alloc] init];
sayHello(f);
sayHello(b);
}
void sayHello(id<SaysHello> greetingObject)
{
[greetingObject sayHello];
}
@implementation Foo
- (void) sayHello
{
puts("Hello");
}
@end
@implementation Bar
- (void) sayHello
{
puts("Yo");
}
@end |
|
|
|
|
|
|
wtd
|
Posted: Sat Sep 24, 2005 10:51 am Post subject: (No subject) |
|
|
rizzix wrote: and for the Obj-C one..
obj-c: | #import <objc/Object.h>
#include <stdio.h>
@protocol SaysHello
- (void) sayHello;
@end
@interface Foo : Object <SaysHello>
- (void) sayHello;
@end
@interface Bar : Object <SaysHello>
- (void) sayHello;
@end
void sayHello(id);
int main()
{
Foo *f = [[Foo alloc] init];
Bar *b = [[Bar alloc] init];
sayHello(f);
sayHello(b);
}
void sayHello(id<SaysHello> greetingObject)
{
[greetingObject sayHello];
}
@implementation Foo
- (void) sayHello
{
puts("Hello");
}
@end
@implementation Bar
- (void) sayHello
{
puts("Yo");
}
@end |
That actually compiles and works correctly, but you have too many lines. You're working too hard. |
|
|
|
|
|
rizzix
|
Posted: Sat Sep 24, 2005 12:10 pm Post subject: (No subject) |
|
|
hmm i just added 3 lines of code and modifed another 3 lines..
wht's the shorter alternative? |
|
|
|
|
|
Mr. T
|
Posted: Sat Sep 24, 2005 2:07 pm Post subject: Re: Alex's Opinion |
|
|
wtd wrote: Pwned wrote: I think it's about time for a Turing question.
Turing's not interesting enough to do anything tricky.
Well then a challenge will also exist for the person writing the question. |
|
|
|
|
|
wtd
|
Posted: Sat Sep 24, 2005 2:10 pm Post subject: (No subject) |
|
|
rizzix wrote: hmm i just added 3 lines of code and modifed another 3 lines..
wht's the shorter alternative?
code: | @interface Foo : Object <SaysHello>
- (void) sayHello;
@end |
By specifying that Foo implements the SaysHello protocol, you're also saying that Foo has a "- (void) sayHello" method. There's no need to say this again.
code: | @interface Foo : Object <SaysHello>
@end |
Works perfectly well. |
|
|
|
|
|
|
|