These are chat archives for Codewars/codewars.com/kata-solving-help

5th
Dec 2016
AVA_Zen
@AlifirenkoVA
Dec 05 2016 08:32
Hello! Please give me a tip. I desided kata, all tests are passed, but "Random Tests" - is not (( I don't get it why... Kata name "Counting Duplicates", language "c#". Please give me advise what to do. Thanks a lot.
D/S
@dantesolis
Dec 05 2016 08:35
@AlifirenkoVA I don't C#, so I won't be of much but do you have a code snippet by any chance?
Peeps, I'm tryign to solve this kata sort and star, but my random tests fail.
My code:
function twoSort(s) {
 // sort the vector
 // extract the first value
 // add *** between each letter
 let sorted = s.sort();
 console.log(sorted);
 let first = sorted.shift();
 console.log(first)
 return first.split('').join('***');
}
  Random tests
  Testing for MadeSafeCoin,Steem,Ripple
 Log
 [ 'MadeSafeCoin', 'Ripple', 'Steem' ]
MadeSafeCoin
✘ It should work for random inputs too - Expected: 'R***i***p***p***l***e', instead got: 'M***a***d***e***S***a***f***e***C***o***i***n'
 Completed in 1ms
AVA_Zen
@AlifirenkoVA
Dec 05 2016 08:40
@dantesolis my code:
public class Kata
        {
            public static int DuplicateCount(string str)
            {
                var alphabets = "abcdefghijklmnopqrstuvwxyz123456789".ToCharArray();
                int[] nums = new int[alphabets.Length];

                var tmp = str.ToLower().ToCharArray();

                if (!string.IsNullOrEmpty(str))
                {
                    foreach (var item in tmp)
                    {
                        for (int i = 0; i < alphabets.Length; i++)
                        {
                            if (item == alphabets[i])
                            {
                                nums[i] += 1;
                            }
                        }
                    }

                    return nums.Count(num => num > 1);
                }
                else { 
                    return 0;
                }

            }

            public void ShowStr(string str)
            {
                Console.WriteLine(DuplicateCount(str));
            }
        }
RandomTests
✔ 1 assertion passed
✘ Expected: 8
  But was:  7
 Completed in 0.047428ms
any ideas what to do?
D/S
@dantesolis
Dec 05 2016 09:05
@AlifirenkoVA I'm at a loss, but hopefully somebody with more creds than me will be able to help?
Benjamin Kästner
@bkaestner
Dec 05 2016 09:07
@dantesolis Do not change the array. Especially do not remove values from the array with shift.
Many kata authors completely forget that arrays have reference semantics and don't copy them in their random tests.
D/S
@dantesolis
Dec 05 2016 09:18
@bkaestner tried using splice(0, 1) as well but my random tests keep failing. I'm missing something?
Benjamin Kästner
@bkaestner
Dec 05 2016 09:18
Splice also changes the array.
D/S
@dantesolis
Dec 05 2016 09:18
oh
got it to work with slice(0, 1) but at a loss on why does it work
Benjamin Kästner
@bkaestner
Dec 05 2016 09:23
Slice does not change the array.
You've solved it, right?
D/S
@dantesolis
Dec 05 2016 09:24
yep as per the docs 'The original array will not be modified.'

thanks for the help, but don't understand very well what you mean by

Many kata authors completely forget that arrays have reference semantics and don't copy them in their random tests.

are you refering to the index of each item in the arry? sorry if stupid question
new to this
:)
Benjamin Kästner
@bkaestner
Dec 05 2016 09:26
Think of random tests this way:
var random_strings = generateRandomStrings();
var reference_solution = function(str){
   ....
}

Test.assert_equals(yoursolution(random_strings), reference_solution(random_strings));
So, if yoursolution changes random_strings, the reference_solution will use the changed array.
When you've used splice, random_strings "lost" "MadeSafeCoin". Therefore, the authors reference solution returned "Ripple" (with stars).
Because random_strings didn't contain "MadeSafeCoin" anymore.
So what the author should have done:
var random_strings = generateRandomStrings();
var reference_solution = function(str){
   ....
}

Test.assert_equals(yoursolution(makeCopy(random_strings)), reference_solution(random_strings));
//                                  ^^
D/S
@dantesolis
Dec 05 2016 09:41
@bkaestner got it! thanks this is good to for when I start authoring my katas
@AlifirenkoVA that's just a guess, but you're missing 0 in your alphabet.
By the way, a String is enumerable.
AVA_Zen
@AlifirenkoVA
Dec 05 2016 10:04
@bkaestner Wayyy!! Thanks a lot!