Wednesday, 21 August 2019

The Joy of Computing using Python: Programming Assignment 1,2 and 3

1. Given a list of numbers (integers), find second maximum and second minimum in this list.

Input Format:

The first line contains numbers separated by a space.

Output Format:

Print second maximum and second minimum separated by a space

Example:

Input:

1 2 3 4 5

Output:

4 2

Code:

a = input()
z = [int(x) for x in a.split()]
z.sort()

print("%d %d" % (z[len(l1)-2], z[1]),end='', sep=' ')


2. Given a list A of numbers (integers), you have to print those numbers which are not multiples of 5.

Input Format:

The first line contains the numbers of list A separated by a space.

Output Format:

Print the numbers in a single line separated by a space which are not multiples of 5.

Example:

Input:

1 2 3 4 5 6 5

Output:

1 2 3 4 6

Explanation:
Here the elements of A are 1,2,3,4,5,6,5 and since 5 is the multiple of 5, after removing them the list becomes 1,2,3,4,6.

Code:

x = input()
num = list(map(int, x.split()))
l =[]
for i in num:  
    if i%5 != 0:
        l.append(i)
print(*l, sep = " ", end='')


3. You are given a number A which contains only digits 0's and 1's. Your task is to make all digits same by just flipping one digit (i.e. 0 to 1 or 1 to 0 ) only. If it is possible to make all the  digits same by just flipping one digit then print 'YES' else print 'NO'.

Input Format:

The first line contains a number made up of 0's and 1's.

Output Format:

Print 'YES' or 'NO' accordingly without quotes.

Example:

Input:

101

Output:
YES

Explanation: 
If you flip the middle digit from 0 to 1 then all the digits will become same. Hence output is YES.

Code:


num =input()
zeros = 0
for c in num:
    if c == '0':
        zeros += 1
if zeros == 1 or zeros == len(num)-1:
    print ('YES', end='')
else:
    print ('NO',end='')


1 comment: