Skip to content

added two new files #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions check_anagrams.py
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions check_semiprime.py
Original file line number Diff line number Diff line change
@@ -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