Ad
  • Custom User Avatar

    They should not be thought of as Strings ( even if they are - what they really are is just an implementation detail ). Think of them as a three-valued Boolean. They are in a Class of their own.

    If you are familiar with Option types, you could also think of them as an Option Boolean, but that introduces unnecessary complexity.

  • Custom User Avatar

    Yes, the author means that these are string
    I mean strings in Array

  • Custom User Avatar

    The first argument is the string, the second one is the prefix:

    startsWith("nowai", "nowaisir"); // should return false, "nowai" doesn't start with "nowaisir"
    startsWith("hello", "HE") should return false, because the check should be case-sensitive
    startsWith("hello", "he") should return true, because "hello" starts with "he"
    
  • Default User Avatar
    1. Regarding the "only right or wrong answers" part - in general you can always print the input which will help you debug any Codewars kata. In Python you can do this by adding:
    def your_function(l):
      print(l) # <--- this print statement will show you the input, here 'l', in 
      # the console when you run tests
      
      the_rest_of_your_code ...
    
    1. Regarding why your code is wrong: it's a nice approach, but re-read the kata description: "...but ignores any duplicate items". What you are currently doing is summing the input list as though it were a set (i.e. counting each element exactly once) when in fact you should be "dropping" the elements that appear more than once. For example, your code on [1, 1, 2, 2, 3] gives 1+2+3 when the expected answer is 3, since you should not include 1 (it appears more than once) or 2 (it appears more than once) at all.
  • Custom User Avatar

    Because you're escaping the backslash like that and you should use a line-break instead: '\n'

    See the sample test:

    test.assert_equals(generate_shape(3), '+++\n+++\n+++')
    
  • Custom User Avatar

    Some more words: it's probably not useful to spend a lot of time on some katas, if your approach times out. That probably means you are missing some knowledge. A good advice I could give you is: try to practice on very easy katas (8/7 kyus). You will probably be able to solve a good amount of them without too much efforts. Once you solved a kata, you should have a look over the solutions that have been published by other users. By doing this, you will learn a lot of good tips and approaches you had no idea about, and you will see that you will also be able to solve many katas like this one easily. That's what I did, I learnt great part of what I know about programming by doing this.

  • Custom User Avatar

    Your approach is too inefficient to pass this kata. The problem of the method you are using (in both codes you are providing; they are actually equivalent) is that for each item in the list, you need to make a complete loop over the list to get the value you are interested in. If the array has 10 elements, your code will have to make 10 * 10 operations. If the array has 1000 elements, then it will be 1000 * 1000, etc. It's easy to understand that this approach is extremely slow with large arrays. It's hard to give you a concrete answer without giving away the solution. Actually it is possible to solve this kata with only 2 * n operations (with n being the length of the array). Try to think how you could adapt your second code to do it. If you don't manage it, try to make some research with good keywords. In Python, there are some data structures available that almost do all the job for you.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    I've copied your solution locally to my computer -- as akar-0 said, in future if you use markdown it's much easier for people to troubleshoot -- your logic is basically fine, your code currently has 2 errors in its implementation.

    The first error is a small "programming" error, which is introducing inaccuracies - check your mathematical operations in your code to see if you can find a place where you aren't working correctly with integer values.

    The second error is a more important "mathematical" error: you don't need to use large test cases to debug it: try your code on the following string and then calculate the correct answer manually (pen and paper) to see where you are going wrong:

    'ABBB' your code gives 8, correct answer is 4. Try to figure out where the discrepancy comes from.

  • Custom User Avatar

    Please use markdown to format your code or it's not readable, it is explained there: https://docs.codewars.com/training/troubleshooting/#post-discourse

    Your code has probably the same problem as several persons that have posted questions recently (see comments below). Some operation you are doing is introducing useless inaccuracies, I guess.

  • Custom User Avatar

    You're trying to read the length of None in some part of your code, look for that.