| 1 |
/* |
| 2 |
* Copyright (c) 2021 Goldman Sachs. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* and Eclipse Distribution License v. 1.0 which accompany this distribution. |
| 6 |
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* and the Eclipse Distribution License is available at |
| 8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 |
*/ |
| 10 |
|
| 11 |
package org.eclipse.collections.impl |
| 12 |
|
| 13 |
import org.junit.Test |
| 14 |
import org.slf4j.LoggerFactory |
| 15 |
|
| 16 |
object ScalaAnagramTest |
| 17 |
{ |
| 18 |
val LOGGER = LoggerFactory.getLogger(classOf[ScalaAnagramTest]) |
| 19 |
} |
| 20 |
|
| 21 |
class ScalaAnagramTest |
| 22 |
{ |
| 23 |
val SIZE_THRESHOLD = 10 |
| 24 |
val WORDS = Stream("alerts", "alters", "artels", "estral", "laster", "ratels", "salter", "slater", "staler", "stelar", "talers", "least", "setal", "slate", "stale", "steal", "stela", "taels", "tales", "teals", "tesla") |
| 25 |
|
| 26 |
def log(string: String) |
| 27 |
{ |
| 28 |
ScalaAnagramTest.LOGGER.info(string) |
| 29 |
} |
| 30 |
|
| 31 |
@Test |
| 32 |
def testAnagrams |
| 33 |
{ |
| 34 |
WORDS.groupBy(_.sorted) |
| 35 |
.values |
| 36 |
.filter(_.size > SIZE_THRESHOLD) |
| 37 |
.toList |
| 38 |
.sortWith(_.size > _.size) |
| 39 |
.map(list => list.size + ": " + list) |
| 40 |
.foreach(log) |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
def testAnagramsLonghand |
| 45 |
{ |
| 46 |
WORDS.groupBy(word => word.sorted) |
| 47 |
.values |
| 48 |
.filter(list => list.size > SIZE_THRESHOLD) |
| 49 |
.toList |
| 50 |
.sortWith((list1, list2) => list1.size > list2.size) |
| 51 |
.map(list => list.size + ": " + list) |
| 52 |
.foreach(listString => log(listString)) |
| 53 |
} |
| 54 |
} |