0% found this document useful (0 votes)
20 views24 pages

Solution For Step 3 and Step 4 - First Set of Problems

The document outlines various programming problems, including finding the sum of two numbers, the smallest of three numbers, checking for prime numbers, determining if a string is a palindrome, identifying leap years, summing numbers from 1 to n, checking for isograms, and counting character repetitions in a string. Each problem includes steps for understanding, designing test cases, and providing solutions with examples of valid and invalid inputs. The document emphasizes constraints, assumptions, and expected outputs for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views24 pages

Solution For Step 3 and Step 4 - First Set of Problems

The document outlines various programming problems, including finding the sum of two numbers, the smallest of three numbers, checking for prime numbers, determining if a string is a palindrome, identifying leap years, summing numbers from 1 to n, checking for isograms, and counting character repetitions in a string. Each problem includes steps for understanding, designing test cases, and providing solutions with examples of valid and invalid inputs. The document emphasizes constraints, assumptions, and expected outputs for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Problem: Find the sum of 2 numbers

Step 1 - Understand the Problem


Is there any term in the problem that you don't understand? Read and underst 1
See if you can explain this clearly to a 10 year old 2
3
Step 2 - Design Test Data/Test Cases (Input and Expected Output) 4
Assumptions, Constraints & Questions 5
1 Input numbers can be whole numbers or decimal numbers 6
2 Maximum of 4 digit numbers can be given as input
3 Numbers can be positive, negative or 0
4 Output is one single number

Test Cases or Test Data


Category Input Exptected Output
123, 121 244
-12, 39.5 27.5
-12, -12 -24
0, 0 0
0, -121 -121
2222, 9989 12211
123.4, 56.3 179.7
0, 9998.5 9998.5
Valid -9999, -9989 -19988
99999, -999 "Invalid input"
-993499, 12 "Invalid input"
123.123, a "Invalid input"
blank, 123 "Invalid input"
alphanumeric, 292 "Invalid input"
Invalid SpecialChar, 234 "Invalid input"
Step 3: Solution
Get the first number
Get the second number
If first or second number is out of range, print "Invalid Input". End.
Else If first or second number is blank or alphanumeric or blank or speical character, print "Invalid input". End.
Else add the first and second number (these can be any numbers between -9999 to 9999)
Print the result. End.

Step 4: Test the Solution


Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
nt "Invalid input". End.
Problem: Find the smallest of 3 numbers
Step 1 - Understand the Problem
Is there any term in the problem that you don't understand? Read and understand i 1
See if you can explain this clearly to a 10 year old 2
3
Step 2 - Design Test Data/Test Cases (Input and Expected Output) 4
Assumptions, Constraints & Questions 5
1 Input numbers can be whole numbers or decimal numbers 6
2 Maximum of 4 digit numbers can be given as input 7
3 Numbers can be positive, negative or 0 8
4 Output is one single number
5 The numbers may be repeated

Test Cases or Test Data


Category Input Exptected Output
0,1,2 0
-1,-2,-3 -3
0,0,0 0
9, -12, 9 -12
-9999,0,9999 -9999
100, 70,1234 70
Valid 1.98, 980, 0.6 0.6
99999, -999, 0 "Invalid input, Please try with valid input"
-993499, 12, 12 "Invalid input, Please try with valid input"
123.123, -1, a "Invalid input, Please try with valid input"
blank, 123, 456 "Invalid input, Please try with valid input"
alphanumeric, 292"Invalid input, Please try with valid input"
Invalid SpecialChar, 234, 1."Invalid input, Please try with valid input"
Step 3: Solution
Get the first number
Get the second number
Get the third number
If first or second or third number is out of range, print "Invalid Input". End.
Else If first or second or third number is blank or alphanumeric or blank or speical character, print "Invalid input". En
Compare first and second number and make note of the smallest among those two as the temporary result (if both
Compare the above temporary result to the third number and note down the smallest of those two as the final resul
Print the final result

Step 4: Test the Solution


Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
are equal, pick first one)
lt (if both are equal, pick first one)
Problem: Check if the given number is a prime number Prime - A whole number greater than 1 that ca
Step 1 - Understand the Problem
Is there any term in the problem that you don't understand? Read and underst 1
See if you can explain this clearly to a 10 year old 2
3
Step 2 - Design Test Data/Test Cases (Input and Expected Output) 4
Assumptions, Constraints & Questions 5
1 -ve numbers are not prime
2 Decimal numbers are not valid input
6
Test Cases or Test Data
Category Input Exptected Output
0 "Not Prime"
1 "Not Prime"
-1 "Not Prime"
2 "Prime"
3 "Prime"
-3 "Not Prime"
1999 "Prime"
-681 "Not Prime"
Valid 25 "Not Prime"
1.2 "Invalid Input"
-445.7 "Invalid Input"
blank "Invalid Input"
alphanumeric "Invalid Input"
Invalid SpecialChar "Invalid Input"
me - A whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1
Step 3: Solution
Get the input number, let’s call it n
If n is decimal or blank or alphanumeric or special character, print “Invalid input” and End program.
Else If n is negative or 0 or 1, print “Not prime” and End program
If n is 2, print "Prime"and End program
Else take one number at a time, from 2 till n-1, and divide n with this number
5a. If remainder is 0 when divided by any of these numbers, print “Not prime” and End program
5b. Else Continue with next number
Print “Prime” and End program

