Author |
Message |
wtd
|
Posted: Mon Jan 02, 2006 3:48 am Post subject: Test your skills (2006) |
|
|
Io TYS.
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
|
|
|
wtd
|
Posted: 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
|
Posted: 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
|
Posted: 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
|
Posted: 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
|
Posted: Tue Feb 07, 2006 7:23 pm Post subject: (No subject) |
|
|
There's a much better way.
Think bitwise. |
|
|
|
|
|
Andy
|
Posted: 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
|
Posted: 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
|
|
|
bugzpodder
|
Posted: 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
|
Posted: 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? |
|
|
|
|
|
bugzpodder
|
Posted: 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
|
Posted: Wed Feb 08, 2006 1:08 pm Post subject: (No subject) |
|
|
But surely C++ compilers can tell the difference? |
|
|
|
|
|
bugzpodder
|
Posted: Wed Feb 08, 2006 2:33 pm Post subject: (No subject) |
|
|
clearly not? lol |
|
|
|
|
|
Martin
|
Posted: 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
|
Posted: 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? |
|
|
|
|
|
|