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中使用字符串操作、迭代、条件语句和内置函数来实现所需结果。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP