python 实现
import re
def create_abbreviation(phrase):
"""
生成输入字符串的缩写,只包含字符串中的大写字母。
输入字符串仅限于A-Za-z空格、短横线和下划线。
:param phrase: 输入的英文短语或单词。
:type phrase: str
:return: 生成的缩写,由输入字符串中的大写字母组成。
:rtype: str
"""
allowed_char_reg = r'^[A-Za-z\-\_ ]+$'
if not re.match(allowed_char_reg, phrase):
raise ValueError('Input string contains invalid characters.')
# 初始化一个空字符串来存储缩写
abbreviation = ''
# 遍历输入字符串的每个字符
for char in phrase:
# 检查当前字符是否为大写字母,并且是否在允许的字符范围内
if char.isupper():
# 如果是大写字母,则将其添加到缩写字符串中
abbreviation += char
# 返回生成的缩写字符串
return abbreviation
# 测试示例
print(create_abbreviation("United States")) # 输出: US
print(create_abbreviation("Structured Query Language")) # 输出: SQL
print(create_abbreviation("Python Programming Language")) # 输出: PPL
print(create_abbreviation("PostGreSQL")) # 输出: PGSQL
print(create_abbreviation("C Sharp Programming Language")) # 输出: CSPL
print(create_abbreviation("MicroSoft Intermediate Language")) # 输出: MSIL
print(create_abbreviation("SET if Not eXists")) # 输出: SETNX
print(create_abbreviation("JavaScript Object Notation")) # 输出: JSON
print(create_abbreviation("eXtensible Markup Language")) # 输出: XML
print(create_abbreviation("Asynchronous Javascript And Xml")) # 输出: AJAX
print(create_abbreviation("HyperText Markup Language")) # 输出: HTML
print(create_abbreviation("HyperText Transfer Protocol")) # 输出: HTTP
print(create_abbreviation("Cascading Style Sheets")) # 输出: CSS
print(create_abbreviation("Document Object Model")) # 输出: DOM
print(create_abbreviation("File Transfer Protocol")) # 输出: FTP
print(create_abbreviation("Simple Mail Transfer Protocol")) # 输出: SMTP
print(create_abbreviation("HyperText Transfer Protocol Secure")) # 输出: HTTPS
javascript 实现
/**
* 生成输入字符串的缩写,只包含字符串中的大写字母。
* 输入字符串仅限于A-Za-z空格、短横线和下划线。
*
* @param {string} phrase - 输入的英文短语或单词。
* @returns {string} - 生成的缩写,由输入字符串中的大写字母组成。
*/
function create_abbreviation(phrase) {
let allowedCharReg = /^[A-Za-z\-\_ ]+$/;
if(allowedCharReg.test(phrase) === false) {
throw new Error('Input string contains invalid characters.');
}
// 初始化一个空字符串来存储缩写
let abbreviation = '';
// 遍历输入字符串的每个字符
for (let char of phrase) {
// 检查当前字符是否为大写字母,并且是否在允许的字符范围内
if (char === char.toUpperCase() && char !=char.toLowerCase()) {
// 如果是大写字母,则将其添加到缩写字符串中
abbreviation += char;
}
}
// 返回生成的缩写字符串
return abbreviation;
}
// 测试示例
console.log(create_abbreviation("United States")); // 输出: US
console.log(create_abbreviation("Structured Query Language")); // 输出: SQL
console.log(create_abbreviation("Python Programming Language")); // 输出: PPL
console.log(create_abbreviation("PostGreSQL")); // 输出: PGSQL
console.log(create_abbreviation("C Sharp Programming Language")); // 输出: CSPL
console.log(create_abbreviation("MicroSoft Intermediate Language")); // 输出: MSIL
console.log(create_abbreviation("SET if Not eXists")); // 输出: SETNX
console.log(create_abbreviation("JavaScript Object Notation")); // 输出: JSON
console.log(create_abbreviation("eXtensible Markup Language")); // 输出: XML
console.log(create_abbreviation("Asynchronous Javascript And Xml")); // 输出: AJAX
console.log(create_abbreviation("HyperText Markup Language")); // 输出: HTML
console.log(create_abbreviation("HyperText Transfer Protocol")) // 输出: HTTP
console.log(create_abbreviation("Cascading Style Sheets")) // 输出: CSS
console.log(create_abbreviation("Document Object Model")) // 输出: DOM
console.log(create_abbreviation("File Transfer Protocol")) // 输出: FTP
console.log(create_abbreviation("Simple Mail Transfer Protocol")) // 输出: SMTP
console.log(create_abbreviation("HyperText Transfer Protocol Secure")) // 输出: HTTPS
发表评论