Can someone explain this Python code to me? [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 1 year ago.
Improve this question
def spin_words(sentence):
# Your code goes here
return " ".join([x[::-1] if len(x) >= 5 else x for x in sentence.split(" ")])
I don't understand this one.

sentence.split(" ") gets all the words in the sentence input parameter by separating when it finds a whitespace.
for x in sentence.split(" ") applies the loop to each word resulting from the previous statement
x[::-1] if len(x) >= 5 else x reverses the word if its length is greater than 5
return " ".join returns the result of the previous statement, joining the words with whitespaces

Related

Replacing a string's character with a letter [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 8 days ago.
Improve this question
I'm coding hangman and the problem I'm facing is that i cant fill in the blanks of the word with the correct location with the letters.
I want to be able to type in a letter and it'll fill in the blank with that letter at the correct location.
code block:
#converts selected word into a array
hidden_word = input('Enter a word ')
List = [m[0] for m in hidden_word]
x = len(hidden_word)
d = [(('_')*x)]
d = "".join(d)
print(d)
#fills in gaps with letter you've guessed
first_guess = input('Guess a letter ')
if first_guess in List:
y = str.find(hidden_word, first_guess)
count = List.count(first_guess)
f = d.replace(d[y],first_guess,count)
print(f)
print('YESS')
else:
print('NOO')

Determine if the string is palindrome [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 last year.
Improve this question
This function takes a string as input and returns True if string is palindrome and
False otherwise. A palindrome is a symmetric sequence of characters, reading
the same forward and backward.
For example: radar, anna, mom, dad, …
You can do this like the below:
def is_palindrome(phrase):
string = ""
for char in phrase:
if char.isalnum():
string += char
print(string)
return string[::-1].casefold() == string.casefold()
word = input("Please enter a word to check: ")
if is_palindrome(word):
print("'{}' is a palindrome".format(word))
else:
print("'{}' is not a palindrome".format(word))

Alphabatic Anagrams doesn't work with words that contains letters with multiple occurence [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
Hello how are you? I have a challenge https://www.codewars.com/kata/53e57dada0cb0400ba000688/solutions/python and I've stuck on it for a 2 days, I created the first solution but it was slow(using itertools.permutations), and then I've make this block of code.
import math
def listPosition(word):
n = 0
wlsort = list(word)
wlsort.sort()
wcopy = word
while wcopy:
for i in range(wlsort.index(wcopy[0])):
n += math.factorial(len(wlsort) - 1)
wlsort.pop(wlsort.index(wcopy[0]))
wcopy = wcopy[1:]
return n + 1
but it doesn't work with words like test bookkeeper, Do you have any hints, or idea on how to solve it?
I think my problem is that when I have a word like car it will brute-force it like ->
acr, arc...
and if i have caar it will brute-force it like ->
aacr, aarc and it doesn't do acar because it is brute-forcing alphabetically.
Here is my attempt. It's quite straightforward, if you read the req. carefully. Please let me know if you have any questions.
from math import factorial as fact
def listPosition(word):
count = 0
while len(word):
first = word[0]
uniqs = set(word)
possibles = fact(len(word))
for ch in uniqs:
possibles /= fact(word.count(ch))
for ch in uniqs:
if ch < first:
count += possibles/len(word) * word.count(ch)
word = word[1:]
return count + 1

Sort the sequence alphabetically-Python [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 1 year ago.
Improve this question
I want to write a python function that accepts a hyphen separated sequence of colors as input and returns the colors in a hyphen separated sequence after sorting them alphabetically
constraint: All colors will be completely in either lower case or upper case
sample input: green-red-yellow-black-white
sample output: black-green-red-white-yellow
My code -
my_str = input("Enter a string: ")
words = [word.lower() for word in my_str.split()]
words.sort()
print("The sorted words are:")
for word in words:
print(word)
This works -
def str_sort(string):
b = string.split('-')
b.sort()
c = '-'.join(b)
return c
a = input('Enter a sequence of colors separated with hyphen: ')
str_sort(a)
This will solve your problem:
inputs = "green-red-yellow-black-white"
def sort_inputs(inputs):
inputs_list = inputs.split('-')
sorted_input = sorted(inputs_list)
final = "-".join(term for term in sorted_input)
return final
final = sort_inputs(inputs)
print(final)

How to replace single word in string not replacing other with same chars [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 6 years ago.
Improve this question
I have this code:
s = 'letter of letters'
for i in s.split(" "):
if len(i) > 4:
s = s.replace(i, i[::-1])
print s
But this prints "rettel of rettels" which means code replace all 'letter' in string, but i need to replace only by every word themself.
How it possible to chagne single word but not all in string? I need 'rettel of srettel'
You need to collect all the modified words separately, instead of modifying the string in your loop:
words = s.split(' ')
results = []
for word in words:
if len(word) > 4:
results.append(word[::-1])
else:
results.append(word)
print(' '.join(results))

Categories

Resources