Friday, 23 October 2020

Strings: Making Anagrams by given two strings on Haccerrank Interview Preparation kit solution in python

 # Complete the makeAnagram function below.

def makeAnagram(a, b):
    lst1 = list(a)
    lst2 = list(b)
    print(lst1,lst2)
    i = -1
    c=0
    while(i<len(lst1)-1):
        i = i+1
        if lst1[i] in lst2:
            print(lst1[i])
        else:
            print("not in both")
            lst1.remove(lst1[i])
            c=c+1
            i=i-1
    print(lst1)
    i=-1
    while(i<len(lst2)-1):
        i = i+1
        if lst2[i] in lst1:

            print(lst2[i])
        else:
            print("not in both")
            lst2.remove(lst2[i])
            c=c+1
            i=i-1 
    print(lst2)  
    print("val of c",c)
    lst1.sort()
    lst2.sort()
    print(lst1,"\n",lst2)
    lst = list(set(lst1))
    for i in lst:
        if lst1.count(i) == lst2.count(i):
            print("same frew")
        else:
            diff = lst1.count(i) - lst2.count(i)
            print("dff in freq" , diff)
            if diff > 0:
                c= c+diff
            else:
                diff = -1*diff
                c= c+diff
                
    return c

Sunday, 18 October 2020

Special Pattern Printing in C using loops hackerrank.

 #include <stdio.h>

#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{

    int n;
    scanf("%d", &n);
    // Complete the code to print the pattern.
    int arr[100000];
    for(int i=0;i<=2*n - 2;i++)
    arr[i]= n;

    for(int i=0;i<=2*n - 2;i++)
    printf("%d ",arr[i]);
    printf("\n");

    int start = 1;
    int end = 2*n - 3;

    for(int j=1;j<n;j++)
    {
        //printf("\n%d %d\n",start,end);
        for(int i=start;i<=end;i++)
        arr[i]= arr[i] - 1

        for(int i=0;i<=2*n - 2;i++)
        printf("%d ",arr[i]);

        start++;
        end--;

    printf("\n");
    
    }

start = start -1;
end = start;
    
    //("\n%d %d\n",start,end);

    for(int j=1;j<n;j++)
    {
        //printf("\n%d %d\n",start,end);
        for(int i=start;i<=end;i++)
        arr[i]= arr[i] + 1

        for(int i=0;i<=2*n - 2;i++)
        printf("%d ",arr[i]);

        start--;
        end++;

    printf("\n");
    
    }
    return 0;
}