Nothing Special   »   [go: up one dir, main page]

Coding - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

1. Read a number and check if it is divisible by 7.

2. WAP to check if number last digit is 4.


3. WAP to check the number is divisible by 3 and last digit is 4.
4. WAP to check the number is divisible by 7 or last digit is 5.
5. Take an integer A as input. You have to tell whether A is divible by both 5 and 11 or
not.
6. Read three integers and print their maximum.
7. Read Three angles of triangles and determine its types(Right traingle,Obtuse
triangle,actute triangle).
8. WAP to check to check whether a person is eligible for voting or not.
9. WAP to check whether an year is a leap year or not.
10. WAP to accept a number from 1 to 7 and display the name of day like 1 for sunday, 2
for monday, etc.
11. Given 5 numbers A, B, C, D, E as input. Print the average of these 5 numbers.
12. You are given 3 integer angles(in degrees) A, B and C of a triangle. You have to tell
whether the triangle is valid or not.A triangle is valid if sum of its angles equals to
180.
13. Write a program to input two numbers(A & B) from user and print the maximum
element among A & B.
14. Write a program to input three numbers(A, B & C) from user and print the minimum
element among A, B & C.
15. Accept the percentage from the user and display the grade according to the following
criteria.
● Below 25 – D
● 25 to 45 – C
● 45 to 65 – B
● 65 to 85 – A
● Above 85 – A+
1.Write a program that takes a positive integer N as input from the user and prints all natural
numbers from 1 to N, with each number followed by a space.
Input:- N = 5
Output:- 1 2 3 4 5

2.Write a program to print all Natural numbers from N to 1 where you have to take N as input
from user.
Input:- N = 5
Output:- 5 4 3 2 1

3.Write a program to print all even numbers from 1 to N where you have to take N as input
from the user.
Input:- N = 10
Output:- 2 4 6 8 10

4.Write a program to print all odd numbers from 1 to N where you have to take N as input
from user.
Input:- N = 10
Output:- 1 3 5 7 9

5.Write a program to find sum all Natural numbers from 1 to N where you have to take N as
input from user
Input:- N = 10
Output:- 55

6.You are given an integer A, you need to find and return the sum of all the even numbers
between 1 and A. Even numbers are those numbers that are divisible by 2.
Input:- A = 5
Output:- 6
Explaination:- Even numbers between [1, 5] are (2, 4).

7.Take an integer A as input. You have to print the sum of all odd numbers in the range [1,
A].
Input:- A= 4
Output:- 4
Explaination:- For A = 4, Odd numbers 1 and 3 lie in the range [1, 4]. Sum = 1 + 3 = 4.

8.Take integer N as input and Print the count of digits of that number.
Input:- N = 10101
Output:- 5
Explaination:- 10101 has 5 digits

9.Take integers N as input. Your task is to calculate and print the sum of the digits of the
given number N.
Input:- N = 1589
Output:- 23
Explaination:- For the number 1589, the digits are 1,5,8,9. The Sum(1589) = 1+5+8+9 = 23.

10.You are given an integer A as input and you need to determine whether it is a palindrome
or not. A palindrome integer is one whose digits, when reversed, result in the same number.
For example, 121 is a palindrome because its reverse is also 121, but 123 is not a palindrome
because its reverse is 321.Note: The given integer will not have any leading zeros.

Input:- A = 131
Output:- Yes
Explaination:- For A = 131, reverse(A) = reverse(131) = 131, which is same as A.

11.Take a number A as input, print its multiplication table having the first 10 multiples.
Input:-
3
Output:-
3*1=3
3*2=6
3*3=9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30

12.You are given two integers A and B. You have to find the value of A^B.
Input:- A = 2 , B = 3
Output:- 8
Explaination:- For A=2 and B=3, the value of 2^3 = 2 * 2 * 2 = 8.
1.Sum of list
Given an array compute the sum of all elements.

Input :-
A = [1 2 3 4 5]
Output:
15

2.Copy the Array

You are given a constant array A and an integer B.

You are required to return another array where Arr[i] = A[i] + B.

Input :-
A = [1 2 3 2 1]
B=3

Output:
[4 5 6 5 4]

3.Max and Min of an Array


Take input an array A of size N and write a program to print maximum and minimum
elements of the input array .Here N represents the length of the array .
Input :-
A = [1 2 3 4 5]
Output:
51

4.Search Element
You are given array A and an integer B. You have to tell whether B is present in array A or
not.
Input:-
A = [1 5 9 1]
B=5
Output:
1

5.Negative Integers
Write a program to print all negative numbers from input array A of size N.
Input:-
A = [1 -5 2 -8 -4]

Output:
-5
-8
-4

6.Even Odd Elements


