0% found this document useful (0 votes)
23 views20 pages

Python Practice Problems

Uploaded by

nsrijavarma
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)
23 views20 pages

Python Practice Problems

Uploaded by

nsrijavarma
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

Conditional Statements

Q1. Ride Eligibility at Amusement Park

You are managing an amusement park. To ride the “Dragon Coaster”, rules are:

 Must be at least 120 cm tall.


 Children under 12 years need an adult with them.

Task: Given age and height, return "Allowed" or "Not Allowed".

Test Cases:

1. Input: age=15, height=130 → Output: Allowed


2. Input: age=10, height=130 → Output: Allowed
3. Input: age=11, height=119 → Output: Not Allowed
4. Input: age=20, height=100 → Output: Not Allowed
5. Input: age=12, height=120 → Output: Allowed
6. Input: age=8, height=121 → Output: Allowed
7. Input: age=7, height=110 → Output: Not Allowed
8. Input: age=30, height=150 → Output: Allowed

Explanation (Test 2): Age=10 (<12), height=130 (≥120) → allowed with adult → "Allowed".

Q2. Online Shopping Discount

In an e-commerce site:

 Orders above RS 500 → 20% discount


 Orders between RS 200–500 → 10% discount
 Orders below RS 200 → no discount

Task: Given order value, return final price after discount.

Test Cases:

1. Input: 600 → Output: 480


2. Input: 500 → Output: 450
3. Input: 250 → Output: 225
4. Input: 199 → Output: 199
5. Input: 1000 → Output: 800
6. Input: 300 → Output: 270
7. Input: 50 → Output: 50
8. Input: 700 → Output: 560
Explanation (Test 1): Bill=600 (>500) → 20% off → 600–120=480.

Q3. Exam Result System

Rules:

 Pass if marks ≥ 40 in all 3 subjects.


 Else → Fail.

Test Cases:

1. Input: 45 50 60 → Output: Pass


2. Input: 40 40 40 → Output: Pass
3. Input: 39 50 60 → Output: Fail
4. Input: 45 39 60 → Output: Fail
5. Input: 45 50 39 → Output: Fail
6. Input: 90 90 90 → Output: Pass
7. Input: 100 100 100 → Output: Pass
8. Input: 20 50 70 → Output: Fail

Explanation (Test 2): All subjects ≥ 40 → "Pass".

Q4. Traffic Fine System

City traffic rules:

 Speed ≤ 60 → "No Fine"


 Speed 61–80 → "Small Fine"
 Speed > 80 → "Heavy Fine"

Test Cases:

1. Input: 55 → Output: No Fine


2. Input: 60 → Output: No Fine
3. Input: 61 → Output: Small Fine
4. Input: 75 → Output: Small Fine
5. Input: 80 → Output: Small Fine
6. Input: 81 → Output: Heavy Fine
7. Input: 100 → Output: Heavy Fine
8. Input: 40 → Output: No Fine

Explanation (Test 5): Speed=80 → falls in 61–80 → "Small Fine".


Q5. Movie Ticket Price & Eligibility

A cinema hall decides ticket price based on age and also restricts who can watch certain
movie types:

Ticket Price Rules:

 Kids (<13): RS 5
 Teens (13–19): RS 7
 Adults (20–59): RS 10
 Seniors (60+): RS 6

Movie Type Rules (eligibility):

 "U" → Universal (all ages can watch)


 "UA" → Parental guidance required for kids <13
 "A" → Adults only (18+)
 "S" → Seniors only (60+)

Task:
Given age and movie type, return either the ticket price (if eligible) or "Not Allowed" (if not
eligible).

Test Cases

1. Input: Age=10, MovieType="U" → Output: 5


