Python程式:查詢得分最高的單詞


在Python中,字串是一種基本資料型別,用於表示字元序列。它們用單引號('')或雙引號("")括起來,並提供廣泛的操作和方法用於操作。

示例

假設我們已經輸入了一個字串。我們現在將使用上述方法從輸入字串中找到得分最高的單詞。

輸入

inputString = "hello tutorialspoint python users and learners"

輸出

Resultant maximum scoring word from the input string:  tutorialspoint

在上面的輸入字串中,單詞tutorialspoint的字元位置值之和最大。因此,它是輸入字串中得分最高的單詞。

方法一:使用for迴圈、split()、ord()、ascii_lowercase函式

在這種方法中,我們將使用for迴圈、split()、ord()、ascii_lowercase函式的組合來查詢得分最高的單詞。

ascii_lowercase是一個預初始化的字串,在Python 3中用作字串常量。Python的字串ascii_lowercase返回小寫字母。

ord()函式(返回給定字元的Unicode程式碼/ASCII值作為數字)

ascii_lowercase(返回小寫字母)

string.split(separator, maxsplit)
ord(character)
string.ascii_lowercase

演算法(步驟)

以下是執行所需任務的演算法/步驟。

  • 使用import關鍵字匯入string模組。

  • 建立一個變數來儲存輸入字串並列印給定的字串。

  • 初始化一個值為0的變數來表示當前單詞得分。

  • 初始化另一個值為0的變數來儲存最高分。

  • 建立一個空字串來儲存輸入字串中得分最高的單詞。

  • 使用split()函式(將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空格)將輸入字串拆分為單詞列表,並使用for迴圈遍歷該列表中的單詞。

  • 將當前單詞的分數初始化為0。

  • 使用另一個巢狀的for迴圈遍歷當前單詞的每個字元。

  • 使用if條件語句檢查當前字元是否是小寫。

  • 將當前字元的ASCII值差新增到分數中。

  • 使用if條件語句檢查當前單詞的分數是否大於最高分。

  • 如果條件為true,則用當前單詞的分數更新最高分。

  • 將當前單詞更新為得分最高的單詞。

  • 列印輸入字串中得分最高的單詞的結果。

示例

以下程式使用for迴圈、split()、ord()和ascii_lowercase函式返回輸入字串中得分最高的單詞。

# importing string module
import string
# input string
inputString = "hello tutorialspoint python users and learners"
# Printing input string
print("Input string: ", inputString)
# Storing current word score
curr_score = 0
# Storing maximum score
maximumScore = 0
# empty string for storing the maximum scoring word from a string
maxScoreWord = ''
# Splitting input string into a list of words and traversing through words of that list
for word in inputString.split():
    # Initializing the score of the current word as 0
    curr_score = 0
    # Traversing through each character of the current word
    for character in word:
        # Checking whether the character is lowercase
        if character in string.ascii_lowercase:
            #  Adding the ASCII value difference of the current character to the score
            curr_score += ord(character) - 96
    # checking whether the score of the current word is greater than the maximum score
    if curr_score > maximumScore:
        # updating the maximum score with the current word score if the condition is true
        maximumScore = curr_score
        # updating the current word as the maximum scoring word
        maxScoreWord = word
# printing resultant maximum scoring word from the input string
print("Resultant maximum scoring word from the input string: ", maxScoreWord)

輸出

Input string:  hello tutorialspoint python users and learners
Resultant maximum scoring word from the input string:  tutorialspoint

方法二:使用for迴圈、sum()和ord()函式

在這種方法中,我們將學習如何使用簡單的for迴圈()、sum()和ord()函式來查詢得分最高的單詞。

sum()函式(返回iterable中所有專案的總和)

ord()函式(返回給定字元的Unicode程式碼/ASCII值作為數字)

split()函式(將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空格)

sum(iterable, start=0)
ord(character)

演算法(步驟)

以下是執行所需任務的演算法/步驟。

  • 使用split函式將輸入字串拆分為單詞列表,並使用for迴圈遍歷該列表中的單詞。

  • 透過迭代單詞並檢查它是否為小寫字母,獲得每個字元的ASCII值差的總和。

  • 使用if條件語句檢查當前單詞的分數是否大於最高分。

  • 如果條件為true,則用當前單詞的分數更新最高分。

  • 將當前單詞更新為得分最高的單詞。

  • 列印輸入字串中得分最高的單詞的結果。

示例

以下程式使用for迴圈、sum()和ord()函式返回輸入字串中得分最高的單詞。

# importing string module
import string
# input string
inputString = "hello tutorialspoint python users and learners"
# Printing input string
print("Input string: ", inputString)
# Storing current word score
curr_score = 0
# Storing maximum score
maximumScore = 0
# empty string for storing the maximum scoring word from a string
maxScoreWord = ''
# Splitting input string into a list of words and traversing through words of that list
for word in inputString.split():
    #  getting the score of current word
    curr_score = sum(
        ord(character) - 96 for character in word if character in string.ascii_lowercase)
    # checking whether the score of the current word is greater than the maximum score
    if curr_score > maximumScore:
        # updating the maximum score with the current word score if the condition is true
        maximumScore = curr_score
         # updating the current word as the maximum scoring word
        maxScoreWord = word
# printing resultant maximum scoring word from the input string
print("Resultant maximum scoring word from the input string: ", maxScoreWord)

輸出

Input string:  hello tutorialspoint python users and learners
Resultant maximum scoring word from the input string:  tutorialspoint

結論

總之,提供的程式碼演示了兩種不同的方法來從輸入字串中查詢得分最高的單詞。這兩種方法都涉及迭代輸入字串中的單詞並根據字元的ASCII值計算分數。得分最高的單詞是透過比較計算出的分數來確定的。程式碼展示了在Python中使用字串操作、迭代、條件語句和內建函式來實現所需結果。

更新於:2023年8月17日

瀏覽量:123

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.