Month: March 2021

Python Example – Write a Python program to convert a string to datetime

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert a string to datetime.   Sample Solution: Python Code: from datetime import datetime date_object = datetime.strptime(‘Jul 1 2014 2:43PM’, ‘%b %d %Y %I:%M%p’) print(date_object) Sample Output: 2014-07-01 14:43:00   Write a Python program to convert a string to datetime …

Python Example – Write a Python program to find the greatest common divisor (gcd) of two integers

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the greatest common divisor (gcd) of two integers.   Sample Solution: Python Code: def Recurgcd(a, b): low = min(a, b) high = max(a, b) if low == 0: return high elif low == 1: return 1 else: return Recurgcd(low, …

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 …