FAILING THE 6KYU CHALLENGES - Let's Code Codewars

Поділитися
Вставка
  • Опубліковано 14 чер 2024
  • Today we do some Codewars challenges again. And I fail the 6KYU challenge.
    ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
    📚 Programming Books & Merch 📚
    🐍 The Python Bible Book: www.neuralnine.com/books/
    💻 The Algorithm Bible Book: www.neuralnine.com/books/
    👕 Programming Merch: www.neuralnine.com/shop
    💼 Services 💼
    💻 Freelancing & Tutoring: www.neuralnine.com/services
    🌐 Social Media & Contact 🌐
    📱 Website: www.neuralnine.com/
    📷 Instagram: / neuralnine
    🐦 Twitter: / neuralnine
    🤵 LinkedIn: / neuralnine
    📁 GitHub: github.com/NeuralNine
    🎙 Discord: / discord
  • Наука та технологія

КОМЕНТАРІ • 23

  • @smnomad9276
    @smnomad9276 12 днів тому +9

    That's very respectable of you to be honest, not afraid to show your real programming level(which is solid) in front of your followers. We need more episodes of this series🔥

  • @alexanderserafeim9416
    @alexanderserafeim9416 10 днів тому +1

    Very nice video. Its really nice to see creators being honest about how much effort it actually takes to work on these problems and not just magically type the solution in 60 sec.

  • @Dyanosis
    @Dyanosis 12 днів тому +2

    You only want numbers that are 2 diff, so you only need to check ahead of your current number until you find a diff that is greater than 2. If it's greater than 2, then break out of the j loop.
    For the second problem, even though you probably solve it, you're modifying a lst that is being enumerated. Python doesn't seem to care about this, but any other OOP language (java, c#, etc) would throw a compile error because you're modifying a list in a loop.
    To put this another way - python's "foreach" appears to only care about the range and just returns "lst[next_idx]". But you're having a problem because you're removing a customer from the list but the loops index still moves forward, therefore 2,4 doesn't get seen until the iter has an index out of bounds but then still discovers that there are elements in the list. A good solution would be to do "while list" (which basically says "while list is not empty or None") instead of a "for customer in list"
    Lastly - How long will it take him to realize that his else is not actually connected to the if. This is one thing I hate about python is that there isn't really a way to actually scope your code to ensure it's doing what you think it's doing. I'm used to languages that use curly braces to define scope. And it's kind of dumb that else statements are even allowed after a for statement.

  • @smallg9109
    @smallg9109 12 днів тому +3

    i think the issue is that your test customers were (pre) sorted before you run the code but the large random tests were not necessarily sorted so your room allocation becomes messy (i.e. more empty gaps not getting filled leading to more rooms being needed), adding some code to sort the customers first should solve the random tests

  • @redeux
    @redeux 11 днів тому +1

    Honestly as a newer challenge that doesn't seem like a 6kyu unless you already know how to solve that problem. I really appreciate you showing your thought process though, and i respect you more now for sharing your "fail"

  • @thomasgoodwin2648
    @thomasgoodwin2648 12 днів тому +3

    Remember that a condition of the program was that overlap was ok IF customer 2 arrives the same day that customer 1 leaves. One of the bools needs to be

  • @elmusicandi
    @elmusicandi 12 днів тому +3

    Well done mate. That second one was indeed hard for 6k

  • @ashraf_isb
    @ashraf_isb 12 днів тому +1

    well its hard for me to think, thank you for putting this accross! appreciated

  • @ayoubkrimi6038
    @ayoubkrimi6038 12 днів тому +3

    using "in" operator with sets or dicts in python takes O(1) time complexity

    • @Dyanosis
      @Dyanosis 12 днів тому +1

      True - and for clarity in case people don't know why - it's because internally it's just doing a "return dict[key] is not None", same for set.

    • @dandyexplorer4252
      @dandyexplorer4252 10 днів тому

      @@Dyanosis How is it just doing a "return dict[key] is not None"? "a in mydict" checks if the "a" key is in "mydict". So this is True: "a in {a: None}", but "{a: None}[a] is not None" is False

  • @user-xm1ls2ch9n
    @user-xm1ls2ch9n 12 днів тому +1

    Wow what a level of video i really enjoyed

  • @saschazapf5232
    @saschazapf5232 11 днів тому +1

    First, imho this not should be rated as 6kyu, 5kyu would fit better. Love this kind of content. More of this pls. I got stuck, when my solution gives all right answer but i always got errormsg that my number of rooms exceed number of customer, which isnever the case. I guess there are some problems in the testcase. I wait for answer.

  • @mk-ck8or
    @mk-ck8or 10 днів тому +1

    kudos

  • @bobby_ridge
    @bobby_ridge 12 днів тому +2

    guys, hi. i did it very quickly, but i don't know where is problem (it work only on simple tests):
    def allocate_rooms(c):
    c.sort(key=lambda _: _[0])
    rooms = {i: 0 for i in range(1, len(c)+1)}
    queue = []
    for user in c:
    for key in rooms.keys():
    if rooms[key] < user[0]:
    rooms[key] = user[-1]
    queue.append(key)
    break

    return queue
    P.s. Heeeeelp

    • @mk-ck8or
      @mk-ck8or 10 днів тому +1

      I have a similar solution. I don't think it's the only thing, but one place were you messed up is by sorting the customers right at the start and forgetting the original order.
      def allocate_rooms(a):
      indices = [i[0] for i in sorted(enumerate(a), key=lambda x:x[1])]
      a = sorted(a)
      rooms = [0]
      res = [0] * len(a)
      for j, (arrival, departure) in enumerate(a):
      found_empty_room = False
      for i, r in enumerate(rooms):
      if arrival > r:
      rooms[i] = departure
      found_empty_room = True
      res[indices[j]] = i + 1
      break
      if not found_empty_room:
      rooms.append(departure)
      res[indices[j]] = len(rooms)
      return res

  • @pankajchauhan6431
    @pankajchauhan6431 10 днів тому +1

    Helpful forever, can you suggest Smart money concept coding for trading.

  • @DsoftN
    @DsoftN 12 днів тому +1

    def allocate_rooms(customers: list) -> list:
    customers.sort(key=lambda x: x[0])
    result = []
    rooms = {}
    for customer in customers:
    for room in rooms:
    if int(rooms[room]) < customer[0]:
    rooms[room] = customer[1]
    result.append(int(room))
    break
    else:
    rooms[str(len(rooms) + 1)] = customer[1]
    result.append(len(rooms))
    return result

    • @DsoftN
      @DsoftN 12 днів тому

      Now I found this algorithm on CodeWars and saw that I have an error in the code.
      The problem is that it seems to require that the rooms be ordered the way the customers are ordered in the list.
      This code passes all tests.
      def allocate_rooms(customers):
      ## Write code here

      customers_ind = []
      for i in range(len(customers)):
      customers_ind.append([customers[i][0], customers[i][1], i])
      customers_sorted = sorted(customers_ind, key=lambda x: x[0])

      result = []
      rooms = {}
      for customer in customers_sorted:
      for room in rooms:
      if rooms[room] < customer[0]:
      rooms[room] = customer[1]
      result.append(room)
      break
      else:
      rooms[len(rooms) + 1] = customer[1]
      result.append(len(rooms))
      solution = [0] * len(customers_ind)

      for i in range(len(result)):
      solution[customers_sorted[i][2]] = result[i]

      return solution

    • @supermalavox
      @supermalavox 9 днів тому +2

      Didn't your solution fail in the random tests? Also, doesn't sorting modify the original list - something that should not happen, if I read the description correctly?

    • @DsoftN
      @DsoftN 9 днів тому

      @@supermalavox You're right !
      I wrote this solution before I tested it (my mistake).
      Later I realized that I must not modify the original list so I posted the correct solution but now I see that my second post has not been published:
      def allocate_rooms(customers):
      ## Write code here

      customers_ind = []
      for i in range(len(customers)):
      customers_ind.append([customers[i][0], customers[i][1], i])
      customers_sorted = sorted(customers_ind, key=lambda x: x[0])

      result = []
      rooms = {}
      for customer in customers_sorted:
      for room in rooms:
      if rooms[room] < customer[0]:
      rooms[room] = customer[1]
      result.append(room)
      break
      else:
      rooms[len(rooms) + 1] = customer[1]
      result.append(len(rooms))
      solution = [0] * len(customers_ind)

      for i in range(len(result)):
      solution[customers_sorted[i][2]] = result[i]

      return solution