Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework_6 - Рябцев Никита #220

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions homeworks/homework_5/src/main/scala/Exercises.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,37 @@ object Exercises {
case class Dog(override val name: String) extends Animal


case class Shelter[+T <: Animal](list: List[T]) {

case class Shelter ...
private val _list = list
val getNames: List[String] = _list.map(animal => animal.name)

def +[A >: T <: Animal](animal: A): Shelter[A] = Shelter(_list :+ animal)

def ++[A >: T <: Animal](shelter: Shelter[A]): Shelter[A] = Shelter(_list ::: shelter.list)

def feed(meat: Food[T]): List[String] =
_list.map(animal => meat.feed(animal))
}

trait Food ...

case object Meat extends Food[Animal] ...
trait Food[-T <: Animal] {
val _name: String

case object Milk extends Food[Cat] ...
def feed(animal: T): String =
animal.name + " eats " + _name
}

case object Meat extends Food[Animal] {
override val _name = "meat"
}

case object Bread extends Food[Dog] ...
case object Milk extends Food[Cat] {
override val _name = "milk"
}


case object Bread extends Food[Dog] {
override val _name = "bread"
}
}
50 changes: 38 additions & 12 deletions homeworks/homework_6/src/main/scala/Exercises.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
import scala.annotation.tailrec

object Exercises {


def reverse[T](seq: Seq[T]): Seq[T] = ???
def reverse[T](seq: Seq[T]): Seq[T] = seq match {
case element :: Nil => Seq(element)
case element :: tail => reverse(tail) :+ element
}

/**
* https://ru.wikipedia.org/wiki/Числа_Фибоначчи
*
* @param idx
* @return
*/
def fibonacci4Index(idx: Int): Int = ???

def fibonacci(idx: Int): Seq[Int] = ???
def fibonacci4Index(idx: Int): Int = fibonacciAccumulator(0, 1, idx)

lazy val MORSE = Map("A" -> ".-", "B" -> "-...", "C" -> "-.-.", "D" -> "-..", "E" -> ".", "F" -> "..-.",
"G" -> "--.", "H" -> "....", "I" -> "..", "J" -> ".---", "K" -> "-.-", "L" -> ".-..",
"M" -> "--", "N" -> "-.", "O" -> "---", "P" -> ".--.", "Q" -> "--.-", "R" -> ".-.",
"S" -> "...", "T" -> "-", "U" -> "..-", "V" -> "...-", "W" -> ".--", "X" -> "-..-",
"Y" -> "-.--", "Z" -> "--..")
@tailrec
def fibonacciAccumulator(a: Int, b: Int, i: Int): Int = i match {
case 0 => a
case i => fibonacciAccumulator(b, a + b, i - 1)
}

def morse(text: String): String = ???
def fibonacci(idx: Int): Seq[Int] =
(0 to idx).map(fibonacci4Index)

// @tailrec
// def fibonacciListAccumulator(list: List[Int], a: Int, b: Int, i: Int): List[Int] = i match {
// case 0 => a :: Nil
// case i => fibonacciListAccumulator()
// }

def wordReverse(text: String): String = ???

lazy val MORSE = Map("A" -> ".-", "B" -> "-...", "C" -> "-.-.", "D" -> "-..", "E" -> ".", "F" -> "..-.",
"G" -> "--.", "H" -> "....", "I" -> "..", "J" -> ".---", "K" -> "-.-", "L" -> ".-..",
"M" -> "--", "N" -> "-.", "O" -> "---", "P" -> ".--.", "Q" -> "--.-", "R" -> ".-.",
"S" -> "...", "T" -> "-", "U" -> "..-", "V" -> "...-", "W" -> ".--", "X" -> "-..-",
"Y" -> "-.--", "Z" -> "--..")

def morse(text: String): String =
text
.toUpperCase
.map(s => MORSE.getOrElse(s.toString, s.toString))
.mkString(" ")


def wordReverse(text: String): String = text.foldLeft(("", "")) {
case ((text, word), char) if char.isLetter => (text, word + char)
case ((text, word), char) => (text + word.headOption.fold("")(c => if (c.isUpper) word.toLowerCase.reverse.capitalize else word.reverse) + char, "")
} match {
case (text, word) => text + word
}
}
3 changes: 2 additions & 1 deletion homeworks/homework_6/src/test/scala/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ object Test extends TestSuite{

'morse - {
assert(Exercises.morse("SOS") == "... --- ...")
assert(Exercises.morse("Hello world!") == ".... . .-.. .-.. --- .-- --- .-. .-.. -..!")
val actual = Exercises.morse("Hello world!")
assert(actual == ".... . .-.. .-.. --- .-- --- .-. .-.. -..!")
}

'wordReverse - {
Expand Down