21 December 2017

UVA 11541 Python solution

from sys import stdin
scan = lambda : stdin.readline()

test = int(scan())

for case in range(1,test+1):
    text = scan()
    counter =0    print("Case %d: "%case,end='')
    for i in range(0,len(text)):
        if text[i]>='A' and text[i]<='Z':
            txt = text[i]
        elif text[i]>='0' and text[i]<='9':
            counter = (counter*10) + int(text[i])
        if i == len(text)-1 or (text[i+1]>='A' and text[i+1]<='Z'):
            for k in range(1,counter+1):
                print(txt,end='')
            counter =0    print()

UVA 11417 python solution

from sys import stdin
scan = lambda : stdin.readline()

def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)


while True:
    num = int(scan())
    if num ==0:
        break    sum=0    for i in range(1,num):
        for j in range(i+1,num+1):
            sum = sum + gcd(i,j)

    print(sum)

UVA 10773 python solution

import sys
from math import asin,tan
scan = lambda : sys.stdin.readline()

test = int(scan())

for case in range(1,test+1):
    d,v,u = map(int,scan().split())
    if u<=v or u==0 or v==0:
        print("Case %d: can't determine"%case)
    else:
        theta = asin(v/u)
        u1 = v/tan(theta)
        t1 = d/u1
        t2 = d/u
        print("Case %d: %.3f"%(case,t1-t2))

20 December 2017

Example of decorator in pyhon

from time import time

def timer(any_function):
    def count_time():
        start = time()
        any_function()
        stop = time()
        print("%.15f"%(stop-start),'seconds')
        return    return count_time

@timerdef hello():
    print("Hello World")
    return
@timerdef another_function():
    for item in [1,2,3,4,5,6,7,8,9]:
        print("%d "%item,end='')
    print()
    return
hello()
another_function()

19 December 2017

unpacking argument in python

# how to unpack argument in a functiondef health_calculator(age,apple_amnt, num_ciggrt):
    #this equation is just for consideration    health = (100-age) + (apple_amnt*3.5) - (num_ciggrt*2.5)
    print(health)

dieat_list = [45,10,1]
health_calculator(dieat_list[0],dieat_list[1],dieat_list[2])
health_calculator(*dieat_list)

Keyword arguments in python

def dummey_text(name='samsung',action='android',item='samsung j5'):
    print(name, action, item)

dummey_text()
dummey_text('huwaie','android','huwaie mini tab')
dummey_text('japan_x','java')
dummey_text(item='DUS5',name='galaxy note', action='java')

Default value argument in python

def gender(sex='unknow'):
    if sex == "m":
        sex = "male"
    elif sex == 'f':
        sex = "female"   
    print(sex)

gender('m')
gender('f')
gender()

UVA 11388

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
int main()
{
int test,a,b,g,l,m;
sc("%d",&test);
while(test--)
{
sc("%d %d",&a,&b);
if(b%a==0)
pf("%d %d\n",a,b);
else
pf("-1\n");
}
return 0;
}

UVA 106 Fermat vs. Pythagoras

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
int data[1000001];
int gcd(int a, int b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
void finding_value(int n)
{
int tri=0 , total=0,up,y,x,z;
memset(data,0,sizeof(data));
int m = sqrt(n);
if(m*m<n)
m++;
for(int r=1 ; r<=m ; r++)
{
up = min((n-r*r),r-1);
for(int s=1 ; s<=up ; s++)
{
x = r*r-s*s;
y = 2*r*s;
z = r*r + s*s;
if(x*x + y*y == z*z && z<=n)
{
if(gcd(x,y)==1)
{
tri++;
for(int k=1 ; k*z<=n ; k++)
{
data[k*x] =1;
data[k*y] =1;
data[k*z] =1;
}
}
}
}
}
for(int k=1 ; k<=n ; k++)
{
if(data[k]==0)
total++;
data[k]=0;
}
pf("%d %d\n",tri,total);
}
int main()
{
int n;
while(sc("%d",&n)==1)
{
finding_value(n);
}
return 0;
}

UVA 10679 - I Love Strings!!