【每日一题1110】末位排序

:mage:t2:给定一个英文单词和空格组成的字符串words,请编写一个函数,按照每个单词最后一个字母在字母表中的顺序排列,返回重新排列后的列表。如果两个单词拥有相同的字母,则在返回结果中按照给定的原字符串中的位置进行排列。

示例:
输入:take me to semynak,输出:["take", "me", "semynak", "to"]

题目难度:简单
题目来源:codewars: Sort by Last Char

def solution(words: str) -> list:
    pass

assert solution("man i need a taxi up to ubud") == ["a", "need", "ubud", "i", "taxi", "man", "to", "up"]
assert solution("take me to semynak") == ["take", "me", "semynak", "to"]
assert solution("massage yes massage yes massage") == ["massage", "massage", "massage", "yes", "yes"]
def solution(words: str) -> list:
    word_list = [w[::-1] for w in words.split(' ')]
    word_list.sort()
    return [w[::-1] for w in word_list]


assert solution("man i need a taxi up to ubud") == ["a", "need", "ubud", "i", "taxi", "man", "to", "up"]
assert solution("take me to semynak") == ["take", "me", "semynak", "to"]
assert solution("massage yes massage yes massage") == ["massage", "massage", "massage", "yes", "yes"]

人生苦短,我学python

def solution(words:str):
    return sorted(words.split(),key=lambda x:x[-1])
2 个赞

尝试Java实现

import java.util.Arrays;

public class SortByLastChar {
    public static void main(String[] args) {
        String[] words = {"man i need a taxi up to ubud", "take me to semynak", "massage yes massage yes massage"};
        for (String word : words) {
            String[] wordsList = new SortByLastChar().sortByLastChar(word);
            System.out.println(word + " ->" + Arrays.deepToString(wordsList));
        }
    }

    public String[] sortByLastChar(String words) {
        String[] wordsList = words.split(" ");
        String[] sortByLastCharList = new String[wordsList.length];

        //生成最后一个字母 + 序号
        for (int i = 0; i < wordsList.length; i++) {
            sortByLastCharList[i] = wordsList[i].substring(wordsList[i].length() - 1) + i;
        }

        //按照最后一个字母排序
        Arrays.sort(sortByLastCharList);

        //生成新的字符串
        for (int i = 0; i < wordsList.length; i++) {
            int j = new Integer(sortByLastCharList[i].substring(1));
            sortByLastCharList[i] = wordsList[j];
        }
        return sortByLastCharList;
    }
}

运行结果:
image

1 个赞
def solution(words):
    all_word = words.split(" ")
    return sorted(all_word,key=lambda x:x[-1])
def solution(words: str) -> list:
    '''
    给定一个英文单词和空格组成的字符串words,请编写一个函数,按照每个单词最后一个字母在字母表中的顺序排列,返回重新排列后的列表。
    如果两个单词拥有相同的字母,则在返回结果中按照给定的原字符串中的位置进行排列。
    示例:输入:take me to semynak,输出:["take", "me", "semynak", "to"]。
    :param words:
    :return:
    '''
    # 将传入的字符串转换为列表,以单空格分隔、拆分出每一个单词
    content = words.split(" ") # 输出:['take', 'me', 'to', 'semynak']

    # 拆分每个单词的字母,转换成列表,进行反转(仅索引)、再拼接回一个列表。将所有完成反转的单词组装回列表,结果如:['ekat', 'em', 'ot', 'kanymes']
    content_opposite = []
    for word in content:
        word_list = "_".join(word).split("_")
        word_list.reverse()
        content_opposite.append("".join(word_list))

    # 将所有字母反转后的单词,进行排序;即将末尾排序转换为首字母排序,遵循字符串的排序规则。结果如:['ekat', 'em', 'kanymes', 'ot']
    content_opposite.sort()

    # 完成排序后,再对每一个单词进行反转,还原为原有的字符串。注意此处不做排序,结果如:['take', 'me', 'semynak', 'to']
    content_list = []
    for word in content_opposite:
        word_list = "_".join(word).split("_")
        word_list.reverse()
        content_list.append("".join(word_list))

    return content_list


