174 karma · joined August 24, 2007
Here is the corrected C version:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char const *argv[])
{
char *str = malloc(sizeof(char) * 238890);
char buf[5];
int i, n = 0, len;
for (i = 0; i < 50000; ++i) {
len = sprintf(buf, "%d", i);
strcat(str + n, buf);
n += len;
}
return 0;
}
It runs 100 times faster than the original program.Edit: If you think I "cheated" by doing some calculations that I shouldn't do, here is a C++ version that runs just as fast as the improved C version:
#include <string>
#include <cstdio>
int main()
{
std::string str;
char buf[6];
for (int i = 0; i < 50000; ++i)
{
sprintf(buf, "%d", i);
str += buf;
}
} print parse_expr("1+2/3/4-5*6")
Result: (+ 1 (/ 2 (/ 3 (- 4 (* 5 6))))) def parse(s):
for operator in ["+-", "*/"]:
depth = 0
for p in xrange(len(s) - 1, -1, -1):
if s[p] == ')': depth += 1
if s[p] == '(': depth -= 1
if not depth and s[p] in operator:
return [s[p]] + parse(s[:p]) + parse(s[p+1:])
s = s.strip()
if s[0] == '(':
return parse(s[1:-1])
return [s]
print parse("1+3* 4/haha-5*(2+3.14 ) * (4+5)")
Edit: grammerHow would you, in your favorite language, implement a program that displays GDP information for every country for years from 1970-2007 using a nice graph. Countries should be selectable using a fancy popup menu that displays country name and country shape...
Using Mathematica 6:
MenuView[Row[{Rasterize[
Show[CountryData[#, "Shape"], ImageSize -> {40, 40}],
Background -> None], #}] ->
Dynamic[DateListPlot[CountryData[#, {{"GDP"}, {1970, 2005}}]]] & /@
CountryData[], ImageSize -> Automatic]Our school day was up to six hours long (7 school hours (45 min each) + 5 min breaks in between). We received generally the same quality education as many other Central/Eastern European countries. Btw., the reason schools introduce extra shifts is to be able to accomodate more pupils in same classrooms.
Merry Christmas!
#include <stdio.h>
#define VERSION_INT 3209
#if VERSION_INT < 100
#error VERSION_INT less than 100
#elif VERSION_INT < 1000
#define VERSION_STR (char[]){VERSION_INT/100+'0','.',(VERSION_INT%100)/10+'0',(VERSION_INT%10)+'0',0
#elif VERSION_INT < 10000
#define VERSION_STR (char[]){VERSION_INT/1000+'0',(VERSION_INT%1000)/100+'0','.',(VERSION_INT%100)/1
#endif
int main(void)
{
printf("%d\n", VERSION_INT);
printf("%s\n", VERSION_STR);
return 0;
}
Output: 3209
32.09
This is not very elegant, and you have to split your version number to major and minor part, but it does the job. Oh, yes, minor version must be between 0 and 99 (inclusive).
#define VERSION_MAJOR 2
#define VERSION_MINOR 9
#define VERSION_INT (VERSION_MAJOR * 100 + VERSION_MINOR)
#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#if VERSION_MINOR<10
#define VERSION_STR STRINGIFY(VERSION_MAJOR) ".0" STRINGIFY(VERSION_MINOR)
#else
#define VERSION_STR STRINGIFY(VERSION_MAJOR) "." STRINGIFY(VERSION_MINOR)
#endif