Diberi string, putar huruf pertama setiap kata mundur satu kata dan putar huruf terakhir setiap kata maju satu kata

Contoh masukan: "who welld horly"
Contoh output "mengapa halo dunia"

Contoh masukan: "bes le uoogit"

def solve (phrase: str) -> str:
    pass


print(solve("who welld horly"))
print(solve("bes le uoogit"))
-1
Hanish Kiran Sanghrajka 8 Januari 2021, 11:27

3 jawaban

Jawaban Terbaik

Cukup gunakan split() dan perpustakaan koleksi untuk memutar daftar.

from collections import deque

def convert(input):
    newlist = []
    listoffrontchar = []
    listofbackchar = []
    listofwords = input.split()
    count = 0
    for items in listofwords:
        listoffrontchar.append(items[0])    #collect front chars information
        listofbackchar.append(items[-1])    #collect back chars information
        listofwords[count] = items[1:-1]    #remove front and back chars in list
        count += 1

    # rotate both your front and back characters
    rotatedfrontchar = deque(listoffrontchar)
    rotatedfrontchar.rotate(-1)
    rotatedbackchar = deque(listofbackchar)
    rotatedbackchar.rotate(1)

    count = 0
    #form a new list
    for items in listofwords:
        newlist.append(rotatedfrontchar[count] + items + rotatedbackchar[count])
        count += 1
    print(newlist)

convert("who welld horly")

Memberi anda,

['why', 'hello', 'world']

Lihat komentar dalam kode untuk pemahaman yang lebih baik

0
Han 11 Januari 2021, 03:00
sentence = "who welld horly"

words = sentence.split(" ")
first_letters = [word[0] for word in words]
last_letters = [word[-1] for word in words]

cycled_first_letters = first_letters[1:] + [first_letters[0]] 
cycled_last_letters = [last_letters[-1]] + last_letters[:-1]

def change_word(word, first_letter, last_letter):
    return first_letter + word[1:-1] + last_letter
    
new_words = [change_word(word, first_letter, last_letter) for word, first_letter, last_letter in zip(words, cycled_first_letters, cycled_last_letters)]
print(" ".join(new_words))
1
K. Karatopcu 8 Januari 2021, 08:48

Memutar daftar dapat dilakukan jika Anda menggunakan pengindeksan panjang daftar modulo:

a = [1, 2, 3]
a[0] == a[3 % 3] == a[6 % 3] == a[9999 % 3] == 1  # all the same element

Anda hanya peduli tentang 1 di sebelah kiri/kanan dari posisi sebenarnya, Anda juga dapat melakukan ini dengan kondisi terner untuk pengindeksan: Saya menggunakannya untuk kasus -1 ( hanya untuk tujuan demonstrasi.

def solve (phrase: str) -> str: 
    items = phrase.split()
    ll = len(items)

    # iterate a copy so we do not use already changed letters
    for idx, word in enumerate(items[:]):
        # fix word after
        items[(idx + 1) % ll] = items[(idx + 1) % ll][:-1] + word[-1]
        # fix word before, if index negative use last word of list instead
        # items[(idx - 1) % ll] = ... would work as well
        q = -1 if idx-1 < 0 else idx - 1
        items[q] = word[0] + items[q][1:]

    return ' '.join(items)


print(solve("who welld horly"))
print(solve("bes le uoogit"))

Keluaran:

why hello world
let us boogie
0
Patrick Artner 8 Januari 2021, 08:56