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

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

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:使用内置函数

使用内置函数如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)。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程