Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:35:41+00:00 2026-06-01T17:35:41+00:00

I’m stuck with my homework task, somebody help, please.. Here is the task: Find

  • 0

I’m stuck with my homework task, somebody help, please..

Here is the task:
Find all possible partitions of string into words of some dictionary

And here is how I’m trying to do it:
I use dynamical programming concept to fill matrix and then I’m stuck with how to retrieve data from it

-- Task5_2
retrieve :: [[Int]] -> [String] -> Int -> Int -> Int -> [[String]]
retrieve matrix dict i j size
    | i >= size || j >= size = []
    | index /= 0 = [(dict !! index)]:(retrieve matrix dict (i + sizeOfWord) (i + sizeOfWord) size) ++ retrieve matrix dict i (next matrix i j) size
    where index = (matrix !! i !! j) - 1; sizeOfWord = length (dict !! index)


next matrix i j
    | j >= (length matrix) = j
    | matrix !! i !! j > 0 = j
    | otherwise = next matrix i (j + 1)

getPartitionMatrix :: String -> [String] -> [[Int]]
getPartitionMatrix text dict = [[ indiceOfWord (getWord text i j) dict 1  | j <- [1..(length text)]] | i <- [1..(length text)]]

--------------------------
getWord :: String -> Int -> Int -> String
getWord text from to = map fst $ filter (\a -> (snd a) >= from && (snd a) <= to) $ zip text [1..]


indiceOfWord :: String -> [String] -> Int -> Int
indiceOfWord _ [] _ = 0
indiceOfWord word (x:xs) n
    | word == x  = n
    | otherwise = indiceOfWord word xs (n + 1)


-- TESTS
dictionary = ["la", "a", "laa", "l"]
string = "laa"
matr = getPartitionMatrix string dictionary
test = retrieve matr dictionary 0 0 (length string)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:35:42+00:00Added an answer on June 1, 2026 at 5:35 pm

    Here is a code that do what you ask for. It doesn’t work exactly like your solution but should work as fast if (and only if) both our dictionary lookup were improved to use tries as would be reasonable. As it is I think it may be a bit faster than your solution :

    module Partitions (partitions) where
    import Data.Array
    import Data.List
    
    data Branches a = Empty | B [([a],Branches a)] deriving (Show)
    
    isEmpty Empty = True
    isEmpty _     = False
    
    flatten :: Branches a -> [ [ [a] ] ]
    flatten Empty  = []
    flatten (B []) = [[]]
    flatten (B ps) = concatMap (\(word, bs) -> ...) ps
    
    type Dictionary a = [[a]]
    
    partitions :: (Ord a) => Dictionary a -> [a] -> [ [ [a] ] ]
    partitions dict xs = flatten (parts ! 0)
        where 
          parts = listArray (0,length xs) $ zipWith (\i ys -> starting i ys) [0..] (tails xs)
          starting _ [] = B []
          starting i ys 
              | null words = ...
              | otherwise  = ...
              where 
                words = filter (`isPrefixOf` ys) $ dict
                go word = (word, parts ! (i + length word))
    

    It works like this : At each position of the string, it search all possible words starting from there in the dictionary and evaluates to a Branches, that is either a dead-end (Empty) or a list of pairs of a word and all possible continuations after it, discarding those words that can’t be continued.

    Dynamic programming enter the picture to record every possibilities starting from a given index in a lazy array. Note that the knot is tied : we compute parts by using starting, which uses parts to lookup which continuations are possible from a given index. This only works because we only lookup indices after the one starting is computing and starting don’t use parts for the last index.

    To retrieve the list of partitions from this Branches datatype is analogous to the listing of all path in a tree.

    EDIT : I removed some crucial parts of the solution in order to let the questioner search for himself. Though that shouldn’t be too hard to complete with some thinking. I’ll probably put them back with a somewhat cleaned up version later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a text area in my form which accepts all possible characters from
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.