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

Python Algorithms and Operations Guide

Key answers

Uploaded by

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

Python Algorithms and Operations Guide

Key answers

Uploaded by

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

Algorithms for Python Lab Record

(From 4.d to Last)


[Link]. 4.d – Selection Sort
Step 1: Start

Step 2: Enter n numbers into list

Step 3: For each position i in list:

Step 4: Find the smallest element from i to end

Step 5: Swap it with element at i

Step 6: Print sorted list

Step 7: Stop

[Link]. 4.e – Insertion Sort


Step 1: Start

Step 2: Enter n numbers into list

Step 3: For i=1 to n-1:

Step 4: Take current element

Step 5: Shift larger elements to right

Step 6: Insert current element in correct place

Step 7: Print sorted list

Step 8: Stop

[Link]. 5.a – Matrix Addition, Subtraction, Multiplication


Step 1: Start

Step 2: Enter two matrices A and B

Step 3: Add: C[i][j] = A[i][j] + B[i][j]

Step 4: Subtract: C[i][j] = A[i][j] - B[i][j]


Step 5: Multiply: C[i][j] = sum(A[i][k]*B[k][j])

Step 6: Print results

Step 7: Stop

[Link]. 5.b – Matrix Transpose


Step 1: Start

Step 2: Enter matrix A

Step 3: Transpose: swap rows with columns

Step 4: Print transpose

Step 5: Stop

[Link]. 5.c – Sum of Main and Anti Diagonal


Step 1: Start

Step 2: Enter square matrix A

Step 3: Sum main diagonal: add A[i][i]

Step 4: Sum anti diagonal: add A[i][n-i-1]

Step 5: Print sums

Step 6: Stop

[Link]. 6.a – Circulate a List


Step 1: Start

Step 2: Enter list L

Step 3: Repeat for given rotations:

Step 4: Move first element to last

Step 5: Shift others left

Step 6: Print list after each rotation

Step 7: Stop
[Link]. 6.b – Find Largest in List
Step 1: Start

Step 2: Enter list of numbers

Step 3: Set largest=first element

Step 4: For each number: if bigger than largest, update

Step 5: Print largest

Step 6: Stop

[Link]. 7.a – Factorial using Recursion


Step 1: Start

Step 2: Define function fact(n): if n<=1 return 1 else n*fact(n-1)

Step 3: Enter number

Step 4: Call fact(number)

Step 5: Print result

Step 6: Stop

[Link]. 7.b – Towers of Hanoi


Step 1: Start

Step 2: Define moveTower(height, fromPole, toPole, withPole):

Step 3: If height>0:

Step 4: Move tower-1 to helper

Step 5: Move disk to target

Step 6: Move tower-1 from helper to target

Step 7: Call function with n disks

Step 8: Stop

[Link]. 8.a – String Operations


Step 1: Start
Step 2: Enter string s

Step 3: Reverse: s[::-1]

Step 4: Check palindrome: compare with reverse

Step 5: Count vowels by checking a,e,i,o,u

Step 6: Print results

Step 7: Stop

[Link]. 8.b – Remove and Replace Character


Step 1: Start

Step 2: Enter string s

Step 3: Enter char to remove, char to replace, new char

Step 4: Remove: build new string skipping char

Step 5: Replace: build new string replacing old with new

Step 6: Print results

Step 7: Stop

[Link]. 9.a – Item in Library


Step 1: Start

Step 2: Create dictionary of books

Step 3: Enter book name

Step 4: If found, print details else Not found

Step 5: Stop

[Link]. 9.b – Components of Car


Step 1: Start

Step 2: Create dictionary with car parts and values

Step 3: Print each part and options

Step 4: Stop
[Link]. 9.c – Weight of Motor Bike
Step 1: Start

Step 2: Create dictionary with bike models and weights

Step 3: Print each with weight

Step 4: Stop

[Link]. 9.d – Retail Purchase Billing


Step 1: Start

Step 2: Create items and prices

Step 3: Repeat until user exits: choose product and qty

Step 4: Add cost to total

Step 5: Print bill

Step 6: Stop

[Link]. 9.e – Frequency of Characters


Step 1: Start

Step 2: Enter word s

Step 3: Create empty dictionary

Step 4: For each char in s: count frequency

Step 5: Print dictionary

Step 6: Stop

[Link]. 10.a – File Copy


Step 1: Start

Step 2: Open source file for reading

Step 3: Open destination file for writing

Step 4: Read content and write to new file

Step 5: Close both files


Step 6: Stop

[Link]. 10.b – Longest Word in File


Step 1: Start

Step 2: Open file and read words

Step 3: Find longest word by length

Step 4: Print it

Step 5: Stop

[Link]. 10.c – Most Frequent Word in File


Step 1: Start

Step 2: Open file and read words

Step 3: Count each word using dictionary

Step 4: Find word with maximum count

Step 5: Print it

Step 6: Stop

[Link]. 11.a – Divide by Zero Exception


Step 1: Start

Step 2: Enter two numbers a and b

Step 3: Try to compute a/b

Step 4: If division by zero, show error message

Step 5: Stop

[Link]. 11.b – Age Validation


Step 1: Start

Step 2: Enter age

Step 3: If age<18 → Not eligible

Step 4: Else → Eligible


Step 5: Stop

[Link]. 11.c – Mark Validation


Step 1: Start

Step 2: Enter mark

Step 3: If mark<0 or >100 → Invalid

Step 4: Else → Print mark

Step 5: Stop

[Link]. 11.d – Simple Program using Pandas


Step 1: Start

Step 2: Create DataFrame with country, capital, population

Step 3: Print unsorted data

Step 4: Sort by population

Step 5: Print sorted data

Step 6: Stop

[Link]. 12.a – Matrix Operations using NumPy


Step 1: Start

Step 2: Create two numpy matrices

Step 3: Add using [Link], subtract using [Link]

Step 4: Multiply using [Link]

Step 5: Print results

Step 6: Stop

[Link]. 12.b – Bar Chart using Matplotlib


Step 1: Start

Step 2: Enter x labels and y values

Step 3: Plot bar chart with labels and values


Step 4: Show chart

Step 5: Stop

[Link]. 12.c – Solve Simultaneous Equations


Step 1: Start

Step 2: Represent equations in matrix form A, b

Step 3: Use [Link](A,b)

Step 4: Print solution

Step 5: Stop

[Link]. 13 – Introduction to Pygame


Step 1: Start

Step 2: Import pygame

Step 3: Initialize screen

Step 4: Run loop until quit event

Step 5: Draw shapes and update screen

Step 6: Stop

[Link]. 14 – Elliptical Orbits in Pygame


Step 1: Start

Step 2: Set up pygame window

Step 3: Use sine and cosine to find orbit positions

Step 4: Draw orbit path and moving circle

Step 5: Update display repeatedly

Step 6: Stop

[Link]. 15 – Bouncing Ball in Pygame


Step 1: Start

Step 2: Load ball image


Step 3: Move ball with speed values

Step 4: If ball hits boundary, reverse direction

Step 5: Draw ball in new position

Step 6: Repeat until quit

Step 7: Stop

You might also like