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

Pages: 1-4041-8081-

2010 Homework No. 3 - findsuss

Name: Anonymous 2010-01-15 22:56

Write a program in the language of your choice that takes a string and inputs "SUSSMAN" for every time those characters appear. In case I'm not being clear, that means output "SUSSMAN" for every 3 's's and 1 'u', 'm', 'a', and 'n' in the string. Inputting "ssssumanssssssssssuman", for example, would print "SUSSMAN" twice. "ssuman", however, would print nothing.

I've written an inelegant solution in C, which I'll post in a second. It tends to crash after execution, however, and I cannot for the life of me figure out what I'm doing wrong. It does everything it's supposed to but refuses to terminate politely. Even stranger, it only happens when there is enough characters for two or more sussmen. I guess the problem is in the susscount loop, but I can't figure out where. Mind giving me a hand, /prog/?

Name: Anonymous 2010-01-15 22:58

/*       SUSSMAN        */
/* BEWARE, SHITTY COEDS */

#include <stdio.h>

int main()
{
    char suss[1];
    char letters[] = "SUMANsuman";
    int count[5] = {0}; /* S, U, M, A, N */
    int susscount = 0;
   
    int x, c;
   
    printf("Enter a string (terminate with 'Q'): ");
    for (x = 0; scanf("%c", &suss[x]), suss[x] != 'Q'; x++);
   
    for (x = 0; suss[x] != 'Q'; x++)
    {
        for (c = 0; c <= 9; c++)
        {
            if (suss[x] == letters[c])
            {
                if (c < 5)
                    count[c]++;
                else
                    count[c-5]++;
            }
        }
    }
   
    while (count[0] > 0)
    {
   
        for (x = 1, c = 0; x < 5; x++)
        {
            if (count[x] >= 1)
                c++;
            count[x]--;
        }
       
        if (count[0] >= 3)
            c++;
        count[0] -= 3;
       
        if (c == 5)
            susscount++;
    }
   
    for (; susscount > 0; susscount--)
        printf("SUSSMAN\n");
   
    return 0;
}

Name: Anonymous 2010-01-15 23:04

char suss[1];

Surely that can’t be right. Also, I am a fan of the following notation:

    while (susscount --> 0)
        printf("SUSSMAN\n");

Name: Anonymous 2010-01-15 23:13

>>3

GCC bitches if you use char suss[];

Also, COOL FREE UNDOCUMENTED PERL OPERATOR

Name: Anonymous 2010-01-15 23:13

The homework series was less bad back when it was not done by the guy appending numbers to it.

Name: Anonymous 2010-01-15 23:20

>>5

;_;

Name: Anonymous 2010-01-15 23:34

>>4
Please stop trying to use C.

Name: Anonymous 2010-01-15 23:38

●█████▄▄▄▄▄▄▄▄
▄▅███████▅▄▃▂
███sage tank███████►
◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲☼◤

Name: HMA FAN 2010-01-16 0:10

Your C code is a fucking abomination.

Here is some better SATORI CODE with COOL FREE ANNOTATIONS.
import Array
import Data.Maybe
import Data.Char

countChars :: String -> [(Char, Int)]
countChars = assocs . accumArray (+) 0 (minBound,maxBound)
             . (map (\x -> (toUpper x,1)))

countSuss :: [(Char,Int)] -> Int
countSuss m = minimum $ [floor . (/3) . fromIntegral $ count 'S'] ++ map count "UMAN"
    where count c = fromMaybe 0 $ lookup c m

findSuss = unwords . flip take (repeat "SUSSMAN") . countSuss . countChars


Example:
*Main> findSuss "SSS SSS SSS U U U M M M A A A N N N N N N N"
"SUSSMAN SUSSMAN SUSSMAN"

Name: Anonymous 2010-01-16 0:23

Golfing version to further ridicule your implementation.
import Array
import Data.Maybe
import Data.Char

findSuss xs = unwords $ take sussmen (repeat "SUSSMAN")
    where counts = assocs . accumArray (+) 0 (minBound,maxBound) . (map (\x -> (toUpper x,1))) $ xs
          sussmen = minimum $ [floor . (/3) . fromIntegral $ count 'S'] ++ map count "UMAN"
          count c = fromMaybe 0 $ lookup c counts

Name: Anonymous 2010-01-16 0:25


;;; I first misunderstood this problem as a search for 3 consecutive S, U, ...
;;; Here's the solution for that:

(defun sussman-count (string search-string
                        &aux (string (string-upcase string))
                           (search-string (string-upcase search-string)))
  (let ((s-len (length search-string))
        (b-len (length string)))
    (do ((index (search search-string string)
                (search search-string string :start2 index))
         (i 0 (1+ i)))
        ((not index) i)
      (when (<= (+ index s-len) b-len)
        (incf index s-len)))))

;;; (sussman-count "ssssumanssssssssssuman" "sss" ) => 4

(defun make-search-string (string count)
  (map 'list
       #'(lambda (char)
           (make-string count :initial-element char))
       (remove-duplicates (string-upcase string))))

(defparameter *search-strings* '#.(make-search-string "Sussman" 3))
;=> ("UUU" "SSS" "MMM" "AAA" "NNN")

(defun count-all-sussmans (string search-strings)
  (loop
    for search-string in search-strings
    sum (sussman-count string search-string)))

; (count-all-sussmans "ssssusmanssssssssssuuumannnn" *search-strings* ) => 6

(defun print-sussmans (n &optional (stream t))
  (loop repeat n do (format stream "~&Sussman~&")))

;CL-USER> (print-sussmans 2 )
;Sussman
;Sussman

(defun print-required-sussmans (string)
  (print-sussmans (count-all-sussmans string *search-strings*)))

;CL-USER> (print-required-sussmans "ssssusmanssssssssssuuumannnn")
;Sussman
;Sussman
;Sussman
;Sussman
;Sussman
;Sussman


;;; Okay, so this solves a harder problem than what OP wanted. It's my fault that I misunderstood the problem.

;;; So this counts every s,u,m,a,n character, and for every 3 of them prints as many sussmans as needed
(defun count-letters (string &optional (letters "Sussman"))
  (loop for letter across (remove-duplicates (string-upcase letters))
     sum (truncate (count letter string :key #'char-upcase :test #'char=) 3)))

(defun print-wizardly-sussmans (string)
  (loop repeat (count-letters string) do (format t "~&SUSSMAN~&")))

; (count-letters "Sssuuusssmaan" "su") => 3

;CL-USER> (print-wizardly-sussmans "ssssumanssssssssssuman")
;Sussman
;Sussman
;Sussman
;Sussman


;;; Oh fuck, I misread the problem again, " that means output "SUSSMAN" for every 3 's's and 1 'u', 'm', 'a', and 'n' in the string."
;;; Solution should be ( reusing the original sussman-count from up there )

(defun print-wizardly-sussmans-for-real (string)
  (loop repeat (sussman-count string "sssuman") do (format t "~&SUSSMAN~&")))

;CL-USER> (print-wizardly-sussmans-for-real "ssssumanssssssssssuman")
;SUSSMAN
;SUSSMAN

; That seems to be right. I hope I understood the problem correctly this last time.


I really shouldn't code while sleeping.

Name: Anonymous 2010-01-16 0:36

Just reposting your code more cleanly for comparison purposes.
(defun count-letters (string &optional (letters "Sussman"))
  (loop for letter across (remove-duplicates (string-upcase letters))
     sum (truncate (count letter string :key #'char-upcase :test #'char=) 3)))
(defun print-wizardly-sussmans-for-real (string)
  (loop repeat (sussman-count string "sssuman") do (format t "~&SUSSMAN~&")))

Name: Anonymous 2010-01-16 0:39

Damn where's the COBOL guy when you need him most?...

Name: Anonymous 2010-01-16 0:42

Actually should be


(defun sussman-count (string search-string
                        &aux (string (string-upcase string))
                           (search-string (string-upcase search-string)))
  (let ((s-len (length search-string))
        (b-len (length string)))
    (do ((index (search search-string string)
                (search search-string string :start2 index))
         (i 0 (1+ i)))
        ((not index) i)
      (when (<= (+ index s-len) b-len)
        (incf index s-len)))))
(defun print-wizardly-sussmans-for-real (string)
  (loop repeat (sussman-count string "sssuman") do (format t "~&SUSSMAN~&")))


but I believe your implementation is more complicated than needed, >>11-kun. "smanuss" should print a Sussman, whether the letters are in order or not. It does depend on how we interpret >>1.

Name: Anonymous 2010-01-16 0:45

>>8

I wasn't trying to write anything impressive. My goal here is to:
A) Illustrate a problem to be solved
and
B) Provide a (somewhat, in this case) working solution to the problem to prove that it can be solved

The quicker I can write a solution to the problem, the more time people have to write their own solutions, which is all that I really care about - seeing /prog/'s code. My implementation may have been ugly, but I was able to pump it out in 20 minutes, so I'm happy.

Name: Anonymous 2010-01-16 0:55

>>14
I didn't understand the exact meaning in >>1, so I wrote 3 solutions for possible meanings I thought were possible. Maybe one has the right meaning, maybe none. I'll read OP's statements more carefully tommorow and see if they match my understanding or not.
sussman-count is indeed more complicated than it should be, it's pretty low level. It can be written more elegantly for sure.

Name: Anonymous 2010-01-16 1:21

>>15
I wasn't trying to write anything impressive.
You certainly accomplished that.
Protip: C doesn't have variable length arrays or array bounds checking. Your scanf loop is writing all over random memory, and it's a miracle it doesn't crash right there (though it will if you input longer strings).

Name: Anonymous 2010-01-16 2:01

>>17

I'm a dipshit, you fixed it. I never realized that C didn't have variable length arrays. In fact, I've even thought on several occasions that "fixed length arrays would be much easier to work with, why would a supposedly low-level language allow this, etc," never managing to make the connection that the language was just letting me get away with it the whole time. Thank you for explaining what my mistake was, I would have kept doing it if you hadn't explained that.

I'm not an experienced C programmer, if you couldn't tell; I'm just picking this stuff up as I go along.

Name: Anonymous 2010-01-16 2:40

>>17
What is your definition of a variable length array? You can just malloc your array as you please. The array size is static, but you can extend(re-alloc) the array if needed. Some C functions are unsafe as they can write to a buffer without having a way to control how much they would write, or if they should re-allocate a new buffer. IMO, it's much simpler and safer if the callee allocates the buffer for you and maybe provides some length metadata on the side. It's possible to design safe APIs for C and use C safely, but the coder needs to make a conscious effort to do so. (Or the coder could just use something more high-level than C...)

Name: Anonymous 2010-01-16 3:38

>>17
Protip: C doesn't have variable length arrays
Yes, it does.

Name: clever guy 2010-01-16 3:58


s,u,m,a,n = 0,0,0,0,0
local line = io.read()
for i = 1, #line do
  local letter = line:sub(i, i)
  if _G[letter] then _G[letter] = _G[letter] + 1 end
end
for i = 1, math.min(s / 3, u, m, a, n) do print"Sussman" end

Obviously, using lua made it too easy. I might do one in C or something later.

Name: Anonymous 2010-01-16 4:07

>>18
YHBT. Good luck with C, it's a painful language to program in, especially at first, but necessary. Avoid scanf, it's unsafe and not needed here. Just use getchar and loop while( c != '\n' ).
>>16
Oh I know, I didn't correct anything in your code, I was just reposting it cleanly because the noise impeded my reading. It's a different interpretation of OP from >>9 but not wrong per se.
>>20
realloc my anus

Name: Anonymous 2010-01-16 4:48

>>20,22
C99 has them.  Unless they are a GCC extension.  I forget.

Name: Anonymous 2010-01-16 4:56

>>19,20
Maybe you should read the thread.

>>23
Both. C99 has them, and GNU C has an incompatible implementation.
Everyone knows C99 isn't C, though.

Name: Anonymous 2010-01-16 5:08

AMATEUR LEVEL SOLUTIONS

<?php 


function subchr_count($needle, $haystack) {
    $haycount = count_chars($haystack,1);
    $needlecount = count_chars($needle,1);
    $retval = 0;
   
    do {
        foreach ($needlecount as $chr => $num) {
            if (!isset($haycount[$chr]) ||
                ($haycount[$chr] -= $num) < 0) {
                break 2;
            }
           
        }
    } while (++$retval);
   
    return $retval;
}


function main()
{
    while(true) {
        $continue = 'y';
        fwrite(STDOUT, "\nEnter string of characters to search in: \n"); 
        do { 
            $haystack = fgets(STDIN); 
        } while (trim($haystack) == ''); 
       
        fwrite(STDOUT, "\nEnter string of characters to search for: \n"); 
        do { 
            $needle = rtrim(fgets(STDIN), "\r\n");
        } while ($needle == '');
       
        $t = microtime(true);
        $result = subchr_count($needle, $haystack);
        $t = microtime(true) - $t;
        $result ?
            $findings = "\nCongratulations, \"$needle\" was found $result times!" :
            $findings = "\nI'm sorry, \"$needle\" was not found.";
        fwrite(STDOUT, $findings);
        fwrite(STDOUT, "\nSearch query took " . number_format($t, 5) . " seconds.\n");
       
       
        fwrite(STDOUT, "\nTry again? y/n \n"); 
        while(true) {
            $continue = fgetc(STDIN);
            switch($continue) {
                case 'y':
                    break 2;
                case 'n':
                    break 3;
                default:
                    break;
            }
        }
    }
   
    exit(0);
}

main();

/* EOF */



Enter string of characters to search in:
ssssssssssssssssuuuuuuuuuuuuuuuussssssssssssssssmmmmmmmmmmmmmmmmmmmaaaaaaaaaaaaaaaannnnnnnnnnnnnnn

Enter string of characters to search for:
sussman

Congratulations, "sussman" was found 10 times!
Search query took 0.00026 seconds.

Try again? y/n
n

Name: Anonymous 2010-01-16 5:10

$continue = 'y';

DISREGARD

Name: Anonymous 2010-01-16 5:37

eval"\$$_++"for+split//,lc<>;print"SUSSMAN\n"x(sort{$a<=>$b}($s/3,$u,$m,$a,$n))[0]

Obviously, using Perl made it too easy. I might do one in C or something later.

Name: Anonymous 2010-01-16 5:40

>>27
Actually, that block passed to sort is quite useless. So this works too, and is a bit shorter too.

eval"\$$_++"for+split//,lc<>;print"SUSSMAN\n"x(sort($s/3,$u,$m,$a,$n))[0]

Name: Anonymous 2010-01-16 7:11

>>28
VALID LINE NOISE

Name: Anonymous 2010-01-16 9:44


#!/usr/bin/env ruby
h={}
STDIN.read().split(//).reject{|e|!("SUMAN".include?e)}.each{|e|h[e]?h[e]+=1.0:h[e]=1.0}
h["S"]&&h["S"]/=3
loop { h.each_pair{|k,v|v<1.0&&exit;h[k]-=1.0}; puts "SUSSMAN" }

Name: Anonymous 2010-01-16 10:51

(define count-suss
  (λ(string)
    (let ((string (string-downcase string))
          (count-s (λ(char string)
                     (let ((empty-s? (λ(s) (string=? s ""))))
                       (let loop ((string string)
                                  (count 0))
                         (if (empty-s? string)
                             count
                             (loop (substring string 1)
                                   (+ count (if (char=? char
                                                        (string-ref string 0))
                                                1
                                                0)))))))))
      (let ((s-count (count-s #\s string))
            (u-count (count-s #\u string))
            (m-count (count-s #\m string))
            (a-count (count-s #\a string))
            (n-count (count-s #\n string)))
        (min (quotient s-count 3)
             u-count
             m-count
             a-count
             n-count)))))

(define homework
  (λ(string)
    (let ((output (λ() (display 'sussman)(newline)))) ;'
      (let loop ((susscount (count-suss string)))
        (if (one? susscount)
            (output)
            (begin (output)
                   (loop (- susscount 1))))))))

Name: Anonymous 2010-01-16 10:55

>>31
Why don't you just convert the string to a list of chars with string->list instead of taking substrings of length 1 and using string-ref on that?

Name: Anonymous 2010-01-16 10:58

Oh man infinite loop...
(define homework
  (λ(string)
    (let ((output (λ() (display 'sussman)(newline))))
      (let loop ((susscount (count-suss string)))
        (cond ((< 0 susscount) (begin (output)
                                       (loop (- susscount 1)))))))))

Name: Anonymous 2010-01-16 11:01

>>32
No reason, really.

Name: Anonymous 2010-01-16 12:40

import Control.Monad.Instances
import Data.Function
import List

findSuss s =
   mapM_ (const $ putStrLn "sussman") $ takeWhile (== length "sussman")
                                      $ zipWith (subtract `on` length) =<< tail
                                      $ iterate (\\ "sussman") s

Name: Anonymous 2010-01-16 13:45

ENTERPRISE C!!!

#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
  char s[9],*p = argv[1];
  memset(s,0,9);
  if(p) for(;*p;p++) {
    s[*p=='S'?(s[0]?(s[2]?3:2):0):(*p=='U'?1:(*p=='M'?4:(*p=='A'?5:(*p=='N'?6:8))))]=*p;
    if(strlen(s)==7) { printf("%s\n",s); memset(s,0,9); }
  }
  return 0;
}

Name: Anonymous 2010-01-16 13:47

>>22

Funny, I was using getchar at first, not sure why I switched to scanf.

Anyways, here's a less disgraceful C version.

#include <stdio.h>

int main()
{
    char suss[300];
    char letters[] = "SUMANsuman";
    char count[5] = {0}; /* S, U, M, A, N */
   
    int x, c;
   
    printf("Enter a string: ");
    for (x = 0; (suss[x] = getchar()) != '\n'; x++);
   
    for (x = 0; suss[x] != '\n'; x++)
    {
        for (c = 0; c <= 9; c++)
        {
            if (suss[x] == letters[c])
            {
                if (c < 5)
                    count[c]++;
                else
                    count[c-5]++;
            }
        }
    }
   
    while (count[0] > 0)
    {
        if (count[0] >= 3 && count[1] > 0 && count[2] > 0 && count[3] > 0 && count[4] > 0)
            printf("SUSSMAN\n");
       
        count[0] -= 3;
        for (x = 1; x < 5; x++)
            count[x]--;
    }
   
    return 0;
}

Name: Anonymous 2010-01-16 14:01

>>9,10
What the fuck is that Array abomination?

My haskal version:
import Control.Arrow ((&&&))
import Data.List (group, sort, lookup)
import Data.Maybe (fromMaybe)
import Data.Char (toLower)

count = map (head &&& length) . group . sort . map toLower

find = (fromMaybe 0 .) . flip lookup

a /// b = minimum $ map (uncurry $ (quot . find a)) b

rearrange src pat = unwords . take n $ repeat pat
  where n = count src /// count pat

main = putStrLn . flip rearrange "SUSSMAN" =<< getLine


Gotta write a C one next; does C do TCO?

Name: Anonymous 2010-01-16 14:25

My C version:
#include <stdio.h>

int main(int argc, char **argv)
{
    int s, u, m, a, n, pos;
    char c;
    char cs[] = "SUSSMAN ";
    int* vs[] = {&s, &u, &s, &s, &m, &a, &n, &pos, 0};
    while ((c = getchar()) != '\n')
    {
        switch (c)
        {
            case 's': case 'S': ++s; break;
            case 'u': case 'U': ++u; break;
            case 'm': case 'M': ++m; break;
            case 'a': case 'A': ++a; break;
            case 'n': case 'N': ++n; break;
        }
        while (*vs[pos])
        {
            *vs[pos]--;
            printf("%c", cs[pos++]);
            pos &= 7;
        }
    }
    printf("\n");
    return 0;
}

Name: Anonymous 2010-01-16 14:44

>>39
oh fuck I'm stupid.
Add int _ = 1; somewhere and s/&pos/&_/.

Name: Anonymous 2010-01-16 14:47

>>40
s/1/INT_MAX/

Name: Lisp 2010-01-16 14:52

<?php
$chars = str_split( strtolower($argv[1]) );
foreach($chars as $char)
{
    $char_counts[$char]++;
}

while
(
        $char_counts['s'] >= 3 &&
        $char_counts['u'] &&
        $char_counts['m'] &&
        $char_counts['a'] &&
        $char_counts['n']
)
{
    echo 'SUSSMAN' . "\n";
    $char_counts['s'] = $char_counts['s'] - 3;
    $char_counts['u']--;
    $char_counts['m']--;
    $char_counts['a']--;
    $char_counts['n']--;
}
?>

Name: Anonymous 2010-01-16 14:56

I wonder what Leah Culver's solution would be like.

Name: Anonymous 2010-01-16 14:57


while (<STDIN> =~ m/sussman/ig)
{
  print "sussman\n";
}


i dont realy get what this is about but here it is.

Name: Anonymous 2010-01-16 15:16

>>42
$char_counts['s'] = $char_counts['s'] - 3;
wtf

Name: Anonymous 2010-01-16 15:54

>>45
That's right, she should have used ((char_counts['s']--)--)--.

Name: Anonymous 2010-01-16 16:09

Now making use of PHP FEATURES like variable variables and error suppression !

$s = $u = $m = $a = $n = 0;
foreach (str_split(strtolower($argv[1])) as $c) $$c++;
while (@$_++ < min($s, $u, $m, $a, $n)) print "sussman\n";

Name: Anonymous 2010-01-16 16:23

>>47
Interesting, but suman would incorrectly print one sussman\n.

Name: Anonymous 2010-01-16 16:28

>>47
variable variables
genius!!

Name: Anonymous 2010-01-16 16:33

>>48
Oops !
$s = $u = $m = $a = $n = 0;
foreach (str_split(strtolower($argv[1])) as $c) $$c++;
$s = (int) $s / 3;
while (@$_++ < min($s, $u, $m, $a, $n)) print "sussman\n";

Name: Anonymous 2010-01-16 16:34

>>50
s/$s = (int) $s / 3;/$s = floor($s / 3);/ rather

Name: Anonymous 2010-01-16 16:57

>>38
Array version might be the fastest here. Time to profile!
   ____ ∧ ∧   / ̄ ̄ ̄ ̄ ̄ ̄
 ~' ____(,,゚Д゚)< 
OMG OPTIMIZED
   UU    U U    \______

      ∧ ∧  / ̄ ̄ ̄ ̄
    (,,゚Д゚)< 
VROOM VROOM
     ⊂  ⊃ \____
    ~|  |
      し`J

Name: Anonymous 2010-01-16 17:09

>>52
Premature optimizations are the root of all evil.
    -Some fag nobody cares about

Name: Anonymous 2010-01-16 17:14

>>53
Premature optimizations are the root of all evil.
the guy specifically mentions that he is going to profile
IHBT

Name: Anonymous 2010-01-16 17:19

import Control.Monad
import List

findSuss text =
   when (intersect "sussman" text == "sussman")
        (putStrLn "sussman" >> findSuss (text \\ "sussman"))

Name: >>55 2010-01-16 17:34

I fucked up on the last one.

import Control.Monad
import List

findSuss text =
   when (length text - length next == length "sussman") (putStrLn "sussman" >> findSuss next)
   where next = text \\ "sussman"

Name: Anonymous 2010-01-16 17:46

>>56
So when's a while?

Name: EXPERT PROFILER 2010-01-16 17:47

HASKAL RESULTS ARE OUT
My array-based code (>>10) wins for now. Extra points to >>38 who gets very close using elegant arrows.

Everything run with a 91KB text file containing "sussman sussman"... repeated until the file was big enough.
Compiled with ghc --make -02 -auto-all -fforce-recomp -prof findsuss.hs and ran with findsuss findt +RTS -p -sstderr -hd > findtout (on Windows, so the > pipes the output to a file in order to save my emacs console from death). I have the memory allocation profiles (in PDF) for all posts, will post if there's interest.


Run time in seconds (Core 2 Duo laptop)
>>10   >>38   >>35    >>56   >>55  
0.30   0.50   32.07   32.68  44.93


>>35
  22,532,708,420 bytes allocated in the heap
   2,509,842,544 bytes copied during GC
       1,849,172 bytes maximum residency (1725 sample(s))
          86,004 bytes maximum slop
               6 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0: 41396 collections,     0 parallel,  2.61s,  2.65s elapsed
  Generation 1:  1725 collections,     0 parallel,  2.31s,  2.08s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time   26.79s  ( 27.46s elapsed)
  GC    time    4.91s  (  4.73s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.37s  (  0.36s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time   32.07s  ( 32.55s elapsed)

  %GC time      15.3%  (14.5% elapsed)

  Alloc rate    841,231,738 bytes per MUT second

  Productivity  83.5% of total user, 82.3% of total elapsed

>>55
  22,534,919,924 bytes allocated in the heap
   1,712,117,048 bytes copied during GC
         243,460 bytes maximum residency (1639 sample(s))
          39,136 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0: 41552 collections,     0 parallel,  2.62s,  2.97s elapsed
  Generation 1:  1639 collections,     0 parallel,  0.36s,  0.49s elapsed

  INIT  time    0.02s  (  0.00s elapsed)
  MUT   time   40.92s  ( 41.43s elapsed)
  GC    time    2.98s  (  3.46s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.02s  (  0.04s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time   43.93s  ( 44.93s elapsed)

  %GC time       6.8%  (7.7% elapsed)

  Alloc rate    550,509,504 bytes per MUT second

  Productivity  93.1% of total user, 91.1% of total elapsed

>>38
     109,663,904 bytes allocated in the heap
      77,659,004 bytes copied during GC
       4,606,460 bytes maximum residency (19 sample(s))
          29,116 bytes maximum slop
              13 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:   191 collections,     0 parallel,  0.19s,  0.15s elapsed
  Generation 1:    19 collections,     0 parallel,  0.14s,  0.11s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time    0.16s  (  0.18s elapsed)
  GC    time    0.33s  (  0.26s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.02s  (  0.00s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    0.50s  (  0.44s elapsed)

  %GC time      65.6%  (58.1% elapsed)

  Alloc rate    702,973,743 bytes per MUT second

  Productivity  31.2% of total user, 35.3% of total elapsed

>>10
      31,085,184 bytes allocated in the heap
       7,515,104 bytes copied during GC
       4,689,640 bytes maximum residency (3 sample(s))
         842,360 bytes maximum slop
              12 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:    43 collections,     0 parallel,  0.23s,  0.23s elapsed
  Generation 1:     3 collections,     0 parallel,  0.02s,  0.03s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time    0.05s  (  0.07s elapsed)
  GC    time    0.25s  (  0.26s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.00s  (  0.00s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    0.30s  (  0.34s elapsed)

  %GC time      84.2%  (77.8% elapsed)

  Alloc rate    664,199,141 bytes per MUT second

  Productivity  15.8% of total user, 13.8% of total elapsed

>>56
  22,530,904,044 bytes allocated in the heap
   2,630,688,952 bytes copied during GC
       1,851,628 bytes maximum residency (1709 sample(s))
          87,692 bytes maximum slop
               6 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0: 41401 collections,     0 parallel,  3.03s,  2.91s elapsed
  Generation 1:  1709 collections,     0 parallel,  1.68s,  1.93s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time   27.66s  ( 27.57s elapsed)
  GC    time    4.71s  (  4.84s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.31s  (  0.38s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time   32.68s  ( 32.79s elapsed)

  %GC time      14.4%  (14.8% elapsed)

  Alloc rate    814,596,463 bytes per MUT second

  Productivity  84.6% of total user, 84.3% of total elapsed

Name: Anonymous 2010-01-16 17:50

>>58
Oh, and
> ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.10.4


Of course we could try bytestrings or the new GHC version if we wanted true VROOM VROOM performance.

Name: Anonymous 2010-01-16 17:54

import Control.Monad
import List

findSuss text =
   when (null ("sussman" \\ text))
        (putStrLn "sussman" >> findSuss (text \\ "sussman"))

Name: Anonymous 2010-01-16 17:58

>>57
Nope, there's a recursion in that when's second argument.

Name: Anonymous 2010-01-16 18:00

By the way, I didn't check for correctness, but some outputs were of a different size than expected. Watch for case sensitivity, whether you put spaces in the output, etc.

>>60
Are you a ruby programmer?
  57,146,166,712 bytes allocated in the heap
  19,402,948,040 bytes copied during GC
       1,858,492 bytes maximum residency (7076 sample(s))
          82,776 bytes maximum slop
               6 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0: 102215 collections,     0 parallel, 27.52s, 28.74s elapsed
  Generation 1:  7076 collections,     0 parallel, 12.03s, 11.98s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time   62.17s  ( 63.40s elapsed)
  GC    time   39.55s  ( 40.72s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.92s  (  0.95s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time  102.63s  (105.07s elapsed)

  %GC time      38.5%  (38.8% elapsed)

  Alloc rate    919,245,230 bytes per MUT second

  Productivity  60.6% of total user, 59.2% of total elapsed

My test suite is dirty but here it is:
import Control.Arrow ((&&&))
import Data.List (group, sort, lookup)
import Data.Maybe (fromMaybe)
import Data.Char (toLower,toUpper)
import Array

count = map (head &&& length) . group . sort . map toLower

find38 = (fromMaybe 0 .) . flip lookup

a /// b = minimum $ map (uncurry $ (quot . find38 a)) b

rearrange src pat = unwords . take n $ repeat pat
  where n = count src /// count pat

findSuss38 = putStrLn . flip rearrange "SUSSMAN"

findSuss10 xs = putStrLn . unwords $ take sussmen (repeat "SUSSMAN")
    where counts = assocs . accumArray (+) 0 (minBound,maxBound) . (map (\x -> (toUpper x,1))) $ xs
          sussmen = minimum $ [floor . (/3) . fromIntegral $ count 'S'] ++ map count "UMAN"
          count c = fromMaybe 0 $ lookup c counts

findSuss35 s =
    mapM_ (const $ putStrLn "sussman") $ takeWhile (== length "sussman")
              $ zipWith (subtract `on` length) =<< tail
              $ iterate (\\ "sussman") s
findSuss55 text =
   when (intersect "sussman" text == "sussman")
        (putStrLn "sussman" >> findSuss55 (text \\ "sussman"))

findSuss56 text =
   when (length text - length next == length "sussman") (putStrLn "sussman" >> findSuss56 next)
   where next = text \\ "sussman"

findSuss60 text =
   when (null ("sussman" \\ text))
        (putStrLn "sussman" >> findSuss60 (text \\ "sussman"))

main = testsuss
testsuss = do [f] <- getArgs
              putStrLn $ "Reading sussfile: " ++  f
              text <- readFile f
              findSuss60 text

gen = do args <- getArgs
         writeFile (head args) (writeSuss 100000000)

writeSuss = unwords . flip take (repeat "sussman")

Name: Anonymous 2010-01-16 18:01

Missed some imports
import Control.Monad
import Control.Monad.Instances
import Data.Function
import List
import System.Environment

Name: Anonymous 2010-01-16 18:52

>>62
Are you a ruby programmer?
No, i'm just Lazy.

Name: Anonymous 2010-01-16 19:25

>>62
Here, speedy, try this one:

import Control.Monad
import Data.Function
import Data.Map

findSuss text =
   flip replicateM_ (putStrLn "sussman") $ minimum $ elems $
      (intersectionWith div `on` (fromListWith (+) . flip zip (repeat 1))) text "sussman"

Name: Anonymous 2010-01-16 19:47

inelegant as fuck

sussmen = sussmen' (0,0,0,0,0)
  where sussmen' (s,u,m,a,n) str
          | and [s>2,u>0,m>0,a>0,n>0] = 1 + sussmen' (s-3,u-1,m-1,a-1,n-1) str
          | null str = 0
        sussmen' z@(s,u,m,a,n) (c:cs) = case toLower c of
          's' -> sussmen' (s+1,u,m,a,n) cs
          'u' -> sussmen' (s,u+1,m,a,n) cs
          'm' -> sussmen' (s,u,m+1,a,n) cs
          'a' -> sussmen' (s,u,m,a+1,n) cs
          'n' -> sussmen' (s,u,m,a,n+1) cs
          _   -> sussmen' z cs

main = putStrLn . unwords . flip replicate "SUSSMAN" . sussmen =<< getLine

Name: Anonymous 2010-01-16 19:56

>>65
Much better.
      35,261,328 bytes allocated in the heap
      10,478,824 bytes copied during GC
       2,554,132 bytes maximum residency (4 sample(s))
         421,348 bytes maximum slop
               7 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:    60 collections,     0 parallel,  0.02s,  0.02s elapsed
  Generation 1:     4 collections,     0 parallel,  0.02s,  0.01s elapsed

  INIT  time    0.02s  (  0.00s elapsed)
  MUT   time    0.08s  (  0.07s elapsed)
  GC    time    0.03s  (  0.04s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.00s  (  0.00s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    0.12s  (  0.11s elapsed)

  %GC time      25.0%  (33.3% elapsed)

  Alloc rate    376,723,589 bytes per MUT second

  Productivity  62.5% of total user, 74.3% of total elapsed

Name: Anonymous 2010-01-16 19:58

>>66
Indeed, that was my first implementation idea too but it was so ugly I went back to an array. Very good performance though.
      15,028,916 bytes allocated in the heap
         364,968 bytes copied during GC
         527,856 bytes maximum residency (2 sample(s))
          16,680 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:    25 collections,     0 parallel,  0.00s,  0.00s elapsed
  Generation 1:     2 collections,     0 parallel,  0.00s,  0.00s elapsed

  INIT  time    0.00s  (  0.00s elapsed)
  MUT   time    0.09s  (  0.08s elapsed)
  GC    time    0.00s  (  0.00s elapsed)
  RP    time    0.00s  (  0.00s elapsed)
  PROF  time    0.00s  (  0.00s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    0.09s  (  0.08s elapsed)

  %GC time       0.0%  (4.8% elapsed)

  Alloc rate    160,565,341 bytes per MUT second

  Productivity 100.0% of total user, 111.4% of total elapsed

Name: Anonymous 2010-01-16 20:04

Jesus, who invited all the Haskell weenies?

Name: Anonymous 2010-01-16 20:07

>>68
And here is an improvement on it, makes it stricter and removes the explicit recursion, and uses the simpler way to count the sussmen. Run time about 0.08s. Using foldl makes it take 0.11s.
findSuss66b = count . foldl' sussmen (0,0,0,0,0)
    where sussmen z@(s,u,m,a,n) c = case toLower c of
                                      's' -> (s+1,u,m,a,n)
                                      'u' -> (s,u+1,m,a,n)
                                      'm' -> (s,u,m+1,a,n)
                                      'a' -> (s,u,m,a+1,n)
                                      'n' -> (s,u,m,a,n+1)
                                      _   -> z
          count (s,u,m,a,n) = minimum [floor $ fromIntegral s / 3,u,m,a,n]

findSuss66 = putStrLn . unwords . flip replicate "SUSSMAN" . findSuss66b

Name: Anonymous 2010-01-16 20:12

>>68
Sure it was, buddy.

Name: Anonymous 2010-01-16 20:14

>>69
What's the matter, jealous?

Name: PHP > Haskell 2010-01-16 20:15

If Haskell was readable ...

>>66
>>42

>>70
>>50

Name: Anonymous 2010-01-16 20:54

>>73
Read >>38 and tell me the code isn't ELEGANT AS FUCK.

Name: Anonymous 2010-01-16 21:02

>>74
Haskell is just like English except it isn't readable.

Name: Anonymous 2010-01-16 21:13

>>74
I prefer >>65, personally. Arrows are obscure as fuck.

Name: Anonymous 2010-01-16 21:17

>>76
The only part in which I am using Control.Arrow is &&&, and how is that obscure?

Name: xiagox 2010-01-16 21:51

/* sussmanChallenge.c
 * write a program that takes a string and outputs "SUSSMAN" for every time
 * those character appear. Example, "ssssumanssssssssssuman" would print
 * "SUSSMAN" twice
 */

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

main(){
int i, s=0, u=0, m=0, a=0, n=0;

printf("Enter string to be processed.\n");

while(i = getchar()){
if(i == '\n')
    break;
else{
    if(i == 115) // 's' = 115
        s++;
    else if(i == 117) // 'u' = 117
        u++;
    else if(i == 109) // 'm' = 109
        m++;
    else if(i == 97) // 'a' = 97
        a++;
    else if(i == 110) // 'n' = 110
        n++;
    }
}

/* printing code */
while(1 == 1){
if(s>2 && u>0 && m>0 && a>0 && n>0){
    printf("SUSSMAN\n");
    s-3; u--; m--; a--; n--;
}
if(s<3 || u<1 || m<1 || a<1 || n<1)
    break;
}
return 0;
}

only handles lower case versions of 's', 'u', 'm', 'a' and 'n'.
For uppercase put more cases :P

Name: Anonymous 2010-01-16 22:07

>>78
That while() loop is bad and you should feel bad.

Name: Anonymous 2010-01-16 22:09

>>78
s-3

wut

Name: Anonymous 2010-01-16 22:35

>>80
Not to mention

main()
115 instead of 's'
1 == 1

Name: xiagox 2010-01-16 23:08

/* sussmanChallengeFixedLOL.c
 * write a program that takes a string and outputs "SUSSMAN" for every time
 * those character appear. Example, "ssssumanssssssssssuman" would print
 * "SUSSMAN" twice
 */

#include <stdio.h>

main(){
int i, s=0, u=0, m=0, a=0, n=0;

printf("Enter string to be processed.\n");

while(i = getchar()){
if(i == '\n')
    break;
else{
    if(i == 's' || i == 'S')
        s++;
    else if(i == 'u' || i == 'U')
        u++;
    else if(i == 'm' || i == 'M')
        m++;
    else if(i == 'a' || i == 'A')
        a++;
    else if(i == 'n' || i == 'N')
        n++;
    }
}

/* printing code */
while(1){
if(s>2 && u>0 && m>0 && a>0 && n>0){
    printf("SUSSMAN\n");
    s-=3; u--; m--; a--; n--;
}
if(s<3 || u<1 || m<1 || a<1 || n<1)
    break;
}
}

Name: Anonymous 2010-01-16 23:09

>>78
1) You can do c == 'c' instead of c == 115...
2) You can be case insensitive with a simple bit manipulation (if it's ASCII) or by using tolower
3) while(1 == 1) do something normal like while(1) or for(;;) instead
4) You can know the number of sussmen to print like the haskellers by getting the minimum of (s/3,u,m,a,n) instead of the ridiculous if statement in a loop
5) Your indentation is shit, get the IDE to redo it
6) If you don't benchmark it we can't tell the length of your penis. 100KB of Sussmen, FINAL DESTINATION.

Name: PHP FAGGOT 2010-01-17 3:25

Spurred on by >>58, I timed my own code.
Surprisingly, PHP isn't all that ENTERPRISE:

[a]Version Number:   Windows NT 6.0 (Build 6002)
Exit Time:        0:20 am, Monday, January 18 2010
Elapsed Time:     0:00:11.429
Process Time:     0:00:03.588
System Calls:     1316551
Context Switches: 804022
Page Faults:      101397
Bytes Read:       1215555
Bytes Written:    384265
Bytes Other:      568334[/a]

Name: Anonymous 2010-01-17 5:17

Grabbed Alice in Wonderland from the tubes, stripped pointless header and footer info...
(time (count-suss aiw))
cpu time: 130688 real time: 131371 gc time: 35048
2107

Speedy!

Name: Anonymous 2010-01-17 6:14

>>81
what's wrong with main() ?

Name: Anonymous 2010-01-17 6:40

>>86
Dear fucking god, how are people still learning to write such terrible non-conformant C in 2010?

The standard says that main() MUST return int.

There are 2 acceptable prototypes for main():

int main(void)
int main(int argc, char **argv)[/m]

You may use char *argv[] instead of char **argv of course, but it's the same thing.

>>78 was obviously written by someone with severe brain-damage.

Name: Anonymous 2010-01-17 6:47

>>87
dont forget **env, faggot

Name: Anonymous 2010-01-17 6:49

>>87
main() is C99 compliant. Return type is omitted and assumed to be int and args are just omitted; there's nothing wrong with that.

Name: Anonymous 2010-01-17 8:40

Name: Anonymous 2010-01-17 9:05

>>90
int main(argc, argv)
int argc; char **argv;


I barfed.

Name: Anonymous 2010-01-17 9:11

>>91
You'd better stay out of the keyboard if you can't handle the heat.

Name: Anonymous 2010-01-17 9:38


#include <stdio.h>

#define S count[0]
#define U count[1]
#define M count[2]
#define A count[3]
#define N count[4]

int main (void)
{
  int count[5] = {0};
  int  nofs = 0;
  char *i = NULL;
  char *str = "sussssssssman";
  int min = 0;

  for (i = str; *i; i++)
    {
      switch (*i)
    {
    case 'u': U++; break;
    case 'm': M++; break;
    case 'a': A++; break;
    case 'n': N++; break;
    case 's':
      {
        nofs++;
        if (nofs == 3)
          {
        S++;
        nofs = 0;
          }
      } break;
    }
    }

  min = S;
  for (nofs = 0; nofs < 5; nofs++)
    if (count[nofs] < min)
      min = count[nofs];

  printf ("\"sussman\" appears %d times.\n", min);

  return 0;
}

Name: Anonymous 2010-01-17 11:22

>>87
What would my microcontroller return the value of main to?

Name: Anonymous 2010-01-17 11:42

>>76
Arrows are our friends.

import Control.Arrow
import Control.Monad
import Data.Function
import Data.Char
import Data.Map hiding (map)

findSuss text =
   (`replicateM_` putStrLn "sussman") $ minimum $ elems $
      (intersectionWith div `on` (fromListWith (+) . map (toLower &&& const 1))) text "sussman"

Name: Anonymous 2010-01-17 19:51

>>89
Return type is omitted and assumed to be int and args are just omitted; there's nothing wrong with that.
Both false.

Assumed int is illegal everywhere in C99. It was deprecated in C89.

A function declaration without an argument list is deprecated in C99. Your compiler will warn you if you use proper cflags. You should write main(void). This is because declaring a function without an argument list, and without immediately defining the function, means it is variadic.

Name: Anonymous 2010-01-17 20:59

-std=c99 -pedantic -Wall -Werror is the only way to go

Name: Anonymous 2010-01-18 4:51

>>96
Doesn't mean it's variadic, just that you don't know its arguments.

Name: Anonymous 2010-01-18 5:43

>>98
MY ANUS IS VARIADIC. It can accept one or more cocks

Name: Anonymous 2010-01-18 9:16

>>99
I'd force my strongly-typed compiler into a cute girl's anus if you catch my drift.

Name: Anonymous 2010-01-18 10:54

>>100
I'd catch a drift from a cute girl's anus if you know what I mean.

Name: Anonymous 2010-01-18 11:17

>>99
Actually since there's only one type involved it can be monomorphic:
myanus :: [Cock] -> (Pleasure, Maybe Semen)

Name: Anonymous 2010-01-18 11:56

>>101
FART FETISH??

Name: Anonymous 2010-01-19 21:15

bampu pantsu

Name: Anonymous 2010-12-09 15:28

Name: Anonymous 2010-12-17 1:37

Erika once told me that Xarn is a bad boyfriend

Name: Anonymous 2010-12-21 3:13

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