We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
pents (standard linked list) failed tests 5 and 6, pentsA (array) and pentsV (vectors) passed everything.
I think since you know the maximum size of the list and you are repeatedly reading from them, then the way to go is array or vector. The whole advantage of a linked list is that you can have dynamic/aribtrary sizes (at the cost of speed of reading)
slight change to your solution fixes the problem
importControl.Monad(forM)importqualifiedData.VectorasV(fromList,(!),Vector)pentagonCounterList::V.VectorInt-- pracitice good habitspentagonCounterList=V.fromList$[pentagonCounterx|x<-[0..10^5]]-- added upper bound and coverted to vectorpentagonCounter::Int->IntpentagonCounter1=1pentagonCounter2=5pentagonCountern=n+2*(n-1)+(pentagonCounterListV.!(n-1))-- now reading from vectormain::IO()main=do-- using getContents makes the program not work as specified even though it passes the tests here.-- _ <- getLine don't get into habbit of throwing away useful information.t<-read<$>getLinens<-forM[1..t]$constgetLine-- forM is same as flip mapM -- so we "loop" t times. Much easier to read when you have a lot more lines of code to loop, which might not be apparent here.-- | const is same as \_ ->.-- | since we are waiting till we have all input, we bind it to ns otherwise we could just have the rest of code in heremapM_print[pentagonCounter$readi|i<-ns]-- print x = putStrLn (show x)-- `main` not much different in length to your original but works properly
What I meant with the last bit is that you can replace
This saves you having to store the values then mapM over them. However, each result is printed as soon as soon as the input is given (which is not the specification).
By the way, there is a memoize package called MemoTrie, which use tries and is not only faster but allows you to clean up your code.
so your pentagonCounter and pentagonCounterList funtions can be replaced with
Pentagonal Numbers
You are viewing a single comment's thread. Return to all comments →
is just really slow
i tried a whole host of solutions to see which would time out and I tried with 3 data structures, standard linked list, array and vector
pents (standard linked list) failed tests 5 and 6, pentsA (array) and pentsV (vectors) passed everything.
I think since you know the maximum size of the list and you are repeatedly reading from them, then the way to go is array or vector. The whole advantage of a linked list is that you can have dynamic/aribtrary sizes (at the cost of speed of reading)
slight change to your solution fixes the problem
What I meant with the last bit is that you can replace
with
This saves you having to store the values then mapM over them. However, each result is printed as soon as soon as the input is given (which is not the specification).
By the way, there is a memoize package called MemoTrie, which use tries and is not only faster but allows you to clean up your code.
so your
pentagonCounter
andpentagonCounterList
funtions can be replaced withbut you can't use that package on this website apparently.