Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Random cool shit you made thread.

Name: Anonymous 2008-06-04 10:39

GO!

Name: Anonymous 2008-06-04 10:42

And, as the first contributor:

Creating a .png image out of a binary file and back again:

bin2png:


#!/usr/bin/env php
<?php
$opts = getopt("hf:o:");
if (isset($opts['h']) || !isset($opts['f'])) {
    $usage = <<<EOD
 Usage: $argv[0] [-h] -f file.bin [-o file.png]

EOD;
    fwrite(STDERR, $usage);
    die();
}
$f = @ file_get_contents($opts['f']);
if (!$f) {
    fwrite(STDERR, "Invalid file.\n");
    die();
}

$len = strlen($f);
$data = $len . "\0" . basename($opts['f']) . "\0" . $f;

$size = ceil(sqrt(strlen($data) / 3));

$img = imagecreatetruecolor($size, $size);
for ($a = 0; $a < $size; ++$a) {
    for ($b = 0; $b < $size; ++$b) {
        if (isset(   $data[((($a * $size) + $b) * 3)    ])) {
            $R = ord($data[((($a * $size) + $b) * 3)    ]);
        } else {
            $R = 255;
        }
        if (isset(   $data[((($a * $size) + $b) * 3) + 1])) {
            $G = ord($data[((($a * $size) + $b) * 3) + 1]);
        } else {
            $G = 255;
        }
        if (isset(   $data[((($a * $size) + $b) * 3) + 2])) {
            $B = ord($data[((($a * $size) + $b) * 3) + 2]);
        } else {
            $B = 255;
        }
        $c = imagecolorallocate($img, $R, $G, $B);
        imagesetpixel($img, $b, $a, $c);
    }
}
header ("Content-type: image/png");
if (!isset($opts['o']) || empty($opts['o'])) {
    $opts['o'] = null;
} else {
    echo "Writing image to ${opts['o']}.\n";
}
imagepng($img, $opts['o'], 9);

?>




png2bin:



#!/usr/bin/env php
<?php
$opts = getopt("hf:o");
if (isset($opts['h']) || !isset($opts['f'])) {
    $usage = <<<EOD
 Usage: $argv[0] [-h] -f file.bin [-o]
   o: Extracts file to original filename instead of stdout.

EOD;
    fwrite(STDERR, $usage);
    die();
}
$img = @ imagecreatefrompng($opts['f']);
if (!$img) {
    fwrite(STDERR, "Invalid file.\n");
    die();
}
$x = imagesx($img);
$y = imagesy($img);
$o = "";
for ($a = 0; $a !== $x; ++$a) {
    for ($b = 0; $b !== $y; ++$b) {
        $c = imagecolorat($img, $b, $a);
        $o .= chr(($c >> 16) & 0xFF) . chr(($c >> 8 ) & 0xFF) . chr($c & 0xFF);
    }
}
list($l, $name, $f) = explode("\0", $o, 3);
if (!is_numeric($l) || empty($name) || empty($f)) {
    fwrite(STDERR, "Invalid file.");
    die();
}
if (isset($opts['o'])) {
    echo "Writing $l KiB to $name.\n";
    file_put_contents($name, substr($f, 0, $l));
} else {
    echo substr($f, 0, $l);
}
?>

Name: Anonymous 2008-06-04 10:58

factorial in Haskell

fact 1 = 1
faxt x = x * fact (x-1)

Name: Anonymous 2008-06-04 10:58

nerd

Name: Anonymous 2008-06-04 10:58

