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: