Saturday, June 3, 2017

Smallest Substring - All Distinct Letters

A string value S containing only alphabets (both lower and uppercase) is passed as input to the program. The program must print the size of the smallest sub-string which contains all the distinct alphabets in the string S. (lowercase and uppercase characters are treated as two different unique characters)

Input Format:

First line contains S.

Output Format:

First line contains the size of the smallest substring which contains all the distinct characters in the string S.

Example Input/Output 1:

Input:
abcdfffccbbfeaad

Output:
8

Explanation:
The smallest sub string that contains all the distinct letters is cbbfeaad whose length is 8. 

Solution:

s = input()
st = set(s)
a = [0]*len(s)
for i in range(len(s)):
    for j in range (1,len(s)+1):
        x = s[i:j]
        xt = set(x)
        if len(st) == len(xt):
            if len(x)<len(a):
                a = x

print(a)                
print(len(a))

Share:

Friday, June 2, 2017

Add Numbers - Base N

Two numbers X and Y are provided with reference to base N. Add the numbers and print their sum with reference to base 10. 

Input Format: 

First line will contain the value of N. Second line will contain X and Y separated by one or more spaces. 

Output Format: 

First line will contain the sum of X and Y to the base 10. 

Example Input/Output 1: 

Input: 2 1010 11 
Output: 13 
Explanation: 1010 to the base 2 is 10. 11 to the base 2 is 3. Hence the sum is 10+3 = 13.

Solution:

b = int(input())
x = input().split()
print(int(x[0],b)+(int(x[1],b)))
Share:

Next & Previous Immediate Prime Difference

A number N is passed as the input. The program must print the difference between the next immediate prime number and the previous immediate prime number. If the given number N itself is a prime number then print 0 (zero). 

Input Format: 

The first line will contain the value of the number N. 

Output Format: 

The difference between the next immediate prime number and the previous immediate prime number for N. 

Example Input/Output 1: 

Input: 14 
Output: 4 
Explanation: The next immediate prime number is 17. The previous immediate prime number is 13. Hence their difference = 17-13 = 4 is printed as output. 

Solution:

def prim3(n,a):
    x = n
    for i in range(2,int((n/2) + 5)):
        if n%i == 0 and a:
            n -= 1
        elif n%i == 0:
            n += 1
    return 0 if n == x else n
n = int(input())
print(prim3(n,0)-prim3(n,1))

Share:

Thursday, June 1, 2017

Longest sub-string with N unique characters

The program must find the length L of the longest sub-string with N unique characters in a given string value S 

Input Format:

The first line will contain the string value S.
The second line will contain the number of unique characters N.

Output Format:

Length L of the sub-string longest sub-string with N unique characters.

Example Input/Output 1: 

Input:
abcdcdabcdef
2

Output:
4

Explanation:
The longest possible substring with 2 unique characters in abcdcdabcdef is cdcd whose length is 4.

Solution:

s = input()
n = int(input())
a = []
for i in range(len(s)):
    for j in range(1,len(s)+1):
        st = s[i:j]
        y = set(st)
        if len(y) == n:
            if len(a) < len(st):
                a = st

print(len(a))
Share:

Wednesday, May 31, 2017

sWAP cASE

You are given a string S. The task is to Swap Cases. In other words, convert all lowercase letters to uppercase letters and vice versa.
For Example:
Www.HackerRank.com → wWW.hACKERrANK.COM
Pythonist 2 → pYTHONIST 2
Input Format
A single line containing a string S.
Output Format
Print the modified string S.

Solution:

def swap_case(s):
    a = s.swapcase()
    return a
b = input()
print(swap_case(b))

Share:

Smallest Discount item

Choose item with the smallest discount offered among n items given

Input Format 

First line will contain the value of  n
Followed by n lines contains item name, cost and discount percent separated by spaces.

Output Format

The name of the item which offers least discount 


a = int(input())
c = 999999
for i in range (a):
    b = input().split()
    d = int(b[1])*(int(b[2])/100)
    if c>d:
        c = d
        s = b[0]

print(s)

Share:

Balanced Brackets

Given n strings of brackets, determine whether each sequence of brackets is balanced. If a string is balanced, print YES on a new line; otherwise, print NO on a new line.
Input Format
The first line contains a single integer n denoting the number of strings.
Each line  of the  subsequent lines consists of a single string s denoting a sequence of brackets.
Output Format
For each string, print whether or not the string of brackets is balanced on a new line. If the brackets are balanced, print YES; otherwise, print NO.

t = { ')': '(', ']':'[', '}':'{' }
for i in range(int(input())):
    s = []
    for x in input():
        if s and t.get(x) == s[-1]:
            s.pop()
        else:
            s.append(x)
    print("NO" if s else "YES")


Share: