【每日一题0630】字母表顺序

给定一个字符串,用它在字母表中的位置替换每个字母。如果文本中有任何不是字母的内容,请忽略它,不要返回。例如:"a" = 1 , "b" = 2

难度:中等
来源:codewars

def alphabet_position(input_string):
    pass

msg = "The sunset sets at twelve o' clock."
assert alphabet_position(msg) == "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"
# coding: utf-8
def alphabet_position(input_string):
    """
    给定一个字符串,用它在字母表中的位置替换每个字母。如果文本中有任何不是字母的内容,请忽略它,不要返回。
    例如:"a" = 1 , "b" = 2
    """
    string_ord = [str(ord(s)-96) for s in input_string.lower() if 96 < ord(s) < 123]
    return " ".join(string_ord)


msg = "The sunset sets at twelve o' clock."
assert alphabet_position(msg) == "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"
public static ArrayList<Object> changeChart(String str) {
	ArrayList<Object>  arr = new ArrayList<>();
	for(int i=0;i<str.length();i++) {
   if(Character.isLowerCase(str.charAt(i))) {
	arr.add(str.charAt(i)-96);
   }else if(Character.isUpperCase(str.charAt(i))) {
   arr.add(str.charAt(i)-64);
   }
	}
	return arr;
}

def alphabet_position(msg:str):
    """ 字母替换 """
    msg = "The sunset sets at twelve o' clock."
    # 获取小写字母表
    lowercase_list = string.ascii_lowercase
    # 获取大写字母表
    uppercase_list = string.ascii_uppercase
    # 数据清理
    msg = msg.replace(' ', '').replace("'", "").replace(".", '')
    result = []
    for i in msg:
        try:
            result.append(lowercase_list.index(i) + 1)
        except ValueError:
            result.append(uppercase_list.index(i) + 1)
    print('输出结果:',result)

def alphabet_position(input_string):
msg = input_string.lower()
res = [str(ord(item)-96) for item in msg if ord(item) in range(97, 124) ]
return " ".join(res)

msg = “The sunset sets at twelve o’ clock.”
print(alphabet_position(msg))

import re
msg = "The sunset sets at twelve o' clock."
def alphabet_position(msg):
    return ' '.join(([str(ord(m) - 96) for m in re.sub(r'[^a-zA-Z]', '', msg).lower() if msg]))
assert alphabet_position(msg) == "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"

麻烦老师看看这样写是否ok?

def alphabet_translate(str_a):
    str_a=str_a.lower()
    res=''
    for i in str_a:
        if ord(i)>=97 and ord(i)<123:
            x=ord(i)-96
            res=res+(str(x))+' '
    return res
aaa=input()
print(alphabet_translate(aaa))
def alphabet_position(input_string):
    ll = input_string.upper()
    str1 = ''
    for l in ll:
        num = ord(l) - 64
        if 1 <= num <= 26:
            str1 = str1 + str(num) + ' '
    return str1.strip()


msg = "The sunset sets at twelve o' clock."
assert alphabet_position(msg) == "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"

题解:

import string

def alphabet_position(input_string):
    alphabet_letters = string.ascii_lowercase
    positions = [str(alphabet_letters.find(x) + 1) for x in input_string .lower() if x in alphabet_letters]
    return ' '.join(positions)

msg = "The sunset sets at twelve o' clock."
assert alphabet_position(msg) == "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"

:partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face:

  def alphabet_position(input_string):
      result = []
      for x in input_string:
          if x.isalpha():
              result.append(str(ord(x.lower()) - 96))
      return " ".join(result)
def alphabet_position(input_string):
    res_list=[]
    for i in input_string.lower():
        if i.isalpha():
            res_list.append(str(ord(i)-96))
    return ' '.join(res_list)

msg = "The sunset sets at twelve o' clock."
assert alphabet_position(msg) == "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"