0% found this document useful (0 votes)
4 views5 pages

Python Algorithms for Beginners

Uploaded by

aneeq.wales
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)
4 views5 pages

Python Algorithms for Beginners

Uploaded by

aneeq.wales
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

Python Programming Algorithms

1. Check whether a number is even or odd


1. Start
2. Read a number n
3. If n % 2 == 0, print “Even”
4. Else print “Odd”
5. Stop

2. Display reversal of a number


1. Start
2. Read a number n
3. Initialize rev = 0
4. While n > 0:
digit = n % 10
rev = rev * 10 + digit
n = n // 10
5. Print rev
6. Stop

3. Find the biggest number among 3 numbers


1. Start
2. Read three numbers a, b, c
3. If a > b and a > c, print “a is biggest”
4. Else if b > c, print “b is biggest”
5. Else print “c is biggest”
6. Stop

4. Countdown from a number to zero (while loop)


1. Start
2. Read number n
3. While n >= 0:
Print n
n=n-1
4. Stop

5. Check whether a year is a leap year


1. Start
2. Read year y
3. If y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
Print “Leap Year”
4. Else print “Not Leap Year”
5. Stop

6. Generate first N natural numbers


1. Start
2. Read number n
3. For i from 1 to n:
Print i
4. Stop

7. Check whether a number is palindrome


1. Start
2. Read number n
3. Store original value in temp = n
4. Reverse number
5. If temp == rev, print “Palindrome”
6. Else print “Not Palindrome”
7. Stop

8. Find factorial of a number


1. Start
2. Read number n
3. Initialize fact = 1
4. For i in range(1, n+1):
fact = fact * i
5. Print fact
6. Stop

9. Sum of N natural numbers


1. Start
2. Read number n
3. Initialize sum = 0
4. For i in range(1, n+1):
sum = sum + i
5. Print sum
6. Stop

10. Check whether number is Armstrong


1. Start
2. Read number n
3. Store temp = n
4. Initialize sum = 0
5. Count digits d
6. While n > 0:
digit = n % 10
sum = sum + (digit ** d)
n = n // 10
7. If sum == temp, print “Armstrong”
8. Else print “Not Armstrong”
9. Stop

11. Generate prime numbers up to n


1. Start
2. Read n
3. For each i in 2..n:
flag = 0
For j in 2..i//2:
If i % j == 0, flag = 1
4. If flag == 0, print i
5. Stop

12. Find minimum and maximum in a list


1. Start
2. Read list L
3. Set min = L[0], max = L[0]
4. For each element:
If element < min, update min
If element > max, update max
5. Print min and max
6. Stop

13. Function max_of_three()


1. Define function max_of_three(a, b, c)
2. Compare and return largest
3. Stop

14. Factorial using recursion


1. Define function fact(n)
2. If n == 0 or 1: return 1
3. Else return n * fact(n-1)
4. Stop

15. Linear search


1. Start
2. Read list L and x
3. For each element:
If element == x, print Found
4. Else print Not Found
5. Stop

16. Binary search


1. Start
2. Read sorted list L and x
3. low=0, high=len(L)-1
4. While low<=high:
mid=(low+high)//2
If L[mid]==x, Found
Else adjust low/high
5. Stop

17. Exchange values of two variables


1. Start
2. Read a,b
3. Swap a,b=b,a
4. Print
5. Stop
18. Circulate values of n variables
1. Start
2. Read n variables
3. Move last to first
4. Print list
5. Stop

19. Distance between two points


1. Start
2. Read (x1,y1) and (x2,y2)
3. dist = sqrt((x2-x1)^2 + (y2-y1)^2)
4. Print dist
5. Stop

20. Largest number in list (function)


1. Define largest(L)
2. max=L[0]
3. For each element, if greater update max
4. Return max
5. Stop

21. Area of shape


1. Start
2. Ask shape type
3. If circle: area=πr²
4. If rectangle: area=l*b
5. If triangle: area=½*b*h
6. Print area
7. Stop

22. Character count in string


1. Start
2. Read string s
3. count=0
4. For each char in s: count++
5. Print count
6. Stop

23. Replace characters in string


1. Start
2. Read s, old, new
3. Replace using [Link](old,new)
4. Print
5. Stop

24. Reverse a string


1. Start
2. Read s
3. Reverse s[::-1]
4. Print
5. Stop

You might also like