Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
S

SirTimothy

@SirTimothy
About
Posts
10
Topics
2
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Poetry Written in Code Contest
    S SirTimothy

    Adapted from an excerpt from Dr. Seuss' Fox In Socks. Not quite as smooth as I'd like, but it works.

    if (fighter1 is TweetleBeetle
    && fighter2 is TweetleBeetle
    && theWeapon is Paddle
    && thePlace is Puddle
    && thePlace.place is Bottle)
    return new TweetleBeetleBottlePuddlePaddleBattleMuddle();

    If fighter one is tweetle beetle, Fighter two is tweetle beetle, And the weapon is a paddle, And the place is a puddle, And the place's place is bottle, then return new TweetleBeetleBottlePuddlePaddleBattleMuddle

    The Lounge tutorial

  • Obj-C string construction
    S SirTimothy

    So I was looking at some code I wrote a few years back, when I was first learning objective-C. I found many places where I did something like this:

    char buffer[100];
    sprintf(buffer, "the value: %d", someValue);
    NSString* string = [NSString stringWithCString:buffer];

    of course, with more useful text and values. I think there may have even been some times when I malloc'd buffer rather than having it on the stack... for those who aren't familiar with obj-C, this could be more sanely/appropriately written as:

    NSString* string = [NSString stringWithFormat:"the value: %d", someValue"];

    The Weird and The Wonderful learning swift data-structures

  • top this
    S SirTimothy

    I've tidied up that code for you:

    int g = 0;
    

    _more:
    while((++g < 10) || (printf("%d\n", g), g < 10))
    goto _more;

    or:

    printf("10\n");

    The Weird and The Wonderful

  • Opinions
    S SirTimothy

    Nah, not a teacher. Actually, just graduated from university. Why'd I write it? Bored, felt like taking some reasonably clean code and totally butchering it, thought I'd see how ugly I could make it :) Of course, it could be much worse, but whatever, it was fun!

    The Weird and The Wonderful data-structures question discussion lounge learning

  • Opinions
    S SirTimothy

    It is kinda amazing, isn't it? The y[iarray] thing, I've seen in a few different websites, basically the compiler just expands it to *(y + iarray) which is the same as iarray[y] and it carries on its merry way. The O(n^2) complexity is hidden in the single loop, I reset the values of x and y in the conditional (the y=x&=~x bit) when y gets past the array length and x is non-zero. Pretty near impossible to prove n^2 runtime, or to prove correctness, or even to prove that it terminates, but it's fun and it works. I thought about writing some more sorts like this, but haven't gotten to it...

    The Weird and The Wonderful data-structures question discussion lounge learning

  • Opinions
    S SirTimothy

    Wow! For loops are neat! I rewrote my code, got rid of the gotos, used for loops, and put in some comments like in the article you linked to. I think it's much improved now. I'm especially proud of my reduction of the number of loops. The main sorting part used to involve two nested loops (one inside the other), but I was able to reduce it to a single loop!

    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    #define ARRAY_SIZE 20

    #define PNT(s, l, a) fprintf(std##s, l, a)
    #define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)

    void print_array(int *array) {
    int x = ARRAY_SIZE;
    char* fmts[] = { "%d, ", "%d\n" }; /* fmts has two strings in it */
    for (--array; PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0;); /* These for loops are really neat!
    I can do a bunch of things all in one line! Do something, test a conditional, do another thing! Or I can leave some of those
    out, if I don't need them. */
    for (;;) break; /* I can even do this! */
    }

    main() {
    int iarray[ARRAY_SIZE];
    int x, y;
    int ofs = iarray - &ofs; /* ofs is an offset */
    void (*prnt_func)/* These comment things are pretty neat too. I can add annotations to my code, without having to hide them in variable names
    or strings that do nothing. */(int*) = &print_array;
    "Here's how I would have had to annotate code before. It's kind of a pain.";
    srand((unsigned int)time(NULL));
    x ^= x; /* ^= looks like a duck. I'm going to call it the "quack operator" */
    for(;((y = rand/*om number generator*/(), x < /* I'm a comment in the middle of an expression */ARRAY_SIZE) && ((((x
    = x + 1) - 1)[iarray] = /*number from 0 to 99*/(y - (y / 100) * 100)) || 1));); /* lot's of nested () there! */
    PNT2LINES(out, Before sort, ---------------, \n);
    for((*prnt_func)(&ofs+ofs),y^=y,x&=~x;(y+1>=ARRAY_SIZE&&(x&&!(y=x&=~x)))||y+1<ARRAY_SIZE;(++y,iarray[y]<(y-1)
    [iarray])&&(y[iarray]^=iarray[y-1]^=iarray[y]^=*(iarray+y-1),x|=y)); /* This used to be two loops, when it was with GOTOs,
    but I was able to
    reduce it to one. That must be faster, right? It's also still easy to read, so it's a total win-win! */
    /* That line had a lot of quack operators! */
    PNT2LINES(out, After sort, ---------------, \n);
    (*prnt_func)(&ofs + ofs);
    }

    The Weird and The Wonderful data-structures question discussion lounge learning

  • Opinions
    S SirTimothy

    That, of course, is so that I can change the function name, and only need to change one place where it's used. Gotta think about maintainability and future modifications!

    The Weird and The Wonderful data-structures question discussion lounge learning

  • Opinions
    S SirTimothy

    nah, just felt like doing it

    The Weird and The Wonderful data-structures question discussion lounge learning

  • Opinions
    S SirTimothy

    mmm... last week? maybe the week before?

    The Weird and The Wonderful data-structures question discussion lounge learning

  • Opinions
    S SirTimothy

    Hey folks, I don't generally post on these forums, but I do enjoy the CP newsletters. Anyways, I wanted to share a lovely snippet I wrote a little while ago. Maybe get some opinions? It's a bubble sort in C. It generates 20 random numbers from 0 to 99, and sorts them! Of course, it's fairly straightforward, so I'm sure you could guess that.

    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    #define ARRAY_SIZE 20

    #define PNT(s, l, a) fprintf(std##s, l, a)
    #define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)

    void print_array(int *array) {
    int x = ARRAY_SIZE;
    char* fmts[] = { "%d, ", "%d\n" };
    --array;
    LBL0:
    if (PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0)
    goto LBL0;
    }

    main() {
    int iarray[ARRAY_SIZE];
    int x, y;
    int ofs = iarray - &ofs;
    void (*prnt_func)(int*) = &print_array;
    srand((unsigned int)time(NULL));
    x ^= x;
    LBL1:
    if ((y = rand(), x < ARRAY_SIZE) && ((((x = x + 1) - 1)[iarray] = (y - (y / 100) * 100)) || 1))
    goto LBL1;
    PNT2LINES(out, Before sort, ---------------, \n);
    (*prnt_func)(&ofs + ofs);
    LBL2:
    y = x &= ~x;
    LBL3:
    if (y + 1 < ARRAY_SIZE) {
    if ((++y, iarray[y] < (y-1)[iarray]) && (y[iarray] ^= iarray[y-1] ^= iarray[y] ^= *(iarray + y - 1), x |= y));
    goto LBL3;
    }
    if (x = x) goto LBL2;
    PNT2LINES(out, After sort, ---------------, \n);
    (*prnt_func)(&ofs + ofs);
    }

    The Weird and The Wonderful data-structures question discussion lounge learning
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups