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

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-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")

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