Step 4: Test the Solution


Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
d program.
Problem: Find if a given string is a palindrome What is a String? - A String is a sequence o
Step 1 - Understand the Problem What are characters? - can be any symbol
Is there any term in the problem that you don't understand? Read and understWhat is a Palindrome? - A string of charact
See if you can explain this clearly to a 10 year old
1
Step 2 - Design Test Data/Test Cases (Input and Expected Output) 2
Assumptions, Constraints & Questions 3
1 Empty string is a palindrome 4
2 Maximum string length is 100 5
3 Space will be ignored in input 6

Test Cases or Test Data


Category Input Exptected Output
"" Yes
"a" Yes
"121" Yes
"madam" Yes
"abcde" No
"1@3fyf3@1" Yes
"nurses run" Yes
"lool" Yes
Valid "hello world" No
String length > 100 "Invalid Input"

Invalid
hat is a String? - A String is a sequence of Characters.
hat are characters? - can be any symbol on your keyboard. digits, alphabets, special characters
hat is a Palindrome? - A string of characters is a palindrome if the character sequence is same whether we read from right to le
Step 3: Solution
Get the input String, let's call it S1
If length of input String is greater than 100, print "Invalid Input" and End program
If String is empty or has a single character, print "Yes" and End program
Remove all spaces from S1
Create a new String called S2 by reversing the String S1 E.g. If S1 = "abc", S2 = "cba"
Starting comparing one letter at a time from left to right, between S1 and S2
6a. If any character doesn't match, print "No" and End Program
6b. Else continue with next character comparison
Print "Yes" and End Program

Step 4: Test the Solution


Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
er we read from right to left or left to right

, S2 = "cba"
Problem: Check if a given year is a leap year A year is a leap year if - EITHER the year is multi
Step 1 - Understand the Problem
Is there any term in the problem that you don't understand? Read and underst 1
See if you can explain this clearly to a 10 year old 2
3
Step 2 - Design Test Data/Test Cases (Input and Expected Output) 4
Assumptions, Constraints & Questions
1 Year should be a whole number
2 BC years is invalid input, only AD will be considered 5

Test Cases or Test Data


Category Input Exptected Output
1900 No
2000 Yes
2004 Yes
2022 No
589 No
0 Yes
100 No
2050 No
Valid 3012 Yes
123.5 "Invalid Input"
100 BC "Invalid Input"
One Hundred "Invalid Input"
abc "Invalid Input"
-10 "Invalid Input"
Invalid
ear is a leap year if - EITHER the year is multiple of 400 OR the year is a multiple of 4 and not multiple of 100
Step 3: Solution
Get the input number, let’s call it n
If n is decimal or blank or alphanumeric or special character or -ve, print “Invalid input” and End program.
If n is divisible by 400, print "Yes, its a LEAP YEAR" and End program
Else if n is divisible by 4
4a. If n is divisible by 100, print "Not a LEAP YEAR" and End program
4b. Else, print "Yes, its a LEAP YEAR" and End program
Else print "No" and End program

Step 4: Test the Solution


Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
d End program.
Problem: Given a number n. Add all the numbers from 1 to n A single input n is given and you have to ad
Step 1 - Understand the Problem
Is there any term in the problem that you don't understand? Read and underst 1
See if you can explain this clearly to a 10 year old 2
3
Step 2 - Design Test Data/Test Cases (Input and Expected Output) 4
Assumptions, Constraints & Questions 5
1 Number will only be whole numbers. No decimal numbers. 6
2 n will be between 0 to 9999
3 n will not be -ve number 7

Test Cases or Test Data


Category Input Exptected Output
0 0
1 1
3 6 1+2+3
10 55 1 + 2 + 3 .. 10
100 5050
Valid 9999 49995000
-10 "Invalid input"
11.2 "Invalid input"
abc "Invalid input"
1/2 "Invalid input"
10000 "Invalid Input"
Invalid
single input n is given and you have to add all numbers from 1 to n.
Step 3: Solution
Get the number n as input
If number n is Decimal or -ve number or greater than 9999 or alphanumeric or special character, print "Invalid Input
If number n is 0, print 0 and End Program
If number n is 1, print 1 and End Program
Create one number, let's called it Result and initialize that to 0, Result = 0
Repeat the below step for each number from 1 till n
6a. Take that number and add it to Result
Print Result and End Program

Step 4: Test the Solution


Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
racter, print "Invalid Input" and End program
Problem: Find if a string is an isogram A String with no repeating letters is an isog
Step 1 - Understand the Problem
Is there any term in the problem that you don't understand? Read and underst 1
See if you can explain this clearly to a 10 year old 2
3
Step 2 - Design Test Data/Test Cases (Input and Expected Output) 4
Assumptions, Constraints & Questions 5
1 String can include any character on keyboard 6
2 Space will be ignored in String while checking for isogram
3 Empty string is an isogram
4 Maximum string length is 100 7

Test Cases or Test Data


Category Input Exptected Output
"ab" Yes
"aba" No
"helo abc" Yes
"hello world" No
"" Yes
"abcdefghijkopqr2345" Yes
"121" No
"!@#$%^*()_+<>?|{" Yes
Valid "g" Yes
String length > 100 "Invalid input"

Invalid
String with no repeating letters is an isogram i.e. each character appears maximum once.
Step 3: Solution
Get the input String, let's call it S
If length of input String is greater than 100, print "Invalid Input" and End program
If String is empty or has a single character, print "Yes" and End program
Remove all spaces from S
Create one container where you can keep track of all characters seen so far, as we go through the String character
Repeat the below 2 steps for each character in S, from left to right
6a. Check if that character is already there in the container. If it is there, then print "No" and End Program
6b. Else add this character to the container and Continue with next character
Print "Yes" and End Program

Step 4: Test the Solution


Works
Works
Works
Works
Works
Works
Works
Works
Works
Works
ough the String character by character, from left to right. Initially this container will be empty.

d End Program
Problem: Find the number of times each character is repeated in a string For the given string, we will take each uniqu
Step 1 - Understand the Problem
Is there any term in the problem that you don't understand? Read and underst 1
See if you can explain this clearly to a 10 year old 2
3
Step 2 - Design Test Data/Test Cases (Input and Expected Output) 4
Assumptions, Constraints & Questions 5
1 String can include any character on keyboard 6
2 Space will be ignored
3 Empty string will result in no output (there is no character to count)
4 Captial letters and small letters will be treated as different cha 7
5 Maximum string length is 100

Test Cases or Test Data


Category Input Exptected Output
"hello world" h:1, e:1, l:3, o:2, w:1, r:1, d:1
"a" a:1
"bbbbbbbb" b:8
"121" 1:2, 2:1
"^GTRRRrrrr" ^:1, G:1, T:1, R:3, r:4
"" (No output)

Valid
String length > 100 "Invalid input"

Invalid
r the given string, we will take each unique charcter in the string and see how many times its repeated and print count for each
Step 3: Solution
Get the input String, let's call it S
If length of input String is greater than 100, print "Invalid Input" and End program
If String is empty print no output and End program
Remove
how many alltimes
spaces from
it has S
occured so far, as we go through the String character by character, from left to
right. Initially this container will be empty.
Repeat the below 2 steps for each character in S, from left to right
6a. Check if that character is already there in the container. If it is there, increase its occurrence count by 1
6b. Else add this character to the container and initialize its Occurrence count to 1. Continue with next character
For each character stored in the container, print the Character and its Occurrence Count and End Program

Step 4: Test the Solution

Works
A year is a leap year if - EITHER the year is multiple of 400 OR the year is a multiple of 4 and not multiple of 100
Works
Works
Works
Works

Works
print count for each unique character

count by 1
h next character
nd Program

You might also like