Monday, 27 April 2020

Note : some modification required !! Hackerearth Byteland Problem : Solution in python

 PROBLEM STATEMENT
Points: 50
In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).
You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins. You have one gold coin. What is the maximum amount of American dollars you can get for it?
Input The input will contain several test cases (not more than 10). Each testcase is a single line with a number n, 0 <= n <= 1 000 000 000. It is the number written on your coin.
Output For each test case output a single line, containing the maximum amount of American dollars you can make.
Explanation You can change 12 into 6, 4 and 3, and then change these into $6+$4+$3 = $13. If you try changing the coin 2 into 3 smaller coins, you will get 1, 0 and 0, and later you can get no more than $1 out of them. It is better just to change the 2 coin directly into $2.
SAMPLE INPUT
 
12
2

SAMPLE OUTPUT
 
13
2

Time Limit: 9.0 sec(s) for all input files combined.


Solution in python :





list1 = []
while True :
    try :
        n = input()
        if n :
            count = 0
            n = int(n)
            count+=int(n/2)+int(n/3)+int(n/4)
            if n>count :
               count = n
            list1.append(count)
    except EOFError :
           break
for i in list1 :
    print(i) 

Hackerearth : checking horizontal and vertical symmetric matrix : Solution in python

PROBLEM STATEMENT
Points: 30
You are given a square matrix of size n. Rows are indexed 1 to n from top to bottom and columns are indexed 1 to n form left to right. Matrix consists of only '*' and '.'. You need to check whether matrix is symmetric or not. if it is, check it is symmetric about vertical axis or horizontal axis or both.
A matrix is said to be symmetric about horizontal axis if 1st row is identical to nth row, 2nd is identical to (n−1)th row and so on...
A matrix is said to be symmetric about vertical axis if 1st column is identical to nth column, 2nd identical to (n−1)th and so on for all columns.
INPUT :
First line contains t,the number of test cases. First line of each test case contains n the size of matrix. Each of next n lines contain n characters.
OUTPUT:
Output t lines, answer for each test case. Print "HORIZONTAL" if symmetric about horizontal axis. Print "VERTICAL" if symmetric about vertical axis. Print "BOTH" if symmetric about both axes. print "NO" if it is not symmetric.
Constraints :
1<t≤500
1<n<50
SAMPLE INPUT
 
3
4
*.*.
.*.*
*.*.
.*.*
3
.*.
*.*
.*.
3
..*
**.
..*


SAMPLE OUTPUT
 
NO
BOTH
HORIZONTAL

Solution in python:

