From caba50e4ed85e02acc7f90403b6d6f16a8861529 Mon Sep 17 00:00:00 2001 From: NageshOdedra Date: Wed, 7 Aug 2024 09:39:36 +0530 Subject: [PATCH] added two new files --- check_anagrams.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ check_semiprime.py | 34 +++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 check_anagrams.py create mode 100644 check_semiprime.py diff --git a/check_anagrams.py b/check_anagrams.py new file mode 100644 index 0000000..2b3b857 --- /dev/null +++ b/check_anagrams.py @@ -0,0 +1,50 @@ +# Problem: Two strings of sizes m and n are given, +# we have to find how many characters need to be +# removed from both the string so that they become +# anagrams of each other + +# Anagrams: Words that are made from rearranging the letters of another word + +# Algorithm: We will use dictionaries to keep track of characters. +# The idea is to get the common occuring characters and derive +# uncommon characters + +import string + +# letters will be a string of the form "abc...xyz" +# CHARACTER_HASH looks like this {'a'0, 'b':0 ...., 'z':0} +letters = string.ascii_lowercase +CHARACTER_HASH = dict(zip(letters, [0] * len(letters))) + + +# This method will mark all the letters occuring in 'text_a' +def mapLettersToHash(text_a): + for char in text_a: + if char in CHARACTER_HASH.keys(): + CHARACTER_HASH[char] += 1 + + +# This method will count the letters present in 'text_b', also found in 'text_a' +# These will be charcaters whose frequency in HASH is greater than zero +def computeCommonLetters(text_b): + common_letters = 0 + for char in text_b: + if CHARACTER_HASH[char] > 0: + common_letters += 1 + return common_letters + + +# Now we derive how many uncommon letters are present, +# This is done by subtracting twice the count of common letters +# from the total length of both the strings +def computeUncommonLetters(text_a, text_b, common_letters): + return abs(len(text_a) + len(text_b) - (2 * common_letters)) + +if __name__ == "__main__": + text_1 = "hello" + text_2 = "billion" + + mapLettersToHash(text_1) + common = computeCommonLetters(text_2) + result = computeUncommonLetters(text_1, text_2, common) + print(result) diff --git a/check_semiprime.py b/check_semiprime.py new file mode 100644 index 0000000..3b48682 --- /dev/null +++ b/check_semiprime.py @@ -0,0 +1,34 @@ +# Context: A semiprime is a product of two prime +# numbers, not necessarily distinct. +# Squares of prime numbers are also semiprimes. + +# Problem: Find the numbers which are semiprimes, +# within a given range. For e.g. 1 to 100. + + +def isSemiprime(num): + # start with the smallest prime + prime = 2 + # initialize counter to 0 + count = 0 + # Design of while loop: + # 1. if count exceeds 2, it is not a semiprime, e.g. 30 = 2*3*5 + # 2. when the number becomes 1, we have found the second prime + while count < 3 and num != 1: + # if the number is divisible by current prime, + # increment count, else move to new prime + if not (num % prime): + num = num / prime + count = count + 1 + else: + prime = prime + 1 + # if count is two, given number is a semiprime + return count == 2 + + +for i in range(1, 100): + if isSemiprime(i): + print(i, end=" ") + +# Result: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 +# 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95