185 lines
5.6 KiB
Scala
185 lines
5.6 KiB
Scala
import scala.io.StdIn
|
|
|
|
/**
|
|
* Created by claudio on 08/10/16.
|
|
*/
|
|
object Main {
|
|
val vowels = {
|
|
val vow = Set('a', 'e', 'i', 'o', 'u')
|
|
vow ++ vow.map(_.toUpper)
|
|
}
|
|
|
|
def getReverseString(s: String): String = s.reverse
|
|
|
|
def getVowels(s: String): String =
|
|
for {
|
|
c <- s
|
|
if vowels.contains(c)
|
|
} yield c
|
|
|
|
def getNumUpperStrings(as: Array[String]): Int = as.count(s => ('A' to 'Z').contains(s(0)))
|
|
|
|
def getNumSpacesBeforeVowels(s: String): Int = {
|
|
def iterate(i: Int = 0, spaces: Int = 0, sum: Int = 0): Int = {
|
|
if (i >= s.length) sum
|
|
else if (s(i) == ' ') iterate(i + 1, spaces + 1, sum)
|
|
else if (vowels.contains(s(i))) iterate(i + 1, 0, sum + spaces)
|
|
else iterate(i + 1, 0, sum)
|
|
}
|
|
|
|
iterate()
|
|
}
|
|
|
|
def getSpacesAsAsterisks(s: String): String = s.map(c => if (c == ' ') '*' else c)
|
|
|
|
def isMatchBetweenSeq(a: Array[String], b: Array[String]): Array[String] =
|
|
for {
|
|
a1 <- a
|
|
b1 <- b
|
|
if a1 == b1
|
|
} yield a1
|
|
|
|
def getAlternateOrder(a: Array[Int]): Array[Int] = {
|
|
val limit = a.length.toDouble / 2 - 1
|
|
val max = math.ceil(limit).toInt
|
|
val res = for {
|
|
i <- 0 to max
|
|
j <- a.indices by a.length - 1
|
|
if j == 0 || limit % 1 == 0 || i != max
|
|
} yield a(if (j == 0) i else j - i)
|
|
res.toArray
|
|
}
|
|
|
|
def isEvenSumEqToOddSum(a: Array[Int]): Boolean = a.count(_ % 2 == 0) == a.count(_ % 2 == 1)
|
|
|
|
def getReversePositives(a: Array[Int]): Array[Int] = a.filter(_ > 0).reverse
|
|
|
|
def hasThreeConsecutivesEqual(a: Array[Int]): Boolean = {
|
|
for {
|
|
i <- 0 to a.length - 3
|
|
if a(i) == a(i + 1) && a(i + 1) == a(i + 2)
|
|
} return true
|
|
false
|
|
}
|
|
|
|
def hasDoubled(a: Array[Int]): Boolean =
|
|
a.exists(elem => {
|
|
for (i <- a; if i == 2 * elem) return true
|
|
false
|
|
})
|
|
|
|
def hasPositiveAndThenNegative(a: Array[Int]): Boolean = {
|
|
for {
|
|
i <- 0 to a.length - 2
|
|
if a(i) >= 0 && a(i + 1) < 0
|
|
} return true
|
|
false
|
|
}
|
|
|
|
private def inputString: String = {
|
|
print("Inserisci stringa: ")
|
|
StdIn.readLine()
|
|
}
|
|
|
|
private def inputIndex: Int = {
|
|
print("Inserisci la lunghezza della sequenza: ")
|
|
val len = StdIn.readInt()
|
|
require(len > 0)
|
|
len
|
|
}
|
|
|
|
private def inputSeqString: Array[String] = {
|
|
val strings = for (i <- 1 to inputIndex) yield {
|
|
print(s"Inserisci #$i stringa: ")
|
|
StdIn.readLine()
|
|
}
|
|
strings.toArray
|
|
}
|
|
|
|
private def inputIntArray: Array[Int] = {
|
|
val ints = for (i <- 1 to inputIndex) yield {
|
|
print(s"Inserisci #$i intero: ")
|
|
StdIn.readInt()
|
|
}
|
|
ints.toArray
|
|
}
|
|
|
|
private def printIntArray(a: Array[Int]) = a.foreach(elem => print(elem + " "))
|
|
|
|
private def useMenu(menuItems: List[MenuItem]) = {
|
|
println("\nMenù\n")
|
|
menuItems.foreach(Console println _)
|
|
print("Scelta: ")
|
|
val input = StdIn.readInt()
|
|
val chosen = menuItems.filter(_.index == input)
|
|
if (chosen.isEmpty) println("Voce non riconosciuta")
|
|
else chosen.head.action()
|
|
}
|
|
|
|
private def intMenu(array: Array[Int]): Unit = {
|
|
val menuItems = List(
|
|
MenuItem(1, "Stampa in modo buffo", () => printIntArray(getAlternateOrder(array))),
|
|
MenuItem(2, "Scopri se la somma dei pari equivale alla somme dei dispari",
|
|
() => println(if (isEvenSumEqToOddSum(array)) "Somma pari == somma dispari" else "NO")),
|
|
MenuItem(3, "Stampa al contrario i positivi", () => printIntArray(getReversePositives(array))),
|
|
MenuItem(4, "Scopri se ci sono tre numeri uguali consecutivi",
|
|
() => println(if (hasThreeConsecutivesEqual(array)) "Tre consecutivi uguali" else "NO")),
|
|
MenuItem(5, "Scopri se un elemento è il doppio di un altro",
|
|
() => println(if (hasDoubled(array)) "Elemento = 2*Altro elemento" else "NO")),
|
|
MenuItem(6, "Scopri se un positivo e un negativo sono consecutivi",
|
|
() => println(if (hasPositiveAndThenNegative(array)) "Positivo consecutivo a negativo" else "NO")),
|
|
MenuItem.exit
|
|
)
|
|
useMenu(menuItems)
|
|
intMenu(array)
|
|
}
|
|
|
|
private def stringMenu(): Unit = {
|
|
val menuItems = List(
|
|
MenuItem(1, "Stampa al contrario", () => println("Risultato: " + getReverseString(inputString))),
|
|
MenuItem(2, "Stampa solo le vocali", () => println("Risultato: " + getVowels(inputString))),
|
|
MenuItem(3, "Trova numero di stringhe", () => println("Risultato: " + getNumUpperStrings(inputSeqString))),
|
|
MenuItem(4, "Trova numero spazi prima di una vocale",
|
|
() => println("Risultato: " + getNumSpacesBeforeVowels(inputString))),
|
|
MenuItem(5, "Sostituisci gli spazi con asterischi",
|
|
() => println("Risultato: " + getSpacesAsAsterisks(inputString))),
|
|
MenuItem(6, "Trova elementi uguali in due sequenze", () => {
|
|
println("Inserisci prima sequenza")
|
|
val a = inputSeqString
|
|
println("Inserisci seconda sequenza")
|
|
val b = inputSeqString
|
|
val results = isMatchBetweenSeq(a, b)
|
|
if (results.isEmpty) println("Nessuna corrispondenza trovata")
|
|
else for (res <- results) println(s"'$res' comune.")
|
|
}),
|
|
MenuItem.exit
|
|
)
|
|
useMenu(menuItems)
|
|
stringMenu()
|
|
}
|
|
|
|
def main(argv: Array[String]): Unit = mainMenu
|
|
|
|
def mainMenu: Unit = {
|
|
val menuItems = List(
|
|
MenuItem(1, "Lavora con le stringhe", stringMenu),
|
|
MenuItem(2, "Lavora con gli interi", () => {
|
|
intMenu(inputIntArray)
|
|
}),
|
|
MenuItem.exit
|
|
)
|
|
useMenu(menuItems)
|
|
mainMenu
|
|
}
|
|
}
|
|
|
|
class MenuItem(val index: Int, val desc: String, val action: () => Unit) {
|
|
|
|
override def toString: String = s"$index: $desc"
|
|
}
|
|
|
|
object MenuItem {
|
|
def apply(i: Int, d: String, f: () => Unit) = new MenuItem(i, d, f)
|
|
|
|
val exit: MenuItem = MenuItem(0, "Esci dal programma", () => System.exit(0))
|
|
} |