Showing posts with label easy. Show all posts
Showing posts with label easy. Show all posts

Sunday, March 10, 2013

Python FizzBuzz oneliner soluton

Solution-1
for x in range(100):print x%3/2*'Fizz'+x%5/4*'Buzz'or x+1
"""These one liners are not preferred,can be used when min num chars are to be used(when ur being graded based 
on nums of chars"""
Solution-2

for i in range(1,101):print"FizzBuzz"[i*i%3*4:8--i**4%5]or i


Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

Friday, March 1, 2013

Count of Maximum(CodeChef )

Given an array A of length N, your task is to find the element which repeats in A maximum number of times as well as the
corresponding count. In case of ties, choose the smaller element first.
Input

First line of input contains an integer T, denoting the number of test cases. Then follows description of T cases. Each case
begins with a single integer N, the length of A. Then follow N space separated integers in next line. Assume that 1 <= T
<= 100, 1 <= N <= 100 and for all i in [1..N] : 1 <= A[i] <= 10000
Output
For each test case, output two space separated integers V & C. V is the value which occurs maximum number of times and C is its count.
Example Input:
2
5
1 2 3 2 5
6
1 2 2 1 1 2v Output:
2 2
1 3
Description:
In first case 2 occurs twice whereas all other elements occur only once.
In second case, both 1 and 2 occur 3 times but 1 is smaller than 2.
Solution:
from collections import Counter
def com(N,A):
    num_array=A.split()
    c=Counter(num_array)
    C=max(c.values())
    l1=[]
    for k in c:
        if c[k]==C:
            l1.append(k)
    l2=[int(k) for k in l1]
    V1=min(l2)
    print V1,C

t=input()
for i in range(t):
    N=input()
    A=raw_input()
    com(N,A)
This question is special because i've written everything with a little help from a friend @ stackoverflow.com http://stackoverflow.com/questions/15169474/whats-wrong-with-my-solutioncount-of-maximumcode-chef

Decreasing String-codechef

Question:

You need to find a string which has exactly K positions in it such that the character at that position comes alphabetically later than the character immediately after it. If there are many such strings, print the one which has the shortest length. If there is still a tie, print the string which comes the lexicographically earliest (would occur earlier in a dictionary). Input The first line contains the number of test cases T. Each test case contains an integer K (≤ 100). Output Output T lines, one for each test case, containing the required string. Use only lower-case letters a-z.
Sample Input
2
1
2
Sample Output
ba
cba
http://www.codechef.com/problems/DECSTR
Solution:http://www.codechef.com/viewsolution/701336 I tried but it took time to understand,here's a solution.n very first line we can learn many things..
STRING = ''.join(map(lambda x: chr(x+97), reversed(range(26))) * 4)
 
def shortest_k_string(k):
 if k < 26: return STRING[103-(k+0):]
 if k < 51: return STRING[103-(k+1):]
 if k < 76: return STRING[103-(k+2):]
 if k < 101: return STRING[103-(k+3):]
 
t = int(raw_input())
for i in range(t):
 k = int(raw_input())
 print shortest_k_string(k)
Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

The Best Box(codejam problem solved)

Question: Johnny needs to make a rectangular box for his physics class project. He has bought P cm of wire and S cm2 of special paper. He would like to use all the wire (for the 12 edges) and paper (for the 6 sides) to make the box. What is the largest volume of the box that Johnny can make? Input The first line contains t, the number of test cases (about 10). Then t test cases follow. Each test case contains two integers P and S in a line (1 ≤ P ≤ 40000, 1 ≤ S ≤ 20000). You may assume that there always exists an optimal solution for the given input cases. Output For each test case, print a real number that is the largest volume of the box that Johnny can make, rounded to two decimal places. Example Input: 2 20 14 20 16 Output: 3.00 4.15 Output details First case: the dimensions of the largest box may be 3, 1 and 1. Second case: the dimensions of the largest box may be 7/3, 4/3 and 4/3. Link to this question At first I couldn't do this but after having a look at this solution it became easier.. test cases have their importance... Original Soln==>http://www.codechef.com/viewsolution/701328
from math import sqrt
def highest_vol(p,s):
    """
    s=2(lb+bh+hl)==>formula for surface area of cuboid
    p=4(l+b+h)=====>Sum of all edges
    based on test case results given we can observe that b=h substitute in the above equations
    l=p/4-2h
    3h^2-ph/2+s/2=0 solve this quadratic for h1,h2
    """
    constant=sqrt(p*p-24*s)
    H=[(p+constant)/12.,(p-constant)/12.]
    L=[(p-8*H[0])/4.,(p-8*H[1])/4.]
    V=[L[0]*H[0]*H[0],L[1]*H[1]*H[1]]
    return max(V)
inp=int(raw_input())
for i in range(inp):
     p,s= map(int, raw_input().split())
     print '%0.2f' % highest_vol(p,s)


Thursday, February 28, 2013

NOT A TRI (codechef problem unsolved)


from sys import stdin,stdout
from collections import deque
def main():
    new_line=deque()
    line=stdin.readline().split()
    while int(line[0]) != 0:
        for i in line:
            new_line.append(int(i))
        gab=new_line.popleft()
        highest_number=max(new_line)
        new_line.remove(highest_number)
        sum_two_sides=0
        count=0
        j=0
        while j<=len(new_line):
            sum_two_sides=new_line(j)+new_line(j+1)
            if sum_two_sides > highest_number:
                count=count+1
                j=j-1
        line=stdin.readline().split()
        print(count)
main() 
Here I've posted the code I don't know what's wrong if some body happens to solve this help me out :)) http://www.codechef.com/status/NOTATRI,cybercam check my submissions :)) Link to the problem My incomplete Solution: