<jupyter_start><jupyter_text>Introduction **What?** Questions around STRING manipulation **Reverse integer** - Given an integer, return the integer with reversed digits.- Note: The integer could be either positive or negative.<jupyter_code>def solution(x):
    string = str(x)
    
    if string[0] == '-':
        return int('-' + string[1:][::-1])
    else:
        return int(string[::-1])
    
print(solution(-231))
print(solution(345))<jupyter_output>-132
543<jupyter_text>**Average words length** - For a given sentence, return the average word length. - Note: Remember to remove punctuation first.<jupyter_code>def solution(stc):
    dummy = stc
    notWanted = "!?',;."
    for p in notWanted:
        dummy = dummy.replace(p, '')
    dummySplit = dummy.split(" ")
    sum_=0
    average = sum([len(i) for i in dummySplit])/len(dummySplit)
    
    return round(average, 2)
    
str1 = "Ciao, come andiamo?"
str2 = "I need to work very hard to learn more about algorithms in Python!"
print(solution(str1))
print(solution(str2))<jupyter_output>5.0
4.08<jupyter_text>**Add strings** - Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.- Both num1 and num2 contains only digits 0-9.- Both num1 and num2 does not contain any leading zero.<jupyter_code>num1 = '364'
num2 = '1836'

# Approach 1
def solution(num1,num2):
    """
    eval() method to dynamically evaluate string-based inputs
    """
    return eval(num1) + eval(num2)
            
print(solution(num1,num2))


# Approach 2
def solution(num1,num2):
    
    return int(float(num1) + float(num2))
            
print(solution(num1,num2))


# Approach 3
def solution(num1, num2):
    """
    ord( ) function to re-build the two strings as actual numbers trough 
    the Unicode code points of their characters.
    """
    n1, n2 = 0, 0
    m1, m2 = 10**(len(num1)-1), 10**(len(num2)-1)

    for i in num1:
        n1 += (ord(i) - ord("0")) * m1 
        m1 = m1//10        

    for i in num2:
        n2 += (ord(i) - ord("0")) * m2
        m2 = m2//10

    return n1 + n2
print(solution(num1, num2))<jupyter_output>2200
2200
2200<jupyter_text>**First Unique Character** - Given a string, find the **first** AND **non-repeating** character in it and return its index. - If it doesn't exist, return -1.  Note: all the input strings are already lowercase.<jupyter_code># Approach 1
def solution(s):
    frequency = {}
    for i in s:
        if i not in frequency:
            frequency[i] = 1
        else:
            frequency[i] +=1
    for i in range(len(s)):
        if frequency[s[i]] == 1:
            return print("First and non repeating charactert: ", s[i], " with index: ", i)
    return -1

print(solution('alphabet'))
print(solution('barbados'))
print(solution('crunchy'))
# Approach 2
import collections

def solution(s):
    # build hash map : character and how often it appears
    # gives back a dictionary with words occurrence count 
    #Counter({'l': 1, 'e': 3, 't': 1, 'c': 1, 'o': 1, 'd': 1})
    count = collections.Counter(s) 
    # print(count)
    # find the index
    for idx, ch in enumerate(s):
        if count[ch] == 1:
            return idx     
    return -1

print(solution('alphabet'))
print(solution('barbados'))
print(solution('crunchy'))<jupyter_output>Counter({'a': 2, 'l': 1, 'p': 1, 'h': 1, 'b': 1, 'e': 1, 't': 1})
1
Counter({'b': 2, 'a': 2, 'r': 1, 'd': 1, 'o': 1, 's': 1})
2
Counter({'c': 2, 'r': 1, 'u': 1, 'n': 1, 'h': 1, 'y': 1})
1<jupyter_text>**Valid palindrome** -  The “Valid Palindrome” problem is a real classic the task is to check weather by removing **at most one** character, the string matches with its reversed counterpart. - When s = ‘radkar’ the function returns Trueas by excluding the ‘k’ we obtain the word ‘radar’ that is a palindrome.<jupyter_code>def solution(s):
    for i in range(len(s)):
        t = s[:i] + s[i+1:]
        if t == t[::-1]: return True

    return s == s[::-1]
s = 'radkar'  
solution(s)
# At most one character which means the same caharcter can be repeated
s = 'radkkar'  
solution(s)
s = 'radkpar'  
solution(s)<jupyter_output><empty_output><jupyter_text>**Pull the number from a string** - Pull the number out of a mixed word-number string.- https://stackoverflow.com/questions/4289331/how-to-extract-numbers-from-a-string-in-python<jupyter_code># solution
def solution(txt):
    for i in txt_.split("_"):    
        try:
            print(eval(i))
        except:
            pass
        
solution("Dense_123")
txt = "h3110 23 cat 444.4 rabbit 11 2 dog"
def solution(txt):
    print([int(s) for s in txt.split() if s.isdigit()])
solution(txt)
solution("Dense_123")<jupyter_output>[23, 11, 2]
[]
{'fraction_non_alphanumeric': 0.14622741764080766, 'fraction_numerical': 0.03825717321997875, 'mean_word_length': 3.1868327402135233, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '318710', 'license': 'OLDAP-2.3', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1599, 'n_words': 522, 'path': 'Coding_Interview_exercises/Questions around STRING manipulation.ipynb', 'repo_name': 'kyaiooiayk/Python-Programming', 'size': '4705'}