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

Pages: 1-

Rhyme_Game

Name: Anonymous 2012-10-26 21:06

Haskell

A group of friends are standing in a circle, playing a game. They proceed around the circle, chanting a rhyme and pointing at the next person (clockwise) on each word. When the rhyme finishes whoever speaks the last word leaves the circle. They then start again, pointing where they left off, and the game continues until only one person is left.


For example, suppose there are six friends (numbered 1 to 6) standing around the circle and the rhyme is "Eenee, Meenee, Mainee, Mo!". The first person to leave the circle is number 4, followed by 2 then 1, 3 and 6. Number 5 is left.


Sample run
Number of friends: 7
Words in rhyme: 3
Number 4 is left


Part 1

Write a program that inputs two numbers (each between 1 and 1000 inclusive), the first being n, the number of friends, and the second the number of words in the rhyme. The output should be the number of the last person left. You should assume that the friends are numbered (clockwise) from 1 to n, and that the count begins at person 1.

Part 2

12 friends stand in a circle and use a rhyme with 5 words. In what order do they leave the circle? In other words display the number of the person as they leave.

Name: Anonymous 2012-10-26 21:50

Rhymes are for fucking assfaggots

Name: Anonymous 2012-10-27 12:46

>>2
assmaggots

Name: Anonymous 2012-10-28 10:36

Part 1


rhyme :: Integer -> Integer -> [Integer]
rhyme rhymeLength numPeople = loop rhymeLength [] (cycle [1..numPeople])
  where
  loop n removed remaining
    | next == head rest   = next:removed
    | otherwise           = loop n (next:removed) (filter (/=next) rest)
    where
    next:rest = drop (fromIntegral (n-1)) remaining

main = do
  putStr "Number of friends: "
  numPeople <- fmap read getLine
  putStr "Words in rhyme: "
  rhymeLength <- fmap read getLine
  let lastPerson = head $ rhyme rhymeLength numPeople             
  putStrLn ("Number " ++ show lastPerson ++ " is left")


Part 2

rhyme as given above computes the removal order backwards, so:

mapM_ print $ reverse $ rhyme 5 12

Criticisms please; I know this thing is woefully inefficient, for example a rhyme the length of a Shakespeare play on a circle the size of a small town didn't compute in any reasonable amount of time.

Name: Anonymous 2012-10-28 18:05

>>4
I appreciate this, thanks!

Name: Anonymous 2012-10-28 18:49

>>4
YOU HELPED HIM allowed ey to avoid the learning exercise and postpone ey's mastery of haskell

Name: Anonymous 2012-10-28 19:15

f(x, 1, w) = x
f(x, p, w) = f([w%p+1], p-1, w)

Or if you prefer:

f(w,p) = [(p-1)+sum(x in [0, (p-1]) (w%(p-x))]%p

First position defined as 0.

Name: Anonymous 2012-10-30 16:11

so :: Int -> [a] -> ([a], a)
so k xs = (\((x, [l]), y) -> (y++x, l)) . (\(x,y) -> (splitAt p x, y)) . splitAt (p+1) $ xs
    where p = mod (k-1) (length xs)

fw :: Int -> Int -> Int
fw p r = head . last . takeWhile (not . null) $ iterate (fst . so r) [1..p]

lo :: [Int] -> Int -> [Int]
lo [x] _ = []
lo xs k = snd x : lo (fst x) k
    where x = so k xs


fw 20123 651
14010
(8.67 secs, 6956458900 bytes)

fw 2023 10
1527
(0.04 secs, 60975116 bytes)

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