Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Test your skills (2005)
Index -> General Programming
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rizzix




PostPosted: Sat Sep 24, 2005 2:25 pm   Post subject: (No subject)

ah but that's more work.. i needed to delete 2 extra lines Laughing
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Sep 24, 2005 2:52 pm   Post subject: (No subject)

A C++ question.

What part ofthe following prevents it from compiling in Visual C++ 6.0?

c++:
template <typename T>
class Foo
{
   private:
      T m_value;
   
   public:
      Foo();
      Foo(T);

      T value() const;

      template <typename _T>
      void add(_T);
};

template <typename T>
Foo<T>::Foo() : m_value(T()) { }

template <typename T>
Foo<T>::Foo(T initValue) : m_value(initValue) { }

template <typename T>
T Foo<T>::value() const
{
   return m_value;
}

template <typename T>
template <typename _T>
void Foo<T>::add(_T other)
{
   m_value += other;
}


Will this successfully compile with GCC or a newer version of Microsoft's C++ compiler?

Modify the above so that it will compile in Visual C++ 6.0.
wtd




PostPosted: Sat Sep 24, 2005 10:12 pm   Post subject: (No subject)

I have the following simple Ada generic package:

ada:
generic
    Size : Positive;
    type Item_Type is private;
  package Generic_Stack is
    procedure Push(E : in  Item_Type);
    procedure Pop (E : out Item_Type);
    Overflow, Underflow : exception;
  end Generic_Stack;


With body:

ada:
package body Generic_Stack is
    type Table is array (Positive range <>) of Item_Type;
    Space : Table(1 .. Size);
    Index : Natural := 0;

    procedure Push(E : in Item_Type) is
    begin
      if Index >= 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:

ada:
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. Wink
wtd




PostPosted: Sun Sep 25, 2005 1:57 am   Post subject: (No subject)

I have the following Python class:

Python:
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




PostPosted: Sun Sep 25, 2005 10:45 am   Post subject: (No subject)

Python:
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




PostPosted: Sun Sep 25, 2005 11:20 am   Post subject: (No subject)

Hikaru79 wrote:
Python:
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. Smile
wtd




PostPosted: Tue Sep 27, 2005 12:31 pm   Post subject: (No subject)

Ruby challenge!

Write a class Foo whereby you can run this code:

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:

code:
>>> map(lambda x: x + x, Foo())
['foofoo', 'barbar', 'bazbaz']
wtd




PostPosted: Wed Sep 28, 2005 3:16 am   Post subject: (No subject)

Write the shortest possible piece of code that makes it possible to create a very simple object with:

Python:
x()
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Sep 29, 2005 5:10 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]").


code:
putStr "Enter a number: " >> hFlush stdout >> hGetLine stdin >>= print . product . enumFromTo 1 . read
wtd




PostPosted: Thu Oct 20, 2005 9:11 pm   Post subject: (No subject)

code:
putStr "Enter a number: " >> hFlush stdout >> hGetLine stdin >>= print . product . enumFromTo 1 . read


Rewrite this without using the >> operator. You're still permitted to use the >>= operator.
Martin




PostPosted: Thu Oct 20, 2005 10:18 pm   Post subject: (No subject)

I just edited the title of the topic to reflect, well, present times.
wtd




PostPosted: Thu Oct 20, 2005 10:25 pm   Post subject: (No subject)

Thanks.

Now answer some questions. Razz
rizzix




PostPosted: Thu Oct 20, 2005 10:48 pm   Post subject: (No subject)

code:
main = sequence[putStr "Enter a number: ", hFlush stdout, hGetLine stdin >>= print . product . enumFromTo 1 . read]
???
wtd




PostPosted: Thu Oct 20, 2005 11:34 pm   Post subject: (No subject)

While that works, I was thinking of:

code:
putStr "Enter a number: " >>= \_ -> hFlush stdout >>= \_ -> hGetLine stdin >>= print . product . enumFromTo 1 . read
wtd




PostPosted: Wed Nov 23, 2005 8:21 pm   Post subject: (No subject)

A new C++ Test Your Skills.

I start with the following source:

c++:
#include <iostream>

using namespace std;

int main()
{
   int a = 4, b = 5;

   cout << a << b << endl;
   cout << a << b << endl;

   return 0;
}


This prints:

code:
45
45


Add two lines of code to this which will make it print:

code:
45
54


You may not compact code to meet the two line limit, nor may you introduce new variables, nor may you use macros. You also may not alter any of the code in the original source.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 7 of 10  [ 147 Posts ]
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
Jump to:   


Style:  
Search: