Algorithms
Make a function that looks for a duplicate number in an array.
For example [2,3,4,5,2] returns 2
The Intergers Allowed are [1, 2, 3, 4, 5, 6]
class Kumite
{
public static function findduplicatenumber(nums:Array<Int>)
{
var turtle = nums[0];
var rabbit = nums[0];
while (true)
{
turtle = nums[turtle];
rabbit = nums[nums[rabbit]];
if (turtle == rabbit)
{
break;
}
}
var ptr1 = nums[0];
var ptr2 = turtle;
while (ptr1 != ptr2)
{
ptr1 = nums[ptr1];
ptr2 = nums[ptr2];
}
return ptr1;
}
}
// Tests are written using https://github.com/haxe-utest/utest
import utest.Assert;
import Solution;
class SolutionTest extends utest.Test {
function testExample() {
Assert.equals(3, Kumite.findduplicatenumber([1,2,3,3,5]));
}
}
Some code that returns the length of an array
class BeginnerKumite
{
public static function returnlength(A:Array<String>)
{
var al = A.length;
return al;
}
}
// Tests are written using https://github.com/haxe-utest/utest
import utest.Assert;
import Solution;
class SolutionTest extends utest.Test {
function testExample() {
Assert.equals(3, BeginnerKumite.returnlength(['as','as','as']));
Assert.equals(0, BeginnerKumite.returnlength([]));
}
}
class FirstKumite{ public static function string(n:String) { if(n == '') { n = 'Nothing'; } return n; } }
- class FirstKumite{
public static function helloHaxe(n:String)- public static function string(n:String)
- {
- if(n == '')
- {
return 'Nothing';}else{return n;}- n = 'Nothing';
- }
- return n;
- }
- }
// Tests are written using https://github.com/haxe-utest/utest import utest.Assert; import Solution; class SolutionTest extends utest.Test { function testExample() { //it is so infuriating to see the "Expected" on the left... // What would you call the function? Assert.equals('Nothing', FirstKumite.string('')); } }
- // Tests are written using https://github.com/haxe-utest/utest
- import utest.Assert;
- import Solution;
- class SolutionTest extends utest.Test {
- function testExample() {
- //it is so infuriating to see the "Expected" on the left...
// Wtf am I doingAssert.equals('Nothing', FirstKumite.helloHaxe(''));- // What would you call the function?
- Assert.equals('Nothing', FirstKumite.string(''));
- }
- }
class FirstKumite{ public static function helloHaxe(n:String) { if(n == '') { return 'Nothing'; } else { return n; } } }
- class FirstKumite{
public static function helloHaxe() {return "Hello, World!";}- public static function helloHaxe(n:String)
- {
- if(n == '')
- {
- return 'Nothing';
- }
- else
- {
- return n;
- }
- }
- }
// Tests are written using https://github.com/haxe-utest/utest import utest.Assert; import Solution; class SolutionTest extends utest.Test { function testExample() { //it is so infuriating to see the "Expected" on the left... // Wtf am I doing Assert.equals('Nothing', FirstKumite.helloHaxe('')); } }
- // Tests are written using https://github.com/haxe-utest/utest
- import utest.Assert;
- import Solution;
- class SolutionTest extends utest.Test {
- function testExample() {
- //it is so infuriating to see the "Expected" on the left...
Assert.equals("Hello, World!", FirstKumite.helloHaxe());- // Wtf am I doing
- Assert.equals('Nothing', FirstKumite.helloHaxe(''));
- }
- }