- Prepare
- All Platforms
- Programming
- Aptitude
- Syllabus
- Interview Preparation
- Interview Exp.
- Off Campus
- Prime Video
- Prime Mock

- Prime Video
- OffCampus
- Internship
- Placement Stats
- Prime Video
- Prime Mock

^{0}Notifications Mark All Read

No New notification

- Login
- Get Prime

# Python Code for Antikythera Mechanism (TCS Codevita) | PrepInsta

**Antikythera Mechanism**

TCS CodeVita is a coding competition organized by TCS every year, in search of world’s best coder. This is a global level coding competition in which coders from all around the world compete for the title of World’s Best Coder. Antikythera Mechanism is one of the sample problem of this year TCS CodeVita season 11 competition.

**Question -: **During expedition in the shipwreck near coast an archaeologist found an artefact with gears inside it.Later it was referred as Antikythera Mechanism.

You got the X-ray of the mechansim. From that you found it has lot of gears coupled in complicated ways.To understand the working of this mechanism you are going to study the effects of gears in action.

You will be given the position and size of each gear in order. Find how many times does the last gear rotate for each time the first gear rotates. It is not necessary that all the gears coupled to the first one including the last one.

The gear details will be given like

X Y Rr

X,Y = > coordinates of the centre of the gear

Rr => The reference radius of the gearA gear will rotate another gear if the imaginative circles made by the radius Rr from their respective centres touch externally at a point.

Now consider a gear chain of N gears. Also consider gear 1 with Rr of r1 units, gear 2 of r2 units, gear 3 of r3 units and so on.The relation between Rr and rotation is given by

n1 * r1 = n2 * r2

where n1 is the number of times the gear 1 rotates and n2 is the number time gear 2 rotates.

n2 can be calculated using the formula

n2 = ( n1 * r1 ) / r2

now continuing for gear 2 and gear 3

=> n2 * r2 = n3 * r3

we can calculate n3 no of times the third gear will rotate, using

=> n3 = ( n2 * r2 ) / r3

By substituting the value calculated for n2 one can calculate n3 i.e. no of times gear 3 rotates with respect to n1. If we continue like this till the last gear. one can calculate the nN i.e. no of times the gear N rotates given n1 i.e number of rotations of gear 1.

At every step the direction of rotation will change. If gear 1 is rotating clockwise direction then gear 2 will rotate in anti clockwise direction. If gear 1 and gear N rotate in same direction, print the output as postive integer. else negative integer.

The possible cases where processing fails are as follows

- the Gear 1 and Gear N gears connect to two or more other gears
- the Gear 1 or Gear N are not connected any gear
- Any gear in the chain between Gear 1 and Gear N are connected to more than two gears
- Any gear in the chain between Gear 1 and Gear N is connected to less than two gears

If gear chain exist between Gear 1 and Gear N, then

print the no of times the Gear N rotates. this number can be positive or negative depending on direction as explained earlier

Else print “Could Not Process”

**Constraints**

0 < N < 50

0 < X < 100

0 < Y < 100

0 < Rr < 10

**Input**

First line contains an integer N which gives the total number of gears

Next line will have the three integers X Y Rr seperated by space representing gear details as mentioned above

The Input may consists of gears that is not in the gear chain between Gear 1 and Gear N.

Gears will not overlap each other.

**Output**

Print single float value upto two decimal places denoting the number of times the Gear N will rotate if Gear 1 rotates one time. else print “Could Not Process”

**Time Limit (secs)**

1

**Examples**

**Example 1**

**Input**

4

1 1 1

4 1 2

1 4 1

4 4 1

**Output**

1.00

**Explanation**

Input consist of 4 gears. X Y co ordinates and the Reference radius is provided in the input.

Upon computation it is clear that a chain of gear exist between Gear 1 and Gear 4. The Gear chain would be Gear 1, Gear 2 and Gear 4.

The Gear 1 is connected directly to the Gear 2. Here Rr of Gear 1 is 1 and the Rr of Gear 2 is 2.So given gear 1 is rotated 1 time in clockwise direction, gear 2 will rotate 0.5 time in anticlockwise direction. Next the Gear 2 is connected to Gear 4 i.e. Gear N. Gear 2 rotating 0.5 time in anti clockwise direction will make Gear 4 rotate 1 time in clockwise direction bcoz of reference radius of Gear 4 is 1. Since Gear 4 and Gear 1 both rotating in same direction, the output is a positive number. Since Gear 4 rotation is 1 the output has to be printed 1.00. Note that Gear 3 is not in the gear chain between Gear 1 and Gear 4.

**Example 2**

**Input**

5

4 9 2

7 9 1

9 9 1

7 7 1

6 4 1

**Output**

Could Not Process

**Explanation**

Gear 1, Gear 3 and Gear 4 are connected to Gear 2. This violates the criteria and since there is no other gear chain that can reach from Gear 1 to Gear 5 without constraint violation, output should be printed as “Could Not Process”.

def process_gear_chain(N, gears): gear_dict = {} for i in range(N): gear_dict[i + 1] = {'X': gears[i][0], 'Y': gears[i][1], 'Rr': gears[i][2], 'connections': set()} for i in range(N): for j in range(i + 1, N): distance = ((gears[i][0] - gears[j][0])**2 + (gears[i][1] - gears[j][1])**2)**0.5 if distance == gears[i][2] + gears[j][2]: gear_dict[i + 1]['connections'].add(j + 1) gear_dict[j + 1]['connections'].add(i + 1) # Check for Gear 1 and Gear N connections if len(gear_dict[1]['connections']) != 1 or len(gear_dict[N]['connections']) != 1: return "Could Not Process" # Check for intermediate gears' connections for i in range(2, N): if len(gear_dict[i]['connections']) != 2: return "Could Not Process" # Calculate rotation for each gear n1 = 1 for i in range(1, N): gear = gear_dict[i] next_gear = gear_dict[list(gear['connections'])[0] if i == 1 else list(gear['connections'])[1]] n2 = (n1 * gear['Rr']) / next_gear['Rr'] n1 = n2 # Check if Gear 1 and Gear N rotate in the same direction if (gears[0][2] > 0 and gears[N - 1][2] > 0) or (gears[0][2] < 0 and gears[N - 1][2] < 0): return "{:.2f}".format(n1) else: return "{:.2f}".format(-n1) # Input reading N = int(input()) gears = [] for _ in range(N): gear = list(map(int, input().split())) gears.append(gear) # Process and output result = process_gear_chain(N, gears) print(result)

Login/Signup to comment