Why are repeated letters skipped in some strings? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
this code is meant to 'explode' a given string s
def string_splosions(s):
"""erp9yoiruyfoduifgyoweruyfgouiweryg"""
new = ''
for i in s:
new += s[0:int(s.index(i))+1]
return new
For some reason this code can return the correct 'explosion' for most words however words which have a repeated letter they do not print correctly.
examples.
Correct outputs is would be:
Code --> CCoCodCode
abc --> aababc
pie --> ppipie
incorrect outputs when s is
Hello --> HHeHelHelHello (should be HHeHelHellHello)
(Note: in the incorrect output there should be 1 more l in the second to last repeat.)

You should transcribe the code instead of posting a picture:
def string_splosion(s):
new = ''
for i in s:
new += s[0:int(s.index(i))+1]
return new
The problem is that index(i) returns the index of the first instance of that character, which is 2 for both l's in "Hello". The fix is to just use the index directly, which is also simpler:
def string_splosion(s):
new = ''
for i in range(len(s)):
new += s[:i+1]
return new
Or even:
def string_splosion(s):
return ''.join(s[:i+1] for i in range(len(s)))

Related

Complete the recursive spell() function to produce the expected result [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 days ago.
Improve this question
def spell(txt):
txt = input()
spell(txt)
Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line.
Sample Input
HELLO
Sample Output
O
L
L
E
H
I used recursion to get output in reverse order, but didn't get exactly.
Working of spell() function:
The function works by first checking if the length of the string is 1. If it is, the function simply prints the string (since it's the last letter of the original string, and therefore the first one to be printed in reverse order). If the length of the string is greater than 1, the function prints the last letter of the string (which is the next letter to be printed in reverse order), and then calls itself recursively with the string sliced to exclude the last letter.
Code:
def spell(txt):
if len(txt) == 1:
print(txt)
else:
print(txt[-1])
spell(txt[:-1]) #sliced the last character and spell function is call again.
spell(input("Input:\n"))
Output:
Input:
HELLO
O
L
L
E
H

change the order of numbers from the back [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
they need to order with a problem
so I have the text in a certain order of numbers, something like gematria
input [12345] is what we call gematria and what do they need?
they need to line up the digits backwards
[54321]
have a different count and I would need help with that rather than doing twenty different if
def shiftall(s, n):
n %= len(s)
return s[n:] + s[:n]
it didn't help me much it only moves the simple text
For strings:
return s[::-1]
For integers:
return str(s)[::-1]
Note: This would go inside def shiftall(s, n):
Additional note: Now you don't even need the parameter n
If you want to reverse a number, then you can convert it to a string, reverse the string, and then convert it back to a number.
num = 12345
str_num = str(num)
# reverse and convert
num = int(str_num[::-1])
input=[12345,43545436,88888,843546]
def shiftall(s):
d=[]
for i in s:
res=''.join(reversed(str(i)))
d.append(int(res))
return d
print(shiftall(input))

What does i for i in s mean? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def CodelandUsernameValidation(s):
if len(s)>4 and len(s)<25 and s[0].isalpha() and [i for i in s if i.isalnum() or i=="_"]!=[] and s[-1]!="_":
return True
else:
return False
# keep this function call here
print(CodelandUsernameValidation(input()))
It's a list comprehension if you expand, it'll look like this ->
result = []
for i in s: # will fetch element one by one from iterable
if i.isalnum() or i=="_": # checking condition
result.append(i) # if true, then append it to the list
This above code can be rewritten as -
result = [i for i in s if i.isalnum() or i=="_"]
That produces a list of those characters in s that are either alphanumeric or underlines. The code is actually incorrect, because it will pass if ANY of the characters are alphanumeric or underline, whereas the intent surely was that ALL of the characters must be alphanumeric or underline. Here's a better way to write it:
def CodelandUsernameValidation(s):
return 4 < len(s) < 25 and s[0].isalpha() and all(i.isalnum() or i=='_' for i in s) and s[-1] != '_'
print(CodelandUsernameValidation(input()))

How to change iterations in a for loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
file = ['Tool','Cool','Pool']
word = 'Cool'
guesses = ['T','P']
for i in range(len(file)):
if len(file[i]) == len(word):
for j in range(len(file[i])):
print(file[i][j])
for k in range(len(guesses)):
print(guesses[k])
if file[i][j] == guesses[k]:
i + 1
In my piece of code, I am having issue moving to the next item in the list file. When file[0][0] == 'T' and guesses[0] == 'T' occurs I would like to move on to the next item in the file list. I figured i + 1 was suitable but it doesn't seem to work.
What you want to do here is to use continue, but since you have a few nested loops it won't work correctly so we can use a helper method to reduce it to one for loop and then use continue:
def find_any(what, where):
for that in what:
if that in where:
return True
return False
file = ['Tool','Cool','Pool']
word = 'Cool'
guesses = ['T','P']
for i in range(len(file)):
if len(file[i]) == len(word) and find_any(guesses, file[i]):
continue # This will skip to the next file
# Do things with file[i] here

How to print a returned tuple in another method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
If I have two methods:
one is a method that changes a string into a tuple and returns it.
other method prints out the tuple returned.
At the end how would I do this?
Input would be something like "12345 firstName lastName otherInfo".
def information(sentence):
splitUp = sentence.split(' ')
return sentence
def printAsString():
"".join(sentence)
print(sentence)
Here is one way to do it. But what is your objective? This code is kind of pointless...
def information(sentence):
'''Splits the sentence on space and returns it as tuple'''
split_up = tuple(sentence.split(' '))
return split_up
def print_as_string():
'''Prints tuple as string'''
sentence = "welcome to SO dear friend"
print(" ".join(information(sentence)))
if __name__ == "__main__":
print_as_string()
It would be
print ''.join(employeeInfo)
But my question is where on earth did employeeInfo came form?

Categories