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

  • Home
  • SEARCH
  • 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 8543919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:25:32+00:00 2026-06-11T12:25:32+00:00

I am attempting to create a Scala method that will take one parent group

  • 0

I am attempting to create a Scala method that will take one parent group of parentheses, represented as a String, and then map each subgroup of parentheses to a different letter. It should then put these in a map which it returns, so basically I call the following method like this:

val s = "((2((x+3)+6)))"
val map = mapParentheses(s)

Where s could contain any number of sets of parentheses, and the Map returned should contain:

"(x+3)" -> 'a'

"(a+6)" -> 'b'

"(2b)" -> 'c'

"(c)" -> 'd'

So that elsewhere in my program I can recall ‘d’ and get “(c)” which will become “((2b))” then ((2(a+6))) and finally ((2((x+3)+6))). The string sent to the method mapParentheses will never have unmatched parentheses, or extra chars outside of the main parent parentheses, so the following items will never be sent:

  • “(fsf)a” because the a is outside the parent parentheses
  • “(a(aa))(a)” because the (a) is outside the parent parentheses
  • “((a)” because the parentheses are unmatched
  • “)a(“ because the parentheses are unmatched

So I was wondering if anyone knew of an easy (or not easy) way of creating this mapParentheses method.

  • 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-06-11T12:25:33+00:00Added an answer on June 11, 2026 at 12:25 pm

    Classic recursive parsing problem. It can be handy to hold the different bits. We’ll add a few utility methods to help us out later.

    trait Part {
      def text: String
      override def toString = text
    }
    class Text(val text: String) extends Part {}
    class Parens(val contents: Seq[Part]) extends Part {
      val text = "(" + contents.mkString + ")"
      def mapText(m: Map[Parens, Char]) = {
        val inside = contents.collect{
          case p: Parens => m(p).toString
          case x => x.toString
        }
        "(" + inside.mkString + ")"
      }
      override def equals(a: Any) = a match {
        case p: Parens => text == p.text
        case _ => false
      }
      override def hashCode = text.hashCode
    }
    

    Now you need to parse into these things:

    def str2parens(s: String): (Parens, String) = {
      def fail = throw new Exception("Wait, you told me the input would be perfect.")
      if (s(0) != '(') fail
      def parts(s: String, found: Seq[Part] = Vector.empty): (Seq[Part], String) = {
        if (s(0)==')') (found,s)
        else if (s(0)=='(') {
          val (p,s2) = str2parens(s)
          parts(s2, found :+ p)
        }
        else {
          val (tx,s2) = s.span(c => c != '(' && c != ')')
          parts(s2, found :+ new Text(tx))
        }
      }
      val (inside, more) = parts(s.tail)
      if (more(0)!=')') fail
      (new Parens(inside), more.tail)
    }
    

    Now we’ve got the whole thing parsed. So let’s find all the bits.

    def findParens(p: Parens): Set[Parens] = {
      val inside = p.contents.collect{ case q: Parens => findParens(q) }
      inside.foldLeft(Set(p)){_ | _}
    }
    

    Now we can build the map you want.

    def mapParentheses(s: String) = {
      val (p,_) = str2parens(s)
      val pmap = findParens(p).toSeq.sortBy(_.text.length).zipWithIndex.toMap
      val p2c = pmap.mapValues(i => ('a'+i).toChar)
      p2c.map{ case(p,c) => (p.mapText(p2c), c) }.toMap
    }
    

    Evidence that it works:

    scala> val s = "((2((x+3)+6)))"
    s: java.lang.String = ((2((x+3)+6)))
    
    scala> val map = mapParentheses(s)
    map: scala.collection.immutable.Map[java.lang.String,Char] =
      Map((x+3) -> a, (a+6) -> b, (2b) -> c, (c) -> d)
    

    I will leave it as an exercise to the reader to figure out how it works, with the hint that recursion is a really powerful way to parse recursive structures.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to create an inbound Mule endpoint that will receive Syslog messages
I'm attempting to create an xquery expression that will return selected nodes but will
I am attempting to create a report, utilizing a matrix, that only displays columns
I'm attempting to create a CustomControl which will have various properties affected by an
I'm attempting to create a single Controller class to handle all foreseeable surveys that
I'm attempting create a spinning reload button, such as the one found in the
I'm attempting to create a Parcelable class in Android so that I can pass
i'm attempting to create an header which is divided into 3 divs they will
I'm attempting to write a CompiledQuery using Linq-to-Entities that will replace a stored procedure
I'm attempting to create a method in Haskell which I would think is very

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.