r/dailyprogrammer • u/fvandepitte 0 0 • Apr 25 '16
[2016-04-25] Challenge #264 [Easy] Sort my code
Description
Keeping your code clean is one thing. But keeping it sorted is a whole other thing...
Today you will get sorted C++ coded (literaly) like this:
std::cout << "Hello world!" << std::endl;
}
#include <iostream>
int main () {And you have to unsort it into this:
#include <iostream>
int main () {
std::cout << "Hello world!" << std::endl;
}There are some rules you have to follow:
- Lines with
#includealways shows on top. - Indentation consists out of 2 spaces
- Whitespace lines are not obliged
- variables have to be defined before used.
- Every
{must have a}on the same indentation level - Lines that belong to the same method and are of the same indentation, are in order.
Input Description
You'll be given a program that was sorted
sum = i + sum;
{
}
int sum = 0
for (int i = 0; i <= 100; ++i)
std::cout << sum;
return 0;
{
}
#include <iostream>
int main()Output Description
Your program should unsort the lines to something compilable by the compiler:
#include <iostream>
int main()
{
int sum = 0;
for (int i = 0; i <= 100; ++i)
{
sum = i + sum;
}
std::cout << sum;
return 0;
}Challenge Input
sum = i + sum;
{
}
int sum = 0
for (int i = 0; i <= 100; ++i)
std::cout << sum;
return 0;
{
}
#include <iostream>
int main()Challenge Output
#include <iostream>
int main()
{
int sum = 0;
for (int i = 0; i <= 100; ++i)
{
sum = i + sum;
}
std::cout << sum;
return 0;
}Bonus
In C++ a method must be defined before you can use it. That's why when having multiple methods you must sort those methods on top first.
When you have multiple possibilities, you can sort the methods alpabeticly
Input
sum += f(x);
{
}
return ( 1.0 / ( y * y) );
unsigned int start = 1;
unsigned int end = 1000;
double sum = 0;
for( unsigned int x = start; x <= end; ++x )
std::cout << "Sum of f(x) from " << start << " to " << end << " is " << sum << std::endl;
return 0;
{
{
}
}
#include <iostream>
double f(double y)
int main()Output
#include <iostream>
double f(double y)
{
return ( 1.0 / ( y * y) );
}
int main()
{
unsigned int start = 1;
unsigned int end = 1000;
double sum = 0;
for( unsigned int x = start; x <= end; ++x )
{
sum += f(x);
}
std::cout << "Sum of f(x) from " << start << " to " << end << " is " << sum << std::endl;
return 0;
}Note
I have made some adjustments to the challenge after the feedback of /u/jnd-au
Finaly
Have a good challenge idea? Consider submitting it to /r/dailyprogrammer_ideas and there's a good chance we'll use it.
63
u/UnchainedMundane 1 0 Apr 25 '16
Shell
I don't think this challenge should be "Easy" as it pushes the limits of what is computationally possible or sensible. A proper solution would essentially require a working C parser and those are notoriously hard to get right. Not to mention the C preprocessor is at liberty to do whatever it wants, and that means that the position of #include and #define statements can alter the semantics of the whole program.
6
u/jnd-au 0 1 Apr 25 '16 edited Apr 25 '16
Scala. Um, is this even possible? Oh well, here’s my shot anyway (bonus included).
It doesn’t know which order the fizz buzz clauses should be in, and I don’t know how it’s supposed to know.Edit: After I wrote this, the challenge was edited. My original solution works with the new inputs too.2
u/[deleted] May 10 '16
Java
My code is too long, as I designed my app to run inside a Swing GUI. Here is my pastebin of it.
http://pastebin.com/0S4WmC3B
My challenge output is at the bottom.
I tried to keep my logic as generic as possible, and avoid any situation-specific logic to arrive at the correct output. I would really like some input about how I went about things. I have almost no guidance with my learning and I am worried that my code is very inefficient.
1
u/lostsemicolon Apr 25 '16
Ruby
I literally started learning Ruby this morning, so if this is sort of a hodge podge smorgasbord, that's why.
1
u/jpan127 Jun 17 '16
Damn looking at these other codes, I'm not even going to try to attempt this hahah...
0
u/[deleted] Apr 25 '16
[deleted] — view deleted comment