Skip to content

BFS 1 Done#1637

Open
pranjay01 wants to merge 1 commit intosuper30admin:masterfrom
pranjay01:master
Open

BFS 1 Done#1637
pranjay01 wants to merge 1 commit intosuper30admin:masterfrom
pranjay01:master

Conversation

@pranjay01
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Level Order Traversal in Binary tree (Problem1.py)

Your solution is excellent! You have correctly implemented the level order traversal using a queue. Here are a few minor suggestions to improve the code quality further:

  1. Comments: The comments are helpful, but there are a few typos ("it's" should be "its", "prossesing" should be "processing", "lest" should be "list"). Also, consider making the comments more concise and precise.
  2. Initial Check: The condition if root!=None is correct, but in Python, it's more idiomatic to write if root is not None or simply if root (since None is falsy). However, your way is clear and acceptable.
  3. Loop Variable: The loop variable i in the inner for-loop is not used. It's common to use _ for unused variables to indicate that they are not needed (e.g., for _ in range(itemsInCurrentLevel)).
  4. Imports: You imported deque correctly, which is good. However, note that the problem expects the solution to be in Python, so using deque is efficient for queue operations.

Overall, this is a strong solution. Keep up the good work!

VERDICT: PASS


Scheduling Courses (Problem2.py)

Your solution is correct and efficient. You have implemented Kahn's algorithm properly, which is a valid approach for this problem. Here are some suggestions for improvement:

  1. Naming: Consider renaming dependencyMap to something more intuitive like graph or adjList to reflect that it represents the graph edges from a node to its neighbors.

  2. Initialization: You can use defaultdict(list) from collections to simplify the graph construction. This would avoid the need to check if a key exists in the dictionary.

  3. Early Termination: The initial check if len(coursesThatCanBeTaken) == 0 is good, but note that the problem might have no prerequisites, so all courses have indegree zero. Your code handles that correctly. However, the same condition is also handled by the while loop and the final return, so it is not strictly necessary but does not harm.

  4. Code Conciseness: You can initialize the graph more concisely:

    from collections import defaultdict
    graph = defaultdict(list)
    for course, pre in prerequisites:
         graph[pre].append(course)
         indegree[course] += 1
  5. Final Check: The return statement return coursesTaken == numCourses is correct. You don't need to check inside the while loop for coursesTaken == numCourses if you are going to check at the end anyway, but it is an optimization to break early.

Overall, your solution is very good. Keep up the good work!

VERDICT: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants