<jupyter_start><jupyter_text>**100 Codes in Python Programing Part 2** **This file contains 26-50 codes**  Code 26 of 100Displaying the highest common factor (H.C.F) or greatest common divisor (G.C.D) of two numbers which is the largest positive integer that perfectly divides the two given numbers.<jupyter_code>def hcfFn(x, y):

    if x > y:
        smaller = y
    else:
        smaller = x
    for i in range(1, smaller+1):
        if((x % i == 0) and (y % i == 0)):
            hcf = i 
    return hcf

num1 = int(input("Enter the first number: ")) 
num2 = int(input("Enter the second number: "))

print("The H.C.F. is", hcfFn(num1, num2))<jupyter_output>Enter the first number: 60
Enter the second number: 6
The H.C.F. is 6<jupyter_text>Code 27 of 100Computing the LCM-least common multiple of two numbers<jupyter_code>def lcmFn(x, y):

   if x > y:
       greater = x
   else:
       greater = y

   while(True):
       if((greater % x == 0) and (greater % y == 0)):
           lcm = greater
           break
       greater += 1

   return lcm

num1 = int(input("Enter the first number: ")) 
num2 = int(input("Enter the second number: "))

print("The L.C.M. is", lcmFn(num1, num2))<jupyter_output>Enter the first number: 80
Enter the second number: 2
The L.C.M. is 80<jupyter_text>Note: There is a convension that if we know the **HCF** then we can easily find out the **LCM** of any two given numbers.**Formula:** Number1 X Number2 = L.C.M. X G.C.D.Code 28 of 100 Computing LCM Using HCF<jupyter_code>def hcfFn(x, y):

   while(y):
       x, y = y, x % y
   print("\nThe HCF of the given numbers is: ",x)
   return x

def compute_lcm(x, y):
   lcm = (x*y)//hcfFn(x,y)
   print("The product of the given numbers is: ",x*y)
   return lcm


num1 = int(input("Enter the first number: ")) 
num2 = int(input("Enter the second number: "))

print("The L.C.M. is", compute_lcm(num1, num2))<jupyter_output>Enter the first number: 56
Enter the second number: 2

The HCF of the given numbers is:  2
The product of the given numbers is:  112
The L.C.M. is 56<jupyter_text>Code 29 of 100Shuffling Deck of Cards using random module.Shuffling a deck of cards has been the evergreen game since foreverbut how do we do it in python?Well that can be very easily done using the random module so let's have a look at the following code<jupyter_code>import itertools, random

deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))

random.shuffle(deck)

print("You got:")
for i in range(5):
   print(deck[i][0], "of", deck[i][1])<jupyter_output>You got:
9 of Heart
6 of Club
13 of Heart
9 of Club
3 of Diamond<jupyter_text>Code 30 of 100Printing a calender<jupyter_code>import calendar
yy = int(input("Enter the year: "))
mm = int(input("Enter the month: "))
print("\n")
c = calendar.TextCalendar(calendar.SUNDAY) #Sunday is the first day in the calendar 
st = c.formatmonth(yy, mm, 0, 0) #(year, month, horizontal space, vertical space)
print(st)<jupyter_output>Enter the year: 2023
Enter the month: 6


     June 2023
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30<jupyter_text>Code 31 of 100Let us suppose that I want to have meetUps with my team on the first Friday of every month but i want to figure out when is the first friday of every month!!This can be done with the following program:<jupyter_code>import calendar
yy = int(input("Enter the year: "))
print("\n Team meetings will be on: ")
for m in range(1, 13):
    cal = calendar.monthcalendar(yy, m)
    weekone = cal[0]
    weektwo = cal[1]

    if weekone[calendar.FRIDAY] != 0:
        meetday = weekone[calendar.FRIDAY]
    else:
        meetday = weektwo[calendar.FRIDAY]

    print("%2d of %s" % ( meetday, calendar.month_name[m]))<jupyter_output>Enter the year: 2021

 Team meetings will be on: 
 1 of January
 5 of February
 5 of March
 2 of April
 7 of May
 4 of June
 2 of July
 6 of August
 3 of September
 1 of October
 5 of November
 3 of December<jupyter_text>Code 32 of 100Analysing the datetime module of python<jupyter_code>#this program shows my current date-time ie. at the time when I ran this program, when you run this code it will show you your current date-time.

from datetime import date
from datetime import time
from datetime import datetime

today= date.today()
print("Today's date is :", today)

print("Date components: ", today.day,":",today.month,":",today.year)
   
print("Today's weekday number is: ", today.weekday())
  
days = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]
print("Which is a", days[today.weekday()])
  
today = datetime.now()
print("The current date and time is ", today)

t = datetime.time(datetime.now())
print("The current time is", t)<jupyter_output>Today's date is : 2021-07-23
Date components:  23 : 7 : 2021
Today's weekday number is:  4
Which is a friday
The current date and time is  2021-07-23 06:36:50.105280
The current time is 06:36:50.105441<jupyter_text>Code 33 of 100Date-Time module<jupyter_code>from datetime import datetime

n = datetime.now()

  #### Date Formatting ####
  
  # %y/%Y - Year, %a/%A - weekday, %b/%B - month, %d - day of month

print(n.strftime("The current year is: %Y"))
print(n.strftime("The date today is %d %B %Y" ))
print(n.strftime("%A %d %B %Y"))

  # %c - locale's date and time, %x - locale's date, %X - locale's time
  
print(n.strftime("Locale date and time: %c")) # %C make no sense!
print(n.strftime("Locale date: %x"))
print(n.strftime("Locale time: %X"))

  #### Time Formatting ####
  
    # %I/%H - 12/24 Hour, %M - minute, %S - second, %p - locale's AM/PM
print(n.strftime("The current time is: %I:%M:%S %p")) 
print(n.strftime("The current time is: %H:%M"))<jupyter_output>The current year is: 2021
The date today is 24 July 2021
Saturday 24 July 2021
Locale date and time: Sat Jul 24 11:28:52 2021
Locale date: 07/24/21
Locale time: 11:28:52
The current time is: 11:28:52 AM
The current time is: 11:28<jupyter_text>Code 34 of 100timedelta module<jupyter_code>from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta

now = datetime.now()
print ("Today is: " + str(now))
#using timedelta to print the date
print ("\nOne year from now it will be: " + str(now + timedelta(days=365)))

#timedelta with more than one argument
print ("\nIn two weeks and 3 days it will be: " + str(now + timedelta(weeks=2, days=3)))

#using strftime to format string and calculate the date 1 week ago
t = datetime.now() - timedelta(weeks=1)
s = t.strftime("%A %B %d, %Y")
print ("\nA week ago it was " + s)


#Example printing: How many days until April Fools' Day?

today = date.today()  #get today's date
afd = date(today.year, 4, 1)  #get April Fool's date for the same year
#use date comparison to see if April Fool's has already gone for this year
#if it has, use the replace() function to get the date for next year
if afd < today:
  print ("\nApril Fool's day already went by %d days ago" % ((today-afd).days))
  afd = afd.replace(year=today.year + 1)  # if so, get the date for next year

# Now calculate the amount of time until April Fool's Day  
time_to_afd = afd - today
print ("It's just", time_to_afd.days, "days until next April Fools' Day!")<jupyter_output>Today is: 2021-07-24 10:08:13.071032

One year from now it will be: 2022-07-24 10:08:13.071032

In two weeks and 3 days it will be: 2021-08-10 10:08:13.071032

A week ago it was Saturday July 17, 2021

April Fool's day already went by 114 days ago
It's just 251 days until next April Fools' Day!<jupyter_text>**Using Recursion**Code 35 of 100Displaying Fibonacci Sequence Using Recursion<jupyter_code>def recur_fibo(n):
   if n <= 1:
       return n
   else:
       return(recur_fibo(n-1) + recur_fibo(n-2))

nterms = int(input("Enter the number of terms you want to see in the series: "))

if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("\nFibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i), end=" ")<jupyter_output>Enter the number of terms you want to see in the series: 13

Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34 55 89 144<jupyter_text>Code 36 of 100 Factorial of a number using recursion<jupyter_code>def recur_factorial(n):
   if n == 1:
       return n
   else:
       print(n, end=" x ")
       return n*recur_factorial(n-1)

num = int(input("Enter the number whose factorial is to be printed: "))
print("The factorial of", num, "is", end=" ")
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   print(1,"=", recur_factorial(num)) # 3 x 2 x 1 = 6<jupyter_output>Enter the number whose factorial is to be printed: 7
The factorial of 7 is 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040<jupyter_text>Code 37 of 100 Converting Decimal to Binary Using Recursion Converting Decimal to Octal Using Recursion<jupyter_code>## Converting Decimal to Binary Using Recursion
def convertToBinary(n):
   if n > 1:
       convertToBinary(n//2)
   print(n % 2,end = '')

dec = int(input("Enter a decimal number: "))
print("Binary equivalent of", dec, "is: ", end="")
convertToBinary(dec)

## Converting Decimal to Octal Using Recursion
def convertToOctal(n):
   if n > 1:
       convertToOctal(n//8)
   print(n % 8,end = '')

dec = int(input("\n\nEnter a decimal number: "))
print("Octal equivalent of", dec, "is: ", end="")
convertToOctal(dec)<jupyter_output>Enter a decimal number: 69
Binary equivalent of 69 is: 1000101

Enter a decimal number: 348
Octal equivalent of 348 is: 0534<jupyter_text>Code 38 of 100Simple Calculator operations on two matrices<jupyter_code>def two_d_matrix(m, n): 
    Outp = [] 
    for i in range(m):
        row = []
        for j in range(n): 
            num = int(input(f"Index [{i}][{j}]: "))  
            row.append(num)  
        Outp.append(row) 
    return Outp      
  
def sum(A, B):  
    output = [] 
    print("\nSum of the matrix is :")  
    for i in range(len(A)):
        row = []  
        for j in range(len(A[0])):  
              
            row.append(A[i][j] + B[i][j]) 
        output.append(row)  
    return output  
def minus(A, B):  
    output = [] 
    print("\nDifference of the matrix is :")  
    for i in range(len(A)):
        row = []  
        for j in range(len(A[0])):  
              
            row.append(A[i][j] + B[i][j]) 
        output.append(row)  
    return output
  
def pro(A, B):  
    output = [] 
    print("\nProduct of the matrix is :")  
    for i in range(len(A)):
        row = []  
        for j in range(len(A[0])):  
              
            row.append(A[i][j] * B[i][j]) 
        output.append(row)  
    return output 

def div(A, B):  
    output = [] 
    print("\nQuotient of the matrix is :")  
    for i in range(len(A)):
        row = []  
        for j in range(len(A[0])):  
              
            row.append(A[i][j] * B[i][j]) 
        output.append(row)  
    return output 


m = int(input("Enter the value of Rows: "))
n = int(input("Enter the value of Columns: "))
print("\nThe matrices are of size: ", m, "x", n)
print("\nEnter values for the First matrix ") 
A = two_d_matrix(m, n) 
print("\nThe first matrix :")  
print(A)
  
print("\nEnter values for the Second matrix ")  
B = two_d_matrix(m, n)
print("\nThe second matrix: ")  
print(B) 
  
print("\n Select an operation: ")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

while True:
    choice = input("Enter choice(1/2/3/4): ")
    
    if choice in ('1', '2', '3', '4'):

        if choice == '1':
            s= sum(A, B)
            print(s)

        elif choice == '2':
            print(minus(A, B))

        elif choice == '3':
            print(pro(A, B))

        elif choice == '4':
            print(div(A, B))
        break
    else:
        print("Invalid Input")<jupyter_output>Enter the value of Rows: 2
Enter the value of Columns: 2

The matrices are of size:  2 x 2

Enter values for the First matrix 
Index [0][0]: 34
Index [0][1]: 56
Index [1][0]: 8
Index [1][1]: 66

The first matrix :
[[34, 56], [8, 66]]

Enter values for the Second matrix 
Index [0][0]: 429
Index [0][1]: 300
Index [1][0]: 75
Index [1][1]: 0

The second matrix: 
[[429, 300], [75, 0]]
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3

Product of the matrix is :
[[14586, 16800], [600, 0]]<jupyter_text>Code 39 of 100Transpose of a 3x3 Matrix using Nested Loop<jupyter_code>def two_d_matrix(m, n): 
    Output = [] 
    for i in range(m):
        row = []
        for j in range(n): 
            num = int(input(f"Index [{i}][{j}]: "))  
            row.append(num)  
        Output.append(row) 
    return Output

m = int(input("Enter the value of Rows: "))
n = int(input("Enter the value of Columns: "))
print("\nThe matrices are of size: ", m, "x", n)

print("\nEnter values for the Matrix ") 
X = two_d_matrix(m, n) 
print("\nThe first matrix :")  
print(X)

result = [[0,0,0],
         [0,0,0],
          [0,0,0]]

print("\nThe transpose of the matrix: ")
for i in range(len(X)):
   for j in range(len(X[0])):
       result[j][i] = X[i][j]
for r in result:
   print(r)<jupyter_output>Enter the value of Rows: 3
Enter the value of Columns: 3

The matrices are of size:  3 x 3

Enter values for the Matrix 
Index [0][0]: 45
Index [0][1]: 23
Index [0][2]: 675
Index [1][0]: 8
Index [1][1]: 345
Index [1][2]: 20
Index [2][0]: 0
Index [2][1]: 576
Index [2][2]: 22

The first matrix :
[[45, 23, 675], [8, 345, 20], [0, 576, 22]]

The transpose of the matrix: 
[45, 8, 0]
[23, 345, 576]
[675, 20, 22]<jupyter_text>Code 40 of 100Check if it is a palindrome in numeric form!<jupyter_code>n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
    dig=n%10
    rev=rev*10+dig
    n=n//10
if(temp==rev):
    print("The number is a palindrome!")
else:
    print("The number isn't a palindrome!")<jupyter_output>Enter number:355676553
The number is a palindrome!<jupyter_text>Code 41 of 100Check if it is a palindrome in String form!<jupyter_code>def isPalindrome(n):
    return n == n[::-1]

n=(input("Enter a String value: "))
ans = isPalindrome(n)

if ans:
    print("The number is a palindrome!")
else:
    print("The number isn't a palindrome!")<jupyter_output>Enter a String value: kristina
The number isn't a palindrome!<jupyter_text>**Let's Play with some Patterns!!** Code 42 of 100 Printing Asterisk pyramid pattern  Part 1Left Aligned<jupyter_code>for num in range(8):
    for i in range(num):
        print ("*",end=" ") #print *
    print("\r")<jupyter_output>* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * *<jupyter_text>Code 43 of 100 Printing Asterisk pyramid pattern  Part 2 Center Aligned<jupyter_code>def triangle(n):
	k = n - 1
	for i in range(0, n):
		for j in range(0, k):
			print(end=" ")
		k = k - 1
		for j in range(0, i+1):
			print("* ", end="")
		print("\r")
n = int(input("Enter the height of the pyramid: "))
print("\n")
triangle(n)<jupyter_output>Enter the height of the pyramid: 7


      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * *<jupyter_text>Code 44 of 100 Printing Asterisk pyramid pattern  Part 3 Right Aligned<jupyter_code>def pypart2(n):
	k = 2*n - 2
	for i in range(0, n):
		for j in range(0, k):
			print(end=" ")
		k = k - 2
		for j in range(0, i+1):
			print("* ", end="")
		print("\r")
n = int(input("Enter the height of the pyramid: "))
print("\n")
pypart2(n)<jupyter_output>Enter the height of the pyramid: 7


            * 
          * * 
        * * * 
      * * * * 
    * * * * * 
  * * * * * * 
* * * * * * *<jupyter_text>Code 45 of 100 Printing Number pyramid pattern  Part 1 Horizontal<jupyter_code>for num in range(6):
    for i in range(num):
        print (num,end=" ") #print number
    print("\r")<jupyter_output>1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5<jupyter_text>Code 46 of 100 Printing Number pyramid pattern  Part 2 Vertical<jupyter_code>num = 1
for i in range(0, 5):
	num = 1
	for j in range(0, i+1):
		print(num, end=" ")
		num = num + 1
	print("\r")<jupyter_output>1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5<jupyter_text>Code 47 of 100 Printing Alphabet pyramid pattern  Part 1Single Alphabet<jupyter_code>for num in range(6):
    for i in range(num):
        print ("A",end=" ") #print number
    print("\r")<jupyter_output>A 
A A 
A A A 
A A A A 
A A A A A<jupyter_text>Code 48 of 100 Printing Alphabet pyramid pattern  Part 2Multi-Alphabet<jupyter_code>num = 65
	for i in range(0, 5):
		for j in range(0, i+1):
			ch = chr(num)
			print(ch, end=" ")
		num = num + 1

		print("\r")<jupyter_output>A 
B B 
C C C 
D D D D 
E E E E E<jupyter_text>Code 49 of 100 Removing Punctuations From a String and Converting it into lower case.<jupyter_code>punctuations = '''”!()-[]{};:'"\,<>./?@#$%^&*_~“'''

my_string = input("Enter some string: \n")

no_punct = ""
for char in my_string:
   if char not in punctuations:
       no_punct = no_punct + char
print("\nRemoving Punctuations: ")
print(no_punct)
print("\nPrinting in lower case: ")
print(my_string.lower())<jupyter_output>Enter some string: 
“Even if you cannot change all the people around you, you can change the people you choose to be around. Life is too short to waste your time on people who don't respect, appreciate, and value you. Spend your life with people who make you smile, laugh, and feel loved.”

Removing Punctuations: 
Even if you cannot change all the people around you you can change the people you choose to be around Life is too short to waste your time on people who dont respect appreciate and value you Spend your life with people who make you smile laugh and feel loved

Printing in lower case: 
“even if you cannot change all the people around you, you can change the people you choose to be around. life is too short to waste your time on people who don't respect, appreciate, and value you. spend your life with people who make you smile, laugh, and feel loved.”<jupyter_text>Code 50 of 100Sorting Words in Alphabetic Order<jupyter_code>my_string = input("Enter a string: ")
words = [word.lower() for word in my_string.split()]
words.sort()

print("\nThe sorted words are:")
for word in words:
   print(word)<jupyter_output>Enter a string: “Even if you cannot change all the people around you, you can change the people you choose to be around. Life is too short to waste your time on people who don't respect, appreciate, and value you. Spend your life with people who make you smile, laugh, and feel loved.”

The sorted words are:
all
and
and
appreciate,
around
around.
be
can
cannot
change
change
choose
don't
feel
if
is
laugh,
life
life
loved.”
make
on
people
people
people
people
respect,
short
smile,
spend
the
the
time
to
to
too
value
waste
who
who
with
you
you
you
you
you,
you.
your
your
“even
{'fraction_non_alphanumeric': 0.12704482685362226, 'fraction_numerical': 0.042861695347355004, 'mean_word_length': 3.3067246111619397, 'pattern_counts': {'":': 2, '<': 81, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '33962', 'license': 'MIT', 'n_tokens_mistral': 7404, 'n_tokens_neox': 6605, 'n_words': 2452, 'path': 'Python100Codes/Part_2.ipynb', 'repo_name': 'Kristina140699/JavaCodes', 'size': '18828'}