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

Week1 Solution

The document contains 5 programming problems with sample inputs and outputs. The problems include: 1) adding two numbers, 2) finding the remainder when dividing two numbers, 3) summing the first and last digits of a number, 4) determining if an army is ready for battle based on the number of weapons soldiers hold, and 5) printing the second maximum number from a set of three inputs. Solutions are provided in Python for each problem.

Uploaded by

ARIFULLA SHAIK
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)
41 views5 pages

Week1 Solution

The document contains 5 programming problems with sample inputs and outputs. The problems include: 1) adding two numbers, 2) finding the remainder when dividing two numbers, 3) summing the first and last digits of a number, 4) determining if an army is ready for battle based on the number of weapons soldiers hold, and 5) printing the second maximum number from a set of three inputs. Solutions are provided in Python for each problem.

Uploaded by

ARIFULLA SHAIK
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
You are on page 1/ 5

1.

Add Two Numbers


Problem Statement
Shivam is the youngest programmer in the world, he is just 12 years old. Shivam is learning programming
and today he is writing his first program.

The task is very simple: given two integers A and B, write a program to add these two numbers and output
it.
Input
The first line contains an integer T, the total number of test cases. Then follow T lines, each line contains
two Integers A and B.
Output
For each test case, add A and B and display the sum in a new line.
Constraints
• 1 ≤ T ≤ 1000
• 0 ≤ A,B ≤ 10000
Sample Input 1
3
12
100 200
10 40
Sample Output 1
3
300
50

Solution:
T = int(input())
for tc in range(T):
# Read integers a and b.
(a, b) = map(int, input().split(' '))

ans = a + b
print(ans)

2. Find Remainder Problem


Write a program to find the remainder when an integer A is divided by an integer B.
Input
The first line contains an integer T, the total number of test cases. Then T lines follow, each line contains
two Integers A and B.
Output
For each test case, find the remainder when A is divided by B, and display it in a new line.
Constraints
• 1 ≤ T ≤ 1000
• 1 ≤ A,B ≤ 10000
Example
Input
3
12
100 200
40 15
Output
1
100
10
Solution:
T = int(input())
for x in range(T):
A,B = map(int, input().split())
print(A % B)

3. First and Last Digit Problem


If Give an integer N . Write a program to obtain the sum of the first and last digits of this number.
Input
The first line contains an integer T, the total number of test cases. Then follow T lines, each line contains
an integer N.
Output
For each test case, display the sum of first and last digits of N in a new line.
Constraints
• 1 ≤ T ≤ 1000
• 1 ≤ N ≤ 1000000
Example
Input
3
1234
124894
242323

Output
5
5
5

Solution

for i in range(int(input())):
s = input()
print(int(s[0]) + int(s[len(s) - 1]))

(or)

for i in range(int(input())):
s = input()
print(int(s[0]) + int(s[ - 1]))

4. Mahasena Problem
Kattapa, as you all know was one of the greatest warriors of his time. The kingdom of Maahishmati had
never lost a battle under him (as army-chief), and the reason for that was their really powerful army, also
called as Mahasena.
Kattapa was known to be a very superstitious person. He believed that a soldier is "lucky" if the soldier is
holding an even number of weapons, and "unlucky" otherwise. He considered the army as "READY FOR
BATTLE" if the count of "lucky" soldiers is strictly greater than the count of "unlucky" soldiers, and
"NOT READY" otherwise.
Given the number of weapons each soldier is holding, your task is to determine whether the army formed
by all these soldiers is "READY FOR BATTLE" or "NOT READY".
Note: You can find the definition of an even number here.
Input
The first line of input consists of a single integer N denoting the number of soldiers. The second line of
input consists of N space separated integers A1, A2, ..., AN, where Ai denotes the number of weapons that
the ith soldier is holding.
Output
Generate one line output saying "READY FOR BATTLE", if the army satisfies the conditions that
Kattapa requires or "NOT READY" otherwise (quotes for clarity).
Constraints
• 1 ≤ N ≤ 100
• 1 ≤ Ai ≤ 100
Example 1
Input:
1
1

Output:
NOT READY
Example 2
Input:
1
2

