Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package Task1

object Task1 {
/**
* Count the frequency only of english alphabet letters ignoring the case
*
* @param str String that contains different characters
* @return map in which kye is a english alphabet letter and
* value is the appearing frequency of this letter in str string
* if string empty or doesn't contain any letter, you will receive empty map
*/
def getFrequencyOfLetters(str: String): Map[Char, Int] =
str.toCharArray.map(_.toLower).filter(_.isLetter).groupBy(x => x).map(x => (x._1, x._2.size))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package Task1

import org.scalatest._

class FrequencyTest extends FlatSpec with Matchers {
it should "Check correctness" in {
Task1.getFrequencyOfLetters("aaaa") should be(Map[Char,Int](('a',4)))
Task1.getFrequencyOfLetters("aaaa1313 _! test") should be(Map[Char,Int](('a',4),('t',2),('e',1),('s',1)))
Task1.getFrequencyOfLetters("") should be(Map[Char,Int]())
Task1.getFrequencyOfLetters("!!1!??7?") should be(Map[Char,Int]())
Task1.getFrequencyOfLetters("aaAAaaAA") should be(Map[Char,Int](('a',8)))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package task2

abstract case class BaseUser(username: String, level: Int = 0, experience: Int = 0) {
require("[^a-zA-z0-9._]".r.findFirstIn(username).isEmpty, "username doesn't match. It has to use only [a-z][A-Z][0-9]-._ characters")

require(level >= 0, "level has to be higher than 0")

require(experience >= 0, "experience can't be lower than 0")

val expForLevelUp = 500;

/**
* add experiance to user for some action
*
* @param amount Int the number of experiance that user will recive for action
* @return User with updated experiance
*/
def addExperience(amount: Int): BaseUser

/**
* for ProUsers increase payment counter and turns FreeUser to ProUser
*
* @param amount Int the number of days that will be added to payment counter
* @return User with updated payment counter
*/
def pay(amount: Int): BaseUser

/**
* midnight update will update users level, experiance, action limit and payment counter
*
* @return User with updated state
*/
def midnightUpdate(): BaseUser
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package task2

class FreeUser(username: String, level: Int = 0, experience: Int = 0, val actionLimit: Int = 30) extends BaseUser(username, level, experience) {
override def addExperience(amount: Int): FreeUser = if (actionLimit > 0) new FreeUser(username, level, experience + amount, actionLimit - 1) else this

override def pay(amount: Int): ProUser = new ProUser(username, level, experience, amount)

override def midnightUpdate(): FreeUser = {
if (actionLimit < 3)
new FreeUser(username, level + experience / 500, experience % 500, 3)
else
new FreeUser(username, level + experience / 500, experience % 500, actionLimit)
}

override def canEqual(that: Any): Boolean = that.isInstanceOf[FreeUser]

override def equals(o: Any): Boolean = o match {
case o: FreeUser => o.canEqual(this) && o.hashCode() == this.hashCode()
case _ => false
}

override def hashCode(): Int = super.hashCode() + actionLimit.hashCode()

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package task2

class ProUser(username: String, level: Int = 0, experience: Int = 0, paymentCounter: Int = 0) extends BaseUser(username, level, experience) {
override def addExperience(amount: Int): ProUser = new ProUser(username, level, experience + amount, paymentCounter)

def pay(amount: Int): ProUser = new ProUser(username, level, experience, paymentCounter + amount)

override def midnightUpdate(): BaseUser = {
if (paymentCounter == 0) {
new FreeUser(username, level + experience / 500, experience % 500)
} else {
new ProUser(username, level + experience / 500, experience % 500, paymentCounter - 1)
}
}

override def canEqual(that: Any): Boolean = that.isInstanceOf[ProUser]

override def equals(o: Any): Boolean = o match {
case o: FreeUser => o.canEqual(this) && o.hashCode() == this.hashCode()
case _=> false
}

override def hashCode(): Int = super.hashCode() + paymentCounter.hashCode()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package task2

import org.scalatest._

class UserTest extends FlatSpec with Matchers {
it should "throw an IllegalArgumentException for incorrect username" in {
an[IllegalArgumentException] should be thrownBy new FreeUser("nickname check!")
an[IllegalArgumentException] should be thrownBy new ProUser("tester123+4")
}

it should "throw an IllegalArgumentException for incorrect experience and level" in {
an[IllegalArgumentException] should be thrownBy new FreeUser("nickname", -1)
an[IllegalArgumentException] should be thrownBy new FreeUser("nickname", 0, -1)
}

it should "update experience to users from add method" in {
val freeUser = new FreeUser("Username", 0, 1, 3)
val proUser = new ProUser("Username", 0, 2, 2)

freeUser.addExperience(100).equals(new FreeUser("Username", 0, 101, 2))
proUser.addExperience(100).equals(new ProUser("Username", 0, 102, 2))

val noActionUser = new FreeUser("Username", 0, 3, 0)

noActionUser.addExperience(100).equals(new FreeUser("Username", 0, 3, 0))

val zeroExpAdd = new FreeUser("Username", 0, 4, 1)

zeroExpAdd.addExperience(0).equals((new FreeUser("Username", 0, 4, 0)))
}

it should "update payment from pay method" in {
val freeUser = new FreeUser("Username", 0, 1, 3)
val proUser = new ProUser("Username", 0, 2, 2)

freeUser.pay(30).equals(new ProUser("Username", 0, 1, 30))
proUser.pay(30).equals(new ProUser("Username", 0, 2, 32))
}

it should "make correct midnight update" in {
val freeUser = new FreeUser("Username", 1, 1400, 1)
val proUser = new ProUser("Username", 0, 500, 0)

freeUser.midnightUpdate().equals(new FreeUser("Username", 3, 400, 3))
proUser.midnightUpdate().equals(new FreeUser("Username", 1, 0, 30))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package Task3

case class TSStack[T](node: List[T] = List()) {
/**
* make stack empty
*
* @return an empty stack
*/
def clear = this.synchronized(TSStack[T]())

/**
* check is stack empty
*
* @return true if stack is empty and false if not
*/
def isEmpty = this.synchronized(node.isEmpty)

/**
* delete the top element of a stack
*
* @return new stack without top element
*/
def pop = this.synchronized {
if (!isEmpty) TSStack[T](node.tail) else TSStack[T]()
}

/**
* add element to top of stack
*
* @param elem T is an element that will be added to stack
* @return new stack with new top element
*/
def push(elem: T) = this.synchronized {
TSStack(elem :: node)
}

/**
* get the number of elements in stack
*
* @return number of elements in stack
*/
def size = this.synchronized(node.length)

/**
* get the top element of stack
*
* @return the top element of stack
*/
def top = this.synchronized(node.headOption)

/**
* get the string variant of stack
*
* @return string variant of stack
*/
override def toString = this.synchronized(node.toString())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package Task3

import org.scalatest._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration

class Task3TSStack extends FlatSpec with Matchers {

it should "check concurrent push" in {
var pushStack: TSStack[Int] = (TSStack())

(1 to 10000).foreach { _ =>
var f1 = Future {
pushStack = pushStack.push(1)
}
Await.result(f1, Duration.Inf)
}

(1 to 10000).foreach { _ => pushStack = pushStack.push(0) }
pushStack.size should be(20000)
}

it should "Check pop method" in {
var stack: TSStack[Int] = TSStack()
stack = stack.push(0)
stack = stack.push(1)
stack = stack.push(2)
stack = stack.pop
stack should be(new TSStack[Int](List(1, 0)))
stack = stack.pop
stack should be(new TSStack[Int](List(0)))
stack = stack.pop
stack should be(new TSStack[Int](List()))
stack = stack.pop
stack should be(new TSStack[Int](List()))
}

it should "Check top method" in {
var stack: TSStack[Int] = TSStack()
stack = stack.push(0)
stack = stack.push(1)
stack = stack.push(2)
stack.top should be(Some(2))
stack = stack.pop
stack.top should be(Some(1))
stack = stack.pop
stack.top should be(Some(0))
stack = stack.pop
stack.top should be(None)

}
}