2. Input: Age=10, MovieType="UA" → Output: 5 (with parental guidance, allowed)
3. Input: Age=10, MovieType="A" → Output: "Not Allowed"
4. Input: Age=25, MovieType="A" → Output: 10
5. Input: Age=59, MovieType="S" → Output: "Not Allowed"
6. Input: Age=60, MovieType="S" → Output: 6
7. Input: Age=75, MovieType="U" → Output: 6
8. Input: Age=15, MovieType="UA" → Output: 7

Explanation of Test Case 4

 Age = 25 → falls under Adult (ticket price = RS 10)


 MovieType = "A" → only 18+ allowed
 Since 25 ≥ 18, user is allowed → final answer = 10.

Q6. Hotel Room Charges

A hotel charges based on days stayed:

 First 2 days → RS.100/day


 Next 3 days → RS.80/day
 Beyond 5 days → RS.50/day

Task: Given days, return total bill.

Test Cases:

1. Input: 1 → Output: 100


2. Input: 2 → Output: 200
3. Input: 3 → Output: 280
4. Input: 5 → Output: 440
5. Input: 6 → Output: 490
6. Input: 7 → Output: 540
7. Input: 10 → Output: 690
8. Input: 4 → Output: 360

Explanation (Test 6): Stay=7 days → 2×100 + 3×80 + 2×50 = 200+240+100=540.

Q7. Employee Bonus

Bonus rules:

 If years of service ≥ 10 → 10% of salary


 If 5–9 years → 5% of salary
 If <5 years → No bonus

Test Cases:

1. Input: salary=50000, years =12 → Output: 55000


2. Input: salary=40000, years =5 → Output: 42000
3. Input: salary=30000, years =3 → Output: 30000
4. Input: salary=60000, years =10 → Output: 66000
5. Input: salary=45000, years =7 → Output: 47250
6. Input: salary=25000, years =1 → Output: 25000
7. Input: salary=100000, years =15 → Output: 110000
8. Input: salary=70000, years =9 → Output: 73500

Explanation (Test 2): Salary=40000, years =5 → 5% bonus → 2000 extra → 42000.

Q8. Train Ticket Concession

Railway provides concessions:

 Children (<12) → 50%


 Seniors (≥60) → 40%
 Others → no discount
Test Cases:

1. Input: age=10, fare=100 → Output: 50


2. Input: age=12, fare=200 → Output: 200
3. Input: age=60, fare=150 → Output: 90
4. Input: age=30, fare=120 → Output: 120
5. Input: age=70, fare=300 → Output: 180
6. Input: age=8, fare=80 → Output: 40
7. Input: age=59, fare=100 → Output: 100
8. Input: age=61, fare=100 → Output: 60

Explanation (Test 3): Age=60 → senior → 40% off → 150–60=90.

For Loop

Q1. Print characters


A company wants to display characters of its name one by one.

 Sample Input 1:
"INFOSYS"
 Sample Output 1:

I
N
F
O
S
Y
S

 Hint: Iterate string with for loop.

Q2. Library Late Fee

A library charges a late fee for returning books:

 1–5 days late → 2 per day


 6–10 days late → 3 per day
 10 days late → 5 per day

Task: Given days late, calculate fine.

Test Cases:
1. Input: days=3 → Output: 6
2. Input: days=5 → Output: 10
3. Input: days=6 → Output: 18
4. Input: days=10 → Output: 30
5. Input: days=11 → Output: 55
6. Input: days=15 → Output: 75
7. Input: days=1 → Output: 2
8. Input: days=20 → Output: 100

Explanation (Test 3): 6 days late → 3 per day → 18.

Q3. Printing Student Roll Numbers

In a class, students have roll numbers from 1 to n. Print all roll numbers.

Test Cases:

1. Input: n=5 → Output: 1 2 3 4 5


2. Input: n=3 → Output: 1 2 3
3. Input: n=1 → Output: 1
4. Input: n=10 → Output: 1 2 3 4 5 6 7 8 9 10
5. Input: n=7 → Output: 1 2 3 4 5 6 7
6. Input: n=4 → Output: 1 2 3 4
7. Input: n=2 → Output: 1 2
8. Input: n=8 → Output: 1 2 3 4 5 6 7 8

Explanation (Test 1): Loop prints numbers from 1 to 5.

Q4. Birthday Gift Packets

A shopkeeper prepares gift packets with chocolates. Each packet must contain chocolates
equal to its packet number (1st packet =1, 2nd=2…). Find the total chocolates needed for n
packets.

Test Cases:

1. Input: n=3 → Output: 6


2. Input: n=5 → Output: 15
3. Input: n=1 → Output: 1
4. Input: n=10 → Output: 55
5. Input: n=7 → Output: 28
6. Input: n=4 → Output: 10
7. Input: n=6 → Output: 21
8. Input: n=2 → Output: 3

Explanation (Test 2): Chocolates = 1+2+3+4+5 = 15.


Q5. Odd Number Poster Printing

A printing press is designing posters using only odd numbers up to n. Print all odd numbers ≤
n.

Test Cases:

1. Input: n=10 → Output: 1 3 5 7 9


2. Input: n=7 → Output: 1 3 5 7
3. Input: n=5 → Output: 1 3 5
4. Input: n=1 → Output: 1
5. Input: n=12 → Output: 1 3 5 7 9 11
6. Input: n=3 → Output: 1 3
7. Input: n=2 → Output: 1
8. Input: n=15 → Output: 1 3 5 7 9 11 13 15

Explanation (Test 1): Odd numbers ≤ 10 are 1,3,5,7,9.

Q6. Cafeteria Bill Splitting

In a cafeteria, students order multiple items. You must calculate the total bill from prices of
items given as input.

Test Cases:

1. Input: [10,20,30] → Output: 60


2. Input: [5,5,5,5] → Output: 20
3. Input: [100] → Output: 100
4. Input: [12,18] → Output: 30
5. Input: [50,25,25] → Output: 100
6. Input: [0,10,20] → Output: 30
7. Input: [1,2,3,4,5] → Output: 15
8. Input: [7,8,9] → Output: 24

Explanation (Test 1): Total = 10+20+30 = 60.

Q7. Password Strength Checker

A website defines password strength as: (without inbuilt methods)

 Strong if length ≥ 8
 Medium if 5–7 characters.
 Weak if <5
Test Cases:

1. Input: "abc" → Output: Weak


2. Input: "hello" → Output: Medium
3. Input: "mypwd123" → Output: Strong
4. Input: "a" → Output: Weak
5. Input: "pass7" → Output: Medium
6. Input: "longpassword12" → Output: Strong
7. Input: "xyz1" → Output: Weak
8. Input: "secure9" → Output: Medium

Explanation (Test 3): "mypwd123" has 8 characters → "Strong".

Q8. Product Code Generator

A warehouse wants to generate a unique code for each product based on its name. The code
should consist of each character of the product name repeated by its position.

Task:
Given a product name (string), generate a list of codes for each character:

 Format: Character * Position

Input:

 Product name (string)

Output:

 List of generated codes

Test Cases

1. Input: "BOX" → Output: ['B', 'OO', 'XXX']


2. Input: "CAR" → Output: ['C', 'AA', 'RRR']
3. Input: "TOY" → Output: ['T', 'OO', 'YYY']
4. Input: "PEN" → Output: ['P', 'EE', 'NNN']
5. Input: "BAG" → Output: ['B', 'AA', 'GGG']
6. Input: "MUG" → Output: ['M', 'UU', 'GGG']
7. Input: "LAP" → Output: ['L', 'AA', 'PPP']
8. Input: "KEY" → Output: ['K', 'EE', 'YYY']
Explanation (Test 1):

 "BOX" →
o 1st char B → repeat 1 → "B"
o 2nd char O → repeat 2 → "OO"
o 3rd char X → repeat 3 → "XXX"

Nested For Loops


Q1. Multiplication Table Pattern

A school teacher wants to generate multiplication tables for numbers from 1 to N up to M.

Task: Print the multiplication tables.

Input: Two integers N M


Output: Tables from 1 to N, each up to M.

Test Cases

1. Input: 2 3
Output: 1x1=1 1x2=2 1x3=3, 2x1=2 2x2=4 2x3=6
2. Input: 3 2 → Output includes tables of 1,2,3 up to 2
3. Input: 1 5 → Only table of 1 up to 5
4. Input: 5 1 → Only 1 multiplication per table
5. Input: 4 4 → Tables 1 to 4 up to 4
6. Input: 2 5 → Tables 1 and 2 up to 5
7. Input: 3 3 → Tables 1 to 3 up to 3
8. Input: 6 2 → Tables 1 to 6 up to 2

Explanation of Test Case 1:


N=2, M=3 → Tables of 1 and 2, each up to 3.

Q2. Star Pyramid

A builder wants to design a pyramid shape with stars of height N.

Input: N (height)
Output: Star pyramid

Test Cases

1. Input: 3 →

*
***
*****
2. Input: 4
3. Input: 5
4. Input: 2
5. Input: 6
6. Input: 1
7. Input: 7
8. Input: 8

Explanation of Test Case 1:


For N=3 → center aligned pyramid of height 3.

Q3. Hollow Square

A programmer is asked to draw a hollow square of size N using #.

Input: N
Output: Hollow square

Test Cases

1. Input: 3

###
# #
###

2. Input: 4
3. Input: 5
4. Input: 2
5. Input: 6
6. Input: 1
7. Input: 7
8. Input: 8

Explanation of Test Case 1:


N=3 → Outer border filled, inner part hollow.

Q4. Diamond Pattern

Given N (odd), print a diamond pattern using *.

Test Cases

1. Input: 3

*
***
*
2. Input: 5
3. Input: 7
4. Input: 1
5. Input: 9
6. Input: 11
7. Input: 13
8. Input: 15

Explanation of Test Case 1:


Diamond with 3 rows max width.

Q5. Alphabet Pattern

Print alphabet triangle with N rows.

Test Cases

1. Input: 3

A
BB
CCC

2. Input: 4
3. Input: 5
4. Input: 2
5. Input: 6
6. Input: 1
7. Input: 7
8. Input: 8

Explanation of Test Case 1:


Each row prints the same alphabet.

While Loop
Q1. ATM Withdrawal Simulation

A bank ATM dispenses money in multiples of 100. A user enters an amount, and the ATM
keeps dispensing notes until the amount becomes 0.

Input: Amount (integer)


Output: Number of 100 notes dispensed

Test Cases

1. Input: 500 → Output: 5


2. Input: 100 → Output: 1
3. Input: 120 → Output: 1 (only full 100s allowed)
4. Input: 0 → Output: 0
5. Input: 250 → Output: 2
6. Input: 999 → Output: 9
7. Input: 50 → Output: 0
8. Input: 2000 → Output: 20

Explanation of Test Case 1:


500 → keep subtracting 100 until 0 → 5 notes.

Q2. Sum of Digits

A cashier needs to calculate the sum of digits of a bill number until only one digit remains.

Input: Number
Output: Single-digit sum

Test Cases

1. Input: 9875 → Output: 2 (9+8+7+5=29, 2+9=11, 1+1=2)


2. Input: 123 → Output: 6
3. Input: 99 → Output: 9+9=18 → 1+8=9
4. Input: 5 → Output: 5
5. Input: 1001 → Output: 2
6. Input: 4444 → Output: 7
7. Input: 19 → Output: 1
8. Input: 777 → Output: 3

Explanation of Test Case 1:


9875 → 29 → 11 → 2 → final sum is 2.

Q3. Reverse a Number

A ticketing system prints reversed token numbers for privacy. Write a program to reverse
digits of a number using a while loop.

Test Cases

1. Input: 1234 → Output: 4321


2. Input: 1200 → Output: 21
3. Input: 7 → Output: 7
4. Input: 9081 → Output: 1809
5. Input: 100 → Output: 1
6. Input: 505 → Output: 505
7. Input: 111 → Output: 111
8. Input: 2468 → Output: 8642

Explanation of Test Case 2:


1200 → reverse digits → 0021 → Output: 21.

Q4. Collatz Conjecture

For a given number n:

 If n is even → n//2
 If n is odd → 3n+1
Repeat until n=1.

Task: Print sequence.

Test Cases

1. Input: 6 → Output: 6 3 10 5 16 8 4 2 1
2. Input: 7 → Sequence until 1
3. Input: 10 → Sequence until 1
4. Input: 3
5. Input: 5
6. Input: 15
7. Input: 8
8. Input: 1

Explanation of Test Case 1:


6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1.

Q5. Digital Lock Password Attempts

A digital lock allows 3 attempts to guess the password. Stop once correct or after 3 tries.

Input: Actual password & attempts (list)


Output: Unlocked / Locked

Test Cases

1. Input: 1234 [1111, 2222, 1234] → Output: Unlocked


2. Input: 9999 [1111, 2222, 3333] → Output: Locked
3. Input: 5555 [5555] → Output: Unlocked
4. Input: 2468 [2468] → Output: Unlocked
5. Input: 8888 [1234, 8888] → Output: Unlocked
6. Input: 1010 [0000, 1010] → Output: Unlocked
7. Input: 2025 [1111, 2222, 3333] → Output: Locked
8. Input: 1212 [1212, 3434] → Output: Unlocked
Explanation of Test Case 1:
Password = 1234 → third attempt is correct → Unlocked.

Q6. Fibonacci Numbers Until Limit

A museum curator wants Fibonacci numbers below N for exhibit numbers.

Test Cases

1. Input: 10 → Output: 0 1 1 2 3 5 8
2. Input: 20 → Output: 0 1 1 2 3 5 8 13
3. Input: 1 → Output: 0 1 1
4. Input: 5 → Output: 0 1 1 2 3
5. Input: 2 → Output: 0 1 1 2
6. Input: 50 → Output: up to 34
7. Input: 100 → Output: up to 89
8. Input: 200 → Output: up to 144

Explanation of Test Case 1:


Generate Fibonacci ≤10 → 0 1 1 2 3 5 8.

Q7. Sum Until Negative

A shopkeeper enters daily sales. Stop adding when a negative number is entered.

Input: Sales numbers


Output: Total sum before negative

Test Cases

1. Input: [100, 200, 300, -1] → Output: 600


2. Input: [10, 20, -5] → Output: 30
3. Input: [50, -10] → Output: 50
4. Input: [-1] → Output: 0
5. Input: [100, 100, 100, -100] → Output: 300
6. Input: [1, 2, 3, 4, -1] → Output: 10
7. Input: [0, -1] → Output: 0
8. Input: [25, 25, 25, 25, -5] → Output: 100

Explanation of Test Case 1:


100+200+300=600 → stop at -1.

Q8. Palindrome Check Using While


Check if a number is palindrome using while loop.

Test Cases

1. Input: 121 → Output: Palindrome


2. Input: 123 → Output: Not Palindrome
3. Input: 1221 → Output: Palindrome
4. Input: 101 → Output: Palindrome
5. Input: 90 → Output: Not Palindrome
6. Input: 1 → Output: Palindrome
7. Input: 1001 → Output: Palindrome
8. Input: 456 → Output: Not Palindrome

Explanation of Test Case 1:


121 → reverse=121 → Palindrome.

Jumping Statements
Jumping statements → break, continue, pass

Q1. First Divisible Number

A teacher asks to find the first number between 1 and N divisible by both 3 and 5. Stop
when found.

Input: N
Output: FiRS.t number divisible by 15 or None

Test Cases

1. Input: 20 → Output: 15
2. Input: 10 → Output: None
3. Input: 30 → Output: 15
4. Input: 45 → Output: 15
5. Input: 14 → Output: None
6. Input: 60 → Output: 15
7. Input: 100 → Output: 15
8. Input: 15 → Output: 15

Explanation of Test Case 1:


Check numbers → 15 is fiRS.t divisible by 3 and 5 → stop with break.

Q2. Skip Odd Numbers

A class is rolling call. The teacher wants to print only even roll numbers up to N.
Input: N
Output: List of even numbers

Test Cases

1. Input: 5 → Output: 2 4
2. Input: 10 → Output: 2 4 6 8 10
3. Input: 3 → Output: 2
4. Input: 1 → Output: `` (empty)
5. Input: 8 → Output: 2 4 6 8
6. Input: 15 → Output: 2 4 6 8 10 12 14
7. Input: 20 → Output: 2 4 6 8 10 12 14 16 18 20
8. Input: 2 → Output: 2

Explanation of Test Case 1:


Use continue to skip odd numbers.

Q3. First Prime Number in Range

Given a range [L,R], print the first prime.

Test Cases

1. Input: 10 20 → Output: 11
2. Input: 20 25 → Output: 23
3. Input: 30 35 → Output: 31
4. Input: 14 16 → Output: No prime
5. Input: 2 10 → Output: 2
6. Input: 50 60 → Output: 53
7. Input: 90 95 → Output: No prime
8. Input: 97 100 → Output: 97

Explanation of Test Case 1:


Start at 10 → 11 is prime → break.

Q4. Skip Multiples of 5

A coach wants to print numbers from 1 to N but skip multiples of 5.

Test Cases

1. Input: 10 → Output: 1 2 3 4 6 7 8 9
2. Input: 5 → Output: 1 2 3 4
3. Input: 15 → Output: 1 2 3 4 6 7 8 9 11 12 13 14
4. Input: 20 → Output: 1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19
5. Input: 3 → Output: 1 2 3
6. Input: 1 → Output: 1
7. Input: 25 → Output excludes 5,10,15,20,25
8. Input: 30 → Output excludes multiples of 5

Explanation of Test Case 1:


Use continue whenever number % 5 == 0.

Q5. Stop When Sum Exceeds K

A cashier adds amounts until the sum exceeds K. Stop immediately.

Input: List of values, K


Output: Sum at stopping point

Test Cases

1. Input: [10,20,30] , 40 → Output: 60


2. Input: [5,5,5,5] , 12 → Output: 15
3. Input: [50,20,10] , 100 → Output: 80
4. Input: [1,2,3,4,5] , 5 → Output: 6
5. Input: [100] , 50 → Output: 100
6. Input: [10,20,30,40] , 150 → Output: 100
7. Input: [15,15,15] , 40 → Output: 45
8. Input: [1,1,1,1,1,1] , 2 → Output: 3

Explanation of Test Case 1:


10+20=30 (<40), add 30=60 (>40) → stop with break.

Q6. Skip Negative Numbers

A store logs profits/losses. Print only positive values from list.

Input: List of numbers


Output: Only positives

Test Cases

1. Input: [10,-5,20,-10,30] → Output: 10 20 30


2. Input: [-1,-2,-3] → Output: `` (empty)
3. Input: [5,0,-5] → Output: 5 0
4. Input: [100,-50,200] → Output: 100 200
5. Input: [0,0,0] → Output: 0 0 0
6. Input: [1,-1,2,-2] → Output: 1 2
7. Input: [50,-10,60,-5] → Output: 50 60
8. Input: [7] → Output: 7
Explanation of Test Case 1:
Skip -5, -10 using continue.

Q7. Find First Vowel in String

Given a string, find the fiRS.t vowel. Stop when found.

Test Cases

1. Input: "apple" → Output: a


2. Input: "sky" → Output: None
3. Input: "hello" → Output: e
4. Input: "rhythm" → Output: None
5. Input: "umbrella" → Output: u
6. Input: "India" → Output: I
7. Input: "Python" → Output: o
8. Input: "gym" → Output: None

Explanation of Test Case 3:


hello → first vowel is e → stop with break.

Q8. Ignore Special Characters.

A system should extract only alphabets from a string, ignoring digits and symbols.

Test Cases

1. Input: "ab123cd!" → Output: abcd


2. Input: "@Hello#" → Output: Hello
3. Input: "2025year" → Output: year
4. Input: "No$Symbol%" → Output: NoSymbol
5. Input: "!!??" → Output: `` (empty)
6. Input: "A1B2C3" → Output: ABC
7. Input: "Ganesh_007" → Output: Ganesh
8. Input: "cleanTEXT" → Output: cleanTEXT

Explanation of Test Case 1:


Use continue to skip digits/symbols → only abcd.

Strings
Q1. Reverse Words in a Sentence
A teacher gives a sentence and asks students to reverse each word but keep word order
same.

Input: "Hello World"


Output: "olleH dlroW"

Test Cases

1. Input: "Hello World" → Output: "olleH dlroW"


2. Input: "Python is fun" → Output: "nohtyP si nuf"
3. Input: "OpenAI Rocks" → Output: "nepOIA skcoR"
4. Input: "abc def ghi" → Output: "cba fed ihg"
5. Input: "single" → Output: "elgnis"
6. Input: "" → Output: ""
7. Input: "Ganesh ChatGPT" → Output: "hsenaG TPGtahC"
8. Input: "data science" → Output: "atad ecneics"

Explanation of Test Case 1:


Split → [Hello, World], reverse each → ["olleH", "dlroW"], join back.

Q2. Palindrome Sentence (Ignore Spaces)

Check if a sentence is palindrome ignoring spaces and case.

Test Cases

1. "Race car" → Output: True


2. "Madam" → Output: True
3. "hello world" → Output: False
4. "A man a plan a canal Panama" → Output: True
5. "Python" → Output: False
6. "Never odd or even" → Output: True
7. "Ganesh" → Output: False
8. "Was it a car or a cat I saw" → Output: True

Explanation of Test Case 1:


Normalize "Race car" → "racecar" → palindrome.

Q3. Remove Duplicates

Remove duplicate characters from a string but keep first occurrence order.

Test Cases

1. "banana" → Output: "ban"


2. "hello" → Output: "helo"
3. "mississippi" → Output: "misp"
4. "aabbcc" → Output: "abc"
5. "programming" → Output: "progamin"
6. "abc" → Output: "abc"
7. "Ganesh" → Output: "Ganesh"
8. "112233" → Output: "123"

Explanation of Test Case 1:


Keep fiRS.t b,a,n, skip later duplicates.

Q4. Longest Word in Sentence

Find the longest word in a sentence.

Test Cases

1. "I love programming" → Output: programming


2. "Python is great" → Output: Python
3. "AI will change world" → Output: change
4. "a bb ccc dddd" → Output: dddd
5. "ChatGPT rocks" → Output: ChatGPT
6. "big data science" → Output: science
7. "fast and furious" → Output: furious
8. "hello" → Output: hello

Explanation of Test Case 1:


Split → [I, love, programming] → longest is "programming".

Q5. Count Vowels and Consonants

Count number of vowels and consonants in a string.

Test Cases

1. "hello" → Output: Vowels=2, Consonants=3


2. "abc" → Output: Vowels=1, Consonants=2
3. "AEIOU" → Output: Vowels=5, Consonants=0
4. "rhythm" → Output: Vowels=0, Consonants=6
5. "Ganesh" → Output: Vowels=2, Consonants=4
6. "Python" → Output: Vowels=1, Consonants=5
7. "Data Science" → Output: Vowels=5, Consonants=6
8. "" → Output: Vowels=0, Consonants=0

Explanation of Test Case 1:


hello → vowels {e,o}=2, consonants {h,l,l}=3.

You might also like