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

Pages: 1-4041-8081-

Elegant solutions

Name: Anonymous 2007-06-28 15:16 ID:lcs47ZU6

Im bored, show me some good code (any language whatsoever)
Post a problem, then a great solution.

1) Transpose a matrix
(apply #'mapcar #'list '(( 1  2  3  4)
                         ( 5  6  7  8)
                         ( 9 10 11 12)
                         (13 14 15 16)))
(( 1  5  9 13)
 ( 2  6 10 14)
 ( 3  7 11 15)
 ( 4  8 12 16))

Name: Anonymous 2007-06-28 15:24 ID:ERvjIcVI

2) kill a computation if it doesn't terminate after <limit> microseconds

compute_ :: Int -> IO a -> IO (Maybe a)
compute_ limit computation = do
    result <- atomically newEmptyTMVar
    runner <- forkIO $ do c <- computation
                          atomically $ putTMVar result $ Just c
    reader <- forkIO $ do threadDelay limit
                          killThread runner
                          atomically $ putTMVar result $ Nothing
    a <- atomically $ takeTMVar result
    killThread runner
    killThread reader
    return a


*Main> let loop = loop in compute_ 1000000 loop
Nothing

Name: Anonymous 2007-06-28 16:02 ID:9cNOan1x

matrix multiplication.
I wrote a simple lisp code for this, but I want to see other options.

Name: Anonymous 2007-06-28 16:07 ID:Heaven

1) Transpose a matrix
>>> mat = (1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16)
>>> tuple(zip(*mat))
((1, 5, 9, 13), (2, 6, 10, 14), (3, 7, 11, 15), (4, 8, 12, 16))

Name: Anonymous 2007-06-28 16:11 ID:CbPgANVr

3) Running a program for an undefined amount of time
while (true);

Name: Anonymous 2007-06-28 16:14 ID:Heaven

in before factorials with haskell

Name: Anonymous 2007-06-28 16:16 ID:Heaven

>>5
ahahaha

Name: Anonymous 2007-06-28 16:19 ID:lcs47ZU6

op here, i'd write it like this.


(mapcar (lambda (row)
 (apply #'mapcar (lambda (&rest column) (apply #'+ (mapcar #'* row column))) matrix2)) matrix1)

Name: Anonymous 2007-06-28 16:23 ID:lcs47ZU6

(mapcar
 (
lambda (row) (apply (function mapcar)
   (
lambda (&rest column)
    (
apply (function +) (mapcar (function *) row column))) matrix2)) matrix1)

Name: Anonymous 2007-06-28 16:24 ID:lcs47ZU6

(mapcar
 (
lambda (row)
  (
apply (function mapcar)
   (
lambda (&rest column)
    (
apply (function +) (mapcar (function *) row column))) matrix2)) matrix1)

Name: Anonymous 2007-06-28 16:31 ID:Heaven

matmul a b = map (\r -> map (sum . (zipWith (*) r)) (transpose b)) a

Name: Anonymous 2007-06-28 16:37 ID:9cNOan1x

>>8
better than my version. I transposed the second matrix first.
(defun dot-product (l1 l2)
    (apply #'+ (mapcar #'* l1 l2)))
(defun row-by-columns (row columns)
    (mapcar (lambda (c) (dot-product row c)) columns))
(defun matrixm2 (matrix1 matrix2)
    (let ((tr (apply #'mapcar #'list matrix2)))
        (mapcar (lambda (f) (row-by-column f tr)) matrix1))) 

If I've read your code well, you're inserting the apply mapcar directly.

Name: Anonymous 2007-06-28 16:40 ID:Heaven

also, transpose
transpose [x] = map (:[]) x
transpose (x:xs) = zipWith (:) x (transpose xs)

Name: Anonymous 2007-06-28 16:43 ID:Heaven

better version
transpose [] = repeat []
transpose (x:xs) = zipWith (:) x (transpose xs)

Name: Anonymous 2007-06-28 17:05 ID:9cNOan1x

>>11
great solution!
this is how it looks in lisp

(defun matrixm3 (a b)
    (mapcar (lambda (r) (mapcar (compose #'sum (p/ mapcar (function *) r)) (transpose b))) a))

(defun sum (l)
    (apply #'+ l))
(defun transpose (m)
    (apply #'mapcar #'list m))

of course, you need to have defined compose and p/, which is partial aplication.

Name: Anonymous 2007-06-28 17:26 ID:Heaven

>>14
Now with 100% more foldr

tranpose = foldr (zipWith (:)) (repeat [])

Name: Anonymous 2007-06-28 18:29 ID:lcs47ZU6

>>16
very slick, its even written in pointless style.
next challenge: uniq
e.g. 1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5 -> 1 2 3 4 5 8 6 8 9 5

Name: Anonymous 2007-06-28 19:09 ID:FtQRFguh

main(){system("uniq");}

Name: Anonymous 2007-06-28 19:30 ID:8ZraZvvg

Determine if a year is a leap year.

int isLeap(int year){return(year%400)?(year%100)?(year%4)?0:1:0:1;}

Name: Anonymous 2007-06-28 20:04 ID:HMaDqBI7

>>17
<?php
$n = "1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5";
$u = array();
$m = explode( ' ',  $n );
$r = '';

foreach( $m as $v )
{
    if( !$u[ $v ] )
    {
        $u[ $v ] = true;
        $r .= $v . ' ';
    }
}

echo "This list, {$n}, is now unique: ";
echo substr( $r, 0, -1 ); //remove trailing space
?>

Name: Anonymous 2007-06-28 20:05 ID:HMaDqBI7

...forgot some EXPERT [ code ]s

<?php
$n = "1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5";
$u = array();
$m = explode( ' ',  $n );
$r = '';

foreach( $m as $v )
{
    if( !$u[ $v ] )
    {
        $u[ $v ] = true;
        $r .= $v . ' ';
    }
}

echo "This list, {$n}, is now unique: ";
echo substr( $r, 0, -1 ); //remove trailing space
?>

Name: Anonymous 2007-06-28 20:17 ID:Heaven

>>20
>>21
fail

Name: Anonymous 2007-06-28 21:43 ID:W4zqoxYd

In Python, and actually elegant, not crappy:
i, d = '1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5', {}
print 'This list, %s, is now unique:' % i
print ' '.join(d.setdefault(n, n) for n in i.split() if n not in d)

Or if you don't care about preserving order:
i = '1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5'
print 'This list, %s, is now unique:' % i
print ' '.join(sorted(set(i.split())))

Name: Anonymous 2007-06-28 23:02 ID:Heaven

>>23
ONE WORD! FORCED INDENTATION OF THE CODE! THREAD OVER!

Name: Anonymous 2007-06-28 23:18 ID:9cNOan1x

>>23
are you a troll, or just a dumbass? you were supposed to CODE the "set" function! the set function, in fact, it's all you were supposed to code.

there are many ways of writing "uniq", depending if you want efficience or not, or if you want to preserve the order, or not.

Name: Anonymous 2007-06-29 0:19 ID:1AXGUeS5

read lines from file into list or array of strings

(with-open-file (s "test.c")
  (loop for line = (read-line s nil :eof)
        until (eq line :eof)
        collect line))

Name: Anonymous 2007-06-29 0:21 ID:1AXGUeS5

>>2
is that Haskell?

Name: Anonymous 2007-06-29 0:22 ID:1AXGUeS5

>>5
for (;;);

Name: Anonymous 2007-06-29 2:21 ID:TGav32jf

Fork-bomb:
main(){while(1)fork();}
Let's see you suave LISP faggots do that.

Name: Anonymous 2007-06-29 4:30 ID:/T2FVqR7

>>17
uniq [] = []
uniq (x:xs) = x : uniq (dropWhile (== x) xs)

Name: Anonymous 2007-06-29 5:54 ID:GGxG1Je+

>>26
lol hi, heres how a expert BBCode programmer would do it in C:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

char ** read_lines(char * filename) {
  char ** output, * p, ** q;
  struct stat file_stat;
  FILE * fptr = fopen(filename, "r");
 
  output = malloc(fread(p = malloc(fstat(fileno(fptr), &file_stat) + file_stat.st_size), 1, file_stat.st_size, fptr)*sizeof(char *));
  *(q = output) = p;
  do do if(*p == '\n') *p = '\0'; while(*p++); while((*q++ = p) && *p);
  *q++ = NULL;
 
  return output;
}

void print_lines(char ** lines) {
  while(*lines)
    printf("<%s>\n", *lines++);
}

int main(void) {
  char * filename = "test.c";
  print_lines(read_lines(filename));
  return 0;
}

Name: Anonymous 2007-06-29 6:15 ID:GGxG1Je+

>>29
lisp fork bomb cons
#1=(#1# . #1#)

Name: Anonymous 2007-06-29 6:16 ID:HOdGUDqA

>>30
Only works if all the duplicate values are together. For the general case you'll want to use filter.

Name: Anonymous 2007-06-29 6:47 ID:TGav32jf

>>32
Nope, that's cheating. Nice try though.

Name: Anonymous 2007-06-29 7:39 ID:oz4sORyk

>>30

(oncdrs (cons it (remove-if (p/ = it) rec) nil lst)

but this is a bit inefficient, cause you filter after the recursion.
if you don't care about the order:

(oncdrs (if (member it rec) rec (cons it rec)) nil lst)




Name: Anonymous 2007-06-29 8:09 ID:Heaven

>>25
set() is not order preserving

Name: Anonymous 2007-06-29 9:11 ID:uYh3AZ/6

>>33
Just sort the input first. It is faster.

Name: Anonymous 2007-06-29 9:16 ID:A5mpTx2G

>>32
too much syntax.
fork bomb in factor:
USING: threads ;
[ dup in-thread ] dup in-thread


Name: Anonymous 2007-06-29 13:54 ID:/T2FVqR7

37) List of all prime numbers
let primes (x:xs) = x : primes (filter ((/= 0) . (flip mod x)) xs) in primes [2..]

Name: Anonymous 2007-06-30 21:04 ID:bXwJRXAJ

>>39
let primes = List.nubBy (((== 0) . ) . flip mod) [2..]

Name: Anonymous 2007-06-30 23:11 ID:Uh/Yaan4

AMIDOINITRITE??

(defun primer ()
 ((lambda (&rest q)
  (lambda ()
  ((lambda (u n) (funcall u u n))
   (lambda (u n) (if (some (lambda (x y) (and (/= 1 y) (= (mod x y) 0)))
                           ((lambda (x &aux (y (list x))) (setf (cdr y) y)) n) q)
                     (funcall u u (1+ n)) (setf q (cons n q) n n))) (1+ (car q))))) 1))

Name: Anonymous 2007-07-01 5:30 ID:pp6LFGvp

>>26
while(<>){push @lines, $_;}

What's that? 27 keystrokes?

Oh, wait, that's right. Perl isn't elegant, it's hacky and kludgy.

Name: Anonymous 2007-07-01 5:53 ID:Heaven

>>42
@lines = <>;

Name: Anonymous 2007-07-01 7:41 ID:TFsb1KjX

zip(*x)

Python wins.

Name: Anonymous 2007-07-01 9:43 ID:6/IC6p2W

Start>run> type "cmd">type "edit look.bat"
type: "echo off
:look:
echo look around you
goto look"

push alt>save
push alt>exit

type "look"

Name: Anonymous 2007-07-01 9:50 ID:iYCbzeuc

>>44
and that does what?
also THE FORCED INDENTATION OF CODE

Name: Anonymous 2007-07-01 9:58 ID:TFsb1KjX

>>46
You wanted to transpose a matrix, right?
Also, zip(*x) is an expression thus it's not subject to forced indentation of code.

Name: Anonymous 2007-07-01 10:07 ID:iYCbzeuc

>>47
CORRECTION! I WANTED TO LOL @ YOU FOR BEING A FUCKING GAY JEW

Name: Anonymous 2007-07-01 12:36 ID:TFsb1KjX

>>48
I'd rather be a gay jew than a straight muslim

Name: Anonymous 2007-07-01 14:52 ID:mfzzduCs

>>49
uncalled for, that is in no way true for anyone that has a fucking brain, even if its the size of you jewish dick

Name: Anonymous 2007-07-01 15:33 ID:iYCbzeuc

>>50
LOL YHBT YHL HAND!!!

Name: Anonymous 2007-07-01 15:40 ID:dCFpJOs/

>>42
where do you write the name of the file?

Name: Anonymous 2007-07-01 15:47 ID:TFsb1KjX

>>50
Muslim

Who will you bomb tomorrow, bub?

Name: Anonymous 2007-07-01 17:04 ID:wCj+wvsT

I think matlab has the most elegant solutions to these problems
A'
B*A

Name: Anonymous 2007-07-01 18:06 ID:ip5ObYuR

>>54
k now post code to determine how similar two pieces of audio are. using matlab.

Name: Anonymous 2007-07-01 18:24 ID:84OFbcT1

>>55
You're an asshole.

Name: Anonymous 2007-07-01 23:16 ID:ip5ObYuR

>>56
SUCCESSFUL!

Name: Anonymous 2007-07-02 1:38 ID:Heaven

>>54 and >>56 are different people, >>57

Name: Anonymous 2007-07-02 11:38 ID:Heaven

>>55 and >>57 are different people, >>58

Name: Anonymous 2007-07-02 18:30 ID:Heaven

>>56 and >>58 are different people, >>59

Name: Anonymous 2007-07-02 20:17 ID:Heaven

>>57 and >>59 are different people, >>60

Name: Anonymous 2007-07-02 20:39 ID:Heaven

>>58 and >>60 are different people, >>61

Name: Anonymous 2007-07-03 9:25 ID:w/DqIVCq

bump

Name: Anonymous 2007-07-03 9:26 ID:w/DqIVCq

/r/ a one lined code that returns the larger of two numbers

Name: Anonymous 2007-07-03 9:27 ID:fkP3dR0i

>>64

#define bigger(x, z) ((x>z)?(x):(z))

Name: Anonymous 2007-07-03 9:46 ID:ANmcquwZ

template <class T> T bigger (T t1, T t2){ return t1>t2 ? t1 : t2; }

(defun bigger (x y) (if (> x y) x y) )

Name: Anonymous 2007-07-03 11:05 ID:Heaven

>>33
'uniq' the unix command assumes the input is sorted and so should all functions named uniq

Name: Anonymous 2007-07-03 11:12 ID:f8ckGWbD

>>64
tuck - dup abs + 2/ +

or you could just use math::max (http://factorcode.org/responder/browser/browse?apropos=&word=max&vocab=math), which basically does this:
2dup > [ drop ] [ nip ] if

>>65
#define bigger(x,z) (x>z?x:z)

Name: Anonymous 2007-07-03 11:23 ID:/LWDyi+e

>>68
#define bigger(x,z) ([b]([/b]x[b])[/b]>[b]([/b]z[b])[/b]?[b]([/b]x[b])[/b]:[b]([/b]z[b])[/b])

Name: Anonymous 2007-07-03 11:30 ID:Heaven

I AM NOT AN EXPERT BBCODE PROGRAMMER :(
#define bigger(x,z) ((x)>(z)?(x):(z))

Name: Anonymous 2007-07-03 11:44 ID:ANmcquwZ

>>68 It is still fail.

#define bigger(x,z) (x>z?x:z)
bool condition=true;
std::cout << bigger(condition ? 1 : 3, 37) << std::endl;

Yes ladies and gentlemen, the output is 1.

Name: Anonymous 2007-07-03 11:59 ID:Heaven

>>65-71

FUCKING NOOBIE PROGRAMMERS

#define bigger(x,z) (((x)>(z))?(x):(z))

GTFO

Name: Anonymous 2007-07-03 13:05 ID:Heaven

>>71
#define bigger(x,z) ((x)>(z)?(x):(z))
int i=37;
printf("%d\n",bigger(37,i--));


OH SHI-

Name: Anonymous 2007-07-03 13:06 ID:Heaven

>>72 pwnt by >>73

Name: Anonymous 2007-07-03 13:07 ID:LpOa+FrS

lol.

Name: Anonymous 2007-07-03 13:14 ID:LpOa+FrS

FUNCTIONAL PROGRAMMING FTW.

Name: Anonymous 2007-07-03 14:43 ID:Bh7nH8tt

(define (bigger x y) (if (> x y) x y))

Name: Anonymous 2007-07-03 14:54 ID:H+jPXAUI

#!/usr/bin/env ruby
p "\a" while 1

Name: Anonymous 2007-07-03 15:13 ID:Is5GO2aU

#!/usr/bin/bash
:(){:|:};:

Name: Anonymous 2007-07-03 15:15 ID:feUJj+BN

>>78
uh... you printed out "\a" a lot... so what?

Name: Anonymous 2007-07-03 15:41 ID:H+jPXAUI

>>80

"\a" == system bell

what the hell are you doing here

Name: Anonymous 2007-07-03 15:42 ID:feUJj+BN

>>81
\a = GTFO YOU STUPID NOOB

Name: Anonymous 2007-07-04 6:21 ID:Heaven

>>78
you should be forking several processes of printing "\a" infinitley for maximum \a'ing

Name: Anonymous 2007-07-04 7:14 ID:Heaven

printf("%fucking \awesome!");

Name: Anonymous 2009-01-14 14:41

C considered harmful

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