
-----------------------------------
wtd
Sat Jan 01, 2005 5:01 pm

Test your skills (2005)
-----------------------------------
I figure a new year calls for a new tradition.  So, as I come up with questions, I'll post them here, and invite others to PM answers to me within the next week.  20 bits to anyone who can answer correctly.  :)

So, the first question is a Ruby question.

Why does the first print 

[2, 4, 6] 

to the screen, and the second print 

[1, 2, 3]

The code:

p [1, 2, 3].collect { |x| x * 2 }

p [1, 2, 3].collect do |x| x * 2 end

-----------------------------------
MihaiG
Sat Jan 01, 2005 5:26 pm


-----------------------------------
can you post something for VB or turing?

-----------------------------------
wtd
Sat Jan 01, 2005 6:18 pm


-----------------------------------
Possibly, though I'll admit to not being fond of either of those languages.

-----------------------------------
Acid
Sat Jan 01, 2005 7:00 pm


-----------------------------------
Could you post some C++ too?

Not like I'll get the question anyway...

-----------------------------------
wtd
Sat Jan 01, 2005 7:07 pm

C++ skills test (1/1/05 - 1/7/05)
-----------------------------------
I write the following program and compile it with a modern C++ compiler (GCC 3.3):

#include 

main()
{
   cout = Size then
        raise Overflow;
      end if;
      Index := Index + 1;
      Space(Index) := E;
    end Push;

    procedure Pop(E : out Item_Type) is
    begin
      if Index = 0 then
        raise Underflow;
      end if;
      E := Space(Index);
      Index := Index - 1;
    end Pop;

  end Generic_Stack;

That can be used like so:

procedure Test_Proc is
  package Stack_Int is new Generic_Stack(Size => 200, Item => Integer);

  A : Integer;
begin
  Stack_Int.Push(42);
  Stack_Int.Pop(A);
end Test_Proc;

Modify the package declaration for Generic_Stack such that the type used for Item_Type must work with the equality (=) operator.

If you add more than one line, you're doing it wrong.  ;)

-----------------------------------
wtd
Sun Sep 25, 2005 1:57 am


-----------------------------------
I have the following Python class:

class Foo(object):
   def __init__(self):
      self.__bar = 42

Where self.__bar is (essentially) a private variable.  

Add a single line of code to ths class which provides a read-only accessor named "bar" for that private variable.

-----------------------------------
Hikaru79
Sun Sep 25, 2005 10:45 am


-----------------------------------
class Foo(object): 
   def __init__(self): 
      self.__bar = 42
   def bar(self): return self.__bar

? Or does that not count because you have to call "myObject.bar()" instead of "myObject.bar" ?

-----------------------------------
wtd
Sun Sep 25, 2005 11:20 am


-----------------------------------
class Foo(object): 
   def __init__(self): 
      self.__bar = 42
   def bar(self): return self.__bar

? Or does that not count because you have to call "myObject.bar()" instead of "myObject.bar" ?

Exactly.  :)

-----------------------------------
wtd
Tue Sep 27, 2005 12:31 pm


-----------------------------------
Ruby challenge!

Write a class Foo whereby you can run this code:

>> Foo.new.collect { |x| x * 2}
=> ["foofoo", "barbar", "bazbaz"]

Without cheating by having a method all on one line or using semicolons, do this in six lines.  Blank lines won't be included in the count.

One line blocks are permitted.

When you're finished, do the equivalent in Python to make the following possible:

>>> map(lambda x: x + x, Foo())
['foofoo', 'barbar', 'bazbaz']

-----------------------------------
wtd
Wed Sep 28, 2005 3:16 am


-----------------------------------
Write the shortest possible piece of code that makes it possible to create a very simple object with:

x()

-----------------------------------
wtd
Thu Sep 29, 2005 5:10 pm


-----------------------------------
A Haskell question:

Let's have a simple factorial program.

import IO

main =
  do
    putStr "Enter a number: "
    hFlush stdout
    inputLine 7&&$c