Scala: custom shaft order list

I have a part of val that should accidentally fall into the list of results. Here is the code:

 def motivation(name:String, score:Int){ val quote1 = "You're not lost. You're locationally challenged." //score=10 val quote2 = "Time is the most valuable thing a man can spend." //score=20 val quote3 = "At times one remains faithful to a cause." //score=10 val quote4 = "Only because its opponents do not cease to be insipid." //score=10 val quote5 = "Life can be complicated." //score=20 case Some(score) => val quoteRes = shufle(????) } 
  • As I have indicated the amount of each quotation mark so that it can count.
  • How do I randomly select quotes based on score name and also do shuffle order?

for example, if John (name) has 40 (rating), the result could be quotation marks2 + quotes3 + quotes4 or quotes4 + quotes5 or quotes1 + quotes2 + quotes5

0
source share
1 answer

I think I will probably start with all the quotes and their corresponding ratings.

 val quotes = Map( "quote this" -> 10 , "no quote" -> 20 , "quoteless" -> 10 ) 

Then combine them in as many ways as possible.

 // all possible quote combinations val qCombos = (1 to quotes.size).flatMap(quotes.keys.toList.combinations) 

Turn this into a Map using the appropriate point scores.

 // associate all quote combinations with their score total val scoreSum = qCombos.groupBy(_.map(quotes).sum) 

Now you call the search for all quotes, which, when combined, are summed with the specified amount.

 // get all the quote combinations that have this score total scoreSum(20) //res0: IndexedSeq[List[String]] = Vector(List(no quote), List(quote this, quoteless)) 

As for presenting the results in random order, since you already asked about it twice already , and received good answers, I assume that this will not be a problem.

0
source

All Articles