For array A, you have to find the value of absolute difference between the counts of even
and odd elements in the array.
Input:
A = [1 2 3 4]
Output:
0

7.Separate Odd Even


You are given an integer array A.
You have to print the odd and even elements of array A in 2 separate lines.

Input:
A = [1 2 3 4 5]
Output:
135
24

8.Square of Array
You are provided with an integer array A. Return another array B of size same as that of A
such that B[i] = A[i]^2

Input:
A=[2, 6, 8, 1]
Output:
[4, 36, 64, 1]

9.Cube of Array

You are provided with an integer array A. Return another array B of size same as that of A
such that B[i] = A[i]^3
Input:
A=[2, 6, 8, 1]
Output:
[8, 216, 512, 1]

10.Reverse
Given an array A, Find the reverse of it. (Solve this question with for loop)
Input:
A = [3, 5, 1, 2, 1, 2]
Output:
[2, 1, 2, 1, 5, 3]

11. Add two list element:


Given two lists A1 and A2, each containing integers, write a Python program to compute the
element-wise sum of the corresponding elements in the two lists and store the result in a
new list.
Input:
A1=[1, 2, 3,4]
A2=[4, 5, 6,7]
Output:
[5, 7, 9, 11]

12. Find the output :


list = [2,4,6,8,10,12,14,16,18,20]

print(list[:])
print(list[::])
print(list[2:5])
print(list[2:])
print(list[2::])
print(list[:2])
print(list[::2])
print(list[1::2])
print(list[2:10:2])

13. Find the output :


list = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
print(len(list))
print(list[-2 :-5: -1])
print(list[-2:])
print(list[-2::])
print(list[:-2])
print(list[::-2])
print(list[::-1])
14. Find the output :

mylist = [1.4, 2, 3, 4, 5, ‘Suyash’]


mylist.reverse()
print(mylist)

15. Find the output :

● s = “Hello everyone how are you”

print(s.split())

● s = "Hello-everyone-how are you"

print(s.split("-"))
● word = 'Suyash:Chaudhary:Noida'

print(word.split(':'))
● t = “23456”

print(t.split())

● t = "2 3 4 5"
print(t.split())
16. Find the output :

l1 = [1, 2, 3, 5, 8, 9]

l2 = [3, 4, 5 , 6, 7, 10]

result = l1 + l2

print(result)

result1 = l1 * 3
print(result1)
1.Vowels vs Consonants
Write a program to input T strings (S) from user and print count of vowels and consonants in
it.
Input:
2
List
Apple

Output:
13
23

2. Length of String - II
You have a string (A).You have to print length of input string.

Input:
Python

Output:
6

3.Is is Palindrome?

Write a program to input T strings (S) from user and print 1 if it is palindrome otherwise print
0. NOTE:A string is palindrome if it reads the same from backward as from forward.
Input:
3
abcba
axax
abba

Output:
1
0
1

4.Trim (*)

You are given a character string A. You to trim both leading and trailing asterisk
characters('*') in the string and print the resultant string.
Input:

A = "**h*e*l*lo*"

Output:

h*e*l*lo

5.Trim left (*)

You are given a character string A. You to trim leading asterisk characters('*') in the string
and print the resultant string.
Input:

A = "**h*e*l*lo*"
Output:

h*e*l*lo*
6.Trim right (*)

You are given a character string A. You to trim leading asterisk characters('*') in the string
and print the resultant string.
Input:

A = "**h*e*l*lo*"
Output:

**h*e*l*lo

7.Reverse the word


You are given string (A) and you have to print after reversing that.

Input:
String

Output:
gnirtS
8.Reverse the order of words
You are given string (A) and you have to print the reverse order of words.

Input:
Suyash Chaudhary

Output:
Chaudhary Suyash

9.Reverse string
Write a program to reverse the words present in a string. Check example input/output.

Input:
Everyone loves data science

Output:
enoyrevE sevol atad ecneics

9. tolower()

Convert each character of Sting A into lowercase characters if it exists. If the lowercase of a
character does not exist, it remains unmodified.
The uppercase letters from A to Z are converted to lowercase letters from a to z respectively.

Print the lowercase version of the given String.

Input:

A = PythoN

Output:
Python

10.toupper()

Convert each character of String A into Uppercase character if it exists. If the Uppercase of a
character does not exist, it remains unmodified.The lowercase letters from a to z is converted
to uppercase letters from A to Z respectively.

Print the uppercase version of the given the string.

Input:

A = pYthON
Output:
PYTHON

11.Isalnum()

Print 1 if all the characters of a character array are alphanumeric (a-z, A-Z, and 0-9) else,
print 0.

Input:

A = Python45

Output:
1

