Former story https://towardsdatascience.com/10-algorithms-to-solve-before-your-python-coding-interview-feb74fb9bc27
If an integer is given, it returns an integer with the digits reversed. Note: Integers can be positive or negative.
def solution(x):
    string = str(x)
    if string[0] == '-':
        return int('-'+string[:0:-1])
    else:
        return int(string[::-1])    
print(solution(-231))
print(solution(345))
Output:
-132
543
First, practice slicing. The point is to enter a negative integer
Returns the average word length for a given sentence. Note: Don't forget to remove the punctuation marks first.
sentence1 = "Hi all, my name is Tom...I am originally from Australia."
sentence2 = "I need to work very hard to learn more about algorithms in Python!"
def solution(sentence):
    for p in "!?',;.":
        sentence = sentence.replace(p, '')
    words = sentence.split()
    return round(sum(len(word) for word in words)/len(words),2)
    
print(solution(sentence1))
print(solution(sentence2))
Output:
4.2
4.08
In a calculation algorithm using a general character string The points are methods such as .replace () and .split ().
to be continued
Recommended Posts