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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:47:53+00:00 2026-05-27T03:47:53+00:00

Given a map where a digit is associated to several characters scala> val conversion

  • 0

Given a map where a digit is associated to several characters

scala> val conversion = Map("0" -> List("A", "B"), "1" -> List("C", "D"))
conversion: scala.collection.immutable.Map[java.lang.String,List[java.lang.String]] =
  Map(0 -> List(A, B), 1 -> List(C, D))

I want to generate all possible character sequences based on a sequence of digits. Examples:

"00" -> List("AA", "AB", "BA", "BB")
"01" -> List("AC", "AD", "BC", "BD")

I can do this with for comprehensions

scala> val number = "011"
number: java.lang.String = 011

Create a sequence of possible characters per index

scala> val values = number map { case c => conversion(c.toString) }
values: scala.collection.immutable.IndexedSeq[List[java.lang.String]] =
  Vector(List(A, B), List(C, D), List(C, D))

Generate all the possible character sequences

scala> for {
     | a <- values(0)
     | b <- values(1)
     | c <- values(2)
     | } yield a+b+c
res13: List[java.lang.String] = List(ACC, ACD, ADC, ADD, BCC, BCD, BDC, BDD)

Here things get ugly and it will only work for sequences of three digits. Is there any way to achieve the same result for any sequence length?

  • 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-27T03:47:54+00:00Added an answer on May 27, 2026 at 3:47 am

    The following suggestion is not using a for-comprehension. But I don’t think it’s a good idea after all, because as you noticed you’d be tied to a certain length of your cartesian product.

    scala> def cartesianProduct[T](xss: List[List[T]]): List[List[T]] = xss match {
         |   case Nil => List(Nil)
         |   case h :: t => for(xh <- h; xt <- cartesianProduct(t)) yield xh :: xt
         | }
    cartesianProduct: [T](xss: List[List[T]])List[List[T]]
    
    scala> val conversion = Map('0' -> List("A", "B"), '1' -> List("C", "D"))
    conversion: scala.collection.immutable.Map[Char,List[java.lang.String]] = Map(0 -> List(A, B), 1 -> List(C, D))
    
    scala> cartesianProduct("01".map(conversion).toList)
    res9: List[List[java.lang.String]] = List(List(A, C), List(A, D), List(B, C), List(B, D))
    

    Why not tail-recursive?

    Note that above recursive function is not tail-recursive. This isn’t a problem, as xss will be short unless you have a lot of singleton lists in xss. This is the case, because the size of the result grows exponentially with the number of non-singleton elements of xss.

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

Sidebar

Related Questions

given the following code? final Map<String, List<E>> map = new HashMap<String, List<E>>(); List<E> list
Given a binary string coded on 1 byte, is it possible to map several
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
My requirement is that given a string as key to the map, I should
Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //
Given a map such as: Map<String, Integer> = new Hashmap<String, Integer>; How can I
I have a module StringMap built by the functor Map.Make given a type String
Given a list of items, and a map from a predicate function to the
Given std::map<int,std::string> myMap; fillMyMapWithStuff(myMap); // modify key values - I need to add a
Given a Map, how do I look up all keys associated with a particular

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.