Month: February 2021

Python Exercise – Return true if the two given int values are equal or their sum or difference is 5

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program which will return true if the two given integer values are equal or their sum or difference is 5.   Sample Solution Python Code: def test_number5(x, y): if x == y or abs(x-y) == 5 or (x+y) == 5: return …

Python Exercise – Sum of two given integers

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will return 20.   Sample Solution: Python Code: def sum(x, y): sum = x + y if sum in range(15, 20): return 20 else: …

Python Exercise – Sum of three given integers if two values are equal sum will be zero

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sum of three given integers. However, if two values are equal sum will be zero.   Sample Solution Python Code: def sum(x, y, z): if x == y or y == z or x==z: sum = 0 else: sum …

Python Exercise – Get the least common multiple (LCM) of two positive integers

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the least common multiple (LCM) of two positive integers.   Sample Solution:- Python Code: def lcm(x, y): if x > y: z = x else: z = y while(True): if((z % x == 0) and (z % y …

Python Exercise – Compute the greatest common divisor (GCD) of two positive integers

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to compute the greatest common divisor (GCD) of two positive integers.   Sample Solution Python Code: def gcd(x, y): gcd = 1 if x % y == 0: return y for k in range(int(y / 2), 0, -1): if x …

Python Exercise – Accept the base and height of a triangle and compute the area

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program that will accept the base and height of a triangle and compute the area.   Python: Area of a triangle A triangle is a polygon with three edges and three vertices. It is one of the basic shapes in geometry. …

Python Exercise – Print out a set containing all the colors from a list

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print out a set containing all the colors from color_list_1 which are not present in color_list_2. Test Data: color_list_1 = set([“White”, “Black”, “Red”]) color_list_2 = set([“Red”, “Green”]) Expected OutputĀ : {‘Black’, ‘White’}   Sample Solution Python Code: color_list_1 = set([“White”, …

Python Exercise – Print all even numbers from a given numbers list

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print out all even numbers from a given numbers list in the same order and stop the printing if any numbers that come after 237 in the sequence. Sample numbers list: numbers = [ 386, 462, 47, 418, 907, …

Python Exercise – Concatenate all elements in a list into a string and return it

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to concatenate all elements in a list into a string and return it.   Sample Solution Python Code: def concatenate_list_data(list): result= ” for element in list: result += str(element) return result print(concatenate_list_data([1, 5, 12, 2])) Sample Output: 15122   Python …

Python Exercise – Create a histogram from a given list of integers

  (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create a histogram from a given list of integers.   Sample Solution Python Code: def histogram( items ): for n in items: output = ” times = n while( times > 0 ): output += ‘*’ times = times …