r/tinycode • u/nexe • Apr 26 '16
A tiny web server in C
r/tinycode • u/nexe • Apr 26 '16
r/tinycode • u/nexe • Apr 17 '16
r/tinycode • u/nexe • Apr 17 '16
r/tinycode • u/nexe • Apr 16 '16
r/tinycode • u/RegExp33 • Apr 09 '16
r/tinycode • u/xem06 • Feb 14 '16
Not really impressive visually, but an interesting compression challenge :)
r/tinycode • u/quzox • Feb 07 '16
#include <stdio.h>
#include <vector>
using namespace std;
enum Operation {ADD,SUB,MUL,DIV};
void printSoln(vector<int>& soln, vector<Operation>& ops) {
printf("Solved: ");
for (size_t i = 0; i < soln.size(); ++i) {
printf("%d", soln[i]);
if (i < soln.size() - 1) {
Operation op = ops[i + 1];
switch (op) {
case ADD: printf(" + "); break;
case SUB: printf(" - "); break;
case MUL: printf(" * "); break;
case DIV: printf(" / "); break;
}
}
}
printf("\n");
}
int evaluate(vector<int>& seq, vector<Operation>& ops) {
int curr = 0;
for (size_t i = 0; i < seq.size(); ++i) {
Operation op = ops[i];
switch (op) {
case ADD: curr += seq[i]; break;
case SUB: curr -= seq[i]; break;
case MUL: curr *= seq[i]; break;
case DIV: curr /= seq[i]; break;
}
}
return curr;
}
void recSolve(vector<int>& unused, vector<int>& used, vector<Operation>& ops, int curr, int target) {
if (curr == target) { printSoln(used, ops); return; }
if (unused.size() == 0) return;
for (size_t i = 0; i < unused.size(); ++i) {
int num = unused[i];
unused.erase(unused.begin() + i);
used.push_back(num);
for (int opDex = 0; opDex < 4; ++opDex) {
Operation op = (Operation)opDex;
ops.push_back(op);
int newCurr = evaluate(used, ops);
recSolve(unused, used, ops, newCurr, target);
ops.pop_back();
}
used.pop_back();
unused.insert(unused.begin() + i, num);
}
}
int main(int argc, char* argv[]) {
if (argc != 8) {
printf("Usage: CountdownNumbersSolver.exe inputNumber1...inputNumber6 targetNumber\n");
printf("e.g. : CountdownNumbersSolver.exe 1 2 3 4 5 6 200\n");
return 0;
}
vector<int> unused;
unused.push_back(atoi(argv[1]));
unused.push_back(atoi(argv[2]));
unused.push_back(atoi(argv[3]));
unused.push_back(atoi(argv[4]));
unused.push_back(atoi(argv[5]));
unused.push_back(atoi(argv[6]));
vector<int> used;
vector<Operation> ops;
int target = atoi(argv[7]);
int curr = 0;
recSolve(unused, used, ops, curr, target);
return 0;
}r/tinycode • u/aloisdg • Feb 05 '16
First post here. I am a beginner in bash scripting so, any remarks are welcome! This 140-length script is a variation of one that I am using in small project called seasonpaper. You need feh and curl to use it. The script can accept any subreddit. You can replace the url with another.
feh --bg-fill $(curl -s "https://www.reddit.com/r/skyporn.json"|grep -Po '\bhttps?:[^)"]+\.(?:jpg)'|grep -v "reddit"|(head -n1 && tail -n1))
I find it cool so here I am.
Cheers,
r/tinycode • u/xem06 • Feb 03 '16
r/tinycode • • Feb 02 '16
r/tinycode • • Feb 02 '16
r/tinycode • u/networked_ • Jan 29 '16
r/tinycode • u/mus1Kk • Jan 29 '16
Seeing the Haskell post I thought I upped the ante.
0,1,*+*...*Of course you can use a 1 as the first digit if you so desire.
... is the sequence operator and creates a lazy list from 0 to infinity (the last asterisk). The third element *+* is a generator. An expression with an asterisk (called Whatever) for a term is automatically made into a closure and each asterisk is assigned successively an argument. The two arguments are the two previous elements of the sequence.
Retrieving the elements is a simple list operation:
(0,1,*+*...*)[5] # 6th element
(0,1,*+*...*)[5..10] # elements 6 through 10edit: To elaborate a bit regarding the rules. This is normal code. In fact, it's even mentioned in the documentation. There is nothing that would qualify as magic or code golfing. Every operator is used as designed. The terseness of this particular example is just a happy coincidence.
r/tinycode • u/MiT_2020 • Jan 28 '16
r/tinycode • u/rswier • Jan 27 '16
Being snowed in for a couple of days gave me a chance to tinker with c4 and add a few new features. The changes are in two new branches of the project so the main branch remains minimal.
switch-statement adds switch statement support to the compiler. This removes the need for the long chain of if-then-else-if statements that make up the bulk of the code. I wanted to do this with minimal amount of changes to the source and no new machine instructions, and it turns out to be just possible with some trickery. The generated code for case statements is only slightly faster than the original if-then-else version. The next logical improvement would be to add an actual jump table. I leave that as an exercise for the aspiring compiler writer.
c5-AST adds an abstract syntax tree, constant folding, and a split front-end/back-end design. The number of functions required has exploded to five. With an AST, the compiler is not limited to generating code on the fly with parsing. This capability allows function parameter code to be emitted and pushed on the stack in the proper right-to-left order. With a modular code generator, new targets are easily supported such as native x86 machine language featured in c5x86.c. The compiler now resembles something you could actually build upon (or at least shows the way.)
Rob Swierczek
r/tinycode • u/bobcrater • Jan 25 '16