I made a bug :(

fact 1 = 1
fact x = x * fact (x-1)

Name: Anonymous 2008-06-04 11:03

COM/OLE api for two scripting languages.

Name: Anonymous 2008-06-04 11:08

>>5
fact 0 = 1, too.

Name: Anonymous 2008-06-04 11:25

fact n = product [1..n]

Name: Anonymous 2008-06-04 11:31

# This code is intended to convert AngelCode BMFont font files
# (.fnt) into a header file, for use with Nintendo DS homebrew
# development.

# This is intended to be used with the object files created by bin2o
# in devkitPro. If the font name has spaces in it, this will probably break.
# Try using underscores instead.

# Another feature provided is that the kerning nodes are sorted to allow
# binary search of kerning nodes, if you don't want to go to the effort of
# putting them in a proper data structure.

# You are expected to define the FontInfo, CharInfo, and KerningInfo structures yourself.


for file in ARGV
  lines = File.open(file, "r") do |f|
    f.read
  end
 
  face = nil
  size = nil
  width = nil
  height = nil
  line_height = nil
 
  chars = []
  kerning = []
 
  # This could be better, but it works...
  for line in lines
    m = /(\w+)\s+(.*)/.match(line)
    if m
      name = m[1]
      parts = {}
     
      m[2].split(/\s+/).each do |ma|
        ma=ma.split('=')
        parts[ma[0]] = ma[1]
      end
     
      case name
        when "info"
          face = parts["face"].gsub(/(^\")|(\"$)/,'');
          size = parts["size"].to_i
          bold = parts["bold"].to_i
          italic = parts["italic"].to_i
        when "common"
          line_height = parts["lineHeight"].to_i
          width = parts["scaleW"].to_i
          height = parts["scaleH"].to_i
        when "char"
          char = []
          char << parts["id"] << parts["x"] << parts["y"] << parts["width"] << parts["height"] << parts["xoffset"] << parts["yoffset"] << parts["xadvance"] << parts["page"]
          chars << char
        when "kerning"
          kern = []
          kern << parts["first"] << parts["second"] << parts["amount"]
          kerning << kern
        else
      end
    end
  end
 
  #Sort the kernings by first, then second.
  kerning = kerning.map {|k|
    k.map{|ki|
      ki.to_i
    }
  }
 
  kerning.sort! do |a,b|
    if a[0] != b[0]
      a[0] <=> b[0]
    elsif a[1] != b[1]
      a[1] <=> b[1]
    else
      0
    end
  end
 
  # Obviously, modify this part to suit your needs.
  # I should probably put the padding value in there too...
  File.open(file.gsub(/\.fnt$/,'.h'), "w") do |fh|
    fh.puts <<HEADER
#include "types.h"
#include "font.h"
#include "#{face}_#{size}_bin.h"

namespace demo {
    FontInfo #{face}#{size} = {
      "#{face}", #{size}, #{line_height}, #{bold}, #{italic},
      {0,0,0,0},
      {0,0,#{width},#{height}},
      #{face}_#{size}_bin, #{face}_#{size}_bin_size
    };
 
  CharInfo #{face}_#{size}_Chars[#{chars.length}] = {
HEADER

    for char in chars
      fh.puts "    { #{char.join(", ")} },"
    end
 
    # End of the characters, start of the kerning nodes.
    fh.puts <<MIDDLE
  };
 
  KerningInfo #{face}_#{size}_Kerning[#{kerning.length}] = {
MIDDLE

    for kern in kerning
      fh.puts "    { #{kern.join(", ") } },"
    end
 
    #End of the file.
    fh.puts <<END
 
  };
}
END
  end
end

Name: Anonymous 2008-06-04 11:55

http://no-info.no-ip.info:6224/lambda

Although I wasn't the one who made that

Name: Anonymous 2008-06-04 12:11

fact x
  | x < 0     = error "what are you doing"
  | x == 0    = 1
  | otherwise = x * fact (x-1)


Final version.

Name: Anonymous 2008-06-04 12:30

>>8
Negative number failure.

Name: Anonymous 2008-06-04 12:33

unsigned int fact(const unsigned int x){
    if(x==0) return 1;
    return x * fact(x)
}


GCC TAIL RECURSION JUST KICKED IN, YO

Name: Anonymous 2008-06-04 12:46

>>13
A little bit of me died inside, the bit that said ''do not use recursion in C``. Also, failed code.

Name: Anonymous 2008-06-04 12:57

>>13
It's not possible to optimize that with tail recursion. fail

Name: Anonymous 2008-06-04 12:57

>>13
also lol @ 'const'.

Name: Anonymous 2008-06-04 13:06

>>13
I write const-correct code because I'm an EXPERT PROGRAMMER!

Name: Anonymous 2008-06-04 13:41

const * const grabs const dick const * const

Name: Anonymous 2008-06-04 13:51

This may surprise you, but I wrote SICP.

Name: Anonymous 2008-06-04 14:04

>>11
Ultimate version:

import Control.Monad.Identity

factorial n | n < 0     = fail "factorial: negative argument."
            | otherwise = product [1..n]

fact = runIdentity . factorial

Name: Anonymous 2008-06-04 14:10

perl < /dev/random

Name: Anonymous 2008-06-04 14:13

fact (n+0) = product [2..n]

Name: Anonymous 2008-06-04 14:33

>>20
Corrected version:

import Control.Monad.Identity

factorial n | n < 0     = fail "factorial: negative argument."
            | otherwise = return $ product [1..n]

fact = runIdentity . factorial

Name: Anonymous 2008-06-04 14:36

STOP POSTING BASIC HASKELL FUNCTIONS FOR FUCKS SAKE

Name: Anonymous 2008-06-04 14:43

A few weeks ago I found myself in a suprising situation: for the first time in the 15 years I've been programming, I actually needed a factorial function in a real-world application.

Name: Anonymous 2008-06-04 14:44

Scheme interpreter in python.

Name: Anonymous 2008-06-04 14:45

>>24
The basic functions are just excuses to share useful techniques. For instance, >>23 enables you to write things like:

print . fact . sum . mapMaybe (factorial . read) . words =<< getLine


which would have had to be much longer, had fact been implemented otherwise.

Name: Anonymous 2008-06-04 14:47

>>24
What are you implying about Haskell and Haskell programmers.

Name: Anonymous 2008-06-04 15:03

0&>:1-:v v* _$.@
   ^    _$>\:^


EXPERT BEFUNGE FACTORIAL

Name: Anonymous 2008-06-04 15:04

>>28
Nothing at all.

Name: Anonymous 2008-06-04 16:06

>>24,30 was clearly implying that Haskell programmers are all lazy.

Name: Anonymous 2008-06-04 16:45

>>1 has made nothing

I, for one, wrote a working English-speaking AI that I am currently training.

Name: Anonymous 2008-06-04 17:04

>>32
I wrote a random word generator which adds whatever you type to its list. I CREATED ARTIFICIAL INTELLIGENCE!

Name: Anonymous 2008-06-04 17:11

>>33
I think everyone here is a CS101 faggot just like myself!

Name: Anonymous 2008-06-04 17:18

Artificial intelligence is a load of bunk. Primitive game playing algorithms and a bit of language processing here and there.

Artificial? Maybe.
Intelligence? NO WAY

Name: Anonymous 2008-06-04 17:33

#include <limits.h>
#include <stdint.h>

#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
#error Please use a compiler that supports the C99 standard to compile this file.
#endif

void sort(uintmax_t *numbers, size_t length){
 uintmax_t temp[length], *arrays[2] = {numbers, temp};
 for(uint_fast8_t i = 0; i < sizeof(uintmax_t) * CHAR_BIT; ++i)
  for(size_t j = 0, start = 0, end = length - 1; j < length; ++j){
   if(!(arrays[i & 1][j] & 1 << i))
    arrays[i & 1 ^ 1][start++] = arrays[i & 1][j];
   if(arrays[i & 1][length - j - 1] & 1 << i)
    arrays[i & 1 ^ 1][end--] = arrays[i & 1][length - j - 1];
}}

Name: Anonymous 2008-06-04 17:34

i=1987;int(*j)()=&i;main(){j();}

Name: Anonymous 2008-06-04 18:20

>>25
Would you care to elaborate?

Name: Anonymous 2008-06-04 18:48

>>32
Same person: >>1,2

Name: Anonymous 2008-06-04 19:17

>>8
fact 0 = 1
fact n = n * (fact n)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List