list2 = []
t = int(input())
for i in range(t) :
    list1 = []
    count = 0 
    count2 = 0
    n  = int(input())
    for j in range(0,n) :
        list1.append(input().split([0])
    for k in range(0,n) :
        if list1[0+k] != list1[n-1-k] :
            count = 0
            break 
        else : 
            count=1

    for j in range(n) :
        for k in range(n) :
                if list1[0+k][0+j] != list1[0+k][n-1-j] :
                    count2 = 0
                    break 
                else : 
                    count2 = 1
    if count==0 and count2==0 :
        list2.append('NO')
    elif count==0 and count2 :
        list2.append('VERTICAL')
    elif count and count2 == 0 :
         list2.append('HORIZONTAL')
    else :
         list2.append('BOTH')
for i in list2 :
    print(i)


Hackerearth Cats , Dogs and fencing problem : Solution in python

PROBLEM STATEMENT
Points: 50
  • Russian version of the problem can be read here.
As you probably know, cats usually fight with dogs.
There are N doghouses in a backyard. For simplicity, we consider the backyard as a plane, and the doghouses as points on the plane. All doghouses are numbered from 1 to N. The i-th doghouse is located at point (i, Y[i]). (Note that this means that there are no two doghouses with the same x-coordinate.)
Cats are about to build some rectangular fence, parallel to the axis. If some doghouse is located at the perimeter of the fence, it should be removed. Your task it to find the maximum possible number of doghouses that may be removed after choosing arbitrary fence sizes and location. Please note that the area covered by the fence may be equal to 0.
Input format
The first line of the input contains the single integer N - the number of doghouses. The next line contains N space-separated integers that are Y-coordinates of the corresponding doghouse.
Output format
In the only line of the output print the maximum number of doghouses that may be removed.
Constraints
1 ≤ N ≤ 1,000, 0 ≤ Y[i] ≤ 1,000.
SAMPLE INPUT
 
5
2 3 4 1 1
SAMPLE OUTPUT
 
4

Solution in python: 


import math
lset = set([])
count = 0
t = int(input())
list1 = input().split()
for j in list1 :
        inj = int(j)
        length = int(math.sqrt(count*count+inj*inj))
        count+=1
        lset.add(length)
l = len(lset)
c = count+1-l+1
print(c)

Saturday, 25 April 2020

Hackerearth Problem min-max of N positive Integers : Solution in Python

PROBLEM STATEMENT
Points: 30
Given A Series Of N Positive Integers a1,a2,a3........an. , Find The Minimum And Maximum Values That Can Be Calculated By Summing Exactly N-1 Of The N Integers. Then Print The Respective Minimum And Maximum Values As A Single Line Of Two Space-Separated Long Integers.
Input Format
First Line Take Input Value Of N
Second Line Take Input N Space Separated Integer Value
Output Format
Two Space Separated Value ( One Maximum Sum And One Minimum Sum )
Constraints
  • 0 < N < 100001
  • 0 <= ai < 1013
SAMPLE INPUT
5
1 2 3 4 5
SAMPLE OUTPUT
10 14
Explanation
Our initial numbers are 1,2,3,4 and 5. We can calculate the following sums using four of the five integers:
  1. If we sum everything except 1, our sum is 2+3+4+5=14 .
  2. If we sum everything except 2, our sum is 1+3+4+5=13 .
  3. If we sum everything except 3, our sum is 1+2+4+5=12 .
  4. If we sum everything except 4, our sum is 1+3+4+5=11 .
  5. If we sum everything except 5, our sum is 1+2+3+4=10 .
As you can see, the minimal sum is 1+2+3+4=10 and the maximal sum is 2+3+4+5=14. Thus, we print these minimal and maximal sums as two space-separated integers on a new line.





Solution in Python :


import array
n = int(input())
nums = input()
list1 = nums.split()
list1 = [int(i) for i in list1]
list1.sort()
total = sum(list1)
min = total - list1[len(list1)-1]
max = total - list1[0]
print(min,max)




Saturday, 18 April 2020

Hackerearth : Roy's birthday and balloons problem : Solution in Python






PROBLEM STATEMENT
Points: 30
Its Roy's birthday and he is decorating his house with balloons.
He has 26 different types of balloons. They are represented by [a-z].
Now Roy does not want to have few particular balloons together. Actually he doesn't want to keep any of the two vowel (i.e [a,e,i,o,u] ) balloons together.
So, in one arrangement no two vowels should be together. Given the string of balloons there can be many such arrangements possible.
Help Roy in finding the number of possible arrangements.
Number of such arrangements can be quite large, so print it modulo 1000000007 (109 + 7)
Also, there's a chance that no such arrangement is possible. Print "-1" in such cases (without quotes)
Input:
First line contains T, number of test cases.
Each of next T lines contains a string composed of lower case letters representing all the balloons.
Output:
For each test case print the required answer.
Constraints:
1 <= T <= 100
1 <= Length of string <= 1000000 (106)
Note: Sum of string lengths over all the test cases in one file does not exceed 1000000 (106)
SAMPLE INPUT
 
2
abe
ae
SAMPLE OUTPUT
 
2
-1
Explanation
Test #1: Possible arrangements are [1] abe [2] eba
Test #2: There are no any arrangements possible such that any two vowels are not together. So print -1.
Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded if any testcase passes.


Solution in Python :


import math
import array
total = array.array('i',[])
i = 0
t = int(input())
while i<t :
    count = 0;
    s = input()
    for j in s :
        if j == 'a' or j == 'e' or j == 'i' or j == 'o' or j == 'u' :
           count+=1
    cons = len(s) - count
    factcons = math.factorial(cons)
    factvowl = math.factorial(count)
    factdiff = math.factorial(abs(cons-count))
    total.append(int(factcons*factvowl*(cons+1)*factcons/(factvowl*factdiff)))
    i+=1
for ele in total :
   print(ele)

Hackerearth string problem : Solution in Python




PROBLEM STATEMENT
Points: 30
Two letter strings are the strings consisting of only two letters "X" and "Y". A string is "super two letter string" if
a) It does not have leading "X" letters.
b) It does not contain P consecutive "X" letters.
Your task is to find total number of Super two letter strings of length N.
Input :
The first line contains the number of test cases T . Each test case consists of two space separated integers - N and P .
Output :
For each test case output total number of Super two letter strings of length N modulo 1000000007(10^9+7).
Constraints :
1 <= T <= 100
1 <= N <= 10^4
1 <= P <= 10
SAMPLE INPUT
2
2 1
4 2
SAMPLE OUTPUT
1
5
Explanation
For first sample : Only possible string is : YY For second sample : Possible strings are : YXYX , YXYY, YYYX, YYXY, YYYY





