Question: keyboard = { ' Q ' : ( 0 , 0 ) , ' W ' : ( 0 , 1 ) , ' E

keyboard ={'Q': (0,0),'W': (0,1),'E': (0,2),'R': (0,3),'T': (0,4),'Y': (0,5),'U': (0,6),'I': (0,7),'O': (0,8),'P': (0,9),'A': (1,0),'S': (1,1),'D': (1,2),'F': (1,3),'G': (1,4),'H': (1,5),'J': (1,6),'K': (1,7),'L': (1,8),'Z': (2,0),'X': (2,1),'C': (2,2),'V': (2,3),'B': (2,4),'N': (2,5),'M': (2,6)} def getDistance(word): total_distance =0 current_pos = keyboard['Q'] # Start position at 'Q'. # Calculate the total distance for every character in the word. for char in word: next_pos = keyboard[char] # Get coordinates of the next character. # Calculate Manhattan distance between current and next position. distance = abs(current_pos[0]- next_pos[0])+ abs(current_pos[1]- next_pos[1]) total_distance += distance # Add to total distance. current_pos = next_pos # Update current position. return total_distance

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!