longest_common_substring.sf 469 B

1234567891011121314151617181920212223
  1. #!/usr/bin/ruby
  2. #
  3. ## https://rosettacode.org/wiki/Longest_Common_Substring
  4. #
  5. func createSubstrings(String word) -> Array {
  6. gather {
  7. combinations(word.len+1, 2, {|i,j|
  8. take(word.substr(i, j-i))
  9. })
  10. }
  11. }
  12. func findLongestCommon(String first, String second) -> String {
  13. createSubstrings(first) & createSubstrings(second) -> max_by { .len }
  14. }
  15. var substr = findLongestCommon("thisisatest", "testing123testing")
  16. say substr
  17. assert_eq(substr, "test")