コード例 #1
0
th
index character from a nonempty

string.
"""

from functions_ex.utils import get_string


def remove_n_char(input1, index):
    """
    to remove element from nth index
    :param input1:
    :param index:
    :return:
    """
    split_input1 = list(input1)
    try:
        split_input1.pop(index - 1)
        return split_input1

    except:
        return "The index went beyound character length"


if __name__ == '__main__':
    string_input = get_string()
    index = get_string()
    result = remove_n_char(string_input, int(index))
    print("".join(result))
コード例 #2
0
Sample String : 'abc'
Expected Result : 'abcing'
Sample String : 'string'
Expected Result : 'stringly'

"""

from functions_ex.utils import get_string


def add_ing(input1):
    """
    function to add ing in any string is it doesn't have else adding ly
    :param input1:
    :return:
    """
    if len(input1) > 2:
        if 'ing' in input1:
            return input1 + 'ly'

        else:
            return input1 + 'ing'

    else:
        return input1


if __name__ == "__main__":
    input1 = get_string()
    result = add_ing(input1)
    print("Expected Result: ", result)
コード例 #3
0
"""
11. Write a Python program to count the occurrences of each word in a given
sentence.
"""

from functions_ex.utils import get_string


def character_count(sentence_input):
    """function to count number of occurence of character in a sentence"""
    count_character = {}
    for i in sentence:
        if i != ' ':
            if i not in count_character:
                count_character[i] = 1
            else:
                count_character[i] = count_character[i] + 1

    return count_character


if __name__ == "__main__":
    sentence = get_string()
    result = character_count(sentence)
    print(result)
コード例 #4
0
ファイル: q38.py プロジェクト: razyesh/insight-workshop
"""
38. Write a Python program to remove a key from a dictionary.
"""

from q33 import generate_dict
from functions_ex.utils import get_integer, get_string


def remove_key(remove_key, dic1):
    """function to remove key from a dictionery"""
    new_dic = {}
    for key, value in dic1.items():
        if key == remove_key:
            pass
        else:
            new_dic[key] = value

    return new_dic


if __name__ == "__main__":
    key_type = input("Please define type of key (s=string or i=integer): ")
    print("Enter the key to remove from dict: ")
    if key_type == 's':
        key = get_string()
    elif key_type == 'i':
        key = get_integer()
    dic1 = generate_dict()
    result = remove_key(key, dic1)
    print(result)
コード例 #5
0
2. Write a Python program to get a string made of the first 2 and the last 2 chars
from a given a string. If the string length is less than 2, return instead of the
empty string.
Sample String : 'Python'
Expected Result : 'Pyon'
Sample String : 'Py'
Expected Result : 'PyPy'
Sample String : ' w'
Expected Result : Empty String

"""

from functions_ex.utils import get_string


def new_string(string):
    if len(string) < 2:
        return "Empty String"

    elif len(string) == 2:
        return string + string

    else:
        return string[:2] + string[-2:]


if __name__ == "__main__":
    user_string = get_string()
    result = new_string(user_string)
    print(result)
コード例 #6
0
"""
4. Write a Python program to get a single string from two given strings, separated
by a space and swap the first two characters of each string.
Sample String : 'abc', 'xyz'
Expected Result : 'xyc abz'
"""

from functions_ex.utils import get_string


def string_concat_swap(string1, string2):
    """
    function to concat two string and swapping first two character
    :param string1:
    :param string2:
    :return:
    """
    original_string = string1
    for i in range(2):
        string1 = string1.replace(string1[i], string2[i])
        string2 = string2.replace(string2[i], original_string[i])

    return string1 + ' ' + string2


if __name__ == "__main__":
    input1 = get_string()
    input2 = get_string()
    result = string_concat_swap(input1, input2)
    print(f"Expected Output: {result}")
コード例 #7
0
ファイル: q26.py プロジェクト: razyesh/insight-workshop
"""
26. Write a Python program to insert a given string at the beginning of all items in
a list.

Sample list : [1,2,3,4], string : emp
Expected output : ['emp1', 'emp2', 'emp3', 'emp4']
"""

from functions_ex.utils import get_string


def append_string(sample_list, string):
    """function to append string is integer list"""
    new_list = []
    for i in sample_list:
        i = string + str(i);
        new_list.append(i)

    return new_list


if __name__ == "__main__":
    sample_list = [1, 2, 3, 4]
    string = get_string()
    result = append_string([1, 2, 3, 4], string)
    print(result)
コード例 #8
0
""" 

1. Write a Python program to count the number of characters (character
frequency) in a string. Sample String : google.com'
Expected Result : {'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1}

"""

from functions_ex.utils import get_string


def count_character(string):
    """function to count number of occurence of character"""
    character_count = {}
    for i in string:
        if i not in character_count:
            character_count[i] = 1

        else:
            character_count[i] = character_count[i] + 1 

    return character_count


if __name__ == "__main__":
    string_input = get_string()
    result = count_character(string_input)
    print(result)
コード例 #9
0
"""
12. Write a Python script that takes input from the user and displays that input
back in upper and lower cases.
"""

from functions_ex.utils import get_string


def convert_case(input1):
    """function to count number of upper and lower case in string"""
    return "Upper:" + input1.upper(), "Lower: " + input1.lower()


if __name__ == "__main__":
    input_string = get_string()
    result = convert_case(input_string)
    print(result)