12.Isalpha()

Print 1 if all the characters of the character array are alphabetical (a-z and A-Z), else print 0.

Input:

A = Python

Output:
1

13.First Occurrence
You are given a character string A, having length N and an integer ASCII code B.
You have to tell the leftmost occurrence of the character having ASCII code equal to B, in A
or report that it does not exist.

Input:

A = "aabbcc"
B = 98

Output:
2
14.First Occurrence Of Word

You are given two character strings A and B.

You have to find the first occurrence of string B in string A, as a substring, and return the
starting position of first occurrence.

A substring is a contiguous sequence of characters within a string. For e.g "at" is a substring
in "catalogue".

Input:

A = "aabababaa"

B = "ba"

Output:
2

15.Count Occurrences

Find the number of occurrences of bob in string A consisting of lowercase English alphabets.

Input:

"abobc"

Output:
1

Input:

"bobob"

Output:
2
16.String operations

Akash likes playing with strings. One day he thought of applying following operations on the
string in the given order:

Concatenate the string with itself.


Delete all the uppercase letters.
Replace each vowel with '#'.
You are given a string A of size N consisting of lowercase and uppercase alphabets. Return
the resultant string after applying the above operations.

NOTE: 'a' , 'e' , 'i' , 'o' , 'u' are defined as vowels.

Input:

A="aeiOUz"

Output:
"###z###z"
Q1.Frequency of Number
Given N array elements . Find the frequency of element in array. (Solve it by using
dictionary.)

Input 1:

A = [2, 6, 3, 8, 2, 8, 2, 3, 8, 8]

B=2

Output 1:

Q2.Find the first non-repeating element

Given N array elements . Find the first non-repesting element

Input 1:

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

Output 1:

Q3. Check Palindrome

Given a string A consisting of lowercase characters.

Check if characters of the given string can be rearranged to form a palindrome.

Print 1 if it is possible to rearrange the characters of the string A such that it becomes a
palindrome else print 0.

Example Input

Input 1:

A = "abcde"

Input 2:

A = "abbaee"

Example Output

Output 1:

0
Output 2:

Example Explanation

Explanation 1:

No possible rearrangement to make the string palindrome.

Explanation 2:

Given string "abbaee" can be rearranged to "aebbea" to form a palindrome.

Q4. First Repeating element

Given an integer array A of size N, find the first repeating element in it.

We need to find the element that occurs more than once and whose index of the first
occurrence is the smallest.

If there is no repeating element, print -1.

Example Input

Input 1:

A = [10, 5, 3, 4, 3, 5, 6]

Input 2:

A = [6, 10, 5, 4, 9, 120]

Example Output

Output 1:

Output 2:

-1
Example Explanation

Explanation 1:

5 is the first element that repeats

Explanation 2:

There is no repeating element, output -1

Q5. Merge two Python dictionaries into one

Input 1:

dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}

dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}

Output 1:

{'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Fourty': 40, 'Fifty': 50}

Q6.Check if a value exists in a dictionary

Input 1:

dict1 = {'a': 100, 'b': 400, 'c': 300}

Output 1:

400 present in a dict

Q7.Iterate through the keys in the dictionary.

Input 1:

person = {"name": "abc", "age": 25}


Output 1:

name
age

Q8.Iterate through the values in the dictionary.


Input 1:

person = {"name": "abc", "age": 25}


Output 1:

abc
25
Q9 .Iterate through both keys and values in the dictionary.

Input 1:

person = {"name": "abc", "age": 25}


Output 1:

name abc
age 25

Q10.Remove all elements from a dictionary.


Input 1:

person = {"name": "abc", "age": 25}


Output 1:

{}

Q11.Get a list of all keys in a dictionary.


Input 1:

person = {"name": "abc", "age": 25}


Output 1:

[‘name’ , ‘age’]

Q12.Get a list of all values in a dictionary.


Input 1:

person = {"name": "abc", "age": 25}


Output 1:

[‘abc’ , 25’]
Q13. Generate a dictionary

Write a Python script to generate and print a dictionary that contains a number (between 1
and n) in the form (x, x*x).

Input 1:

N=5

Output 1:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Q14. Create a list of tuples from the dictionary

Input 1:

dict1 = { 1: 'a', 2: 'b', 3: 'c' }

Output 1:

[(1, 'a'), (2, 'b'), (3, 'c')]

Q15.Write a code to sort dictionaries using a key.

Input 1:

{2: ‘Apple’, 1:’Mango’, 3:’Orange’, 4:’Banana’}

Output 1:

1 Mango

2 Apple

3 Orange

4 Banana

Q16. Common Elements

Given two integer arrays, A and B of size N and M, respectively. Your task is to find all the
common elements in both the array.

