Advertisement
brandblox

Source of all Evil

Apr 7th, 2024
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.80 KB | None | 0 0
  1. # GCD
  2. def gcd(a, b):
  3.     if b == 0:
  4.         return a
  5.     else:
  6.         return gcd(b, a % b)
  7. num1 = int(input("Enter the first positive number: "))
  8. num2 = int(input("Enter the second positive number: "))
  9.  
  10. if num1 < 0 or num2 < 0:
  11.     print("Please enter positive numbers.")
  12. else:
  13.     result = gcd(num1, num2)
  14.     print(f"The GCD of {num1} and {num2} is {result}.")
  15.  
  16.  
  17. #count Vowel consonat balnks
  18. def CVBcheck(sentence):
  19.     vowels = 'aeiou'
  20.     consonants = 'bcdfghjklmnpqrstvwxyz'
  21.     vowel_count = 0
  22.     consonant_count = 0
  23.     blank_count = 0
  24.     for char in sentence.lower():
  25.         if char in vowels:
  26.             vowel_count += 1
  27.         elif char in consonants:
  28.             consonant_count += 1
  29.         elif char == ' ':
  30.             blank_count += 1
  31.     return vowel_count, consonant_count, blank_count
  32.  
  33. sentence = input()
  34. vowel_count, consonant_count, blank_count = CVBcheck(sentence)
  35. print(vowel_count)
  36. print(consonant_count)
  37. print(blank_count)
  38.  
  39.  
  40. #Python program to find mean, median and variance, standard deviation from a list of numbers.
  41. def mean(nums):
  42.     return sum(nums) / len(nums)
  43.  
  44. def variance(nums, avg):
  45.     squared_diff = [(x - avg) ** 2 for x in nums]
  46.     return sum(squared_diff) / len(nums)
  47.  
  48. def std_dev(var):
  49.     return var ** 0.5
  50.  
  51. nums = []
  52. num_count = int(input("Enter the number of elements in the list: "))
  53. for i in range(num_count):
  54.     num = float(input(f"Enter element {i + 1}: "))
  55.     nums.append(num)
  56.  
  57. avg = mean(nums)
  58.  
  59. var = variance(nums, avg)
  60.  
  61. std = std_dev(var)
  62.  
  63. print(f"Mean: {avg}")
  64. print(f"Variance: {var}")
  65. print(f"Standard Deviation: {std}")
  66.  
  67.  
  68. #Python program to perform linear search.
  69. def linear_search(arr, target):
  70.     for i in range(len(arr)):
  71.         if arr[i] == target:
  72.             return i
  73.     return -1
  74.  
  75. num_count = int(input("Enter the number of elements in the list: "))
  76. arr = []
  77. for i in range(num_count):
  78.     num = int(input(f"Enter element {i + 1}: "))
  79.     arr.append(num)
  80.  
  81. target = int(input("Enter the element to search for: "))
  82.  
  83. result = linear_search(arr, target)
  84.  
  85. if result != -1:
  86.     print(f"Element {target} found at index {result}.")
  87. else:
  88.     print(f"Element {target} not found in the array.")
  89.  
  90.  
  91. #Python program to count number of times an item appears in the list
  92. def count_occurrences(lst, item):
  93.     count = 0
  94.     for element in lst:
  95.         if element == item:
  96.             count += 1
  97.     return count
  98.  
  99. num_count = int(input("Enter the number of elements in the list: "))
  100. lst = []
  101. for i in range(num_count):
  102.     num = int(input(f"Enter element {i + 1}: "))
  103.     lst.append(num)
  104.  
  105. target = int(input("Enter the element to count occurrences for: "))
  106.  
  107. occurrences = count_occurrences(lst, target)
  108.  
  109. print(f"The element {target} appears {occurrences} time(s) in the list.")
  110.  
  111.  
  112. #Python program to check if two strings are anagram or not.
  113. def are_anagrams(str1, str2):
  114.     str1 = str1.lower()
  115.     str2 = str2.lower()
  116.     return sorted(str1) == sorted(str2)
  117.  
  118. word1 = input("Enter the first word: ")
  119. word2 = input("Enter the second word: ")
  120.  
  121. if are_anagrams(word1, word2):
  122.     print("The two words are anagrams.")
  123. else:
  124.     print("The two words are not anagrams.")
  125.  
  126.  
  127. #Program to replace all occurrence of a sub-string within a string/paragraph.
  128. def replace_substring(text, old_substring, new_substring):
  129.     return text.replace(old_substring, new_substring)
  130.  
  131. text = input("Enter the text or paragraph: ")
  132. old_substring = input("Enter the substring to replace: ")
  133. new_substring = input("Enter the new substring: ")
  134.  
  135. modified_text = replace_substring(text, old_substring, new_substring)
  136.  
  137. print("Modified Text:")
  138. print(modified_text)
  139.  
  140.  
  141. #Python program to print all even length words in a string.
  142. def print_even_length_words(sentence):
  143.     words = sentence.split()
  144.     for word in words:
  145.         if len(word) % 2 == 0:
  146.             print(word)
  147.  
  148. # Input the string from the user
  149. sentence = input("Enter a string: ")
  150.  
  151. # Print all even-length words in the string
  152. print("Even-length words:")
  153. print_even_length_words(sentence)
  154.  
  155.  
  156.  
  157. #Python program to find frequency of each character in a string.
  158. def character_frequency(sentences):
  159.     sentence = sentences.lower()
  160.     frequency = {}
  161.  
  162.     for char in sentence:
  163.         if char in frequency:
  164.             frequency[char] += 1
  165.         else:
  166.             frequency[char] = 1
  167.  
  168.     return frequency
  169.  
  170. # Input the string from the user
  171. sentence = input("Enter a string: ")
  172.  
  173. # Calculate the frequency of each character
  174. frequency = character_frequency(sentence)
  175.  
  176. # Print the frequency of each character
  177. print("Character frequencies:")
  178. for char, freq in frequency.items():
  179.     print(f"{char}: {freq}")
  180.  
  181.  
  182.  
  183. #Python program to check if a string contains a number or not.
  184. def contains_number(string):
  185.     for char in string:
  186.         if char not in '0123456789':
  187.             continue
  188.         else:
  189.             return True
  190.     return False
  191.  
  192. # Input the string from the user
  193. string = input("Enter a string: ")
  194.  
  195. # Check if the string contains a number
  196. if contains_number(string):
  197.     print("The string contains a number.")
  198. else:
  199.     print("The string does not contain a number.")
  200.  
  201.  
  202.  
  203. #min frequency
  204. def least_occuring_frequency(string):
  205.     frequency = {}
  206.    
  207.     # Count the frequency of each character
  208.     for char in string:
  209.         if char in frequency:
  210.             frequency[char] += 1
  211.         else:
  212.             frequency[char] = 1
  213.    
  214.     # Find the minimum frequency
  215.     min_frequency = min(frequency.values())
  216.    
  217.     return min_frequency
  218.  
  219. # Input the string from the user
  220. string = input("Enter a string: ")
  221.  
  222. # Calculate the frequency of the least occurring element
  223. frequency = least_occuring_frequency(string)
  224.  
  225. # Print the frequency of the least occurring element
  226. print("Frequency of least occurring element:", frequency)
  227.  
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement