Initial commit

This commit is contained in:
Smaug123
2020-10-15 20:27:52 +01:00
parent b8d7a8cbe5
commit df044cd9dc
3 changed files with 1127 additions and 1 deletions

View File

@@ -1,2 +1,6 @@
# rewriting-technical-mathematica
Rewriting the Technical Interview (https://aphyr.com/posts/353-rewriting-the-technical-interview), in Mathematica
Rewriting the Technical Interview (https://aphyr.com/posts/353-rewriting-the-technical-interview), in Mathematica.
This is nowhere near as neat as the original, and it's much more brittle, but it does the job.
Presented both as .nb and as .txt files (for those who don't fancy parsing M-expressions in their heads).

1084
c-interpreter.nb Normal file

File diff suppressed because it is too large Load Diff

38
c-interpreter.txt Normal file
View File

@@ -0,0 +1,38 @@
MakeExpression[
RowBox[{"(", x_RowBox?(FreeQ[List @@ #, RowBox] &), ")"}], form_] :=
MakeExpression[RowBox[Join[{"{"}, Sequence @@ x, {"}"}]], form]
MakeExpression[RowBox[{"(", x_RowBox, ")"}], form_] :=
MakeExpression[x, form]
MakeExpression[RowBox[{x__, "%", y__}], form_] :=
MakeExpression[RowBox[{"Mod", "[", RowBox[{x, ",", y}], "]"}], form]
MakeExpression[RowBox[{"println", RowBox[{"(", xs___, ")"}]}],
form_] := MakeExpression[RowBox[{"Print", "[", xs, "]"}], form]
MakeExpression[
RowBox[{"if", RowBox[{"(", xs__, ")"}], RowBox[{"{", zs___, "}"}]}],
form_] :=
MakeExpression[RowBox[{"If", "[", xs, ",", zs, "]"}], form]
MakeExpression[
RowBox[{"if", RowBox[{"(", xs__, ")"}], RowBox[{"{", zs___, "}"}],
"else", ys___}], form_] :=
If[ReleaseHold@MakeExpression[xs, form], MakeExpression[zs, form],
MakeExpression[RowBox[{ys}], form]]
MakeExpression[
RowBox[{"for",
RowBox[{"(", RowBox[{init__, ";", cond__, ";", incr__}], ")"}],
RowBox[{"{", expr__, "}"}]}], form_] :=
For[ReleaseHold@MakeExpression[init, form],
ReleaseHold@MakeExpression[cond, form],
ReleaseHold@MakeExpression[incr, form],
ReleaseHold@MakeExpression[expr, form]]
for (i = 1 ; i < 101 ;
i++) {if (i % 15 == 0) {println ("FizzBuzz");} else if (i % 3 ==
0) {println ("Fizz");} else if (i % 5 ==
0) {println ("Buzz");} else {println (i);};}