0

I want to "translate" a list of numbers (range -999, 999) into a list of strings in Scala:

The function:

def translate(listofnumbers: List[Int]): List[String] = {

I want to solve this Problem without using mk.String.

I tried to use pattern matching

def testList( x: Int) = x match{
     case  1 => println("one")

but I don't know how I can combine cases for numbers like 12, 103 etc.

The Result should look like this Example:

println(translate(List(1, 2, 3, 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)))

and get

List(one,two,three,four,five,six......)
Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97
Anna
  • 19
  • 6
  • There is this gist https://gist.github.com/huitseeker/212a0e4d1d2e88f40f0ef80b7bce57c5 which does exactly that. – Xavier Guihot May 26 '18 at 12:16
  • @XavierGuihot That only works for American English. You need to add "and" in a few places for British English :) – Tim May 26 '18 at 13:04
  • This question doesn't seem to present any interesting programming challenges, but instead seems to be mostly about idiosyncrasies of one particular natural language. I'm voting to close this question as off-topic because it seems to belong to [English Language & Usage](https://english.stackexchange.com/) or [English Learners Stack Exchange](https://ell.stackexchange.com/). – Andrey Tyukin May 26 '18 at 13:11
  • @XavierGuihot I've just started with scala, so excuse the stupid question. How can i do it with a list [Int] – Anna May 26 '18 at 13:25
  • 2
    [Here](https://stackoverflow.com/a/3911987/2707792) are all the cases written out in Java. [Here](https://stackoverflow.com/questions/26977523/converting-number-to-word) is another one. – Andrey Tyukin May 26 '18 at 13:26
  • Oh, and [here](https://rosettacode.org/wiki/Number_names#Scala) are another 73 solutions. Two of them are in Scala. As you can see, to make it work properly, you have to tabulate a nightmarish amount of corner cases, so the main difficulty is not the programming per-se, it's the natural language that is difficult. Somewhat better than roman numerals, but still... – Andrey Tyukin May 26 '18 at 13:36
  • my problem is not the natural language. My problem is that i don't know how do to it with a List of numbers List[Int]): List[String]. in all these examples it isn't a List. And like i said before, i'm a absolute beginner, so excuse the question – Anna May 26 '18 at 13:46
  • @Anna Once you have implemented / copied some method `numberToNumberName(i: Int): String`, converting the `List[Int]` to `List[String]` is just `listOfNumbers.map(numberToNumberName)`. – Andrey Tyukin May 26 '18 at 13:57
  • Thank you Andrey !! – Anna May 26 '18 at 14:14
  • That was a nice SO moment. – som-snytt May 26 '18 at 16:21

0 Answers0