Solution  in   Python



import math
import array
ar = array.array('i',[]);
i = 1
c=0
T = input()
while i <=int(T):
    N1 = input()
    n2 = N1
    n = int(n2.split()[0])-1
    P = int(n2.split()[1])
    a=[0 for i in range(n)]
    b=[0 for i in range(n)]
    a[0] = b[0] = 1
    for i in range(1,n):
        a[i] = a[i-1] + b[i-1]
        b[i] = a[i-1]
        c+=a[n-1] + b[n-1]
    ar.append(c)
    i+=1
for j in ar :
    print(j)

Thursday, 11 April 2019

Why can we not see 4d or more than 3 dimension ? what are we missing ?










this is based on a scientific theory mingled with my thinking and with a blend of a scientist's theory.


So i am starting it here..
scientists are using many theories , many theoritical concepts and based on that they are proving many  space related problem and in those problems  and they are shamelessly using more than 3 dimensions because without using more than 3 dimensions they can not prove these theories and can not create any new solution.

But how they are using more than 3d to prove space related problems , because there is no other way to prove that , and the these events in space are practically happenning and are beyond the limits of 3d.

from here a blend of many theories starts:


 you are 3d ...you believe that ..and everyone believes that everything is 3d ,  because no one has seen 4d.

so suppose a 4d object came  but you can see his extra dimension because you are 3d . Then suppose he threw you in a new dimension and you realized that yes ..there is a another dimension.

But no any other person in your neighbourhood have seen this event  .. so when you will tell this to other people then no on will believe on you and some will think that you are mad . 

so you can not prove this and if you will not become mad thinking about this ...after some day you will start thinking like that may be that event could be a dream or that 4d guy could be ghost.

but think about that......

you can not imagine that world is really different in more dimensions.

we are loosing one or more dimensions...we are really really different from what we see ...how we will look like in 4d.

there can be many routes , ways and space , but we can not see it

there can be shorter routes ..there can be a way to other galaxy from the door of you house but we can not see that..

suppose there can be a alien race living in a different dimension beside your home...suppose these alien races got knowledge of 4d ...then no one can imagine ..they can attack on us in any way ...but we can not see them because we do not have knowledge of other dimensions.

world is really different in other dimensions but no one can see it . We are missing the beauty of real world. 
We dont know who we really are.... 

everything looks different as we see it in more dimensions.


cube     will be     hypercube or a Tesseract..
human will be   ?????  


 

Algorithm helped in releasing the black hole image.



New Update watch on twitter

watch Katie bouman


source: wikipidea












The Algorithm name was CHIRP that is Continuous High-resolution Image Reconstruction using Patch priors .
This algorithm was used to combine data from the eight telescope which were in different continents and were working under Event Horizon Telescope .

The Event Horizon Telescope (EHT) is a large telescope array consisting of a global network of radio telescopes and combining data from several very-long-baseline interferometry (VLBI) stations around the Earth. The aim of the EHT project is to observe the immediate environment of the supermassive black hole Sagittarius A* at the center of the Milky Way, as well as the even larger black hole in the center of the supergiant elliptical galaxy Messier 87, with angular resolution comparable to the black hole's event horizon.[1]  

Source : https://en.wikipedia.org/wiki/Event_Horizon_Telescope

This blackhole contains mass as 6.5 billion suns.







Thursday, 3 May 2018

CallBack Methods are really amazing when working in asynchronous ennvironment ..

I have done some work in javascript and when i was woking with asynchronous calls i was in big trouble because :

# i was using that  result (r) from a asynchronous call  in some other function (f) and the problem was that -

 function (f)  was running before (r) was getting result from a asynchronous call.


# the same problem i faced when getting result with Google API.







                                     But          Then         Whoooa.......


Every problem solved by just using JavaScript Callabck Function.

because the callback function runs only when it is getting the actual result from asynchronous calls and you can get results from that function easily , where you need , is it not simple ?????.


So use callbacks when using asynchronous calls.


Here are some links when you can read about javascript callback functions :


# https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

#https://medium.com/codebuddies/getting-to-know-asynchronous-javascript-callbacks-promises-and-async-await-17e0673281ee   

# https://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function