Jump to content

Python: Reverse Word Order ( Ordinea inversă a cuvintelor )


Recommended Posts

Posted

Task: Scrieți un program ( folosind funcții ) Care solicită utilizatorului un șir alcatuit din mai multe cuvinte. Să se afișeze pe ecran același șir, imprimând cuvintele de la coada la cap.

Exemplu:

Daca introducem șirul:

Ana are mere

Rezultatul afișat pe ecran va fi:

mere are Ana

 

reverse-word.png

 

reverse-word2.png

 

Sursa:

Spoiler

def function(word):
          word = word.split(" ")
          word.reverse()
          return " ".join(word)


if __name__ == "__main__":
          word = input("Introduceti cuvantul: ")
          list_of_words = function(word)
          
          for i in list_of_words:
                    print(i,end="")

 

Funcție scrisă într-o linie de cod:

Spoiler

def function(word):

return " ".join(word.split()[::-1])

 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...