Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # GCD
- def gcd(a, b):
- if b == 0:
- return a
- else:
- return gcd(b, a % b)
- num1 = int(input("Enter the first positive number: "))
- num2 = int(input("Enter the second positive number: "))
- if num1 < 0 or num2 < 0:
- print("Please enter positive numbers.")
- else:
- result = gcd(num1, num2)
- print(f"The GCD of {num1} and {num2} is {result}.")
- #count Vowel consonat balnks
- def CVBcheck(sentence):
- vowels = 'aeiou'
- consonants = 'bcdfghjklmnpqrstvwxyz'
- vowel_count = 0
- consonant_count = 0
- blank_count = 0
- for char in sentence.lower():
- if char in vowels:
- vowel_count += 1
- elif char in consonants:
- consonant_count += 1
- elif char == ' ':
- blank_count += 1
- return vowel_count, consonant_count, blank_count
- sentence = input()
- vowel_count, consonant_count, blank_count = CVBcheck(sentence)
- print(vowel_count)
- print(consonant_count)
- print(blank_count)
- #Python program to find mean, median and variance, standard deviation from a list of numbers.
- def mean(nums):
- return sum(nums) / len(nums)
- def variance(nums, avg):
- squared_diff = [(x - avg) ** 2 for x in nums]
- return sum(squared_diff) / len(nums)
- def std_dev(var):
- return var ** 0.5
- nums = []
- num_count = int(input("Enter the number of elements in the list: "))
- for i in range(num_count):
- num = float(input(f"Enter element {i + 1}: "))
- nums.append(num)
- avg = mean(nums)
- var = variance(nums, avg)
- std = std_dev(var)
- print(f"Mean: {avg}")
- print(f"Variance: {var}")
- print(f"Standard Deviation: {std}")
- #Python program to perform linear search.
- def linear_search(arr, target):
- for i in range(len(arr)):
- if arr[i] == target:
- return i
- return -1
- num_count = int(input("Enter the number of elements in the list: "))
- arr = []
- for i in range(num_count):
- num = int(input(f"Enter element {i + 1}: "))
- arr.append(num)
- target = int(input("Enter the element to search for: "))
- result = linear_search(arr, target)
- if result != -1:
- print(f"Element {target} found at index {result}.")
- else:
- print(f"Element {target} not found in the array.")
- #Python program to count number of times an item appears in the list
- def count_occurrences(lst, item):
- count = 0
- for element in lst:
- if element == item:
- count += 1
- return count
- num_count = int(input("Enter the number of elements in the list: "))
- lst = []
- for i in range(num_count):
- num = int(input(f"Enter element {i + 1}: "))
- lst.append(num)
- target = int(input("Enter the element to count occurrences for: "))
- occurrences = count_occurrences(lst, target)
- print(f"The element {target} appears {occurrences} time(s) in the list.")
- #Python program to check if two strings are anagram or not.
- def are_anagrams(str1, str2):
- str1 = str1.lower()
- str2 = str2.lower()
- return sorted(str1) == sorted(str2)
- word1 = input("Enter the first word: ")
- word2 = input("Enter the second word: ")
- if are_anagrams(word1, word2):
- print("The two words are anagrams.")
- else:
- print("The two words are not anagrams.")
- #Program to replace all occurrence of a sub-string within a string/paragraph.
- def replace_substring(text, old_substring, new_substring):
- return text.replace(old_substring, new_substring)
- text = input("Enter the text or paragraph: ")
- old_substring = input("Enter the substring to replace: ")
- new_substring = input("Enter the new substring: ")
- modified_text = replace_substring(text, old_substring, new_substring)
- print("Modified Text:")
- print(modified_text)
- #Python program to print all even length words in a string.
- def print_even_length_words(sentence):
- words = sentence.split()
- for word in words:
- if len(word) % 2 == 0:
- print(word)
- # Input the string from the user
- sentence = input("Enter a string: ")
- # Print all even-length words in the string
- print("Even-length words:")
- print_even_length_words(sentence)
- #Python program to find frequency of each character in a string.
- def character_frequency(sentences):
- sentence = sentences.lower()
- frequency = {}
- for char in sentence:
- if char in frequency:
- frequency[char] += 1
- else:
- frequency[char] = 1
- return frequency
- # Input the string from the user
- sentence = input("Enter a string: ")
- # Calculate the frequency of each character
- frequency = character_frequency(sentence)
- # Print the frequency of each character
- print("Character frequencies:")
- for char, freq in frequency.items():
- print(f"{char}: {freq}")
- #Python program to check if a string contains a number or not.
- def contains_number(string):
- for char in string:
- if char not in '0123456789':
- continue
- else:
- return True
- return False
- # Input the string from the user
- string = input("Enter a string: ")
- # Check if the string contains a number
- if contains_number(string):
- print("The string contains a number.")
- else:
- print("The string does not contain a number.")
- #min frequency
- def least_occuring_frequency(string):
- frequency = {}
- # Count the frequency of each character
- for char in string:
- if char in frequency:
- frequency[char] += 1
- else:
- frequency[char] = 1
- # Find the minimum frequency
- min_frequency = min(frequency.values())
- return min_frequency
- # Input the string from the user
- string = input("Enter a string: ")
- # Calculate the frequency of the least occurring element
- frequency = least_occuring_frequency(string)
- # Print the frequency of the least occurring element
- print("Frequency of least occurring element:", frequency)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement