diff --git a/Java-solution/Board.java b/Java-solution/Board.java new file mode 100644 index 00000000..72cd2186 --- /dev/null +++ b/Java-solution/Board.java @@ -0,0 +1,38 @@ +import java.util.Map; +import java.util.HashMap; + +public class Board { + private int size; + private Map snakes; + private Map ladders; + + public Board(int size) { + this.size = size; + this.snakes = new HashMap<>(); + this.ladders = new HashMap<>(); + } + + public void addSnake(int head, int tail) { + snakes.put(head, tail); + } + + public void addLadder(int start, int end) { + ladders.put(start, end); + } + + public int getNewPosition(int currentPosition) { + while (snakes.containsKey(currentPosition) || ladders.containsKey(currentPosition)) { + if (snakes.containsKey(currentPosition)) { + currentPosition = snakes.get(currentPosition); + } else if (ladders.containsKey(currentPosition)) { + currentPosition = ladders.get(currentPosition); + } + } + + return currentPosition; + } + + public int getSize() { + return size; + } +} \ No newline at end of file diff --git a/Java-solution/Game.java b/Java-solution/Game.java new file mode 100644 index 00000000..8ac0d6d3 --- /dev/null +++ b/Java-solution/Game.java @@ -0,0 +1,63 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class Game { + private List players; + private Board board; + private Player winner; + private Random random; + + public Game(Board board) { + this.players = new ArrayList<>(); + this.board = board; + this.winner = null; + this.random = new Random(); + } + + public void addPlayer(Player player) { + players.add(player); + } + + public void setSnakes(List snakes) { + for (int[] snake : snakes) { + board.addSnake(snake[0], snake[1]); + } + } + + public void setLadders(List ladders) { + for (int[] ladder : ladders) { + board.addSnake(ladder[0], ladder[1]); + } + } + + public int rollDice() { + return random.nextInt(6) + 1; + } + + public void play() { + int currentPlayerIndex = 0; + while (winner == null) { + Player player = players.get(currentPlayerIndex); + int diceValue = rollDice(); + int initialPosition = player.getPosition(); + int newPosition = initialPosition + diceValue; + + if (newPosition <= board.getSize()) { + newPosition = board.getNewPosition(newPosition); + player.setPosition(newPosition); + } + + System.out.printf("%s rolled a %d and moved from %d to %d%n", player, diceValue, initialPosition, + player.getPosition()); + + if (player.getPosition() == board.getSize()) { + winner = player; + System.out.printf("%s wins the game.%n", player); + break; + } + + currentPlayerIndex = (currentPlayerIndex + 1) % players.size(); + } + } +} diff --git a/Java-solution/Main.java b/Java-solution/Main.java new file mode 100644 index 00000000..04cf82f5 --- /dev/null +++ b/Java-solution/Main.java @@ -0,0 +1,44 @@ +import java.util.Scanner; +import java.util.List; +import java.util.ArrayList; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + int s = scanner.nextInt(); + List snakes = new ArrayList<>(); + for (int i = 0; i < s; i++) { + int head = scanner.nextInt(); + int tail = scanner.nextInt(); + snakes.add(new int[] { head, tail }); + } + + int l = scanner.nextInt(); + List ladders = new ArrayList<>(); + for (int i = 0; i < l; i++) { + int start = scanner.nextInt(); + int end = scanner.nextInt(); + ladders.add(new int[] { start, end }); + } + + int p = scanner.nextInt(); + List players = new ArrayList<>(); + for (int i = 0; i < p; i++) { + String name = scanner.next(); + players.add(new Player(name)); + } + + scanner.close(); + + Board board = new Board(100); + Game game = new Game(board); + game.setSnakes(snakes); + game.setLadders(ladders); + for (Player player : players) { + game.addPlayer(player); + } + + game.play(); + } +} diff --git a/Java-solution/Player.java b/Java-solution/Player.java new file mode 100644 index 00000000..a20e9156 --- /dev/null +++ b/Java-solution/Player.java @@ -0,0 +1,26 @@ +public class Player { + private String name; + private int position; + + public Player(String name) { + this.name = name; + this.position = 0; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void setPosition(int position) { + this.position = position; + } + + @Override + public String toString() { + return name; + } +} \ No newline at end of file diff --git a/docs/clone-1.png b/docs/clone-1.png deleted file mode 100644 index e0df3b2f..00000000 Binary files a/docs/clone-1.png and /dev/null differ diff --git a/docs/clone.png b/docs/clone.png deleted file mode 100644 index e3bde132..00000000 Binary files a/docs/clone.png and /dev/null differ diff --git a/docs/fork.png b/docs/fork.png deleted file mode 100644 index 62014114..00000000 Binary files a/docs/fork.png and /dev/null differ diff --git a/docs/git.md b/docs/git.md deleted file mode 100644 index bba78f4a..00000000 --- a/docs/git.md +++ /dev/null @@ -1,83 +0,0 @@ -# Create Github Account -1. Create account at: https://github.com/join by choosing an unique username -2. Check your email for verification email and click on verification link - -# Install Git Locally - -## Windows -- Download git from https://gitforwindows.org/ -- When you've successfully started the installer, you should see the Git Setup wizard screen. Follow the Next and Finish prompts to complete the installation. The default options are pretty sensible for most users. -- Open a Command Prompt (or Git Bash if during installation you elected not to use Git from the Windows Command Prompt). -- Run the following commands to configure your Git username and email using the following commands, replacing Emma's name and email with your own. These details will be associated with any commits that you create: -``` -git config --global user.name "Emma Paris" -git config --global user.email "eparis@atlassian.com" -``` - -## Linux -- From your shell, install Git using apt-get: -``` -sudo apt-get update -sudo apt-get install git -``` -- Verify the installation was successful by typing git --version which should return the version number: -``` -git --version -``` -- Run the following commands to configure your Git username and email using the following commands, replacing Emma's name and email with your own. These details will be associated with any commits that you create: -``` -git config --global user.name "Emma Paris" -git config --global user.email "eparis@atlassian.com" -``` - -## Mac -- From your shell, install Git using Homebrew: -``` -brew install git -``` -- Verify the installation was successful by typing git --version which should return the version number: -``` -git --version -``` -- Run the following commands to configure your Git username and email using the following commands, replacing Emma's name and email with your own. These details will be associated with any commits that you create: -``` -git config --global user.name "Emma Paris" -git config --global user.email "eparis@atlassian.com" -``` - -These commands have been taken from Atlassian's website. - -# Fork -- In the repository page, click on "Fork" -![Fork](./fork.png) -- After clicking on Fork, you'll be redirected to a new page with a copy of the repository. -- This new repository is where you'll make your code changes. -- Then click on clone in this new page - -# Clone -- In the repository page, click on "Clone or download" - -![Clone](./clone.png) - -- Then click on copy link button - -![Clone-1](./clone-1.png) - -- Go to the terminal and clone the repository on your machine by typing: -```git clone``` followed by the link you copied in previous step and hit enter. - -# Push local changes to remote -Execute these commands on the terminal: -- ```cd machine-coding-feedback``` -- ```git add .``` -- ```git commit -m "solved the problem"``` -- ```git push``` - -# PR -- Open the repository in your profile. The URL would be of this format: ```https://github.com//machine-coding-feedback``` -- Click on "New Pull Request". -![pr](./pr.png) -- Then click on "Create pull request" in the page that opened. -![pr-1](./pr-1.png) -- Again click on the new button that says "Create pull request" -- After this step, a new page would load which would be your pull request. diff --git a/docs/index.md b/docs/index.md deleted file mode 100644 index 4f62c017..00000000 --- a/docs/index.md +++ /dev/null @@ -1,45 +0,0 @@ -# Please go through this document to get your machine coding code reviewed by workat.tech - -## Why Practice for Machine Coding Round? -- In companies like Flipkart, Uber, Swiggy, Ola, Cred, etc the first onsite round is the machine coding round. -- You're given a design problem (like design a parking lot) with a set of requirements. -- Then you need to create a clean, modular and extensible coding solution for the same. -- The code is supposed to be written in a matter of 90 mins. -- After the round, you sit with an interviewer who goes through your code and tries to understand your design decisions and also tries to see if your code works for all the requirements. -- The interviewer may ask you to extend your solution based on some new requirement. -- It is a pretty crucial round since most of the people get eliminated in this round and it is completely different from what everyone generally practices for. - -## How to prepare? -Please go through our article on 'How to prepare for machine coding round?'. - -## Setup -- Ensure that you've a github account. If you do not have one, check this page. -- Ensure that you've git set up locally on your Laptop/PC. If it is not set up, check this page. -- Go to machine-coding-feedback repository. -- Fork the repository. If you're new to git, follow the steps mentioned in the fork section here -- Clone the repository in your local machine.If you're new to git, follow the steps mentioned in the clone section here - -## Coding -- After the setup step, a new folder will be created in your laptop/PC with the name: machine-coding-feedback. -- Open that folder in an IDE of your choice and start coding. - -## Expectations -- Make sure that you have a working and demonstrable code -- Make sure that the code is functionally correct -- Code should be modular and readable -- Separation of concern should be addressed -- Please do not write everything in a single file -- Code should easily accommodate new requirements and minimal changes -- There should be a main method from where the code could be easily testable -- [Optional] Write unit tests, if possible -- No need to create a GUI - -## Submission -- After you're done with coding, you need to commit and push your changes to github. -- You should push your changes to the master branch of your forked repository. If you're new to git, follow the steps mentioned in the push local changes to remote section here -- Create a pull request to our master branch. If you're new to git, follow the steps mentioned in the PR section here - -## Evaluation -- We'll be reviewing everyone's submission and try to provide feedback on how to improve through code review comments on GitHub. -- We also have some volunteers who will help with the review. -- Anyone can volunteer to review. diff --git a/docs/pr-1.png b/docs/pr-1.png deleted file mode 100644 index 3ade58aa..00000000 Binary files a/docs/pr-1.png and /dev/null differ diff --git a/docs/pr.png b/docs/pr.png deleted file mode 100644 index d798f596..00000000 Binary files a/docs/pr.png and /dev/null differ