打印句子中字符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)。

更新于:2023年7月25日

250 次浏览

开始你的职业生涯

通过完成课程获得认证

开始
广告