assert solution("man i need a taxi up to ubud") == ["a", "need", "ubud", "i", "taxi", "man", "to", "up"]
assert solution("take me to semynak") == ["take", "me", "semynak", "to"]
assert solution("massage yes massage yes massage") == ["massage", "massage", "massage", "yes", "yes"]

def solution(words: str) → list:
‘’’
给定一个英文单词和空格组成的字符串words,请编写一个函数,按照每个单词最后一个字母在字母表中的顺序排列,返回重新排列后的列表。如果两个单词拥有相同的字母,则在返回结果中按照给定的原字符串中的位置进行排列。

示例:
输入:take me to semynak,输出:["take", "me", "semynak", "to"]。
:param words:
:return:
'''
word_list = words.split(" ")
for word in word_list:
    if words==" " or word == "":
        word_list.remove(word)
print(word_list)

def compare(dataA: str, dataB: str):
    if dataA[-1:] > dataB[-1:]:
        return -1
    elif dataA[-1:] == dataB[-1:]:
        return 0
    return 1

word_len = len(word_list)
ordered_list = []
while word_list:

    # 找最小值,并把最小值列表记录下来
    word_len = len(word_list)
    min_word_index = 0
    min_word_list = [word_list[0]]
    for j in range( 1, word_len):
        result = compare(word_list[min_word_index], word_list[j])
        if result == -1:
            min_word_index = j
            min_word_list.clear()
            min_word_list.append(word_list[j])
        elif  result == 0:
            min_word_list.append(word_list[j])

    # 当前最小值列表加入到最终的列表中
    ordered_list.extend(min_word_list)

    # 把最小值列表从原有列表中删除掉
    for word in min_word_list:
        word_list.remove(word)

print(ordered_list)
return ordered_list
pass

def test_solution():
assert solution(“take me to semynak”) == [“take”, “me”, “semynak”, “to”]
assert solution(“take”) == [“take”]
assert solution(“a b c”) == [“a”, “b”, “c”]
assert solution(“c b a”) == [“a”, “b”, “c”]
assert solution(“1a 1b 2b 2a”) == [“1a”, “2a”, “1b”,“2b”]
assert solution(“1a 2a”) == [“1a”, “2a”]

def solution(words:str):
    new_list = words.split(' ')
    new = []
    for i in new_list:
        new.append(i[-1])
    new.sort()
    words_list = []
    for i in new:
        for j in new_list:
            if j[-1] == i:
                words_list.append(j)
                new_list.remove(j)
                break
    return words_list



assert solution("man i need a taxi up to ubud") == ["a", "need", "ubud", "i", "taxi", "man", "to", "up"]
assert solution("take me to semynak") == ["take", "me", "semynak", "to"]
assert solution("massage yes massage yes massage") == ["massage", "massage", "massage", "yes", "yes"]

有没有java的参考代码呀

def solution(words: str) -> list:
    return sorted(words.split(' '),key=lambda x:x[-1])

assert solution("man i need a taxi up to ubud") == ["a", "need", "ubud", "i", "taxi", "man", "to", "up"]
assert solution("take me to semynak") == ["take", "me", "semynak", "to"]
assert solution("massage yes massage yes massage") == ["massage", "massage", "massage", "yes", "yes"]
def solution(words: str) -> list:
    l = words.split(" ")
    return  sorted(l,key=lambda x:x[-1])

assert solution("man i need a taxi up to ubud") == ["a", "need", "ubud", "i", "taxi", "man", "to", "up"]
assert solution("take me to semynak") == ["take", "me", "semynak", "to"]
assert solution("massage yes massage yes massage") == ["massage", "massage", "massage", "yes", "yes"]

def solution(words: str) -> list:
    l = words.split(' ')
    l.sort(key=lambda x:x[-1])
    return l


assert solution("man i need a taxi up to ubud") == ["a", "need", "ubud", "i", "taxi", "man", "to", "up"]
assert solution("take me to semynak") == ["take", "me", "semynak", "to"]
assert solution("massage yes massage yes massage") == ["massage", "massage", "massage", "yes", "yes"]