diff --git a/Covariance/.idea/vcs.xml b/Covariance/.idea/vcs.xml
new file mode 100644
index 0000000..6c0b863
--- /dev/null
+++ b/Covariance/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Covariance/.idea/workspace.xml b/Covariance/.idea/workspace.xml
index c509389..b88a917 100644
--- a/Covariance/.idea/workspace.xml
+++ b/Covariance/.idea/workspace.xml
@@ -1,7 +1,12 @@
-
+
+
+
+
+
+
@@ -13,37 +18,16 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
@@ -51,6 +35,9 @@
+
+
+
-
-
+
+
+
+
+
@@ -85,98 +75,141 @@
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -233,29 +266,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -272,27 +282,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -339,105 +328,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -467,24 +357,22 @@
-
+
-
-
+
-
-
+
-
+
@@ -492,8 +380,10 @@
+
+
@@ -514,20 +404,150 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -536,29 +556,103 @@
-
+
-
-
-
+
+
-
+
-
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ No facets are configured
+
+
+
+
+
+
+
+
+
+
+
+ scala-sdk-2.11.8
+
+
+
+
+
+
+
+
+
+
+
+ 1.8
+
+
+
+
+
+
+
+
+
+
+
+ Covariance
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/VerificaStatica/.idea/misc.xml b/VerificaStatica/.idea/misc.xml
index 15d2221..d5d79e0 100644
--- a/VerificaStatica/.idea/misc.xml
+++ b/VerificaStatica/.idea/misc.xml
@@ -1,23 +1,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
\ No newline at end of file
diff --git a/VerificaStatica/.idea/workspace.xml b/VerificaStatica/.idea/workspace.xml
index 5fc45d5..57737da 100644
--- a/VerificaStatica/.idea/workspace.xml
+++ b/VerificaStatica/.idea/workspace.xml
@@ -32,8 +32,8 @@
-
-
+
+
@@ -49,26 +49,15 @@
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
@@ -85,17 +74,8 @@
-
-
-
-
-
-
-
-
-
@@ -176,24 +156,40 @@
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
@@ -203,7 +199,7 @@
-
+
@@ -211,7 +207,11 @@
+
+
+
+
@@ -236,7 +236,11 @@
+
+
+
+
@@ -300,27 +304,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -387,6 +370,10 @@
+
+
+
+
@@ -402,6 +389,10 @@
+
+
+
+
@@ -433,34 +424,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -473,6 +436,10 @@
+
+
+
+
@@ -494,6 +461,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -547,32 +538,36 @@
-
-
+
+
+
+
+
-
-
+
+
+
-
+
+
-
@@ -600,7 +595,7 @@
-
+
@@ -608,7 +603,7 @@
-
+
@@ -616,7 +611,7 @@
-
+
@@ -624,7 +619,15 @@
-
+
+
+
+
+
+
+
+
+
@@ -632,21 +635,21 @@
-
+
-
+
-
+
@@ -654,35 +657,35 @@
-
+
-
+
-
+
-
+
-
-
+
+
diff --git a/VerificaStatica/src/main/scala/Main.scala b/VerificaStatica/src/main/scala/Main.scala
index 562678f..a31409d 100644
--- a/VerificaStatica/src/main/scala/Main.scala
+++ b/VerificaStatica/src/main/scala/Main.scala
@@ -8,136 +8,159 @@ object Main {
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)
+
+ 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 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] ={
+
+ def getAlternateOrder(a: Array[Int]): Array[Int] = {
val limit = a.length.toDouble / 2 - 1
val max = math.ceil(limit).toInt
- val res = for{
+ val res = for {
i <- 0 to max
- j <- a.indices by a.length-1
+ j <- a.indices by a.length - 1
if j == 0 || limit % 1 == 0 || i != max
- } yield a(if (j == 0) i else j-i)
+ } 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{
+ for {
i <- 0 to a.length - 3
- if a(i) == a(i+1) && a(i+1) == a(i+2)
+ 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
+ for (i <- a; if i == 2 * elem) return true
false
})
+
def hasPositiveAndThenNegative(a: Array[Int]): Boolean = {
- for{
+ for {
i <- 0 to a.length - 2
- if a(i) >= 0 && a(i+1) < 0
+ if a(i) >= 0 && a(i + 1) < 0
} return true
false
}
- private def inputString: String ={
+
+ private def inputString: String = {
print("Inserisci stringa: ")
StdIn.readLine()
}
- private def inputIndex: Int ={
+
+ 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 {
+
+ 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 {
+
+ 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]) ={
+
+ 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")
+ if (chosen.isEmpty) println("Voce non riconosciuta")
else chosen.head.action()
}
- private def intMenu(array: Array[Int]): Unit ={
+
+ 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")),
+ () => 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")),
+ () => 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")),
+ () => 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")),
+ () => println(if (hasPositiveAndThenNegative(array)) "Positivo consecutivo a negativo" else "NO")),
MenuItem.exit
)
useMenu(menuItems)
intMenu(array)
}
- private def stringMenu(): Unit ={
+
+ 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(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))),
+ () => println("Risultato: " + getNumSpacesBeforeVowels(inputString))),
MenuItem(5, "Sostituisci gli spazi con asterischi",
- () => println("Risultato: "+getSpacesAsAsterisks(inputString))),
+ () => 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.")
+ 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]) = mainMenu
- def mainMenu: Unit ={
+
+ 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", () => {
@@ -150,13 +173,13 @@ object Main {
}
}
-class MenuItem(i: Int, d: String, f: ()=>Unit){
- val index: Int = i
- val desc: String = d
- val action: () => Unit = f
- override implicit def toString: String = s"$index: $desc"
+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)
+
+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))
}
\ No newline at end of file