NOTE:
● Each element in the result should appear as many times as it appears in both arrays.
● The result can be in any order.

Example Input

Input 1:

A = [1, 2, 2, 1]

B = [2, 3, 1, 2]

Input 2:

A = [2, 1, 4, 10]

B = [3, 6, 2, 10, 10]

Example Output

Output 1:

[1, 2, 2]

Output 2:

[2, 10]

Example Explanation

Explanation 1:

Elements (1, 2, 2) appears in both the array. Note 2 appears twice in both the array.

Explantion 2:

Elements (2, 10) appears in both the array.

Q17. list of squares

WAP a Program to Generate a list of squares using list comprehensions.

Output 1

[1, 4, 9, 16, 25]

Q18. Filter even numbers

WAP a Program to Filter even numbers using list comprehensions.

Output 1
[2, 4, 6, 8, 10]

Q19.List of uppercase characters

WAP a Program to Create a using list comprehensions

Input 1:

text = "hello"

Output 1:

['H', 'E', 'L', 'L', 'O']

Q20.Find the output:-

a=4
print(type(a))
a = 4,
print(type(a))
a = (4)
print(type(a))
a = ()
print(type(a))
a = (4,)
print(type(a))
a = (4,5)
print(type(a))
1.Square root of a number
Given a number A. Return square root of the number if it is perfect square otherwise return -
1.
Note: A number is a perfect square if its square root is an integer.

2.Area of Square
You are given a positive integer A denoting the side of a square. You have to calculate the
area of the square.
Area of a square having side S is given by (S * S).

3.Area of Circle
You are given a positive integer A denoting the radius of a circle. You have to calculate the
area of the circle.

4.Power function
You are given two integers A and B.You have to find the value of A^B.

5.Cube It!
You are given an integer A.You have to find the value of cube of A i.e, A^3.

6.Volume Of Sphere
You are given a positive integer A denoting the radius of a sphere. You have to calculate the
volume of the sphere.
Volume of a sphere having radius R is given by (4 * π * R3) / 3.
NOTE: Since, the answer can be a real number, you have to return the ceil value of the
volume. Ceil value of a real number X is the smallest integer that is greater than or equal to
X.

7.Area Of Ellipse
Given the lengths of semi-major axis A and semi-minor axis B of an ellipse, calculate the
Area of the Ellipse.
Area of ellipse having semi-major axis length a and semi-minor axis length b is given by π *
a * b.
NOTE: Since, the answer can be a real number, you have to return the ceil value of the area.
Ceil value of a real number X is the smallest integer that is greater than or equal to X.

8.Sum the Array


Write a program to print sum of elements of the input array A of size N.
1.Add the matrices

You are given two matrices A & B of same size, you have to return another matrix which is
the sum of A and B.
Note: Matrices are of same size means the number of rows and number of columns of both
matrices are equal.

Input:

A = [[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

B = [[9, 8, 7],

[6, 5, 4],

[3, 2, 1]]

Output:

[[10, 10, 10],

[10, 10, 10],

[10, 10, 10]]

2.Matrix Transpose

Given a 2D integer array A, return the transpose of A.

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's
row and column indices.
Input:

A = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]

Output:

[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

3.Is It Identity Matrix?

You are given a N X N square integer matrix A. You have to tell whether A is an identity
matrix or not.

Identity matrix is a special square matrix whose main diagonal elements are equal to 1 and all
other elements are 0.

Input:

[[1, 0], [0, 1]]

Output:

4.Matrix Subtraction

You are given two integer matrices A and B having same size(Both having same number of
rows (N) and columns (M). You have to subtract matrix B from A and return the resultant
matrix. (i.e. return the matrix A - B).

If A and B are two matrices of the same order (same dimensions). Then A - B is a matrix of
the same order as A and B and its elements are obtained by doing an element wise subtraction
of A from B.

Input:

A = [[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

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

[3, 2, 1]]

Output:

[[-8, -6, -4],

[-2, 0, 2],

[4, 6, 8]]

5.Are Matrices Same ?

You are given two matrices A and B of equal dimensions and you have to check whether two
matrices are equal or not.

NOTE: Both matrices are equal if A[i][j] == B[i][j] for all i and j.

Input:

A = [[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

B = [[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

Output:

1
6. Row & Column Sums

You are given a matrix A, you have to return an array containing sum of each row elements
followed by sum of each column elements of the matrix.

NOTE: If the matrix given is of size (N x M), then the array you return would be of size (N +
M), where first N elements contain the sum of each N rows, and the next M elements contain
the sum of each M columns.

Input:

A = [[1, 2],[4, 5],[8, 9]]

Output:

[3, 9, 17, 13, 16]

You might also like