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.
Haskell solution using Data.Matrix, passes all tests
importqualifiedData.MatrixasMimportData.Char(digitToInt)nOfCompairasonpatternmat=(r!!((M.nrowspattern)-1),c!!((M.ncolspattern)-1))wherer=reverse[1..(M.nrowsmat)]c=reverse[1..(M.ncolsmat)]-- this is nothing like a cross correlation but you get the idea-- submatrix is O(1)crossCorrelation::(Eqa)=>M.Matrixa->M.Matrixa->BoolcrossCorrelationmatptrn=or$map(\(a,aa,b,bb)->ptrn==M.submatrixaaabbbmat)compairwhere(cr,cc)=nOfCompairasonptrnmatpr=M.nrowsptrn-1pc=M.ncolsptrn-1compair=[(a,a+pr,b,b+pc)|a<-[1..cr],b<-[1..cc]]yesNobool|bool="YES"|otherwise="NO"-- typechecking seems to fail with a fold so i had to make this functionstrToIntList::String->[Int]strToIntList[]=[]strToIntList(x:xs)=(digitToIntx):(strToIntListxs)-- there's probably a cuter way to write I/O but that's not the important thing hereaction=dorc<-getLinemat<-sequence$replicate(read$head$wordsrc)getLineprc<-getLineptr<-sequence$replicate(read$head$wordsprc)getLineletmatrix=M.fromLists$mapstrToIntListmatpatter=M.fromLists$mapstrToIntListptrres=yesNo$crossCorrelationmatrixpatterputStrLnresmain=dotimes<-getLinesequence$replicate(readtimes)action
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Grid Search
You are viewing a single comment's thread. Return to all comments →
Haskell solution using Data.Matrix, passes all tests