object FindMultiples: def findMultiples(n: Int): Int = Seq.range(0, n).filter(i => i % 4 == 0 || i % 6 == 0).sum
- object FindMultiples:
- def findMultiples(n: Int): Int =
(for (i <- 0 until n by 2 if i % 4 == 0 || i % 6 == 0) yield i).sum- Seq.range(0, n).filter(i => i % 4 == 0 || i % 6 == 0).sum
object FindMultiples: def findMultiples(n: Int): Int = (for (i <- 0 until n by 2 if i % 4 == 0 || i % 6 == 0) yield i).sum
def find_multiples(n):total = 0for i in range(0, n, 2):if i % 4 == 0 or i % 6 == 0:total += ireturn total- object FindMultiples:
- def findMultiples(n: Int): Int =
- (for (i <- 0 until n by 2 if i % 4 == 0 || i % 6 == 0) yield i).sum
import collection.mutable.Stack import org.scalatest._ import flatspec._ import matchers._ import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import FindMultiples.findMultiples class ExampleSpec extends AnyFlatSpec with ScalaCheckPropertyChecks with should.Matchers: "findMultiples(n)" should "return 0 if n == 3" in { findMultiples(3) should be(0) } it should "return 0 if n == 4" in { findMultiples(4) should be(0) } it should "return 4 if n == 5" in { findMultiples(5) should be (4) } it should "return 4 if n == 6" in { findMultiples(6) should be(4) } it should "return 10 if n == 7" in { findMultiples(7) should be (10) } it should "return the correct result for any given n" in { val moreMultiples = Table( ("n", "multiple"), (20, 64), (35, 198), (120, 2340) ) forAll(moreMultiples) { case (n, multiple) => findMultiples(n) should be (multiple) } }
import codewars_test as testimport solution # or from solution import example- import collection.mutable.Stack
- import org.scalatest._
- import flatspec._
- import matchers._
- import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
- import FindMultiples.findMultiples
@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(find_multiples(20), 64)test.assert_equals(find_multiples(35), 198)test.assert_equals(find_multiples(120), 2340)- class ExampleSpec extends AnyFlatSpec with ScalaCheckPropertyChecks with should.Matchers:
- "findMultiples(n)" should "return 0 if n == 3" in {
- findMultiples(3) should be(0)
- }
- it should "return 0 if n == 4" in {
- findMultiples(4) should be(0)
- }
- it should "return 4 if n == 5" in {
- findMultiples(5) should be (4)
- }
- it should "return 4 if n == 6" in {
- findMultiples(6) should be(4)
- }
- it should "return 10 if n == 7" in {
- findMultiples(7) should be (10)
- }
- it should "return the correct result for any given n" in {
- val moreMultiples = Table(
- ("n", "multiple"),
- (20, 64),
- (35, 198),
- (120, 2340)
- )
- forAll(moreMultiples) { case (n, multiple) =>
- findMultiples(n) should be (multiple)
- }
- }
object Adder: def addAllContent(numbers: Array[Int]): Int = numbers.sum
public class Adder {public static int AddAllContent(int[] numbersToAdd) {int sum = 0;- object Adder:
for (int i : numbersToAdd)sum += i;return sum;}}- def addAllContent(numbers: Array[Int]): Int = numbers.sum
import collection.mutable.Stack import org.scalatest._ import flatspec._ import matchers._ import Adder.addAllContent class AdderTest extends AnyFlatSpec with should.Matchers: "addAllContent" should "return 6 for Array(1,2,3)" in { addAllContent(Array(1, 2, 3)) should be (6) } it should "return 21 for Array(1, 2, 3, 4, 5, 6)" in { addAllContent(Array(1, 2, 3, 4, 5, 6)) should be (21) }
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;- import collection.mutable.Stack
- import org.scalatest._
- import flatspec._
- import matchers._
- import Adder.addAllContent
// TODO: Replace examples and use TDD by writing your own tests- class AdderTest extends AnyFlatSpec with should.Matchers:
class SolutionTest {@Testvoid testSomething() {int[] numbers = {1,2,3};assertEquals(Adder.AddAllContent(numbers), 6);}@Testvoid testSomethingElse() {int[] numbers = {1,2,3, 10};assertEquals(Adder.AddAllContent(numbers), 16);}}- "addAllContent" should "return 6 for Array(1,2,3)" in {
- addAllContent(Array(1, 2, 3)) should be (6)
- }
- it should "return 21 for Array(1, 2, 3, 4, 5, 6)" in {
- addAllContent(Array(1, 2, 3, 4, 5, 6)) should be (21)
- }
Fundamentals
Arrays
Strings
object FindTRex: def containsTRex(things: Array[String]): Boolean = things.contains("Tyrannosaurus")
public class findT_Rex {public static boolean containsT_Rex(String[] things) {//insert code!return true;}}- object FindTRex:
- def containsTRex(things: Array[String]): Boolean = things.contains("Tyrannosaurus")
import collection.mutable.Stack import org.scalatest.* import flatspec.* import matchers.* class ExampleSpec extends AnyFlatSpec, should.Matchers: "An empty array" should "result in false" in { FindTRex.containsTRex(Array()) should be (false) } "An array of Dinosaurs without TRex" should "result in false" in { val result = FindTRex.containsTRex(Array("Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx")) result should be (false) } "An array containing a TRex" should "result in true" in { val result = FindTRex.containsTRex(Array("Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose")) result should be (true) }
import org.junit.Test;import static org.junit.Assert.assertEquals;- import collection.mutable.Stack
- import org.scalatest.*
- import flatspec.*
- import matchers.*
// TODO: Replace examples and use TDD by writing your own tests- class ExampleSpec extends AnyFlatSpec, should.Matchers:
class SolutionTest {@Testpublic void tests() {String[] things1 = new String[] {"Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"};String[] things2 = new String[] {"Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"};String[] things3 = new String[] {""};// assertEquals("expected", "actual");assertEquals(false, containsT_Rex(things1));assertEquals(true, containsT_Rex(things2));assertEquals(false, containsT_Rex(things3));}}- "An empty array" should "result in false" in {
- FindTRex.containsTRex(Array()) should be (false)
- }
- "An array of Dinosaurs without TRex" should "result in false" in {
- val result = FindTRex.containsTRex(Array("Triceratops", "Megalosaurus", "Spinosaurus", "Archaeopteryx"))
- result should be (false)
- }
- "An array containing a TRex" should "result in true" in {
- val result = FindTRex.containsTRex(Array("Jackie Chan", "Charlize Theron", "Tyrannosaurus", "Tom Hardy", "Ruby Rose"))
- result should be (true)
- }
object Practice01: def reverse(input: String): String = input.reverse
//import java.util.Scanner;import java.lang.StringBuilder;public class Practice01{public static String reverse(String input) {/*Scanner sc = new Scanner(System.in);System.out.println("Give the desired string to be reversed :");String e = sc.nextLine();*/return new StringBuilder(input).reverse().toString();}}- object Practice01:
- def reverse(input: String): String = input.reverse
import org.scalatest._ import flatspec._ import matchers._ class ExampleSpec extends AnyFlatSpec, should.Matchers: "'Hello World!' reversed" should "result in '!dlroW olleH'" in { val reversed = Practice01.reverse("Hello World!") reversed should be ("!dlroW olleH") } "An empty String reversed" should "be an empty String" in { Practice01.reverse("") should be ("") } "'Reverse' 2x reversed" should "result in 'Reverse'" in { val esrever = Practice01.reverse("Reverse") val reverse = Practice01.reverse(esrever) reverse should be ("Reverse") }
import static org.junit.Assert.*;import org.junit.Test;- import org.scalatest._
- import flatspec._
- import matchers._
// TODO: Replace examples and use TDD by writing your own testspublic class Practice01Test {- class ExampleSpec extends AnyFlatSpec, should.Matchers:
private static void testing(String actual, String expected) {assertEquals(expected, actual);}@Testpublic void test() {testing("!ereht olleH" , Practice01.reverse("Hello there!"));}}- "'Hello World!' reversed" should "result in '!dlroW olleH'" in {
- val reversed = Practice01.reverse("Hello World!")
- reversed should be ("!dlroW olleH")
- }
- "An empty String reversed" should "be an empty String" in {
- Practice01.reverse("") should be ("")
- }
- "'Reverse' 2x reversed" should "result in 'Reverse'" in {
- val esrever = Practice01.reverse("Reverse")
- val reverse = Practice01.reverse(esrever)
- reverse should be ("Reverse")
- }