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 (2006)
Index -> General Programming
Goto page 1, 2, 3, 4, 5, 6  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Jan 02, 2006 3:48 am   Post subject: Test your skills (2006)

Io TYS. Smile

code:
Io> foo(a, 42, a print)
42
==> 42
Io> foo(i, "foo", i with("!") print)
foo!
==> foo!


Write a method which makes the above possible.

code:
Io> foo := method(
       /* your code here */
    )
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Jan 15, 2006 5:09 pm   Post subject: (No subject)

Write a function which appends lists.

code:
(defun list-append (...) ...)


A few samples:

code:
(list-append '(1 2 3) '(4 5 6))    ; (1 2 3 4 5 6)
(list-append '(1 2) '(3 4) '(5 6)) ; (1 2 3 4 5 6)


You may use the following functions or macros.


  • flet
  • cons
  • car
  • cdr
  • if
  • defun (but only the one, in the code I already demonstrated)
  • reduce


You may very explicitly not use the loop macro.

You may not have any code outside of the list-append function.
wtd




PostPosted: Fri Feb 03, 2006 2:27 am   Post subject: (No subject)

C++ gurus: a TYS just for you.

Explain why this code will not compile.

code:
#include <list>
#include <vector>

int main()
{
   std::vector<std::list<int>> foo;

   return 0;
}
Martin




PostPosted: Tue Feb 07, 2006 3:18 am   Post subject: (No subject)

C/C++ (or Java). Complete this function to determine if n is a power of 2.

c:

int isPowerOfTwo (int n)
{
    return <your code!>;
}

c++:
bool isPowerOfTwo (int n)
{
    return <your code!>;
}

Java:
boolean isPowerOfTwo (int n)
{
    return <your code!>;
}
Andy




PostPosted: Tue Feb 07, 2006 9:37 am   Post subject: (No subject)

c++:

bool isPowerOfTwo (int n)
{
    return n % 2 ? n == 1 : isPowerOfTwo(n/2);
}


does that count as cheating? lol
Martin




PostPosted: Tue Feb 07, 2006 7:23 pm   Post subject: (No subject)

There's a much better way.

Think bitwise.
Andy




PostPosted: Tue Feb 07, 2006 10:27 pm   Post subject: (No subject)

haha yea, i was going to do it bitwise, but then i got lazy =P, if i have time, i'll do it at work tomorow
bugzpodder




PostPosted: Wed Feb 08, 2006 12:53 pm   Post subject: (No subject)

c++:
bool isPowerOfTwo (int n)
{
    return n<0?0:(n^(n-1))==-1;
}
Sponsor
Sponsor
Sponsor
sponsor
bugzpodder




PostPosted: Wed Feb 08, 2006 12:56 pm   Post subject: (No subject)

code:
#include <list>
#include <vector>

int main()
{
   std::vector<std::list<int>> foo;

   return 0;
}


>> is an operator in C++. need a space in between.
wtd




PostPosted: Wed Feb 08, 2006 12:58 pm   Post subject: (No subject)

bugzpodder wrote:
code:
#include <list>
#include <vector>

int main()
{
   std::vector<std::list<int>> foo;

   return 0;
}


>> is an operator in C++. need a space in between.


Yes, that is the solution. But why is it a problem? Smile
bugzpodder




PostPosted: Wed Feb 08, 2006 1:07 pm   Post subject: (No subject)

? its a problem because >> is a predefined operator in C++. But i thought I've already said that.
wtd




PostPosted: Wed Feb 08, 2006 1:08 pm   Post subject: (No subject)

But surely C++ compilers can tell the difference?
bugzpodder




PostPosted: Wed Feb 08, 2006 2:33 pm   Post subject: (No subject)

clearly not? lol
Martin




PostPosted: Wed Feb 08, 2006 6:26 pm   Post subject: (No subject)

bugzpodder wrote:
c++:
bool isPowerOfTwo (int n)
{
    return n<0?0:(n^(n-1))==-1;
}


Another one is.

c++:
bool isPowerOfTwo (int n)
{
    return n?(n &-n)==n:0;
}
wtd




PostPosted: Wed Feb 08, 2006 6:30 pm   Post subject: (No subject)

bugzpodder wrote:
clearly not? lol


code:
some_value >> some_other_value


Could clearly never be a type. So... why does C++ get confused? Smile
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 6  [ 77 Posts ]
Goto page 1, 2, 3, 4, 5, 6  Next
Jump to:   


Style:  
Search: