Enable contrast version

# Tutor profile: Anoop N.

Inactive
Anoop N.
Game Developer and Programmer
Tutor Satisfaction Guarantee

## Questions

### Subject:Python Programming

TutorMe
Question:

A robot moves in a plane from the original point (0,0). The robot can move UP, DOWN, LEFT, RIGHT with the given steps. UP 5 DOWN 2 LEFT 4 RIGHT 3 Write a program to compute the distance from the final position of the robot to the initial starting position after the movements have executed.

Inactive
Anoop N.

To solve this problem, think of the Robot moving on a 2D graph. The UP and DOWN movements are on the vertical axis and the LEFT and RIGHT movements are on the horizontal axis. If X is the horizontal axis and Y is the vertical axis: When the robot moves UP, we move in positive direction of Y-axis When the robot moves DOWN, we move in the negative direction of the Y-axis When the robot moves RIGHT, we move in positive direction of the X-axis When the robot moves LEFT, we move in negative direction of the X-axis We just combine the magnitudes of the UP and DOWN movements to get the final Y-component. and then combine LEFT and RIGHT magnitudes to get the final X-component. This gives us a vector (X, Y) as final position. Knowing that the robot started from (0, 0), this vector forms a right angle triangle with the X-Axis. We calculate the length of the hypotenuse to find the distance the robot has moved. We know the adjacent of the triangle is the X-component and the opposite side of the triangle is the Y-component. We use Pythagorean theorem to find the Length of the hypotenuse. Code: import math // we need this library for calculating square root finalPosition = [0, 0] //set up for finding the final position of the robot while True: //keep looping till the user stops providing input movementInput = raw_input() //take the entire line the user enters as input if not movementInput: break //stop the loop once user stops inputting direction commands movement = movementInput.split(" ") //break up the input into a list direction = movement //the direction is first item on the list steps = int(movement) //the number of steps is the second item on the list if direction == "UP": finalPosition = finalPosition + steps // positive direction of Y-axis elif direction == "DOWN" finalPosition = finalPosition - steps //negative direction of Y-axis elif direction == "LEFT" finalPosition = finalPosition - steps //negative direction of X-axis elif direction == "RIGHT" finalPosition = finalPosition + steps //positive direction of X-axis else: pass //calculate length of hypotenuse using the square root of the sum of the sqaures of the x-component and y-component distanceFromOrigin = math.sqrt(finalPosition**2 + finalPosition**2) //output the distance print finalDistance

### Subject:Computer Science (General)

TutorMe
Question:

How do you detect and remove a loop in a linked list?

Inactive
Anoop N.

This problem is divided into 2 sub-problems: 1. Detecting the loop 2. Removing the loop Detecting the loop: In order to detect a loop we will use 2 list-traversal pointers. One pointer that will go node by node and the other one that will skip a node each traversal. We will call these the slow pointer and the fast pointer. If there is no loop, the fast pointer will reach the end of the list and the slow pointer will catch up only when fast pointer has stopped moving and they both will point to Null (end of list). However, in the case of a loop in the list, the fast pointer will start cycling the loop and the slow pointer will catch up to it inside the loop eventually and they both will point to a valid node instead of the end of list NULL. That is when you know there is a loop. We will store the reference to this common node pointed to by both the slow pointer and the fast pointer as KthNodePointer. Note, this can happen be any node in the loop. This is based on the premise that the slow pointer can only catch up to the fast pointer when the fast pointer has finished traversing the list. However, if the slow pointer does catch up to the fast pointer and they are NOT at the end of the list, the only explanation is that there is a loop in the list. That's how you detect the loop. while(slowPointer != NULL) { //increment slow pointer by one Node //increment fast pointer by 2 nodes. Take care of reaching NULL in just one jump if(slowPointer == fastPointer && fastPointer !=NULL) { //loop detected KthNodePointer = slowPointer //or fastPointer, either way it points to same Node break; //break out of while loop } else { //no loop detected } } Removing the loop: Now to remove the loop, we have to find the last node in the loop and change it's nextNode pointer to NULL. This happens in 4 stages: 1. Count the number of nodes in the loop 2. Find the node at the start of the loop 3. Using the node we found in step 2, find the node at the end of the loop 4. Change the nextNode pointer of the last node in loop to NULL To count the number of nodes inside the loop, the simplest way is to start a counter called N at KthNodePointer and traverse the list till you reach KthNodePointer again, which will happen cause it's a loop. For each node you traverse, you increment the counter variable N by 1. Once you have the number of nodes in the loop. We will create 2 more pointers, one at the start of the linked list, and the other at the Nth Node. We will traverse both these pointers one by one till they meet. The node they meet at is the start of the loop. If you map this on paper, they always meet up at the start of the loop. We will create a pointer to this node called startOfLoopPointer. Taking this into account, we will now traverse the loop once more till we reach the node that points to the startOfLoopPointer. At this node, we assign NULL to the nextNode pointer to cut off the loop. //count the number of nodes in loop int N=0; tempPointer0 = KthNodePointer; do { tempPointer0 = tempPointer0->nextNode; N = N + 1; //or N++ } while (tempPointer0 != KthNodePointer); //Find the node at the start of the loop tempPointer1 = ListStartNode; //ListStartNode is usually the 0th Node in a list, not the first for(int i=0 ; i < N ; i++) //set another pointer to Nth Node { tempPointer2 = ListStartNode->nextNode; } while (tempPointer1 != tempPointer2) { tempPointer1 = tempPointer1->nextNode; tempPointer2 = tempPointer2->NextNode; } startOfLoopPointer = tempPointer1 //or tempPointer2, they will both be pointing to first node in loop //find the last node in loop do { tempPointer3=startOfLoopPointer->nextNode; }while(tempPointer3->nextNode != startOfLoopPointer) //cut off the loop tempPointer3->NextNode = NULL;

### Subject:C++ Programming

TutorMe
Question:

What are derived data types?

Inactive
Anoop N.

In order to understand derived data types, we first need to look at fundamental data types in C++. Fundamental data types are the ones in-built into the language like integer, Boolean or char. These are the simplest ways data can be represented. Let's take the analogy of a building a house. The most basic things you need to build a house are bricks, cement, iron, wood, etc. These are your fundamental data types in C++. They are Bool, char, integer, floating point and string. Break them down any further and you get gibberish in binary. And then we combine this 'brick', 'cement' and any of the other basic materials to create things like 'Walls', 'Windows', 'Doors' which can be treated as their own individual entities. They are DERIVED from your fundamental materials and we don't have to stop there. We can derive 'Rooms' from these 'Walls', 'Windows' and 'Doors'. And then a 'House' from 'Rooms', 'Bathrooms', 'Sunroof'. Now translating this to C++, consider the example of creating an employee database. Employee details are stored in a structure (a way of creating derived data types) struct Employee { String name; int age; float salary; } 'Employee' is a derived data type which is created using the fundamental types 'String', 'int' and 'float'. Now we can use this derived data type like how you would use the fundamental type and create variables with it and assign data to it and even go so far as code algebraic operations to work with it. Lets create a Employee Database using the 'Employee' data type we created. struct EmployeeDatabase { Employee listOfEmployees(100); Employee employeeOfTheMonth; String nameOfBranch; } 'EmployeeDatabase' is another derived type created using the derived type Employee and a fundamental type 'String'. There are no restrictions on how you combine these data types to create your own data types.

## Contact tutor

Send a message explaining your
needs and Anoop will reply soon.
Contact Anoop

Start Lesson

## FAQs

What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Lesson" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.
BEST IN CLASS SINCE 2015
TutorMe homepage