+ 5
Spelling backwards in python project?
47 Réponses
+ 45
Try this code:
def spell(txt):
#your code goes here
if txt=="":
return txt
else:
print(txt[len(txt)-1])
return spell(txt[0:len(txt)-1])
txt = input()
print(spell(txt))
+ 17
string[::-1]
+ 14
def spell(txt):
print(txt[::-1])
txt = input()
spell(txt)
+ 11
def spell(txt):
#your code goes here
if txt == "":
return
else:
print(txt[-1])
return spell(txt[:len(txt) - 1])
txt = input()
spell(txt)
+ 7
With Recursive,
def spell(txt):
if txt == "":
return txt
else:
print(txt[- 1])
return spell(txt[0:len(txt) - 1])
+ 4
def reverse(text):
if text:
print(text[-1],end="")
reverse(text[:-1])
# remove 'end' argument if you need each character on its own line ;)
+ 3
def spell(txt):
i = len(txt)
while i > 0:
yield txt
i -= 1
txt = input()
for i in txt[::-1]:
spell(i)
print(i)
spell(txt)
I know this seems less efficient but it does seem like what it wants considering the lessons
+ 2
You can that the reverse of it like you would with a normal list using the slice operator:
list[start:stop:step]
So to reverse:
list[::-1]
+ 2
If you want to reverse the spelling of a string in Python, you can use slicing. Here's a simple example:
```python
word = "example"
reversed_word = word[::-1]
print(reversed_word)
```
This will output:
```
elpmaxe
```
In the example, `word[::-1]` uses slicing to reverse the string `word`. If you want to reverse the spelling of a word in a larger context within a project, you can incorporate this logic accordingly.
+ 1
If you mean the end of module project, mind that the task instruction asks us to use recursion.
+ 1
def spell(txt):
for x in range(1,len(txt)+1):
print(txt[-x])
txt = input()
spell(txt)
+ 1
I tried with Stack:
def spell(txt):
#your code goes here
txt = list(txt)
for l in range(len(txt)):
print(txt.pop())
txt = input()
spell(txt)
+ 1
this is like an adaptation from a code I already saw here, I made it shorter, and I'll add comment to the steps to explain the code. here it goes.
def spell(txt):
if txt ==' '
#if txt is empty
return None
#show nothing
else:
print(txt[-1])
#print last character
return spell(txt[:-1])
#run the fuction again this time without the last character , and keep doing that till it's empty.
+ 1
def spell(txt):
#your code goes here
word = txt[::-1]
for i in word:
print(i)
txt = input()
spell(txt)
+ 1
def spell(txt):
#your code goes here
if txt == "":
return 0
else:
print(txt[-1])
txt = txt[:-1]
spell(txt)
txt = input()
spell(txt)
+ 1
def spell(txt):
#your code goes here
if txt != "":
print(txt[-1])
spell(txt[:-1])
txt = input()
spell(txt)
Probably the easiest while still being recursive
0
Username I may understand that you think using an external c++ code to call from python can (maybe) lead to higher performance, but the cost of writing, debugging and readability is really too high.
Also, you are missing the part on how python can use that code...
And it may be a little to advanced...
0
def spell(txt):
#your code goes here
testo = []
for x in txt:
testo.append(x)
for y in testo[::-1]:
print(y)
txt = input()
spell(txt)
0
gIves the expected result
txt=input()
j=len(txt)-1
for i in txt:
print(txt[j])
j=j-1
0
def spell(txt):
v = reversed(list(txt))
for i in v:
print(i)
txt = input()
spell(txt)