Categories:Viewed: 2 - Published at: a few seconds ago

Write a function to convert the temprature celsius 'c' to fahrenheit 'f' or fahrenheit to celsius

 def temp_converter(temp,temp_given_in = 'f'):
    # Return the converted temprature
    if temp_given_in.lower() == 'f': # Convert to C
        return (temp - 32) * (5/9)
    else: # Convert to F
        return (temp * 9/5) + 32

Python code to merge dictionaries

def merge1():
    test_list1 = [{"a": 1, "b": 4}, {"c": 10, "d": 15},
                  {"f": "gfg"}]
    test_list2 = [{"e": 6}, {"f": 3, "fg": 10, "h": 1},
                  {"i": 10}]
    print("The original list 1 is : " + str(test_list1))
    print("The original list 2 is : " + str(test_list2))
    for idx in range(0, len(test_list1)):
        id_keys = list(test_list1[idx].keys())
        for key in test_list2[idx]:

            if key not in id_keys:
                test_list1[idx][key] = test_list2[idx][key]

    print("The Merged Dictionary list : " + str(test_list1))

Python program for vertical concatenating of mqatrix

def vertical_concatenation():
    test_list = [["this","is"], ["program", "for"], ["vertical","concatenation"]]
    print("The original list : " + str(test_list))
    res = []
    N = 0
    while N != len(test_list):
        temp = ''
        for idx in test_list:
            try: temp = temp + idx[N]
            except IndexError: pass
        res.append(temp)
        N = N + 1
    res = [ele for ele in res if ele]
    print("List after column Concatenation : " + str(res))
vertical_concatenation()

Python code to Get Kth Column of Matrix

def kth_column(test_list=[[4, 5, 6], [8, 1, 10], [7, 12, 5]],k=2):

    print("The original list is : " + str(test_list))
    K =k
    res = list(zip(*test_list)[K])
    print("The Kth column of matrix is : " + str(res))

Python code to print all possible subarrays using recursion

def printSubArrays(arr, start, end):
    if end == len(arr):
        return
    elif start > end:
        return printSubArrays(arr, 0, end + 1)
    else:
        print(arr[start:end + 1])
        return printSubArrays(arr, start + 1, end)

arr = [1, 2, 3]
printSubArrays(arr, 0, 0)

Python Program to find sum of nested list using Recursion

total = 0
def sum_nestedlist(l):
    global total
    for j in range(len(l)):
        if type(l[j]) == list:

            sum_nestedlist(l[j])
        else:
            total += l[j]

sum_nestedlist([[1, 2, 3], [4, [5, 6]], 7])
print(total)

Python program to find power of number using recursion

def power(N, P):
    if (P == 0 or P == 1):
        return N
    else:
        return (N * power(N, P - 1))

print(power(5, 2))

Python program to Filter String with substring at specific position

def f_substring():
    test_list = ['program ', 'to', 'filter', 'for', 'substring']
    print("The original list is : " + str(test_list))
    sub_str = 'geeks'
    i, j = 0, 5
    res = list(filter(lambda ele: ele[i: j] == sub_str, test_list))
    print("Filtered list : " + str(res))