在 geeksforgeeks 上找到了這個代碼塊,似乎無法掌握它。試圖在紙上寫下呼叫堆疊,仍然沒有為我點擊。
以下演算法應該檢查整數num是否為回文
# A function that returns true
# only if num contains one digit
def oneDigit(num):
# comparison operation is faster
# than division operation. So
# using following instead of
# "return num / 10 == 0;"
return ((num >= 0) and
(num < 10))
# A recursive function to find
# out whether num is palindrome
# or not. Initially, dupNum
# contains address of a copy of num.
def isPalUtil(num, dupNum):
# Base case (needed for recursion
# termination): This statement
# mainly compares the first digit
# with the last digit
if oneDigit(num):
return (num == (dupNum[0]) % 10)
# This is the key line in this
# method. Note that all recursive
# calls have a separate copy of
# num, but they all share same
# copy of *dupNum. We divide num
# while moving up the recursion tree
if not isPalUtil(num //10, dupNum):
return False
# The following statements are
# executed when we move up the
# recursion call tree
dupNum[0] = dupNum[0] //10
# At this point, if num
# contains i'th digit from
# beginning, then (*dupNum)
# contains i'th digit from end
return (num % 10 == (dupNum[0]) % 10)
# The main function that uses
# recursive function isPalUtil()
# to find out whether num is
# palindrome or not
def isPal(num):
# If num is negative,
# make it positive
if (num < 0):
num = (-num)
# Create a separate copy of
# num, so that modifications
# made to address dupNum
# don't change the input number.
dupNum = [num] # *dupNum = num
return isPalUtil(num, dupNum)
# Driver Code
n = 12321
if isPal(n):
print("Yes")
else:
print("No")
n = 12
if isPal(n) :
print("Yes")
else:
print("No")
n = 88
if isPal(n) :
print("Yes")
else:
print("No")
n = 8999
if isPal(n) :
print("Yes")
else:
print("No")
# This code is contributed by mits
在這種情況下堆疊如何作業?
uj5u.com熱心網友回復:
這段代碼試圖解決的問題是獲取數字的第一個和最后一個,第二個和倒數第二個......數字。
我們可以使用整數除法和余數運算從右邊一一得到數字的位數。但是從左邊一一獲取數字的位數就比較麻煩。雖然,你可以整數除以一個數由10^n哪里n是數字的數特定數量有減一。例如,說999 integer_divide 100equal 9。可以使用對數計算數字中的位數。這種方法為我們提供了一種無需使用遞回或將其轉換為字串即可檢查數字是否為回文的方法。
但是回到遞回問題。代碼所做的是它有一個不可變的和一個可變的數字副??本。對不可變副本的更改不會反映在呼叫堆疊的其他幀中,而對可變副本的更改會反映在其他幀中。
現在,當我們沿著遞回呼叫堆疊向下時,我們每次將不可變副本整數除以 10,直到我們達到基本情況,即不可變副本只有一位。請注意,該一位數是該數字最左邊的一位數。
現在,函式回傳。當它回傳呼叫堆疊時,我們將可變副本的剩余部分減少 10 ( mutable_copy % 10)。請注意,這是數字最右邊的數字。我們比較最左邊和最右邊。如果它們不相等,我們回傳 False。否則,我們繼續同時將可變副本整數除以 10。這個整數除法的效果是遞回堆疊中幀中的可變副本從右側少一位。這為比較該幀中的相應數字奠定了基礎。
在這里,我對代碼進行了一些編輯,以便您可以直觀地看到發生了什么:
# A function that returns true
# only if num contains one digit
def oneDigit(num):
# comparison operation is faster
# than division operation. So
# using following instead of
# "return num / 10 == 0;"
return ((num >= 0) and
(num < 10))
# A recursive function to find
# out whether num is palindrome
# or not. Initially, dupNum
# contains address of a copy of num.
def isPalUtil(num, dupNum, depth):
# Base case (needed for recursion
# termination): This statement
# mainly compares the first digit
# with the last digit
if oneDigit(num):
print("--"*depth " " "hit base case")
return (num == (dupNum[0]) % 10)
# This is the key line in this
# method. Note that all recursive
# calls have a separate copy of
# num, but they all share same
# copy of *dupNum. We divide num
# while moving up the recursion tree
print(f'{"--"*depth} We are at depth {depth}. Immutable: {num}, Mutable: {dupNum}')
if not isPalUtil(num //10, dupNum, depth 1):
return False
# The following statements are
# executed when we move up the
# recursion call tree
dupNum[0] = dupNum[0] //10
print(f'{"--"*depth} We are at depth {depth}, Immutable: {num}, Mutable: {dupNum}')
# At this point, if num
# contains i'th digit from
# beginning, then (*dupNum)
# contains i'th digit from end
return (num % 10 == (dupNum[0]) % 10)
# The main function that uses
# recursive function isPalUtil()
# to find out whether num is
# palindrome or not
def isPal(num):
# If num is negative,
# make it positive
if (num < 0):
num = (-num)
# Create a separate copy of
# num, so that modifications
# made to address dupNum
# don't change the input number.
dupNum = [num] # *dupNum = num
return isPalUtil(num, dupNum, 1)
運行isPal(n)上述產生以下結果:
-- We are at depth 1. Immutable: 123321, Mutable: [123321]
---- We are at depth 2. Immutable: 12332, Mutable: [123321]
------ We are at depth 3. Immutable: 1233, Mutable: [123321]
-------- We are at depth 4. Immutable: 123, Mutable: [123321]
---------- We are at depth 5. Immutable: 12, Mutable: [123321]
------------ hit base case
---------- We are at depth 5, Immutable: 12, Mutable: [12332]
-------- We are at depth 4, Immutable: 123, Mutable: [1233]
------ We are at depth 3, Immutable: 1233, Mutable: [123]
---- We are at depth 2, Immutable: 12332, Mutable: [12]
-- We are at depth 1, Immutable: 123321, Mutable: [1]
我建議您進一步編輯代碼,使輸出對自己更清晰。
附帶說明一下,我真的認為 GfG 的代碼無論如何都沒有教育意義。注釋冗長且變數名稱不清楚。的使用*表明 C/C 程式員在沒有適當照顧甚至可能不知情的情況下將代碼移植到 python。我的經驗是,與其他資源相比,GfG 材料的質量較低,人們如此頻繁地參考它的唯一原因是他們的鏈接在 SER 中的排名很高。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/374278.html
