Tuesday, October 10, 2017

Toggle Alphabet Case

The program must accept an alphabet X and toggle the case (convert upper case to lower case and vice versa).

Input Format:
The first line denotes the value of character X.

Output Format:
The first line contains the characters X and the toggled case character separated by a hyphen.

Boundary Conditions: X is from a to z

Example Input/Output:
Input:
d
Output:
d-D

Solution:
s=input()
print(s+'-'+s.swapcase())
Share:

Interlace odd-even from A to B

Two numbers A and B are passed as input. The program must print the odd numbers from A to B (inclusive of A and B) interlaced with the even numbers from B to A.

Input Format:
The first line denotes the value of A. The second line denotes the value of B.

Output Format:
The odd and even numbers interlaced, each separated by a space.

Boundary Conditions: 1 <= A <= 9999999 A < B <= 9999999

Example Input/Output 1:
Input:
5
11

Output:
5 10 7 8 9 6 11

Explanation:
The odd numbers from 5 to 11 are 5 7 9 11 The even numbers from 11 to 5 (that is in reverse direction) are 10 8 6 So these numbers are interlaced to produce 5 10 7 8 9 6 11

Solution:
#include <iostream>
using namespace std;
int main()
{
 int n,m,i,j;
 cin>>n>>m;
 for(i=n,j=m;i<=m;i++,j--)
  {
      if(i%2==1)
       cout<<i<<" ";
      if(j%2==0)
       cout<<j<<" ";
  }
}
Share:

Print Calendar Month in Words

The program must accept an integer value N and print the corresponding calendar month in words. 1 - January, 2 - February, .... , 11 - November, 12 - December If any value apart from 1 to 12 is passed as input, the program must print "Invalid Input".

Input Format:
The first line denotes the value of N.

Output Format:
The first line contains the output as per the description. (The output is CASE SENSITIVE).

Boundary Conditions:
1 <= N <= 12

Example Input/Output 1:
Input:
5
Output:
May

Example Input/Output 2:
Input:
105
Output:
Invalid Input

Solution 1:
import calendar
n=int(input())
print(calendar.month_name[n]) if n<13 else print('Invalid Input')

Solution 2:
d={1:'January',2:'February',3:'March',4:'April',5:'May',6:'June',7:'July',8:'August',9:'September',10:'October',11:'November',12:'December'}
n=int(input())
print(d[n]) if n<13 else print('Invalid Input')

Solution 3:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
    switch(n)
    {
        case 1:
            printf("January");
            break;
        case 2:
            printf("February");
            break;
        case 3:
            printf("March");
            break;
        case 4:
            printf("April");
            break;
        case 5:
            printf("May");
            break;
        case 6:
            printf("June");
            break;
        case 7:
            printf("July");
            break;
        case 8:
            printf("August");
            break;
        case 9:
            printf("September");
            break;
        case 10:
            printf("October");
            break;
        case 11:
            printf("November");
            break;
        case 12:
            printf("December");
            break;
        default:
            printf("Invalid Input");
    }
}
Share:

String Concatenation

Two string values S1 and S2 are passed as input. The program must print the output as S1 Technologies S2.

Input Format:
The first line denotes the value of S1. The second line denotes the value of S2.

Output Format:
The first line contains the output as per the description. There must be exactly one space between the words.

Boundary Conditions: Length of S1 is from 1 to 100. Length of S2 is from 1 to 100.

Example Input/Output 1:
Input:
Wipro
Bangalore
Output:
Wipro Technologies Bangalore

Solution:
x,y=input().strip(),input().strip()
print(x+' Technologies '+y)
Share:

Friday, October 6, 2017

Perfect Number

Given a positive integer N as the input, the program must print yes if N is a perfect number. Else no must be printed.

Input Format:
The first line contains N.

Output Format:
The first line contains yes or no

Boundary Conditions: 1 <= N <= 999999

Example Input/Output 1:
Input:
6
Output:
yes

Example Input/Output 2:
Input:
8
Output:
no

Solution 1:
n,s,i=int(input()),1,2
while i*i<=n:
    if n%i==0:
        s+=i+n/i
    i+=1
print('yes') if n==s else print('no')

Solution 2:
#include <iostream>
using namespace std;
int main()
{
 int i,n,s=1;
 cin>>n;
 for(i=2;i*i<=n;i++)
  if(n%i==0)
   s+=i+n/i;
 s==n?cout<<"yes":cout<<"no";
}
Share:

Thursday, October 5, 2017

Second Largest Value

The program must accept a set of N integer values as command line arguments and print the second largest value. If there is no second largest value, print -1 as the output.

Example Input/Output 1:
Input:
20 30 30 20 30 15 12 11 10 6
Output:
20

Example Input/Output 2:
Input:
19 19 19 19 19 19
Output:
-1

Solution:
#include <stdio.h>
#include <stdlib.h>
void main(int c,char *a[])
{
    int i,f=-1,s,n;
    for(i=1;i<c;i++)
     {
         n=atoi(a[i]);
        if(n>f)
        {
            s=f;
            f=n;
        }
        else if(n>s&&n<f)
         s=n;
     }
     printf("%d",s);
}
Share:

Wednesday, October 4, 2017

Only Alphabets and Space

Given a String S, remove all the special characters and numbers from S and print.

Input Format:
The first line contains the string S

Output Format:
The first line contains the string with only alphabets and spaces.

Boundary Conditions: 1 < Length of S < 1000

Example Input/Output 1:
Input:
Hello #$ World!
Output:
Hello World

Solution 1:
import re
print(''.join(re.findall('[A-Za-z]+|\s',input())))

Solution 2:
s=input()
for i in s:
    if i.isalpha() or i==' ':
       print(i,end='')
Share:

Print Largest Even Number - Digits

Given a number N as the input, print the largest even number E that can be formed using the digits present in the number. (There will be at least one even digit).

Input Format:
The first line contains N.

Output Format:
The first line contains E.

Example Input/Output 1:
Input:
1902
Output:
9210

Solution:
n=list(input())
e=min(filter(lambda x:(ord(x)-ord('0'))%2==0,n))
n.remove(e)
n.sort(reverse=True)
n.append(e)
print(''.join(n))
Share:

Reverse Number

The program must accept a number N using command line and prints the reverse of the number N.

Example Input/Output 1:
Input:
12345
Output:
54321

Example Input/Output 2:
Input:
10900
Output:
901

Solution:
#include <stdio.h>
#include <stdlib.h>
void main(int c,int *s[])
{
    int r=0,n=atoi(s[1]);
    while(n)
    {
        r=r*10+n%10;
        n/=10;
    }
    printf("%d",r);
}
Share:

Integer - Sum of Digits

The program must accept an integer value N as a command line argument and print the sum of the digits in N.

Example Input/Output 1:
Input:
14
Output:
5

Example Input/Output 2:
Input:
1932
Output:
15

Solution:
#include <stdio.h>
#include <stdlib.h>
void main(int c,char *n[])
{
    int a=atoi(n[1]),s=0;
    while(a)
    {
        s+=a%10;
        a/=10;
    }
    printf("%d",s);
}
Share: