diff --git a/.scalafmt.conf b/.scalafmt.conf index 467325a..c4a4618 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,14 +1,74 @@ -# See: https://scalameta.org/scalafmt/docs/configuration.html -version = "3.7.17" - +# Version https://scalameta.org/scalafmt/docs/configuration.html#version +version = 3.7.17 +# Dialect https://scalameta.org/scalafmt/docs/configuration.html#scala-dialects runner.dialect = scala213source3 -align.preset = more -style = defaultWithAlign # For pretty alignment. -maxColumn = 100 -docstrings.style = SpaceAsterisk -docstrings.removeEmpty = true -continuationIndent.callSite = 2 -continuationIndent.defnSite = 4 + +# Top-level preset https://scalameta.org/scalafmt/docs/configuration.html#top-level-presets +preset = defaultWithAlign + +# Common https://scalameta.org/scalafmt/docs/configuration.html#most-popular +maxColumn = 120 +assumeStandardLibraryStripMargin = true + +# Alignment https://scalameta.org/scalafmt/docs/configuration.html#alignment +align { + preset = more + allowOverflow = true +} + +# Newlines https://scalameta.org/scalafmt/docs/configuration.html#newlines +newlines { + alwaysBeforeMultilineDef = false + implicitParamListModifierPrefer = before + beforeCurlyLambdaParams = multilineWithCaseOnly + inInterpolation = "avoid" +} + +# Comment processing https://scalameta.org/scalafmt/docs/configuration.html#comment-processing +docstrings { + style = Asterisk + wrap = no + removeEmpty = true +} + +# Spaces https://scalameta.org/scalafmt/docs/configuration.html#spaces +spaces { + inImportCurlyBraces = true # more idiomatic to include whitepsace in import x.{ yyy => zzz } +} + +# Project https://scalameta.org/scalafmt/docs/configuration.html#project +project { + git = true + excludeFilters = ["target/"] +} + +# Rewrite Rules https://scalameta.org/scalafmt/docs/configuration.html#rewrite-rules +rewrite { + rules = [ + AvoidInfix, # https://scalameta.org/scalafmt/docs/configuration.html#avoidinfix + RedundantParens, # https://scalameta.org/scalafmt/docs/configuration.html#redundantparens + SortModifiers, # https://scalameta.org/scalafmt/docs/configuration.html#sortmodifiers + PreferCurlyFors, # https://scalameta.org/scalafmt/docs/configuration.html#prefercurlyfors + Imports, # https://scalameta.org/scalafmt/docs/configuration.html#imports + ] + sortModifiers.order = ["private", "protected", "final", "sealed", "abstract", "implicit", "override", "lazy"] + imports { + expand = true + sort = original + groups = [["java(x)?\\..*"], ["scala\\..*"], ["sbt\\..*"]] + } + trailingCommas.style = keep # https://scalameta.org/scalafmt/docs/configuration.html#trailing-commas +} + +fileOverride { + "glob:**/src/main/scala-3/**" { + runner.dialect = scala3 + } +} + includeCurlyBraceInSelectChains = false -project.git = true -project.excludeFilters = ["target/"] + +continuationIndent { + callSite = 2 + defnSite = 4 +} diff --git a/build.sbt b/build.sbt index ab98fa9..b051e1b 100644 --- a/build.sbt +++ b/build.sbt @@ -1,4 +1,4 @@ -val ScalaTestVersion = "3.2.10" +val ScalaTestVersion = "3.2.17" val FlexmarkVersion = "0.64.8" scalafmtOnCompile := true @@ -8,13 +8,13 @@ lazy val root = (project in file(".")) organization := "io.github.sentenza.hacktoberfest", name := "hacktoberfest-algorithms", version := "0.12.0", - scalaVersion := "2.13.8", + scalaVersion := "2.13.12", libraryDependencies ++= Seq( "org.scala-lang" % "scala-reflect" % "2.13.12", - "org.scalatest" %% "scalatest" % ScalaTestVersion % Test, - "org.scalatestplus" %% "mockito-3-4" % (ScalaTestVersion + ".0") % Test, - "org.scalactic" %% "scalactic" % ScalaTestVersion % Test, - "com.vladsch.flexmark" % "flexmark-all" % FlexmarkVersion % Test + "org.scalatest" %% "scalatest" % ScalaTestVersion % Test, + "org.scalatestplus" %% "mockito-3-4" % "3.2.10.0" % Test, + "org.scalactic" %% "scalactic" % ScalaTestVersion % Test, + "com.vladsch.flexmark" % "flexmark-all" % FlexmarkVersion % Test ), addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.13.2" cross CrossVersion.full), addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1") diff --git a/project/build.properties b/project/build.properties index e8a1e24..abbbce5 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.9.7 +sbt.version=1.9.8 diff --git a/project/plugins.sbt b/project/plugins.sbt index 5751ed4..efb33ab 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,15 +1,9 @@ -// Easily manage scalac settings across scala versions with this -addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.4.4") - // Makes our code tidy addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") // Revolver allows us to use re-start and work a lot faster! addSbtPlugin("io.spray" % "sbt-revolver" % "0.10.0") -// To keep our dependencies up to date -addSbtPlugin("net.vonbuchholtz" % "sbt-dependency-check" % "5.1.0") - // Enables test coverage analysis addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.9") diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/MenuIO.scala b/src/main/scala/io/github/sentenza/hacktoberfest/MenuIO.scala index 1d3b381..a326ac3 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/MenuIO.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/MenuIO.scala @@ -1,13 +1,15 @@ package io.github.sentenza.hacktoberfest -import System.out.println import scala.annotation.tailrec -import scala.util.{Try, Success} -import io.github.sentenza.hacktoberfest.algos.sort.MutableSorting -import io.github.sentenza.hacktoberfest.algos.sort.ImmutableSorting -import io.github.sentenza.hacktoberfest.algos.sort.Sorting import scala.reflect.runtime.universe._ import scala.reflect.ClassTag +import scala.util.Success +import scala.util.Try + +import io.github.sentenza.hacktoberfest.algos.sort.ImmutableSorting +import io.github.sentenza.hacktoberfest.algos.sort.MutableSorting +import io.github.sentenza.hacktoberfest.algos.sort.Sorting +import System.out.println /* * HacktoberFest - Scala Algorithms @@ -43,9 +45,10 @@ object MenuIO { https://github.com/sentenza/hacktoberfest-scala-algorithms/blob/master/LICENSE. """ - /** This function should be called at the very beginning of the Main execution to fetch the - * disclaimer message and the project Logo to be printed out - */ + /** + * This function should be called at the very beginning of the Main execution to fetch the + * disclaimer message and the project Logo to be printed out + */ def printDisclaimer(): Unit = { println(heading + gplDisclaimer) } def readNumberInputs(): Array[Int] = { @@ -117,14 +120,16 @@ object MenuIO { @tailrec def renderInteractiveMenu(entries: List[MenuEntry] = rootEntries): Unit = { println("Please choose:") - entries.sortBy(_.selector).foreach { case MenuEntry(num, label, _) => - println(s"$num: $label") + entries.sortBy(_.selector).foreach { + case MenuEntry(num, label, _) => + println(s"$num: $label") } Try(scala.io.StdIn.readInt()) match { case Success(choice) if entries.exists(_.selector == choice) => - entries.find(_.selector == choice).foreach { case MenuEntry(_, _, code) => - code() + entries.find(_.selector == choice).foreach { + case MenuEntry(_, _, code) => + code() } renderInteractiveMenu() case _ => @@ -139,12 +144,13 @@ object MenuIO { )(implicit show: Show[F[T]], ftt: TypeTag[F[T]]) = { val (_, entries) = collectSortMethods[S, F[T]](sorting) - .foldLeft(1 -> List.empty[MenuEntry]) { case ((count, entries), (sortName, sortFunction)) => - count + 1 -> (entries :+ MenuEntry( - count, - sortName, - () => executeSort(sortName, sortFunction, toSort) - )) + .foldLeft(1 -> List.empty[MenuEntry]) { + case ((count, entries), (sortName, sortFunction)) => + count + 1 -> (entries :+ MenuEntry( + count, + sortName, + () => executeSort(sortName, sortFunction, toSort) + )) } entries diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/adt/Queue.scala b/src/main/scala/io/github/sentenza/hacktoberfest/adt/Queue.scala index 7e02eb1..c3f1a67 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/adt/Queue.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/adt/Queue.scala @@ -15,16 +15,17 @@ package io.github.sentenza.hacktoberfest.adt * along with this program. If not, see . */ -/** Functional Queue Defines the generic type {{{Queue[T]}}} - * - * @see - * Programming in Scala (2nd edition) Ch. 19.7 Note that Queue is covariant in T - * [[https://sentenza.github.io/docs/scala/#variance Read about variance]] A *fully persistent* - * data structure with three operations: - * - head: returns the first element of the queue - * - tail: returns a queue without the first element - * - enqueue: returns a NEW queue with a given element appended to the end - */ +/** + * Functional Queue Defines the generic type {{{Queue[T]}}} + * + * @see + * Programming in Scala (2nd edition) Ch. 19.7 Note that Queue is covariant in T + * [[https://sentenza.github.io/docs/scala/#variance Read about variance]] A *fully persistent* + * data structure with three operations: + * - head: returns the first element of the queue + * - tail: returns a queue without the first element + * - enqueue: returns a NEW queue with a given element appended to the end + */ trait Queue[+T] { /** @return The first element of the queue */ @@ -33,33 +34,38 @@ trait Queue[+T] { /** @return A queue without the first element */ def tail: Queue[T] - /** Appends and element x to the end of the queue - * @param x - * The element to be appended to the end - * @tparam U - * We need to define T as the LOWER BOUND of U, because the type of the function parameters - * must be in contravariant position - * @return - * A new queue containing x at its end - */ + /** + * Appends and element x to the end of the queue + * @param x + * The element to be appended to the end + * @tparam U + * We need to define T as the LOWER BOUND of U, because the type of the function parameters + * must be in contravariant position + * @return + * A new queue containing x at its end + */ def enqueue[U >: T](x: U): Queue[U] - /** Converts the queue in a List - */ + /** + * Converts the queue in a List + */ def toList: List[T] - /** @param other - * Another Queue - */ + /** + * @param other + * Another Queue + */ def equals[U >: T](other: Queue[U]): Boolean - /** @return - * True if the queue is empty - */ + /** + * @return + * True if the queue is empty + */ def isEmpty: Boolean - /** !Queue.isEmtpy() - */ + /** + * !Queue.isEmtpy() + */ def nonEmpty: Boolean def size: Int @@ -67,32 +73,35 @@ trait Queue[+T] { override def toString: String } -/** The companion object Queue. - * @example - * {{{Queue(13, 21, 34, 55, 89, 144)}}} - */ +/** + * The companion object Queue. + * @example + * {{{Queue(13, 21, 34, 55, 89, 144)}}} + */ object Queue { /** Factory method */ def apply[T](xs: T*): Queue[T] = new QueueImpl[T](xs.toList, Nil) - /** This queue is still a pure functional object, even though we're using INTERNALLY reassignable - * immutable fields - * @param leading - * The start fo the queue - * @param trailing - * The end of the queue - */ + /** + * This queue is still a pure functional object, even though we're using INTERNALLY reassignable + * immutable fields + * @param leading + * The start fo the queue + * @param trailing + * The end of the queue + */ private class QueueImpl[+T]( private[this] var leading: List[T], private[this] var trailing: List[T] ) extends Queue[T] { - /** This is an impure function but we need it in order to minimise the complexity of reversing - * an immutable list and then copying across all the elements to have at each moment: queue = - * leading ::: (trailing.reverse) - */ + /** + * This is an impure function but we need it in order to minimise the complexity of reversing + * an immutable list and then copying across all the elements to have at each moment: queue = + * leading ::: (trailing.reverse) + */ private def mirror(): Unit = if (leading.isEmpty) { while (trailing.nonEmpty) { @@ -136,13 +145,14 @@ object Queue { def size: Int = leading.size + trailing.size - /** @param separator - * The String separator (e.g. ",", ":" - * @param l - * The leading list - * @param t - * The trailing list - */ + /** + * @param separator + * The String separator (e.g. ",", ":" + * @param l + * The leading list + * @param t + * The trailing list + */ private def buildPrint[U >: T](separator: String)(l: List[U])(t: List[U]): String = t match { case Nil => l.mkString("Queue(", separator, ")") case _ => { diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/adt/Tree.scala b/src/main/scala/io/github/sentenza/hacktoberfest/adt/Tree.scala index 6f06986..feb1f9f 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/adt/Tree.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/adt/Tree.scala @@ -15,54 +15,59 @@ package io.github.sentenza.hacktoberfest.adt * along with this program. If not, see . */ -/** Represents a simple Binary Tree data structure - */ +/** + * Represents a simple Binary Tree data structure + */ sealed trait Tree[+T] case class Leaf[T](value: T) extends Tree[T] -/** A branch will contain only two pointers - * @param left - * The left recursive Tree - * @param right - * The right recursive Tree - */ +/** + * A branch will contain only two pointers + * @param left + * The left recursive Tree + * @param right + * The right recursive Tree + */ case class Branch[T](left: Tree[T], right: Tree[T]) extends Tree[T] object Tree { - /** Using pattern matching it returns the size of the Tree - * @param tree - * The Tree to compute the size of - * @return - * the number of nodes of a Tree - */ + /** + * Using pattern matching it returns the size of the Tree + * @param tree + * The Tree to compute the size of + * @return + * the number of nodes of a Tree + */ def size[T](tree: Tree[T]): Int = tree match { case _: Leaf[T] => 1 // A Leaf counts 1 case Branch(l, r) => 1 + size(l) + size(r) // A Branch counts 1 } - /** Returns the maximum depth of the Tree recursively - * @param tree - * The tree to work on - * @return - * the maximum path length from the root of a Tree to any leaf - */ + /** + * Returns the maximum depth of the Tree recursively + * @param tree + * The tree to work on + * @return + * the maximum path length from the root of a Tree to any leaf + */ def depth[T](tree: Tree[T]): Int = tree match { case _: Leaf[T] => 0 // A Leaf counts 0 case Branch(l, r) => 1 + (if (depth(l) > depth(r)) depth(l) else depth(r)) // A Branch counts 1 } - /** Returns a Tree with all the values replaced with the given function - * @param tree - * The tree to work on - * @param f - * The function to be applied to each leaf of the Tree - * @tparam F - * The type of the element that will be returned by f - * @return - * A new Tree which will be made by applying f to the original Tree - */ + /** + * Returns a Tree with all the values replaced with the given function + * @param tree + * The tree to work on + * @param f + * The function to be applied to each leaf of the Tree + * @tparam F + * The type of the element that will be returned by f + * @return + * A new Tree which will be made by applying f to the original Tree + */ def map[T, F](tree: Tree[T], f: (T) => F): Tree[F] = tree match { case Leaf(v) => Leaf(f(v)) case Branch(l, r) => Branch(map(l, f), map(r, f)) diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/algos/loopless/LooplessFunctional.scala b/src/main/scala/io/github/sentenza/hacktoberfest/algos/loopless/LooplessFunctional.scala index 48e3d8b..8acd715 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/algos/loopless/LooplessFunctional.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/algos/loopless/LooplessFunctional.scala @@ -15,19 +15,21 @@ package io.github.sentenza.hacktoberfest.algos.loopless * along with this program. If not, see . */ -/** Provides implementation of the algorithms and data structures originally presented in Jamie - * Snape's Master Thesis "Loopless Functional Algorithms" available from - * https://wwwx.cs.unc.edu/~snape/publications/msc/ - */ +/** + * Provides implementation of the algorithms and data structures originally presented in Jamie + * Snape's Master Thesis "Loopless Functional Algorithms" available from + * https://wwwx.cs.unc.edu/~snape/publications/msc/ + */ object LooplessFunctional { - /** Builds a list from a particular value This is considered the principal building block of - * loopless algorithms in the mentioned work - * - * While the function takes a single step operation, in all the sequent algorithms the step is - * actually a composition of `step compose prologue``, where prologue should take `O(n)` time and - * the step `O(1)` - */ + /** + * Builds a list from a particular value This is considered the principal building block of + * loopless algorithms in the mentioned work + * + * While the function takes a single step operation, in all the sequent algorithms the step is + * actually a composition of `step compose prologue``, where prologue should take `O(n)` time and + * the step `O(1)` + */ def unfoldr[A, B](step: B => Option[(A, B)])(b: B): List[A] = step(b) match { case Some((x, y)) => x :: unfoldr(step)(y) @@ -53,7 +55,7 @@ object LooplessFunctional { // prolog for concat, removes empty nestings val prologue: LL => LL = _.filter(_.nonEmpty) - unfoldr(step compose prologue)(nested) + unfoldr(step.compose(prologue))(nested) } } diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/algos/search/Searching.scala b/src/main/scala/io/github/sentenza/hacktoberfest/algos/search/Searching.scala index 2ce3dc4..4b9e171 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/algos/search/Searching.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/algos/search/Searching.scala @@ -15,8 +15,9 @@ package io.github.sentenza.hacktoberfest.algos.search * along with this program. If not, see . */ -/** Definitions of the functions that will implement all the common searching algorithms. - */ +/** + * Definitions of the functions that will implement all the common searching algorithms. + */ trait Searching { // TO BE DEFINED } diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/algos/select/ImmutableSelection.scala b/src/main/scala/io/github/sentenza/hacktoberfest/algos/select/ImmutableSelection.scala index 14eb327..5905aac 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/algos/select/ImmutableSelection.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/algos/select/ImmutableSelection.scala @@ -15,28 +15,30 @@ package io.github.sentenza.hacktoberfest.algos.select * along with this program. If not, see . */ -/** Implementations of this trait should provide non-destructive select operations on linked lists. - * That is, the linked list passed to a select method should not be altered: A value at the - * specified sorted index should be returned. - */ +/** + * Implementations of this trait should provide non-destructive select operations on linked lists. + * That is, the linked list passed to a select method should not be altered: A value at the + * specified sorted index should be returned. + */ object ImmutableSelection extends Selection[List, Int] { - /** @inheritdoc - * - * @param list - * Linked list of sortable, selectable integers - * @param idx - * An sorted index, that is, an index referring to the nth largest element of the list - * @return - * A possible element of the array at the sorted index - */ + /** + * @inheritdoc + * + * @param list + * Linked list of sortable, selectable integers + * @param idx + * An sorted index, that is, an index referring to the nth largest element of the list + * @return + * A possible element of the array at the sorted index + */ def quickSelect(list: List[Int], idx: Int): Option[Int] = { if (idx < 0 || list.size <= idx) return None list match { case Nil => None case pivot :: rest => { - val (smaller, larger) = rest partition (_ <= pivot) + val (smaller, larger) = rest.partition(_ <= pivot) val pivotIdx = smaller.size idx.compare(pivotIdx) match { diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/algos/select/Selection.scala b/src/main/scala/io/github/sentenza/hacktoberfest/algos/select/Selection.scala index 3e3d5f1..9c71bca 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/algos/select/Selection.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/algos/select/Selection.scala @@ -15,12 +15,14 @@ package io.github.sentenza.hacktoberfest.algos.select * along with this program. If not, see . */ -/** Definitions of the functions that will implement all the common selection algorithms. - */ +/** + * Definitions of the functions that will implement all the common selection algorithms. + */ trait Selection[F[_], T] { /* *** Partition-based Selection *** */ - /** Quick Select [[https://en.wikipedia.org/wiki/Quickselect Wikipedia: Quick Select]] - */ + /** + * Quick Select [[https://en.wikipedia.org/wiki/Quickselect Wikipedia: Quick Select]] + */ def quickSelect(xs: F[T], idx: Int): Option[T] } diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/ImmutableSorting.scala b/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/ImmutableSorting.scala index 2d342ac..dc0d8d6 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/ImmutableSorting.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/ImmutableSorting.scala @@ -17,18 +17,20 @@ import scala.annotation.tailrec * along with this program. If not, see . */ -/** Implementations of this trait should provide non-destructive sort operations on lists, returning - * a sorted copy of the provided input list. - */ +/** + * Implementations of this trait should provide non-destructive sort operations on lists, returning + * a sorted copy of the provided input list. + */ object ImmutableSorting extends Sorting[List, Int] { - /** @inheritdoc - * - * @param xs - * List of sortable Integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * + * @param xs + * List of sortable Integers + * @return + * The sorted list + */ def bubbleSort(xs: List[Int]): List[Int] = { def bubbled(list: List[Int]): (List[Int], Boolean) = list match { case Nil => (Nil, false) @@ -57,65 +59,72 @@ object ImmutableSorting extends Sorting[List, Int] { fullyBubbled } - /** @inheritdoc - * - * @param xs - * List of sortable Integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * + * @param xs + * List of sortable Integers + * @return + * The sorted list + */ def cocktailShakerSort(xs: List[Int]): List[Int] = ??? - /** @inheritdoc - * - * @param xs - * List of sortable Integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * + * @param xs + * List of sortable Integers + * @return + * The sorted list + */ def combSort(xs: List[Int]): List[Int] = ??? - /** @inheritdoc - * @param list - * List of sortable integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * @param list + * List of sortable integers + * @return + * The sorted list + */ def insertionSort(list: List[Int]): List[Int] = ??? - /** @inheritdoc - * @param list - * List of sortable integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * @param list + * List of sortable integers + * @return + * The sorted list + */ def selectionSort(list: List[Int]): List[Int] = ??? - /** @inheritdoc - * - * @param xs - * List of sortable Integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * + * @param xs + * List of sortable Integers + * @return + * The sorted list + */ def heapSort(xs: List[Int]): List[Int] = ??? - /** @inheritdoc - * - * @param xs - * List of sortable Integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * + * @param xs + * List of sortable Integers + * @return + * The sorted list + */ def quickSort(list: List[Int]): List[Int] = ??? - /** @inheritdoc - * - * @param list - * List of sortable Integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * + * @param list + * List of sortable Integers + * @return + * The sorted list + */ def mergeSort(list: List[Int]): List[Int] = { @tailrec // (1,3,2,6,4) => ( (4), (6, 2), (3, 1) ) @@ -187,34 +196,37 @@ object ImmutableSorting extends Sorting[List, Int] { merger(splitList, isForward = false, Nil) } - /** @inheritdoc - * - * @param xs - * List of sortable Integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * + * @param xs + * List of sortable Integers + * @return + * The sorted list + */ def bucketSort( xs: List[Int], n: Int = 10, sort: List[Int] => List[Int] = insertionSort ): List[Int] = ??? - /** @inheritdoc - * - * @param xs - * List of sortable Integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * + * @param xs + * List of sortable Integers + * @return + * The sorted list + */ def countSort(xs: List[Int]): List[Int] = ??? - /** @inheritdoc - * - * @param xs - * List of sortable Integers - * @return - * The sorted list - */ + /** + * @inheritdoc + * + * @param xs + * List of sortable Integers + * @return + * The sorted list + */ def radixSort(xs: List[Int], base: Int): List[Int] = ??? } diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/MutableSorting.scala b/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/MutableSorting.scala index 8b83455..2fae96f 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/MutableSorting.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/MutableSorting.scala @@ -17,28 +17,30 @@ import scala.collection.mutable.ArrayBuffer * along with this program. If not, see . */ -/** Implementations of this trait should provide non-destructive sort operations on arrays. That is, - * the array passed to a sort method should not be altered: A new array with the sorted values - * should be returned. - */ +/** + * Implementations of this trait should provide non-destructive sort operations on arrays. That is, + * the array passed to a sort method should not be altered: A new array with the sorted values + * should be returned. + */ object MutableSorting extends Sorting[Array, Int] { - /** @inheritdoc - * - * "the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that - * it leads to some interesting theoretical problems" - * - from The Art of Computer Programming by Donald Knuth NOTE: Although bubble sort is one of - * the simplest sorting algorithms to understand and implement, its {{{O(n^2)}}} complexity - * means that its efficiency decreases dramatically on lists of more than a small number of - * elements. TODO: Try to refactor this method in order to avoid any direct alteration (side - * effect) of the provided array. We should return a completely new array that contains the - * sorted elements. - * - * @param xs - * Array of sortable Integers - * @return - * The sorted array - */ + /** + * @inheritdoc + * + * "the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that + * it leads to some interesting theoretical problems" + * - from The Art of Computer Programming by Donald Knuth NOTE: Although bubble sort is one of + * the simplest sorting algorithms to understand and implement, its {{{O(n^2)}}} complexity + * means that its efficiency decreases dramatically on lists of more than a small number of + * elements. TODO: Try to refactor this method in order to avoid any direct alteration (side + * effect) of the provided array. We should return a completely new array that contains the + * sorted elements. + * + * @param xs + * Array of sortable Integers + * @return + * The sorted array + */ def bubbleSort(xs: Array[Int]): Array[Int] = { var swapped = false @@ -55,13 +57,14 @@ object MutableSorting extends Sorting[Array, Int] { xs } - /** @inheritdoc - * - * @param array - * Array of sortable integers - * @return - * The sorted array - */ + /** + * @inheritdoc + * + * @param array + * Array of sortable integers + * @return + * The sorted array + */ def cocktailShakerSort(array: Array[Int]): Array[Int] = { var (start, end) = (0, array.length - 2) while (start <= end) { @@ -86,12 +89,13 @@ object MutableSorting extends Sorting[Array, Int] { array } - /** @inheritdoc - * @param array - * Array of sortable integers - * @return - * The sorted array - */ + /** + * @inheritdoc + * @param array + * Array of sortable integers + * @return + * The sorted array + */ def combSort(array: Array[Int]): Array[Int] = ??? // TODO: Fix Comb sort! /* @@ -129,12 +133,13 @@ import scala.annotation.tailrec } */ - /** @inheritdoc - * @param array - * Array of sortable integers - * @return - * The sorted array - */ + /** + * @inheritdoc + * @param array + * Array of sortable integers + * @return + * The sorted array + */ def insertionSort(array: Array[Int]): Array[Int] = { for (j <- 1 until array.length) { var i = j - 1 @@ -149,12 +154,13 @@ import scala.annotation.tailrec array } - /** @inheritdoc - * @param array - * Array of sortable integers - * @return - * The sorted array - */ + /** + * @inheritdoc + * @param array + * Array of sortable integers + * @return + * The sorted array + */ def selectionSort(array: Array[Int]): Array[Int] = { for (i <- 0 until array.size - 1) swap( @@ -171,39 +177,42 @@ import scala.annotation.tailrec array } - /** Heap Sort - * - * @param xs - * Array of sortable integers - * @return - * The sorted array - */ + /** + * Heap Sort + * + * @param xs + * Array of sortable integers + * @return + * The sorted array + */ def heapSort(xs: Array[Int]): Array[Int] = ??? - /** @inheritdoc - * @param array - * Array of sortable integers - * @return - * The sorted array - */ + /** + * @inheritdoc + * @param array + * Array of sortable integers + * @return + * The sorted array + */ def quickSort(array: Array[Int]): Array[Int] = { if (array.length <= 1) array else { val pivot = array(array.length / 2) Array.concat( - quickSort(array filter (x => pivot > x)), - array filter (x => pivot == x), - quickSort(array filter (x => pivot < x)) + quickSort(array.filter(x => pivot > x)), + array.filter(x => pivot == x), + quickSort(array.filter(x => pivot < x)) ) } } - /** @inheritdoc - * @param array - * Array of sortable integers - * @return - * The sorted array - */ + /** + * @inheritdoc + * @param array + * Array of sortable integers + * @return + * The sorted array + */ def mergeSort(array: Array[Int]): Array[Int] = { def merge(part1: Array[Int], part2: Array[Int], l: Int, r: Int, mid: Int): Unit = { var left = l @@ -236,21 +245,22 @@ import scala.annotation.tailrec array } - /** Bucket sort is a sorting algorithm that sorts an array of elements by splitting the elements - * into n buckets and then reapplies another sorting method on the different buckets. Then merges - * the sorted buckets. It can also be used recursively until buckets contain one element each. - * - * @param xs - * Array of sortable integers - * @param n - * number of buckets in which to sort the elements. - * @param sort - * The sorting algorithm to apply once buckets are divided. - * @return - * the sorted array - * @author - * Xoeseko - */ + /** + * Bucket sort is a sorting algorithm that sorts an array of elements by splitting the elements + * into n buckets and then reapplies another sorting method on the different buckets. Then merges + * the sorted buckets. It can also be used recursively until buckets contain one element each. + * + * @param xs + * Array of sortable integers + * @param n + * number of buckets in which to sort the elements. + * @param sort + * The sorting algorithm to apply once buckets are divided. + * @return + * the sorted array + * @author + * Xoeseko + */ def bucketSort( xs: Array[Int], n: Int = 10, @@ -269,18 +279,22 @@ import scala.annotation.tailrec val finalArray = new ArrayBuffer[Int]() - for (i <- buckets.indices; j <- buckets(i).indices) { + for { + i <- buckets.indices + j <- buckets(i).indices + } { finalArray += buckets(i)(j) } finalArray.toArray } - /** @inheritdoc - * @param xs - * Array of sortable integers - * @return - * The sorted array - */ + /** + * @inheritdoc + * @param xs + * Array of sortable integers + * @return + * The sorted array + */ def countSort(xs: Array[Int]): Array[Int] = { if (xs.length <= 1) xs else { @@ -307,17 +321,18 @@ import scala.annotation.tailrec } - /** Radix Sort is a sorting algorithm that sorts an array of numbers into according to each - * number's most significant symbols (bits, digits...) depending on the given base - * @param xs - * an array to be sorted - * @param base - * the sorting base and also the number of buckets - * @return - * the sorted array - * @author - * Xoeseko - */ + /** + * Radix Sort is a sorting algorithm that sorts an array of numbers into according to each + * number's most significant symbols (bits, digits...) depending on the given base + * @param xs + * an array to be sorted + * @param base + * the sorting base and also the number of buckets + * @return + * the sorted array + * @author + * Xoeseko + */ def radixSort(xs: Array[Int], base: Int = 10): Array[Int] = { var array = xs def listToBuckets(arr: Array[Int], b: Int, it: Int): Array[Array[Int]] = { @@ -337,7 +352,10 @@ import scala.annotation.tailrec def bucketsToList(buckets: Array[Array[Int]]): Array[Int] = { val numbers = new Array[Int](xs.length) var index = 0 - for (b <- buckets; n <- b) { + for { + b <- buckets + n <- b + } { numbers(index) = n index += 1 } @@ -355,17 +373,18 @@ import scala.annotation.tailrec array } - /** Swaps two elements with indices index1 and index2 in a given array used in bubbleSort and - * selectionSort TODO: get rid of this method, because it only introduces a side effect. Take - * look at the principles of Functional programming. - * - * @param array - * The target array that will be modified - * @param index1 - * The index of the first element that has to be swapped - * @param index2 - * The index of the second element that has to be swapped - */ + /** + * Swaps two elements with indices index1 and index2 in a given array used in bubbleSort and + * selectionSort TODO: get rid of this method, because it only introduces a side effect. Take + * look at the principles of Functional programming. + * + * @param array + * The target array that will be modified + * @param index1 + * The index of the first element that has to be swapped + * @param index2 + * The index of the second element that has to be swapped + */ private def swap(array: Array[Int], index1: Int, index2: Int): Unit = { val tmp = array(index1) array(index1) = array(index2) diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/Sorting.scala b/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/Sorting.scala index db39c51..8df14d9 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/Sorting.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/algos/sort/Sorting.scala @@ -15,75 +15,87 @@ package io.github.sentenza.hacktoberfest.algos.sort * along with this program. If not, see . */ -/** Definitions of the common sorting algorithms. - * - * Note that usually one can claim that the known sorting algorithms would not actually be the same - * if implemented without loops and mutable variables. The reasoning is that you usually lose the - * ability keep the same time-space analysis results. - * - * TODO: find an alternative to: {{{F[_] <: Iterable[_]}}} - * - * [[https://stackoverflow.com/q/6246719/1977778 Higher Kinded Types]] - * - * @tparam F - * A set of iterable elements (Array or Iterable) - * @tparam T - * The type of the elements contained in the set - */ +/** + * Definitions of the common sorting algorithms. + * + * Note that usually one can claim that the known sorting algorithms would not actually be the same + * if implemented without loops and mutable variables. The reasoning is that you usually lose the + * ability keep the same time-space analysis results. + * + * TODO: find an alternative to: {{{F[_] <: Iterable[_]}}} + * + * [[https://stackoverflow.com/q/6246719/1977778 Higher Kinded Types]] + * + * @tparam F + * A set of iterable elements (Array or Iterable) + * @tparam T + * The type of the elements contained in the set + */ trait Sorting[F[_], T] { /* ****** Exchange Sorts ****** */ - /** Bubble Sort [[http://en.wikipedia.org/wiki/Bubble_sort Wikipedia: Bubble Sort]] - */ + /** + * Bubble Sort [[http://en.wikipedia.org/wiki/Bubble_sort Wikipedia: Bubble Sort]] + */ def bubbleSort(xs: F[T]): F[T] - /** Cocktail Shaker Sort - * [[https://en.wikipedia.org/wiki/Cocktail_shaker_sort Wikipedia: Cocktail Shaker Sort]] - */ + /** + * Cocktail Shaker Sort + * [[https://en.wikipedia.org/wiki/Cocktail_shaker_sort Wikipedia: Cocktail Shaker Sort]] + */ def cocktailShakerSort(xs: F[T]): F[T] - /** Comb Sort [[https://en.wikipedia.org/wiki/Comb_sort Wikipedia: Comb Sort]] - */ + /** + * Comb Sort [[https://en.wikipedia.org/wiki/Comb_sort Wikipedia: Comb Sort]] + */ def combSort(xs: F[T]): F[T] - /** Quick Sort [[https://en.wikipedia.org/wiki/Quicksort Wikipedia: Quick Sort]] - */ + /** + * Quick Sort [[https://en.wikipedia.org/wiki/Quicksort Wikipedia: Quick Sort]] + */ def quickSort(xs: F[T]): F[T] /* ****** Selection Sorts ****** */ - /** Selection Sort [[https://en.wikipedia.org/wiki/Selection_sort Wikipedia: Selection Sort]] - */ + /** + * Selection Sort [[https://en.wikipedia.org/wiki/Selection_sort Wikipedia: Selection Sort]] + */ def selectionSort(xs: F[T]): F[T] - /** Heap Sort [[https://en.wikipedia.org/wiki/Heapsort Wikipedia: Heap Sort]] - */ + /** + * Heap Sort [[https://en.wikipedia.org/wiki/Heapsort Wikipedia: Heap Sort]] + */ def heapSort(xs: F[T]): F[T] /* ****** Insertion Sorts ****** */ - /** Insertion Sort [[https://en.wikipedia.org/wiki/Insertion_sort Wikipedia: Insertion Sort]] - */ + /** + * Insertion Sort [[https://en.wikipedia.org/wiki/Insertion_sort Wikipedia: Insertion Sort]] + */ def insertionSort(xs: F[T]): F[T] /* ****** Merge Sorts ****** */ - /** Merge Sort [[https://en.wikipedia.org/wiki/Merge_sort Wikipedia: Merge Sort]] - */ + /** + * Merge Sort [[https://en.wikipedia.org/wiki/Merge_sort Wikipedia: Merge Sort]] + */ def mergeSort(xs: F[T]): F[T] /* ****** Distribution Sorts ****** */ - /** Bucket Sort - */ + /** + * Bucket Sort + */ def bucketSort(xs: F[T], n: Int, sort: F[T] => F[T]): F[T] - /** Count Sort - */ + /** + * Count Sort + */ def countSort(xs: F[T]): F[T] - /** Radix Sort - */ + /** + * Radix Sort + */ def radixSort(xs: F[T], base: Int): F[T] } diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/algos/strings/StringAlgos.scala b/src/main/scala/io/github/sentenza/hacktoberfest/algos/strings/StringAlgos.scala index 32acb75..4c4ec57 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/algos/strings/StringAlgos.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/algos/strings/StringAlgos.scala @@ -18,14 +18,15 @@ package io.github.sentenza.hacktoberfest.algos.strings /** Definitions of common algorithms applied to strings. */ trait StringAlgos { - /** Checks if the first string is the anagram of the second one - * - * @param first - * First string - * @param second - * Second string - * @return - */ + /** + * Checks if the first string is the anagram of the second one + * + * @param first + * First string + * @param second + * Second string + * @return + */ def checkAnagrams(first: String, second: String): Boolean } diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/algos/strings/StringUtil.scala b/src/main/scala/io/github/sentenza/hacktoberfest/algos/strings/StringUtil.scala index d4d26f6..67f39b7 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/algos/strings/StringUtil.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/algos/strings/StringUtil.scala @@ -18,14 +18,15 @@ package io.github.sentenza.hacktoberfest.algos.strings /** Implementation of common algorithms applied to strings. */ object StringUtil extends StringAlgos { - /** Checks if the first string is the anagram of the second one - * - * @param first - * First string - * @param second - * Second string - * @return - */ + /** + * Checks if the first string is the anagram of the second one + * + * @param first + * First string + * @param second + * Second string + * @return + */ override def checkAnagrams(first: String, second: String): Boolean = { first.sorted == second.sorted } diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/util/ArrayUtil.scala b/src/main/scala/io/github/sentenza/hacktoberfest/util/ArrayUtil.scala index 1db3425..e2a5971 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/util/ArrayUtil.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/util/ArrayUtil.scala @@ -15,19 +15,21 @@ package io.github.sentenza.hacktoberfest.util * along with this program. If not, see . */ -/** A list of useful functions to work with arrays - */ +/** + * A list of useful functions to work with arrays + */ class ArrayUtil { - /** Builds an array of random numbers - * - * @param length - * The length of the array to be built - * @param maxElement - * The maximum possible value to assign to an element of the array - * @return - * An array of random integers - */ + /** + * Builds an array of random numbers + * + * @param length + * The length of the array to be built + * @param maxElement + * The maximum possible value to assign to an element of the array + * @return + * An array of random integers + */ def buildRandomArray(length: Int, maxElement: Int = Int.MaxValue / 10): Array[Int] = { val maximum = if (maxElement <= Int.MaxValue / 10) maxElement else Int.MaxValue Array.fill(length) { scala.util.Random.nextInt(maximum) } diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/util/GenericUtil.scala b/src/main/scala/io/github/sentenza/hacktoberfest/util/GenericUtil.scala index a65ee49..7ca8b19 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/util/GenericUtil.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/util/GenericUtil.scala @@ -2,15 +2,16 @@ package io.github.sentenza.hacktoberfest.util object GenericUtil { - /** Tracks (and prints out) the elapsed time when executing a function (a block of code). - * - * Usage: val sorted = GenericUtil.time { MutableSorting.quickSort(randonArray) } - * - * @param block - * By-name block of code - * @return - * The result of the inner operation - */ + /** + * Tracks (and prints out) the elapsed time when executing a function (a block of code). + * + * Usage: val sorted = GenericUtil.time { MutableSorting.quickSort(randonArray) } + * + * @param block + * By-name block of code + * @return + * The result of the inner operation + */ def time[R](block: => R): R = { val t0 = System.nanoTime() val result = block diff --git a/src/main/scala/io/github/sentenza/hacktoberfest/util/ListUtil.scala b/src/main/scala/io/github/sentenza/hacktoberfest/util/ListUtil.scala index e8b8f95..c6324e4 100644 --- a/src/main/scala/io/github/sentenza/hacktoberfest/util/ListUtil.scala +++ b/src/main/scala/io/github/sentenza/hacktoberfest/util/ListUtil.scala @@ -17,17 +17,19 @@ import scala.util.Random * along with this program. If not, see . */ -/** A list of useful functions to work with linked lists - */ +/** + * A list of useful functions to work with linked lists + */ class ListUtil { - /** Builds a list of random numbers - * - * @param length - * The length of the list to be built - * @return - * An array of random integers - */ + /** + * Builds a list of random numbers + * + * @param length + * The length of the list to be built + * @return + * An array of random integers + */ def buildRandomList(length: Int): List[Int] = { val rand = new Random() List.fill(length)(rand.nextInt(length)) diff --git a/src/test/scala/io/github/sentenza/hacktoberfest/adt/QueueSpec.scala b/src/test/scala/io/github/sentenza/hacktoberfest/adt/QueueSpec.scala index 7ccf85d..e8d84e8 100644 --- a/src/test/scala/io/github/sentenza/hacktoberfest/adt/QueueSpec.scala +++ b/src/test/scala/io/github/sentenza/hacktoberfest/adt/QueueSpec.scala @@ -23,7 +23,7 @@ class QueueSpec extends AnyWordSpec with Matchers { "have a tail method that returns a new Queue without its head" in { fiboQueue.tail shouldBe a[Queue[_]] - fiboQueue.tail equals Queue(21, 34, 55, 89, 144, 233) + fiboQueue.tail.equals(Queue(21, 34, 55, 89, 144, 233)) fiboQueue.tail.tail.tail.tail.tail.tail.head shouldBe 233 } @@ -32,9 +32,9 @@ class QueueSpec extends AnyWordSpec with Matchers { val q2 = q1.enqueue(21) val q3 = q2.enqueue(44).enqueue(77) q3 shouldBe a[Queue[_]] - val qChar = Queue(Symbol("a")).enqueue('b') + val qChar = Queue(Symbol("a")).enqueue(Symbol("b")) qChar shouldBe a[Queue[_]] - q2 equals Queue(13, 77, 21) + q2.equals(Queue(13, 77, 21)) } "test for emptiness using isEmpty" in { diff --git a/src/test/scala/io/github/sentenza/hacktoberfest/adt/TreeSpec.scala b/src/test/scala/io/github/sentenza/hacktoberfest/adt/TreeSpec.scala index 36beb33..ae6f332 100644 --- a/src/test/scala/io/github/sentenza/hacktoberfest/adt/TreeSpec.scala +++ b/src/test/scala/io/github/sentenza/hacktoberfest/adt/TreeSpec.scala @@ -1,6 +1,7 @@ package io.github.sentenza.hacktoberfest.adt -import io.github.sentenza.hacktoberfest.adt.{Branch => B, Leaf => L} +import io.github.sentenza.hacktoberfest.adt.{ Branch => B } +import io.github.sentenza.hacktoberfest.adt.{ Leaf => L } import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec @@ -56,8 +57,8 @@ class TreeSpec extends AnyWordSpec with Matchers { } "return the sum of depths for a Tree of Trees" in { - val t = B( - L(1), + val t: Branch[String] = B( + L("1"), treeD2 ) Tree.depth(t) shouldBe 3 diff --git a/src/test/scala/io/github/sentenza/hacktoberfest/algos/select/ImmutableSelectionSpec.scala b/src/test/scala/io/github/sentenza/hacktoberfest/algos/select/ImmutableSelectionSpec.scala index 2db57eb..136b304 100644 --- a/src/test/scala/io/github/sentenza/hacktoberfest/algos/select/ImmutableSelectionSpec.scala +++ b/src/test/scala/io/github/sentenza/hacktoberfest/algos/select/ImmutableSelectionSpec.scala @@ -1,30 +1,32 @@ package io.github.sentenza.hacktoberfest.algos.select +import scala.util.Random + import io.github.sentenza.hacktoberfest.algos.select.ImmutableSelection._ import io.github.sentenza.hacktoberfest.util.ListUtil import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec -import scala.util.Random - class ImmutableSelectionSpec extends AnyWordSpec with Matchers { class TestCase { - /** The default length of the arrays that will be generated in this spec. - */ + /** + * The default length of the arrays that will be generated in this spec. + */ protected val random = new Random() protected val defaultLength: Int = Math.max(100, random.nextInt(1000)) protected val listUtil = new ListUtil() protected val randomIndices: Seq[Int] = List.fill(5)(random.nextInt(defaultLength)).distinct - /** Helper method that returns a couple of array (unsorted, sorted). - * - * @param l - * The length of the array that will be generated - * @return - * (unsortedArray, sortedArray) - */ + /** + * Helper method that returns a couple of array (unsorted, sorted). + * + * @param l + * The length of the array that will be generated + * @return + * (unsortedArray, sortedArray) + */ protected def getLists(l: Int): (List[Int], List[Int]) = { val randomUnsortedList = listUtil.buildRandomList(l) (randomUnsortedList, randomUnsortedList.sorted) diff --git a/src/test/scala/io/github/sentenza/hacktoberfest/algos/sort/ImmutableSortingSpec.scala b/src/test/scala/io/github/sentenza/hacktoberfest/algos/sort/ImmutableSortingSpec.scala index a7f9d05..5e5e5d4 100644 --- a/src/test/scala/io/github/sentenza/hacktoberfest/algos/sort/ImmutableSortingSpec.scala +++ b/src/test/scala/io/github/sentenza/hacktoberfest/algos/sort/ImmutableSortingSpec.scala @@ -1,27 +1,29 @@ package io.github.sentenza.hacktoberfest.algos.sort +import scala.util.Random + import io.github.sentenza.hacktoberfest.algos.sort.ImmutableSorting._ import io.github.sentenza.hacktoberfest.util.ListUtil import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec -import scala.util.Random - class ImmutableSortingSpec extends AnyWordSpec with Matchers { - /** The default length of the arrays that will be generated in this spec. - */ + /** + * The default length of the arrays that will be generated in this spec. + */ private val random = new Random() private val defaultLength = Math.max(100, random.nextInt(1000)) private val listUtil = new ListUtil() - /** Helper method that returns a couple of array (unsorted, sorted). - * - * @param l - * The length of the array that will be generated - * @return - * (unsortedArray, sortedArray) - */ + /** + * Helper method that returns a couple of array (unsorted, sorted). + * + * @param l + * The length of the array that will be generated + * @return + * (unsortedArray, sortedArray) + */ private def getLists(l: Int): (List[Int], List[Int]) = { val randomUnsortedList = listUtil.buildRandomList(l) (randomUnsortedList, randomUnsortedList.sorted) diff --git a/src/test/scala/io/github/sentenza/hacktoberfest/algos/sort/MutableSortingSpec.scala b/src/test/scala/io/github/sentenza/hacktoberfest/algos/sort/MutableSortingSpec.scala index a979e84..b23d0e0 100644 --- a/src/test/scala/io/github/sentenza/hacktoberfest/algos/sort/MutableSortingSpec.scala +++ b/src/test/scala/io/github/sentenza/hacktoberfest/algos/sort/MutableSortingSpec.scala @@ -1,29 +1,31 @@ package io.github.sentenza.hacktoberfest.algos.sort +import scala.util.Random + import io.github.sentenza.hacktoberfest.algos.sort.MutableSorting._ import io.github.sentenza.hacktoberfest.util.ArrayUtil import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec -import scala.util.Random - class MutableSortingSpec extends AnyWordSpec with Matchers { - /** The default length of the arrays that will be generated in this spec - */ + /** + * The default length of the arrays that will be generated in this spec + */ private val defaultLength = new Random().nextInt(100) private val defaultMax = new Random().nextInt(99) private val arrayUtil = new ArrayUtil() - /** Helper method that returns a couple of array (unsorted, sorted) - * - * @param l - * The length of the array that will be generated - * @param m - * The maximum possible element value - * @return - * (unsortedArray, sortedArray) - */ + /** + * Helper method that returns a couple of array (unsorted, sorted) + * + * @param l + * The length of the array that will be generated + * @param m + * The maximum possible element value + * @return + * (unsortedArray, sortedArray) + */ private def getArrays(l: Int, m: Int): (Array[Int], Array[Int]) = { val randomUnsortedArray = arrayUtil.buildRandomArray(l, m) (randomUnsortedArray, randomUnsortedArray.sorted)