Test your skills (2005)
Author |
Message |
[Gandalf]
|
Posted: Thu Nov 24, 2005 1:18 am Post subject: (No subject) |
|
|
c++: | #include <iostream>
using namespace std;
int main()
{
int a = 4, b = 5;
cout << a << b << endl;
cout << b << a << endl;
return 0;
cout << a << b << endl;
} |
Creative solution, no? And in only one extra line. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Thu Nov 24, 2005 1:28 am Post subject: (No subject) |
|
|
Ah, but you've changed the order of two of the lines.
Yes, you could have two return statements in there, but I'd consider that bad form. The ideal solution will involve no such hacks.
In fact, the final solution shouldn't be "hackish" in the slightest. |
|
|
|
|
|
[Gandalf]
|
Posted: Thu Nov 24, 2005 1:33 am Post subject: (No subject) |
|
|
Ah, but you didn't mention that it had to follow good form .
Well, for that I'll have to look into it a little bit more... |
|
|
|
|
|
wtd
|
Posted: Thu Nov 24, 2005 1:35 am Post subject: (No subject) |
|
|
Shame there's no easy way to swap the values in a and b. |
|
|
|
|
|
Martin
|
Posted: Thu Nov 24, 2005 2:21 am Post subject: (No subject) |
|
|
c++: | #include <iostream>
using namespace std;
int main()
{
int a = 4, b = 5;
cout << a << b << endl;
a = a + b - (b = a); // 4 + 5 - (4)
cout << a << b << endl;
return 0;
} |
In half of two lines. Boo yeah! |
|
|
|
|
|
md
|
Posted: Thu Nov 24, 2005 2:25 am Post subject: (No subject) |
|
|
Martin wrote: c++: | #include <iostream>
using namespace std;
int main()
{
int a = 4, b = 5;
cout << a << b << endl;
a = a + b - (b = a); // 4 + 5 - (4)
cout << a << b << endl;
return 0;
} |
In half of two lines. Boo yeah!
I think you need brackets around (a+b), otherwise the second brackets get evaluated first and then b == a == 4, so a + b = 8 - 4 = 4 |
|
|
|
|
|
Martin
|
Posted: Thu Nov 24, 2005 2:30 am Post subject: (No subject) |
|
|
Worked in Java. I don't have a C++ compiler at work. |
|
|
|
|
|
md
|
Posted: Thu Nov 24, 2005 2:32 am Post subject: (No subject) |
|
|
Hmmm... could it be that the evaluation doesn't follow normal bedmas rules? In that case it would certainly work... most interesting... Oh well, I'll give it a shot tomorrow once I get around to checking out gcc on linux |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Thu Nov 24, 2005 2:42 am Post subject: (No subject) |
|
|
Martin wrote: c++: | #include <iostream>
using namespace std;
int main()
{
int a = 4, b = 5;
cout << a << b << endl;
a = a + b - (b = a); // 4 + 5 - (4)
cout << a << b << endl;
return 0;
} |
In half of two lines. Boo yeah!
Well, I did specify in response to Gandalf that the solution should not be "hackish", and I'd say this qualifies.
For instance, make this work with two strings.
c++: | #include <iostream>
#include <string>
using namespace std;
int main()
{
string a = "4", b = "5";
cout << a << b << endl;
a = a + b - (b = a); // 4 + 5 - (4)
cout << a << b << endl;
return 0;
} |
|
|
|
|
|
|
[Gandalf]
|
Posted: Thu Nov 24, 2005 3:18 am Post subject: (No subject) |
|
|
c++: | #include <iostream>
#include <string>
using namespace std;
int main()
{
string a = "4", b = "5";
cout << a << b << endl;
a.swap(b);
cout << a << b << endl;
return 0;
} |
Tony's program compiles and runs fine for me... |
|
|
|
|
|
Martin
|
Posted: Thu Nov 24, 2005 3:24 am Post subject: (No subject) |
|
|
Alright wtd, let's hear the solution. |
|
|
|
|
|
wtd
|
Posted: Thu Nov 24, 2005 4:24 am Post subject: (No subject) |
|
|
Just to make you all hate me...
c++: | #include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a = 4, b = 5;
cout << a << b << endl;
swap(a, b);
cout << a << b << endl;
return 0;
} |
|
|
|
|
|
|
Martin
|
Posted: Thu Nov 24, 2005 7:01 am Post subject: (No subject) |
|
|
It's alright wtd. I've hated you ever since I decided to buy my iMac. |
|
|
|
|
|
wtd
|
Posted: Thu Nov 24, 2005 5:29 pm Post subject: (No subject) |
|
|
Here's a python question.
Python: | class Student(object):
def __init__(self, name, *grades):
self.name = name
self.grades = grades
def __str__(self):
return "%s has an average of %.2f" % (self.name, self.average_grade) |
Add a single line of code which makes the above class work correctly. It must not only work, but also accurately report the student's average grade. |
|
|
|
|
|
Hikaru79
|
Posted: Thu Nov 24, 2005 7:07 pm Post subject: (No subject) |
|
|
Python: | class Student(object):
def __init__(self, name, *grades):
self.name = name
self.grades = grades
self.average_grade = sum(*grades)/len(*grades)
def __str__(self):
return "%s has an average of %.2f" % (self.name, self.average_grade)
foo = Student ( "Adrian", [40,34,40,50,69] )
print foo |
This okay?
EDIT: w00t! 600 posts |
|
|
|
|
|
|
|