Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suffix array beginning using scala

Today I am trying to create suffix arrays using scala. I was able to do it with massive lines of code but then I heard that it can be created by using only few lines by using zipping and sorting.

The problem I have at the moment is with the beginning. I tried using binary search and zipWithIndex to create the following "tree" but so far I haven't been able to create anything. I don't even know if it is possible by only using a line but I bet it is lol.

What I want to do is to get from a word "cheesecake" is a Seq:

 Seq((cheesecake, 0),
     (heesecake, 1),
     (eesecake, 2),
     (esecake, 3),
     (secake, 4),
     (ecake, 5),
     (cake, 6),
     (ake, 7),
     (ke, 8),
     (e, 9))

Could someone nudge me to the correct path ?

like image 816
Duzzz Avatar asked May 06 '15 11:05

Duzzz


People also ask

What is suffix array algorithm?

In computer science, a suffix array is a sorted array of all suffixes of a string. It is a data structure used in, among others, full text indices, data compression algorithms, and the field of bibliometrics.

What will be the suffix array of the string Engineering *?

6. What will be the suffix array of the string “engineering”? Explanation: Correct choice is : 5 0 6 10 2 3 8 4 9 1 7. Because the suffix array formed will be: 5 eering 0 engineering 6 ering 10 g 2 gineering 3 ineering 8 ing 4 neering 9 ng 1 ngineering 7 ring.

How do you add an element to an array in Scala?

Use the concat() Method to Append Elements to an Array in Scala. Use the concat() function to combine two or more arrays. This approach creates a new array rather than altering the current ones. In the concat() method, we can pass more than one array as arguments.


Video Answer


1 Answers

To generate all the possible postfixes of a String (or any other scala.collection.TraversableLike) you can simply use the tails method:

scala> "cheesecake".tails.toList
res25: List[String] = List(cheesecake, heesecake, eesecake, esecake, secake, ecake, cake, ake, ke, e, "")

If you need the indexes, then you can use GenIterable.zipWithIndex:

scala> "cheesecake".tails.toList.zipWithIndex
res0: List[(String, Int)] = List((cheesecake,0), (heesecake,1), (eesecake,2), (esecake,3), (secake,4), (ecake,5), (cake,6), (ake,7), (ke,8), (e,9), ("",10))
like image 59
kosii Avatar answered Oct 03 '22 15:10

kosii