## Sum of an array
print("==========Answer 1 Sum of an array using recursive========")
# 
#This function will accept an array numbers. this function will return sum of array.
# the function will call itself until the array length is zero
def getSum(a):
    
    if len(a)==0:
        return 0
    else:
        return a[0] + getSum(a[1:])

num=[100,25,26,78 ,34, 45, 27, 57]

SumofArray=getSum(num)
print('           ')

print("Recursive function SumofArray",num, "is",SumofArray)
print('           ')
print("***********************************************************")

print("==========Answer 1 Sum of an array using function ========")

#The reduce() function accepts a function and a sequence and returns a single value calculated as follows:
from functools import reduce
num=[2,34,67,89,34,56,78]

def sum(a,b):
    return a+b
# first method using function

sumarray= reduce(sum,num)
# Second method using lambda
sumarray1= reduce(lambda a,b:a+b ,num)
print('           ')

print("using Lambda value of sum",num,"is",sumarray)
print('           ')
print("***********************************************************")
print("Using function value of sum",num,"is",sumarray1)
print('           ')
print("==========Answer2 Product of an array========")
#This function will accept an array numbers. this function will return Product of an array.
# The function will call itself until the array length is zero
import numpy as np

def mul(a):
    if len(a)==0:
        return 1
    else:
        return  a[0] * mul(np.array(a[1:]))

    
num=[10,5,25,2,100,200]

MulofArray=mul(num)
print('           ')

print("Recursive function MulofArray value",num, "is ",MulofArray)

print('           ')
print("***********************************************************")
print('           ')
print("==========Answer2 Product of an array using function========")
def mul(a,b):
    return a*b
# first method using function

mularray= reduce(mul,num)
# Second method using lambda
mularray1= reduce(lambda a,b:a*b ,num)
print('           ')
print("using function value of MulofArray value",num, "is" ,mularray)
print('           ')
print('=================================')
print('           ')
print("using lambda value of MulofArray value",num, "is",mularray1)
print('           ')
print('           ')
print("==========Answer3 Remove all odd numbers========")
#This function will accept an array numbers. this function will return an array after elemeneting the odd numbers.
# The function will call itself until the array length is zero,When the lenth of array is not 0 then the first element of the array
#will be inspected is even or not.Odd numbers 
def getodd(a):
    if len(a)==0:
        return[]
    elif  a[0]%2==1: #cheching if the number is odd
        return  [a[0]]+ getodd(a[1:])
    else : 
        return getodd(a[1:])

num=[10,1,25,2,101,200]

getoddofArray=getodd(num)
print('           ')
print("Recursive function Get_Odd No Of Array",num, "is",getoddofArray)
print('           ')
print("==========Answer3 Remove all odd numbers using function========")

def oddnum(n):
    return n%2!=0

num=[18,22,34,43,59,6,7,85,97,10,11,12,89,39,14]
# in python we have filter function that will take the value from filter
# first method using lambda
odd=list(filter(lambda n:n%2!=0 ,num))
# Second method using function
odd1 =list(filter(oddnum,num))
print('           ')
print("using lambda returns odd numbers value",num, "are",odd)
print('           ')
print('=================================')
print('           ')
print("using function returns odd numbers value",num,  "are",odd1)
print('           ')
print('           ')
print("==========Answer4 Remove all even numbers========")
#This function will accept an array numbers. this function will return Remove all even numbers.
# The function will call itself until the array length is zero
def geteven(a):
    if len(a)==0:
        return[]
    elif  a[0]%2==0:#cheching if the number is even
        #print(a[1:])
        #print("A is ",a[0] )
        #print("value is ",a[0] * getmul(a[1:]))

        return  [a[0]]+ geteven(a[1:])
    else : #a[0]%2==1:
        return geteven(a[1:])

num=[10,1,25,2,101,200]

getevenofArray=geteven(num)
print('           ')
print("Recursive function Get_even No Of Array",num, "is",getevenofArray)
print('           ')
print("=====================================")
print('           ')
print("==========Answer4 Remove all even numbers using function========")


def evennum(n):
    return n%2==0

