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 6729953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:21:03+00:00 2026-05-26T10:21:03+00:00

I was looking at a Perl code golf page (don’t ask why) and came

  • 0

I was looking at a Perl code golf page (don’t ask why) and came across this:

Hole 3 – Smallest Repeating Pattern

Write a subroutine that accepts a string which may consist of a
repeating pattern, and returns the smallest repeating substring. If
the string does not consist of a repeating pattern, the subroutine
should return undef or the empty string. e.g.:

input    output 
'aaaaaa' 'a' 
'ababab' 'ab' 
'aabaab' 'aab' 
'ababaa' ''

Apparently in Perl this can be expressed as sub g3 { pop=~/^(.*?)\1+\z/s&&$1 }

I don’t know much Perl so I don’t understand how this works. What is the best we can do in Scala? I’m more interested in elegance than the precise number of characters.

Here is my attempt, but it’s pretty ugly, which is why I’m asking.

def srp(s: String) =
  s.inits.toList.tail.init.map(i => (i * (s.size / i.size), i)).
    filter(_._1 == s).map(_._2).reverse.headOption.getOrElse("")
  • 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-05-26T10:21:04+00:00Added an answer on May 26, 2026 at 10:21 am

    Elegance is subjective…

    def smallestPat(input: String) = {
       (1 to input.length).view
          .map(i => input.take(i))
          .find{p => 
            Iterator.iterate(p)(_ + p)
              .takeWhile(_.length <= input.length)
              .exists(_ == input) && p != input}
          .getOrElse("")
    }
    
    List("aaaaaa", "ababab", "aabaab", "ababaa") map smallestPat
    // res13: List[String] = List(a, ab, aab, "")
    

    Edit and re-edited: slighty shorter:

    def smallestPat(i: String) = {
       (1 to i.length - 1)
          .map(i.take)
          .find(p => p * (i.length / p.length) == i)
          .getOrElse("")
    }
    

    One more, using grouped:

    def smallestPat(i: String) = {
      (1 to i.size/2).map(i.take)
      .find(p => i.grouped(p.size) forall(p==))
      .getOrElse("")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a Perl version of the code on this page: http://www.linuxquestions.org/questions/linux-general-1/how-to-bring-up-application-window-to-front-from-shell-script-83545/ So
I'm looking through perl code and I see this: sub html_filter { my $text
I'm looking for the Perl equivalent to this Python code: from sys import stdout
I'm looking at some older Perl code on Perl Monks to figure out programming
I need to rewrite some Perl code in python. So I'm looking for the
I'm looking for ways to express this Python snippet in Perl: data = {A:
I'm looking for a way to do this in Perl: $a = 60; $b
I am looking at some old code in Perl, where the author has writtern
I'm looking through the MarkDown code written in Perl by John Gruber, and there
I'm looking to merge customized code from RT3 into RT4, a Perl application. It

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.