列印句子中字元ASCII值最高和最低的單詞的Python程式
ASCII(美國資訊交換標準程式碼)是一種字元編碼系統,它將每個字元表示為唯一的7位二進位制程式碼,即ASCII值是字元的數值表示。ASCII值是範圍從0到127的7位二進位制程式碼。例如,空格字元的ASCII程式碼為32,數字“1”的ASCII程式碼為49,類似地,每個字元在ASCII表中都有其對應的ASCII程式碼。
在Python中,可以使用預定義函式ord()計算字元的ASCII程式碼,該函式以字元作為輸入並返回該字元的ASCII程式碼。
例如,ord(‘A’)返回65。
問題陳述
給定一個字串S。列印具有字元ASCII值平均值最高和最低的單詞。
示例1
輸入
S = “today is a sunny day”
輸出
Highest ASCII value = “sunny” Lowest ASCII value = “a”
解釋
Average of ASCII values: today = (116 + 111 + 100 + 97 + 121) / 5 = 109 is = (105 + 115) / 2 = 110 a = 97 / 1 = 97 sunny = (115 + 117 + 110 + 110 + 121) / 5 = 114.6 day = (100 + 97 + 121) / 3 = 106 Thus, “sunny” has the highest average and “a” has the lowest average.
示例2
輸入
S = “pink city”
輸出
Highest ASCII value = “city” Lowest ASCII value = “pink”
解釋
Explanation: Average of ASCII values: pink = (112 + 105 + 110 + 107) / 4 = 108.5 city = (99 + 105 + 116 + 121) / 4 = 110.25 Thus, “city” has the highest average and “pink” has the lowest average.
方法1:暴力法
該問題的暴力解決方案是將輸入句子分割成單詞,然後計算每個單詞的ASCII值的平均值,以找到字元ASCII值平均值最高和最低的單詞。
為了將輸入字串轉換為單詞列表,我們使用內建的split()函式。
虛擬碼
procedure ascii_avg (word)
sum = 0
For each character c in word
sum = sum + ord(c)
end for
avg = sum / length(word)
ans = avg
end procedure
procedure highLow (sentence)
words = split sentence by whitespace
high_word = words[0]
low_word = words[0]
high_avg = ascii_avg(words[0])
low_avg = ascii_avg(words[0])
for i = 1 to length(words) - 1
word = words[i]
avg = ascii_avg(word)
if avg > high_avg
high_word = word
high_avg = avg
else if avg < low_avg
low_word = word
low_avg = avg
end if
end for
print "Highest ASCII value:", high_word
print "Lowest ASCII value:", low_word
end procedure
示例:Python實現
在下面的程式中,我們使用split函式將句子分割成單詞。
def ascii_avg(word):
"""
Returns the average ASCII value of a word.
"""
return sum(ord(c) for c in word) / len(word)
def highLow (sentence):
"""
Prints the words with the highest and lowest average ASCII values in a sentence.
"""
words = sentence.split()
high_word, low_word = words[0], words[0]
high_avg, low_avg = ascii_avg(words[0]), ascii_avg(words[0])
for word in words[1:]:
avg = ascii_avg(word)
if avg > high_avg:
high_word, high_avg = word, avg
elif avg < low_avg:
low_word, low_avg = word , avg
print("Highest ASCII value:", high_word)
print("Lowest ASCII value:", low_word)
highLow("today is a sunny day")
輸出
Highest ASCII value: sunny Lowest ASCII value: a
方法2:使用堆
解決該問題的另一種方法是使用堆來維護到目前為止最高和最低的ASCII值。此外,我們維護一個字典來將單詞對映到它們的ASCII值,並使用堆提取最高和最低值。
虛擬碼
procedure highLow(sentence)
words = split sentence by whitespace
word_avg = {}
for each word in words
avg = 0
for each character c in word
avg = avg + ord(c)
end for
avg = avg / length(word)
word_avg[word] = avg
end for
high_heap = []
low_heap = []
for word, avg IN word_avg.items()
heapq.heappush(high_heap, (-avg, word))
heapq.heappush(low_heap, (avg, word))
end for
high_word = heapq.heappop(high_heap)[1]
low_word = heapq.heappop(low_heap)[1]
print "Highest ASCII value:", high_word
print "Lowest ASCII value:", low_word
end procedure
示例:Python實現
在下面的程式中,我們使用堆來跟蹤最高和最低的ASCII值,並使用字典來對映值。
import heapq
def highLow(sentence):
"""
Prints the words with the highest and lowest average ASCII values in a sentence.
"""
words = sentence.split()
word_avg = {word: sum(ord(c) for c in word) / len(word) for word in words}
high_heap = [(-avg, word) for word, avg in word_avg.items()]
low_heap = [(avg, word) for word, avg in word_avg.items()]
heapq.heapify(high_heap)
heapq.heapify(low_heap)
high_word = heapq.heappop(high_heap)[1]
low_word = heapq.heappop(low_heap)[1]
print("Highest ASCII value:", high_word)
print("Lowest ASCII value:", low_word)
highLow("today is a sunny day")
輸出
Highest ASCII value: sunny Lowest ASCII value: a
方法3:使用內建函式
使用內建函式,如返回字元ASCII值的ord(),以及用於查詢最大值和最小值的max()和min(),可以解決該問題。
虛擬碼
procedure highLow(sentence) words = split sentence by whitespace high_word = max(words, key=lambda w: sum(ord(c) for c in w) / len(w)) low_word = min(words, key=lambda w: sum(ord(c) for c in w) / len(w)) print "Highest ASCII value:", high_word print "Lowest ASCII value:", low_word end procedure
示例:Python實現
在下面的程式中,我們使用內建的Python函式來查詢具有最高和最低ASCII值的單詞。
def highLow(sentence):
"""
Prints the words with the highest and lowest average ASCII values in a sentence.
"""
words = sentence.split()
# min() and max() are built-in functions
high_word = max(words, key=lambda w: sum(ord(c) for c in w) / len(w))
low_word = min(words, key=lambda w: sum(ord(c) for c in w) / len(w))
print("Highest ASCII value:", high_word)
print("Lowest ASCII value:", low_word)
highLow("today is a sunny day")
輸出
Highest ASCII value: sunny Lowest ASCII value: a
時間複雜度 - O(nlogn)
空間複雜度 - O(n)
方法4:按平均ASCII值對單詞進行排序
透過根據單詞的平均ASCII值對單詞進行排序,我們可以從最後一個元素找到最高值,從第一個元素找到最低值。
虛擬碼
procedure highLow (sentence) words = split sentence by whitespace words_sorted = sort words by key=lambda w: sum(ord(c) for c in w) / len(w) print "Highest ASCII value:", last word in words_sorted print "Lowest ASCII value:", first word in words_sorted end procedure
示例:Python實現
在下面的程式中,我們根據單詞的平均ASCII值對單詞進行排序。
def highLow(sentence):
"""
Prints the words with the highest and lowest average ASCII values in a sentence.
"""
words = sentence.split()
# Sorts the words in ascending order
words_sorted = sorted(words, key=lambda w: sum(ord(c) for c in w) / len(w))
print("Highest ASCII value:", words_sorted[-1])
print("Lowest ASCII value:", words_sorted[0])
highLow("today is a sunny day")
輸出
Highest ASCII value: sunny Lowest ASCII value: a
時間複雜度 - O(nlogn)
空間複雜度 - O(n)
結論
總之,要查詢具有最高和最低平均ASCII值的單詞,我們可以使用上述任何一種方法,其中一些方法易於理解,但時間複雜度較高,為O(n^2),但使用內建函式可以將其降低到O(nlogn)。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP