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

7th
Dec 2016
Ze-Marcos
@MarkSchemmer
Dec 07 2016 02:33
I refactored my code again .
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;

public static class Kata
{

public static long NextSmaller(long n)
{
Console.WriteLine("Max Number is : " + n);
string order = n.ToString().ToCharArray().OrderBy(x => x).Aggregate("", (cur, nex) => cur + nex);
long L = Convert.ToInt64(order);
Console.WriteLine( "i must be bigger than : " + L);

for(var i = n - 2; i >= L; i--)
{
string z = i.ToString().ToCharArray().OrderBy(x => x).Aggregate("", (cur, nex) => cur + nex);

if ( order == z )
{
Console.WriteLine(\$"The next lowest number from {n} is {i} ");
return i;
}
}

Console.WriteLine("There is no next lower number so we return : -1");
return -1;
}
}
but it is failing now on time out error
Does anybody know how to speed this algorithm up ?
Ze-Marcos
@MarkSchemmer
Dec 07 2016 03:01
Please if there is someone I could really use some help
I have refactored my code a little bit more
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;

public static class Kata
{

public static long NextSmaller(long n)
{
string order = n.ToString().ToCharArray().AsParallel().AsOrdered().OrderBy(x => x).Aggregate("", (cur, nex) => cur + nex);
long L = Convert.ToInt64(order);

for(var i = n - 2; i >= L; i--)
{
string z = i.ToString().ToCharArray().AsParallel().AsOrdered().OrderBy(x => x).Aggregate("", (cur, nex) => cur + nex);

if(order == z)
return i;

}

return -1;
}
}
Benjamin Kästner
@bkaestner
Dec 07 2016 05:40
Your algorithm isn't efficient. Every of your ToString() lines needs to create a string, an array, sort them, and the recreate a string.
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:41
Yes
It is my algorithm
and it is about 630 ms slow I guess
Benjamin Kästner
@bkaestner
Dec 07 2016 05:41
Also, you check a lot of numbers.
For example, if the input is 987654321, you would start with 123456789.
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:42
Yes but how would I shorten that ?
that is what I am trying to understand right
now
Benjamin Kästner
@bkaestner
Dec 07 2016 05:43
You don't check all numbers. Instead, you check how you could modify the number to make it smaller.
How would you make 321 smaller?
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:44
My first thought is this var query = "321".ToCharArray().OrderByDecending(x => x);
Benjamin Kästner
@bkaestner
Dec 07 2016 05:44
How would you make 12321 smaller?
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:44
then I would convert it to a long integer
Benjamin Kästner
@bkaestner
Dec 07 2016 05:45
No. As a human. Not as a machine.
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:46
That is a harder question
I guess
Benjamin Kästner
@bkaestner
Dec 07 2016 05:46
Which one? The human one? Or 12321?
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:47
No. As a human ? and how to make 12321
smaller
Benjamin Kästner
@bkaestner
Dec 07 2016 05:47
=> 12312
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:48
I would say that 11223
Benjamin Kästner
@bkaestner
Dec 07 2016 05:48
But that's not the next smaller.
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:48
bu the next smallest is
Benjamin Kästner
@bkaestner
Dec 07 2016 05:48
Right.
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:49
12231
?
I think that is correct
Benjamin Kästner
@bkaestner
Dec 07 2016 05:49
12321 => 12312
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:50
it is the same
Benjamin Kästner
@bkaestner
Dec 07 2016 05:51
$12312 \neq 12231$
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:51
Yes
Benjamin Kästner
@bkaestner
Dec 07 2016 05:51
What's the "next smaller" number of 9111111111111111111?
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:52
I do not know really
well can I guess ?
Natacha Beck
@natacha-beck
Dec 07 2016 05:57
you really have a smallest int ? with the same digits ?
Ze-Marcos
@MarkSchemmer
Dec 07 2016 05:58
I pass all the tests
The only problem is that my algorithm is slow
Benjamin Kästner
@bkaestner
Dec 07 2016 05:59
Yeah, the algorithm is "correct", but it's runtime is linear in the size of the number, not linear in the number of digits (aka logarithmic in the size of the number).
Ze-Marcos
@MarkSchemmer
Dec 07 2016 06:03
Yes you are totally right
I am trying to understand
and I get that I need to change my for loop to make the count shorter
and to make my Linq query statements faster as well
Benjamin Kästner
@bkaestner
Dec 07 2016 06:05
I'm not sure whether Linq is appropriate here.
Ze-Marcos
@MarkSchemmer
Dec 07 2016 06:07
you are probably right because linq is slow when iterating through big lists compared to for loops I guess
but I would like to find a way
though to make it possible
do you have a article I could read
Ze-Marcos
@MarkSchemmer
Dec 07 2016 06:12
or maybe a idea of what I could do
?
Giacomo Sorbi
@GiacomoSorbi
Dec 07 2016 07:59
oh, working on the spin off of my most appreciated kata
and wondering if I'm doing correctly:
1. Write the function
2. Write the test case
Is that correct? And am I testing the right way
const howManyLightsabersDoYouOwn = (name) => {
return name === 'Zach' ? 18 : 0;
}

describe('howManyLightsabersDoYouOwn', function(name) {
it ('should return 18 if name === "Zach"', function() {
var name = 'Zach';
Test.expect(howManyLightsabersDoYouOwn(name) === 18);
})
it ('should return 0 for somene else', function() {
var name = 'Me';
Test.expect(howManyLightsabersDoYouOwn(name) === 0);
})
})
thanks first time writing a test case
Luke Murray
@lukemurray77
Dec 07 2016 12:13

Hey guys, im am attempting this kata, https://www.codewars.com/kata/coding-meetup-number-15-higher-order-functions-series-find-the-odd-names/train/javascript

I think I have solved it- my solution passes the basic tests but fails on the attempt. Can someone offer some advice? here is my code:

function findOddNames(list) {
var count = 0;
list.forEach(function(element, index){
var name = element.firstName.split('');
count = name.reduce(function(result, element, index){
return result += element.charCodeAt(0);
}, 0)

if (count % 2 === 0){
element.firstName = 1;
}
})
return list.filter(function(elem, index){
return elem.firstName !== 1;
})
}
asterixorobelix
@asterixorobelix
Dec 07 2016 12:45
Is anyone else having problems with the Fighters Kata in Javascript? I'm passing all the tests, except for one. However, when I run the code on my own machine, it passes all the tests. https://www.codewars.com/kata/two-fighters-one-winner/train/javascript
My code passes all the asserts, except for the third one, were it says it expected a result "Harald" and it didnt get that returned. However, when I run that test in the terminal on my machine, it does return "Harald"
Giacomo Sorbi
@GiacomoSorbi
Dec 07 2016 18:18
@dantesolis, test cases have been written for you, just submit