<commit_before>package ru.permutation

import collection.mutable.ListBuffer
import org.junit.{Test, Before}
import org.junit.Assert._
import org.scalatest.junit.{JUnitSuite, AssertionsForJUnit, ShouldMatchersForJUnit}

/**
 * User: DKandalov
 */
class Perm1 extends JUnitSuite with ShouldMatchersForJUnit
{
  @Test def shouldCreateListOfAllPermutations() // TODO
  {
    assert(perm(Array()).toList === Array(Array()).toList)
//    assertEquals(perm(Array(1)), Array(Array(1)))
//    assert(perm(Array(1, 2)) === Array(Array(1, 2), Array(2, 1)))
//    println(perm(Array()).toString)
//    println perm(Array(1)).toString
//    println perm(Array(1,2)).toString
    // 123, 312, 132
  }

  def perm(list: Array[Int]): Array[Array[Int]] =
  {
    if (list.size == 0) return Array(Array())
    if (list.size == 1) return Array(list)


    var result = new Array[Array[Int]](0)

    1.to(list.size).foreach { i :Int =>
      val split = list.splitAt(i)
//      var subResult = new Array[Any](0)
      perm(split._1 ++ split._2.tail).foreach { a : Array[Int] =>
        result = result ++ Array(Array(split._2.head) ++ a)
      }
    }

    result
  }

/*
  @Test def aaa()
  {
    assertArrayEquals(Array(2, 1), swapRight(Array(1, 2), 1))
    assertArrayEquals(Array(3, 1, 2), swapRight(Array(1, 2, 3), 2))
    assertArrayEquals(Array(1, 3, 2), swapRight(Array(3, 1, 2), 2))
  }
*/

  def swapRight(list: Array[Int], position: Int): Array[Int] =
  {
    var nextPosition = position + 1
    if (nextPosition >= list.size) nextPosition -= list.size

    Array()
  }

  @Test def shouldCalculateFactorial()
  {
    assertEquals(fact(0), 0)
    assertEquals(fact(1), 1)
    assertEquals(fact(2), 2)
    assertEquals(fact(3), 6)
    assertEquals(fact(4), 24)
  }

  def fact(i: Int): Int =
  {
    if (i < 0) return -1
    if (i < 3) return i
    fact(i - 1) * i
  }<commit_msg>permutation 1 scala<commit_after>package ru.permutation

import collection.mutable.ListBuffer
import org.junit.Assert._
import org.scalatest.junit.{JUnitSuite, AssertionsForJUnit, ShouldMatchersForJUnit}
import org.junit.{Ignore, Test, Before}
import collection.Seq

/**
 * User: DKandalov
 */
class Perm1 extends JUnitSuite with ShouldMatchersForJUnit {
  @Test def shouldCreateListOfAllPermutations() {
    assert(permutation(ListBuffer()) === ListBuffer(ListBuffer()))
    assert(permutation(ListBuffer(1)) === ListBuffer(ListBuffer(1)))
    assert(permutation(ListBuffer(1, 2)) === ListBuffer(ListBuffer(1, 2), ListBuffer(2, 1)))
    assert(permutation(ListBuffer(1, 2, 3)) === ListBuffer(
      ListBuffer(1, 2, 3), ListBuffer(2, 1, 3), ListBuffer(2, 3, 1),
      ListBuffer(1, 3, 2), ListBuffer(3, 1, 2), ListBuffer(3, 2, 1)
    ))
  }

  def permutation(list: ListBuffer[Int]): ListBuffer[ListBuffer[Int]] =
  {
    if (list.size == 0) return ListBuffer(ListBuffer())
    if (list.size == 1) return ListBuffer(list)

    var result = ListBuffer[ListBuffer[Int]]()

    0.until(fact(list.size)).foreach { // used 1.until() instead of 0.until()
      i: Int =>
        result = result + (ListBuffer() ++ list) // didn't copy list into new listBuffer
        swapRight(list, i % list.size)
    }

    result
  }

  @Test def shouldSwapValueInList() {
    assert(swapRight(ListBuffer(1, 2), 0) == ListBuffer(2, 1))
    assert(swapRight(ListBuffer(1, 2), 1) == ListBuffer(2, 1))
    assert(swapRight(ListBuffer(1, 2, 3), 2) == ListBuffer(3, 2, 1))
    assert(swapRight(ListBuffer(3, 2, 1), 0) == ListBuffer(2, 3, 1))
    assert(swapRight(ListBuffer(2, 3, 1), 1) == ListBuffer(2, 1, 3))
  }

  def swapRight(list: ListBuffer[Int], position: Int): ListBuffer[Int] =
  {
    val nextPosition = (position + 1) % list.size
    val tmp = list(position)
    list(position) = list(nextPosition)
    list(nextPosition) = tmp
    list
  }

  @Test def shouldCalculateFactorial() {
    assertEquals(fact(0), 1)
    assertEquals(fact(1), 1)
    assertEquals(fact(2), 2)
    assertEquals(fact(3), 6)
    assertEquals(fact(4), 24)
  }

  def fact(i: Int): Int =
  {
    if (i < 0) return -1
    if (i == 0) return 1
    if (i < 3) return i
    fact(i - 1) * i
  }
{'fraction_non_alphanumeric': 0.1628245571463237, 'fraction_numerical': 0.03615627274933269, 'mean_word_length': 3.559734513274336, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3285824', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1435, 'n_words': 336}