num=[18,22,34,43,59,6,7,85,97,10,11,12,89,39,14]
# in python we have filter function that will take the value from filter
# first method using lambda
even=list(filter(lambda n:n%2==0 ,num))
# Second method using function
even1 =list(filter(evennum,num))
print('           ')
print("using lambda returns even numbers value",num ,"are",even)
print('=================================')
print('           ')
print("using function returns even numbers value ",num ,"are",even1)
print('           ')

print("==========Answer5 Replace a given character with '*'========")
print('           ')
#This function will accept an array numbers. this function will return Replace a given character with '*'.
# The function will call itself until the array length is zero
name="adarshasachan"
def remove(r, s):
    if not s:
        return ''
    if r == s[0]:# inspecting the first charactor 
        return "*"+ remove(r, s[1:])#replacing with *
    else:
        return s[0] + remove(r, s[1:])#not replacing the first charactor
# examle from name Replaceing a  character with '*'
string=remove('a',name)
print('           ')
print("Recursive function Replacing A  character to * from",name,"is" ,string)

print("=====================================")


print('           ')
print("==========Answer6 Find index in array for item.========")

#This function will accept2 parameter an array and counter. this function will return Find index in array for item..
# The function will call itself until not length is zero
def iarray(arr, cha):
  global counter , inputlen
  counter= counter + 1
  if not arr:
    return -1
  if arr[0] == cha:#checking the frist element of array is same as the string that to be found.
    arr.index(cha)
    return(counter-1)
  elif inputlen == counter-1:#if seeach element is not present the return -1
    return -1
  return iarray(arr[1:], cha)
Input1 = ['hjhj','lpls','tali']
Input2 = 'tali'
counter = 0
inputlen = 0
inputlen = len(Input1)
print('           ')
print("======================================================")
print("   String value :" +  str(Input1))
print("    string value :" +  str(Input2))
print(" Recursive function Find index in array for item in a given String :" + str(iarray(Input1, Input2)))

print("=====================================")

print('           ')
print("==========Answer7 Sum of Digits ========")
#This function will accept an  array. this function will return Sum of Digits.
# The function will call itself until the array length is zero
def sum_digit(n):
    if n > 0:# checking if number is greater than 0
        return sum_digit(n // 10) + n % 10 #
    else:
        return 0
number=14567
sumnumber=sum_digit(number)
print('           ')
print("Recursive function Sum of digit",number,"is",sumnumber)

print("=====================================")
print('           ')
print("==========Answer8 Print an Array. ========")
#This function will accept an  array. this function will return Print an array.
# The function will call itself until the the array length is 1
def Printarray(arr): 
     if len(arr)== 1: 
        return print(arr[0])
     else: 
        print(arr[0])
        return Printarray(arr[1:])
num = [1,2,13,8,5,6]
print('           ')
print("Recursive function Print an Array",num,"is")
Printarray(num)

print("=====================================")
print('           ')
print('           ')
print("==========Answer9 Find the minimum element in an array of integers. ========")
#This function will accept an  array. this function will return inimum element in an array of integers.

def Minelement(a): 
    if len(a)==2:
      
        if a[0]<a[1]:# comparing between two number 
            return a[0]
        else:
            return a[1]
    else:
        #print("value of a",a[0])
        #print("value of min ",Minelement(a[1:]))
        if a[0]<Minelement(a[1:]):
            return a[0]
        else:
            return Minelement(a[1:])


num=[15,7,9,4,6,7]
i=Minelement(num)
print('           ')
print(" Recursive function minimum element in an array of integer",num,"is",i)

print("=====================================")
print('           ')
print("==========Answer10 Verify the parentheses ========")
#This function will accept an  array. this function will Verify the parentheses.
# The function will call itself until the array length is zero
def index (counter,a):
    if len(a)==0:
        if counter==0:
            return True
        else:
            return False
    else:
        len (a)< 0
        if a[0] =="(":#if open ( found then increasing the counter
            counter=counter +1
        elif a[0]==")":#if close ) found then decreasing the counter
            counter=counter -1
        return index(counter,a[1:])
counter=0
num="((aaaa(sss()))"
#index(counter,num)
print('           ')
print('           ')
print("Recursive function to Verify the parentheses from :",num,"is .."+ str(index(counter,num)))
print("***************************")
print('           ')
print('           ')
print("END")