Posts

A Java Program to detect cycle in a graph

class GFG { private final int V ; private final List<List<Integer>> adj ; public GFG ( int V) { this . V = V ; adj = new ArrayList<>(V) ; for ( int i = 0 ; i < V ; i++) adj .add( new LinkedList<>()) ; } // This function is a variation of DFSUtil() in // https://www.geeksforgeeks.org/archives/18212 private boolean isCyclicUtil ( int i , boolean [] visited , boolean [] recStack) { // Mark the current node as visited and // part of recursion stack if (recStack[i]) return true; if (visited[i]) return false; visited[i] = true; recStack[i] = true; List<Integer> children = adj .get(i) ; for (Integer c: children) if (isCyclicUtil(c , visited , recStack)) return true; recStack[i] = false; return false; } private void addE...

js newbie "A" wants to learn React from "B" and wants to know in his newtwork who can introduce him to B in the shortest time period.

  Input format: Total Members in UI friend Network = N MemberId1 = N1 MemberId2 = N2 MemberId3 = N3 MemberIdN = Nn Output format: shortest time A takes to reach B Sample Input: 4 2 5 7 9 4 2 9 2 7 2 3 7 9 7 9 5 1 7 9 Sample Output: 5 Solution:3 public class Graph { static class Node { private String name ; private List<Node> shortestPath = new LinkedList<>() ; private Integer distance = Integer. MAX_VALUE ; Map<Node , Integer> adjacentNodes = new HashMap<>() ; public void addDestination (Node destination , int distance) { adjacentNodes .put(destination , distance) ; } public Node (String name) { this . name = name ; } // getters and setters public String getName () { return name ; } public void setName (String name) { this . name = name ; } public List<Node> getShortestPath () { ...

Git Challenge VII

Git Challenge VII   This challenge asks you to create a branch, make some modifications on that branch, and force Git to not perform a fast-forward merge. The high-level steps for this task are: 1. Ensure you’re on the `main` branch. 2. Create a branch named `contact-details`. 3. Switch to that branch. 4. Edit the README.md file and add the following text to the end of the file: "Contact: support@razeware.com". 5. Save your edits to the file. 6. Stage your changes. 7. Commit your changes with an appropriate commit message, such as "Adding README contact information". 8. Switch back to the `main` branch. 9. Pull up the graph of the repository, and don’t forget to use the `--all` option to see history of all branches. Make note of how `main` and `contact-details` look on this graph. 10. Merge in the changes from `contact-details`, using the `--no-ff` option. 11. Enter something appropriate in the merge message in Vim when prompted. Use the cheatsheet above to help yo...

Git Challenge VI

  Git Challenge VI 1. Create a temporary branch with the name of **newBranch**. Solution -      git branch newBranch 2. Switch to that branch. Solution -      git checkout newBranch 3. Use the `touch` command to create an empty **README.md** file in the root directory of your project. Solution -      touch README.md 4. Add that new **README.md** file to the staging area. Solution -      git add README.md 5. Commit that change with an appropriate message. Solution -      git commit -m "Adding README.md" 6.Checkout the **main** branch. Solution -      git checkout main 7. Delete **newBranch** — but Git won’t let you delete this branch in its current state. Why? Solution First, try the following: -      git branch -d newBranch Git responds with: error: The branch 'newBranch' is not fully merged. If you are sure you want to delete it, run 'git branch -D newBranch'. 8. Follow the sug...

Git Challenge V

  Git Challenge V Challenge 1: Show all the details of commits that mark items as “done” For this challenge, you need to find all of the commits where items have been ticked off as “done”; that is, ones that have an “x” inside the brackets, like so: `[x]` Solution You’ll need to search for the above string, and you’ll need to use an option to not only show the basic commit details but also show the contents of the changeset of the commit. To do this, use the search flag "-S" with git log, and also include the "-p" flag to see the contents of each commit: git log -p -S"[x]" Challenge 2: Find all the commits with messages that mention “streaming” You want to search through the commit **messages** to find where you or someone else has used the term “streaming” in the commit message itself, not necessarily in the content of the commit. Tip: What was that strangely named command you learned about earlier in this chapter? Solution Right, you want to use ...

Git Challenge - IV

Git Challenge IV This challenge simply asks you to grab the contents of a file in GitHub and copy it into your global **.gitignore** with the following steps: 1. Navigate to [https://github.com/github/gitignore/tree/master/Global](https://github.com/github/gitignore/tree/master/Global). 2. Find the correct **.gitignore** for your own operating system. 3. Take the contents of that OS-specific **.gitignore**, and add it to your own global **.gitignore**. SOLUTION Here are the steps I followed to complete this challenge. You may have used slightly different methods to accomplish the same result, but here’s how I did it: - Navigate to [https://github.com/github/gitignore/tree/master/Global](https://github.com/github/gitignore/tree/master/Global) in a browser. - Click on the appropriate global .gitignore file for your operating system. I’m on a Mac, so I’ll use the macOS.gitignore file. - Copy the contents of this file and press Command + C (Control + C on a Windows machine) to copy it ...

Git Challenge - III

Git Challenge III This challenge simply reuses the commands you’ve used in this chapter to accomplish the following tasks: 1. Move the newly added **books/management_book_ideas.md** to the **website** directory with `git mv`. 2. You’ve changed your mind and don’t want **management_book_ideas.md** anymore, so remove that file completely. 3. But now you’re having second thoughts: maybe you _do_ have some good ideas about management. Restore that file to its original location in the **books** directory. Again, it’s worth your while to execute "git status" after each change, to see exactly what Git is going at each step. It’s a great way to get your bearings in your early days with Git. SOLUTION Here are the steps I followed to complete this challenge. You may have used slightly different methods to accomplish the same result, but here’s how I did it: - Move the file with "git mv books/management_book_ideas.md website/" - Next, if you try to do "git rm website/...