Day: March 14, 2021

Python Example – Write a Python program to calculate the value of ‘a’ to the power ‘b’

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate the value of ‘a’ to the power ‘b’.   Sample Solution: Python Code: def power(a,b): if b==0: return 1 elif a==0: return 0 elif b==1: return a else: return a*power(a,b-1) print(power(3,4)) Sample Output: 81   Write a Python program …

Python Example – Write a Python program to calculate the geometric sum of n-1.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate the geometric sum of n-1. Note : In mathematics, a geometric series is a series with a constant ratio between successive terms. Example :   Sample Solution: Python Code: def geometric_sum(n): if n < 0: return 0 else: return 1 …

Python Example – Write a Python program to calculate the harmonic sum of n-1

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate the harmonic sum of n-1. Note: The harmonic sum is the sum of reciprocals of the positive integers. Example:   Sample Solution: Python Code: def harmonic_sum(n): if n < 2: return 1 else: return 1 / n + (harmonic_sum(n …

Python Example – Write a Python program to calculate the sum of the positive integers

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate the sum of the positive integers of n+(n-2)+(n-4)… (until n-x =< 0).   Sample Solution: Python Code: def sum_series(n): if n < 1: return 0 else: return n + sum_series(n – 2) print(sum_series(6)) print(sum_series(10)) Sample Output: 12 30   …

Python Example – Write a Python program to get the sum of a non-negative integer.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the sum of a non-negative integer.   Sample Solution: Python Code: def sumDigits(n): if n == 0: return 0 else: return n % 10 + sumDigits(int(n / 10)) print(sumDigits(345)) print(sumDigits(45)) Sample Output: 12 9   Write a Python program …

Python Example – Write a Python program to solve the Fibonacci sequence using recursion

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to solve the Fibonacci sequence using recursion.   Sample Solution: Python Code: def fibonacci(n): if n == 1 or n == 2: return 1 else: return (fibonacci(n – 1) + (fibonacci(n – 2))) print(fibonacci(7)) Sample Output: 13   Write a Python …

Python Example – Write a Python program to get the factorial of a non-negative integer.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the factorial of a non-negative integer.   Sample Solution: Python Code: def factorial(n): if n <= 1: return 1 else: return n * (factorial(n – 1)) print(factorial(5)) Sample Output: 120   Write a Python program to get the factorial …

Python Example – Write a Python program of recursion list sum

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program of recursion list sum.   Sample Solution: Python Code: def recursive_list_sum(data_list): total = 0 for element in data_list: if type(element) == type([]): total = total + recursive_list_sum(element) else: total = total + element return total print( recursive_list_sum([1, 2, [3,4],[5,6]])) Sample Output: …

Python Example – Write a Python program to calculate the sum of a list of numbers

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to calculate the sum of a list of numbers.   Sample Solution: Python Code: def list_sum(num_List): if len(num_List) == 1: return num_List[0] else: return num_List[0] + list_sum(num_List[1:]) print(list_sum([2, 4, 5, 6, 7])) Sample Output: 24   Write a Python program to …

Python Example – Write a Python program to find the kth smallest element in a given a binary search tree

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the kth smallest element in a given a binary search tree.   Sample Solution: Python Code: class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None def kth_smallest(root, k): stack = [] while root or stack: …