0

I want to generate combinations of a string with iterable multiplication.I am try to get combination of string passed to my function.I am passing numbers and get combination of strings like 1 for abc,2 for def

fun generatexCombinations(valuePassed:String){


        val list2 = listOf("A", "B", "C")
        val list3 = listOf("D", "E", "F")
        val list4 = listOf("G", "H", "I")
        val list5 = listOf("J", "K", "L")
        val list6 = listOf("M", "N", "O")
        val list7 = listOf("P", "Q", "R", "S")
        val list8 = listOf("T", "U", "V")
        val list9 = listOf("W", "X", "Y", "Z")


        val listCombination=  mutableListOf<List<String>>()

        val charArray= valuePassed.toCharArray()
        for (char in charArray){
            when(char.toString()){
                "2"->listCombination.add(list2)
                "3"->listCombination.add(list3)
                "4"->listCombination.add(list4)
                "5"->listCombination.add(list5)
                "6"->listCombination.add(list6)
                "7"->listCombination.add(list7)
                "8"->listCombination.add(list8)
                "9"->listCombination.add(list9)
            }
        }





        //this is example of text "biryani"
        val combinationOfStrings= list2 * list4*list7*list9*list2*list6*list4

        //i want to generate above list to multiplied 
        // dynamically accroding to string value passed



    }

how can i achieve it?

i am using iterable multiplication ,please refer website below

https://www.kotlinresources.com/library/kotlindiscretemathtoolkit/

5
  • Don't convert the chars to Strings. You can use char literals: '2' -> ... Commented Nov 11, 2019 at 11:34
  • ok.can you tell me how it should be done? i mean dynamic generation Commented Nov 11, 2019 at 11:48
  • Your example code only uses numeral characters from the valuePassed String, but your example text "biryani" is a String composed entirely of letters. Also, if you evaluated combinationOfStrings, it would be some crazy type like a List<Triple<Triple<String, String, String>, String, String>, String, String>, which I can't imagine would be very useful. Can you explain more clearly what you want? Commented Nov 11, 2019 at 14:34
  • Basically, what is the input (a word or a phone number?), and what is the end result supposed to be (a list of all possible words that match a word on a phone dial?)? Commented Nov 11, 2019 at 14:39
  • input given is just number. if we type 23 then i have to create all thecombination of abc and def . if i type 234 then i have to give all the 3 letter combination of abc def ghi in order. kotlinresources.com/library/kotlindiscretemathtoolkit i have used this extnetion from above link Commented Nov 13, 2019 at 3:48

2 Answers 2

1

Variables can't be created dynamically. They have to be known at compile-time.

Depending on the situation, you can use arrays/lists or maps instead.

Here's a solution:

//                       0   1     2      3      4      5      6      7       8      9
private val lut = listOf("", "", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ")
        .map(String::toList)

fun generateXCombinations(numericString: String) = numericString
        .filter(Char::isDigit)
        .map { lut[it - '0'] }
        .fold(listOf("")) { acc, list ->
            val pairs = acc * list
            pairs.map { (str, char) -> str + char }
        }

EDIT: I included the library and tested it. This code creates a list of strings. Each string is one possible input on the number keyboard.

Sign up to request clarification or add additional context in comments.

2 Comments

i have a list of products. there is search option. user have number keyboard with abc def format on it. when user presss button 2 and 3 i have to find combination of abc and def
please check link for * operator i have used
1

First, let's simplify building listCombination:

val mapOfLists = mapOf(
    '2' to listOf("A", "B", "C"),
    '3' to listOf("D", "E", "F"), 
    ...)

val charArray= valuePassed.toCharArray()
val listCombination = charArray.map { mapOfLists[it] }.filterNotNull()

Or charArray.map { mapOfLists[it]!! } if you are certain charArray won't contain characters for which you don't have a corresponding list. And then it's just

val combinationOfStrings = listCombination.reduce(List::times)

4 Comments

I don't think you can call mapNotNull on a primitive array.
Yes, mapNotNull doesn't have these overloads, map does (kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/…). Thanks.
i have problem after creating listcombination. how can i multiply all those list inside list combination together
The last line does that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.