Output:
READY FOR BATTLE
Example 3
Input:
4
11 12 13 14

Output:
NOT READY
Example 4
Input:
3
234

Output:
READY FOR BATTLE
Example 5
Input:
5
12345

Output:
NOT READY
Explanation
• Example 1: For the first example, N = 1 and the array A = [1]. There is only 1 soldier and he is
holding 1 weapon, which is odd. The number of soldiers holding an even number of weapons = 0, and
number of soldiers holding an odd number of weapons = 1. Hence, the answer is "NOT READY" since
the number of soldiers holding an even number of weapons is not greater than the number of soldiers
holding an odd number of weapons.
• Example 2: For the second example, N = 1 and the array A = [2]. There is only 1 soldier and he is
holding 2 weapons, which is even. The number of soldiers holding an even number of weapons = 1, and
number of soldiers holding an odd number of weapons = 0. Hence, the answer is "READY FOR
BATTLE" since the number of soldiers holding an even number of weapons is greater than the number of
soldiers holding an odd number of weapons.
• Example 3: For the third example, N = 4 and the array A = [11, 12, 13, 14]. The 1st soldier is
holding 11 weapons (which is odd), the 2nd soldier is holding 12 weapons (which is even), the 3rd soldier
is holding 13 weapons (which is odd), and the 4th soldier is holding 14 weapons (which is even). The
number of soldiers holding an even number of weapons = 2, and number of soldiers holding an odd
number of weapons = 2. Notice that we have an equal number of people holding even number of weapons
and odd number of weapons. The answer here is "NOT READY" since the number of soldiers holding an
even number of weapons is not strictly greater than the number of soldiers holding an odd number of
weapons.
• Example 4: For the fourth example, N = 3 and the array A = [2, 3, 4]. The 1st soldier is holding 2
weapons (which is even), the 2nd soldier is holding 3 weapons (which is odd), and the 3rd soldier is
holding 4 weapons (which is even). The number of soldiers holding an even number of weapons = 2, and
number of soldiers holding an odd number of weapons = 1. Hence, the answer is "READY FOR
BATTLE" since the number of soldiers holding an even number of weapons is greater than the number of
soldiers holding an odd number of weapons.
• Example 5: For the fifth example, N = 5 and the array A = [1, 2, 3, 4, 5]. The 1st soldier is
holding 1 weapon (which is odd), the 2nd soldier is holding 2 weapons (which is even), the 3rd soldier is
holding 3 weapons (which is odd), the 4th soldier is holding 4 weapons (which is even), and the 5th
soldier is holding 5 weapons (which is odd). The number of soldiers holding an even number of weapons
= 2, and number of soldiers holding an odd number of weapons = 3. Hence, the answer is "NOT READY"
since the number of soldiers holding an even number of weapons is not greater than the number of
soldiers holding an odd number of weapons.

Solution
n= int(input())
a=list(map(int, input().split(' ')))
c1=0
c2=0
for i in a:
if i % 2==0:
c1 +=1
else:
c2 +=1
if c1>c2:
print("READY FOR BATTLE")
else:
print("NOT READY")

or
t=int(input())
lst=list(map(int,input().split()))
lucky=0
unlucky=0
for x in lst:
if(x%2==0):
lucky+=1
else:
unlucky+= 1
Z= print("READY FOR BATTLE") if(lucky>unlucky) else print("NOT READY")

5. Second Max of Three Numbers


Problem Statement
Write a program that accepts sets of three numbers, and prints the second-maximum number among the
three.
Input
• First line contains the number of triples, N.
• The next N lines which follow each have three space separated integers.
Output
For each of the N triples, output one new line which contains the second-maximum integer among the
three.
Constraints
• 1≤N≤6
• 1 ≤ every integer ≤ 10000
• The three integers in a single triplet are all distinct. That is, no two of them are equal.
Sample Input
3
123
10 15 5
100 999 500
Sample Output
2
10
500

Solution:
t=int(input())
for i in range(t):
l=list(map(int, input().split()))
l.remove(max(l))
print(max(l))

or

for t in range(int(input())):
A = [int(x) for x in input().split()]
A.sort()
print(A[1])

You might also like