Python 程序根据字符的最高和最低 ASCII 值打印句子中的单词

pythonserver side programmingprogramming

ASCII(美国信息交换标准代码)是一种字符编码系统,它将每个字符表示为唯一的 7 位二进制代码,即 ASCII 值是字符的数字表示。 ASCII 值是 7 位二进制代码,范围从 0 到 127。例如,空格字符的 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:使用内置函数

使用内置函数(如 ord() 返回字符的 ASCII 值、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)。


相关文章