求将整数数组转换为目标数组的最少移动次数(Python实现求助)
Hey there! Let's work through this problem together—since you're new to programming (coming from CSS, no less), we'll take it nice and slow so you understand every step.
First, Let's Clarify the Problem
We need to calculate the total minimum number of moves to turn each number in Andrea's array (a) into the corresponding number in Maria's array (m). Each "move" is incrementing or decrementing a single digit of a number. For example, changing 4 to 5 takes 1 move, changing 1 to 0 takes 1 move, etc. The key here is to calculate the cost for each digit individually and sum everything up.
Let's Fix Your Existing Code Issues First
Looking at your code snippet, there are a few clear problems:
- You hardcoded
a = [4321, 3211]andm = [5432, 4302]—this will overwrite the function's input parameters! You need to remove these lines so the function uses the values passed to it. - You used
bbut never defined it—this is a typo, you meantm. - Your logic is modifying entire numbers instead of individual digits, which doesn't match the problem's requirements.
- Syntax errors: Python requires colons (
:) afterif/elifstatements, and the correct keyword iselif(notelse if).
The Core Approach: Process Each Digit Separately
The simplest way to handle individual digits (especially for a beginner) is to convert each number to a string. Strings let us easily iterate over each character (digit) without doing complex math. Here's how it works:
- Loop through each pair of numbers from
aandm(usingzip()to pair corresponding elements). - Convert each number in the pair to a string, so we can access each digit.
- For each corresponding digit pair, calculate the absolute difference (this gives the number of moves needed for that digit).
- Sum all these differences to get the total minimum moves.
Complete Working Code
#!/bin/python3 import math import os import random import re import sys # Complete the 'minimumMoves' function below. # # The function is expected to return an INTEGER. # The function accepts following parameters: # 1. INTEGER_ARRAY a # 2. INTEGER_ARRAY m # def minimumMoves(a, m): total_moves = 0 # Iterate over each pair of numbers from a and m for num_a, num_m in zip(a, m): # Convert numbers to strings to access individual digits str_a = str(num_a) str_m = str(num_m) # Iterate over each corresponding digit pair for digit_a, digit_m in zip(str_a, str_m): # Add the absolute difference of the digits to total moves total_moves += abs(int(digit_a) - int(digit_m)) return total_moves # Example test case (you can uncomment to test) # if __name__ == '__main__': # a = [4321, 3211] # m = [5432, 4302] # print(minimumMoves(a, m)) # Should output 8
Let's Walk Through the Example
Using your test case a = [4321, 3211] and m = [5432, 4302]:
- For
4321→5432:- 4→5: 1 move, 3→4:1, 2→3:1, 1→2:1 → total 4 moves
- For
3211→4302:
-3→4:1, 2→3:1, 1→0:1, 1→2:1 → total 4 moves - Total moves: 4 + 4 = 8, which is what the function returns.
Alternative: Math-Based Digit Extraction (If You're Curious)
If you want to avoid strings, you can extract digits using modulus (%) and integer division (//):
def get_digits(n): digits = [] while n > 0: digits.append(n % 10) # Get the last digit n = n // 10 # Remove the last digit return digits[::-1] # Reverse to get the correct order def minimumMoves(a, m): total_moves = 0 for num_a, num_m in zip(a, m): digits_a = get_digits(num_a) digits_m = get_digits(num_m) for d_a, d_m in zip(digits_a, digits_m): total_moves += abs(d_a - d_m) return total_moves
This works the same way, but strings are more straightforward for beginners.
Don't worry about being stuck—this is totally normal when switching from CSS to Python! Take it one step at a time, and you'll get the hang of it.
内容的提问来源于stack exchange,提问作者Eileen Richardson

