0% found this document useful (0 votes)
6 views9 pages

Essential Python Programs and Algorithms

The document outlines various programming tasks including file operations, mathematical calculations, and algorithms in Python. Key examples include copying file contents, finding the longest word in a file, implementing the Tower of Hanoi, and performing matrix operations. It also covers basic algorithms such as linear search, factorial calculation, and GCD computation.

Uploaded by

VECTOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Essential Python Programs and Algorithms

The document outlines various programming tasks including file operations, mathematical calculations, and algorithms in Python. Key examples include copying file contents, finding the longest word in a file, implementing the Tower of Hanoi, and performing matrix operations. It also covers basic algorithms such as linear search, factorial calculation, and GCD computation.

Uploaded by

VECTOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Important programs

1. Copy contents of two files into [Link]

f1 = open("[Link]", "r")

f2 = open("[Link]", "r")

t = open("[Link]", "w")

[Link]([Link]())

[Link]([Link]())

[Link]()

[Link]()

[Link]()

print("Copied!")

2. Longest word in a file

f = open("[Link]", "r")

words = [Link]().split()

[Link]()

longest = ""

for w in words:

if len(w) > len(longest):

longest = w

print("Longest word:", longest)


3. Tower of Hanoi (Easy)

def hanoi(n, A, B, C):

if n == 1:

print(A, "→", C)

else:

hanoi(n-1, A, C, B)

print(A, "→", C)

hanoi(n-1, B, A, C)

hanoi(3, "A", "B", "C")

4. Matrix Multiplication (Easy)

A = [[1,2],

[3,4]]

B = [[5,6],

[7,8]]

C = [[0,0],

[0,0]]

for i in range(2):

for j in range(2):

C[i][j] = A[i][0]*B[0][j] + A[i][1]*B[1][j]

for r in C:

print(r)
5. Matrix Addition (Super simple)

A = [[1,2],[3,4]]

B = [[5,6],[7,8]]

C = [[0,0],[0,0]]

for i in range(2):

for j in range(2):

C[i][j] = A[i][j] + B[i][j]

print(C)

6. Linear Search

a = [10, 20, 30, 40]

x = int(input("Enter number: "))

for i in range(len(a)):

if a[i] == x:

print("Found at index", i)

break

else:

print("Not found")

7. Sum of n natural numbers (Easy formula)

n = int(input("Enter n: "))

print("Sum =", n*(n+1)//2)

8. Factorial (Easy recursion)


def fact(n):

if n == 0:

return 1

return n * fact(n-1)

print(fact(int(input("Enter number: "))))

9. Reverse of a number

n = int(input("Enter number: "))

rev = 0

while n > 0:

rev = rev*10 + (n%10)

n //= 10

print("Reverse:", rev)

10. Star Pattern

Right Angle Triangle

for i in range(1, 6):

print("*" * i)

Pyramid

for i in range(1, 6):

print(" "*(5-i) + "*"*(2*i-1))


11. Perfect Number

n = int(input("Enter number: "))

s=0

for i in range(1, n):

if n%i == 0:

s += i

print("Perfect" if s==n else "Not perfect")

12. Leap Year

y = int(input("Enter year: "))

if (y%400==0) or (y%4==0 and y%100!=0):

print("Leap year")

else:

print("Not leap")

13. Count vowels in a string

s = input("Enter string: ")

v=0

for ch in s:

if ch in "aeiouAEIOU":

v += 1

print("Vowels:", v)
Converting a list to an array

1. Using Python’s built-in array module

from array import array

lst = [1, 2, 3, 4]

arr = array('i', lst)

print(arr)

'i' → integer array

2. Using NumPy (most common way)

import numpy as np

lst = [1, 2, 3, 4]

arr = [Link](lst)

print(arr)

1. Tower of Hanoi (Recursive)

def hanoi(n, A, B, C):

if n == 1:

print(A, "→", C)

return

hanoi(n-1, A, C, B)

print(A, "→", C)

hanoi(n-1, B, A, C)
hanoi(3, "A", "B", "C")

2. Fibonacci Series

✔ Recursive Fibonacci

def fib(n):

if n <= 1:

return n

return fib(n-1) + fib(n-2)

n = int(input("Enter n: "))

for i in range(n):

print(fib(i), end=" ")

✔ Normal / Iterative Fibonacci

n = int(input("Enter n: "))

a, b = 0, 1

for i in range(n):

print(a, end=" ")

a, b = b, a + b

3. Factorial

✔ Recursive Factorial

def fact(n):

if n == 0:
return 1

return n * fact(n-1)

print(fact(int(input("Enter number: "))))

✔ Normal / Iterative Factorial

n = int(input("Enter number: "))

f=1

for i in range(1, n+1):

f *= i

print("Factorial =", f)

4. GCD (Greatest Common Divisor)

✔ Recursive GCD (Euclid’s Algorithm)

def gcd(a, b):

if b == 0:

return a

return gcd(b, a % b)

print(gcd(20, 12))

✔ Normal / Iterative GCD

a = int(input("Enter a: "))

b = int(input("Enter b: "))
while b != 0:

a, b = b, a % b

print("GCD =", a)

You might also like