Showing posts with label thinkprogrammers. Show all posts
Showing posts with label thinkprogrammers. Show all posts

How To Add Environment Variable In Windows

 How To Add Environment Variable In Windows 


Step 1 : Go to the Location of actual executable which entry you want to add in                                   Environment variables.
                    Example : YouTube-dl executable ..
                    
                    Traverse to the executable and copy the address from Address Bar . 


Step 2 : Right Click on Computer icon on your Desktop and Open Properties . 

Step 3 : Click On advance System Settings



Step 4 : Click On Environment variables .



Step 4 : In System variables find for Path and Click on Edit...



Step 5 : Append ";"  in Variable Value And Paste the Address in the field ..
                    and click OK all the 3 opened windows ...

Step 6 : You have Successfully added entry in Environment variable in PC .

step 7 : Now open cmd and now you can run the added executable command line .

                    for example :




Share:

How To DownLoad Full YouTube PlayList ..!!!

    How To DownLoad Full YouTube PlayList..!!! 



Method 1 : Using Software(4K Downloader) 

Step 1 :  Download 4K downloader here 

Step 2 :  Go to the YouTube PlayList Page And Copy the Address of the URL. 




Step 3 :  Open 4K downloader .
                      ClipBoard will be automatically copied into 4K Downloader.


Step 4 : Click on Paste Link.



Step 5 : It Will open a window Showing Download Full Playlist ..
                  Click on It.


Step 6 : It will show parsing window.It will take 2-3 minutes according to your internet speed.

            If asked for activation . Click on Skip.  
  
             Choose the format and other preferences and Click on Download .

   
          
Step 7 : Then the Downloader starts and download all the videos in playlist .


Step 8 : Downloaded files can be found on location in C drive in my videos / You can                         save your preferences in 4K downloader setting to save the location to store the                  downloaded videos .  

Method 2 : Using Command Line 

Step 1 : Download youtube_dl utility here

Step 2 : Add the entry to your Desktop's Environment Variables.
          

Step 3 : Open Command Prompt.

Step 4 : type youtube-dl <Link of the playlist to be downloaded>


Step 5 : This will automatically download all the videos in the playList in 720p. To choose                      from various options read youtube-dl documentation

Step 6 : The Downloaded files can be found in the <User_currently_logged_in> directory                     in C drive .


Share:

Hacker Rank - Day 23 : BST Level-Order Traversal

                   Day 23 : BST Level-Order Travarsal

Solution In Java 8:

import java.util.*;

import java.io.*;


class Node{

    Node left,right;

    int data;

    Node(int data){

        this.data=data;

        left=right=null;

    }

}

class Solution{


static void levelOrder(Node root){
      Queue<Node> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            Node curr = queue.remove();
            System.out.print(curr.data + " ");

            if (curr.left != null) queue.add(curr.left);
            if (curr.right != null) queue.add(curr.right);
        }
    }

public static Node insert(Node root,int data){
        if(root==null){
            return new Node(data);
        }
        else{
            Node cur;
            if(data<=root.data){
                cur=insert(root.left,data);
                root.left=cur;
            }
            else{
                cur=insert(root.right,data);
                root.right=cur;
            }
            return root;
        }
    }
    public static void main(String args[]){
            Scanner sc=new Scanner(System.in);
            int T=sc.nextInt();
            Node root=null;
            while(T-->0){
                int data=sc.nextInt();
                root=insert(root,data);
            }
            levelOrder(root);
        }
}


Solution In Python 3: 

import sys

class Node:
    def __init__(self,data):
        self.right=self.left=None
        self.data = data
class Solution:
    def insert(self,root,data):
        if root==None:
            return Node(data)
        else:
            if data<=root.data:
                cur=self.insert(root.left,data)
                root.left=cur
            else:
                cur=self.insert(root.right,data)
                root.right=cur
        return root
    def levelOrder(self, root):
        queue = [root]
        while len(queue) is not 0:
            curr = queue[0]
            queue = queue[1:]
            print(str(curr.data) + " ", end="")

            if curr.left is not None:
                queue.append(curr.left)
            if curr.right is not None:
                queue.append(curr.right)
T=int(input())
myTree=Solution()
root=None
for i in range(T):
    data=int(input())
    root=myTree.insert(root,data)
myTree.levelOrder(root)


Share:

Hacker Rank - Day 22 : Binary Search Trees

                   Day 22 : Binary Search Trees

Solution In Java 8:


import java.util.*;

import java.io.*;

class Node{

    Node left,right;

    int data;

    Node(int data){

        this.data=data;

        left=right=null;

    }

}

class Solution{

public static int getHeight(Node root){
         return root == null ? -1 : 1 + Math.max(getHeight(root.left), getHeight(root.right));
    }
public static Node insert(Node root,int data){
        if(root==null){
            return new Node(data);
        }
        else{
            Node cur;
            if(data<=root.data){
                cur=insert(root.left,data);
                root.left=cur;
            }
            else{
                cur=insert(root.right,data);
                root.right=cur;
            }
            return root;
        }
    }
public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();
        Node root=null;
        while(T-->0){
            int data=sc.nextInt();
            root=insert(root,data);
        }
        int height=getHeight(root);
        System.out.println(height);
    }
}


Solution In Python 3: 

class Node:
    def __init__(self,data):
        self.right=self.left=None
        self.data = data
class Solution:
    def insert(self,root,data):
        if root==None:
            return Node(data)
        else:
            if data<=root.data:
                cur=self.insert(root.left,data)
                root.left=cur
            else:
                cur=self.insert(root.right,data)
                root.right=cur
        return root
    def getHeight(self,root):

        return -1 if root is None else 1 + max(self.getHeight(root.left), self.getHeight(root.right))
T=int(input())
myTree=Solution()
root=None
for i in range(T):
    data=int(input())
    root=myTree.insert(root,data)
height=myTree.getHeight(root)

print(height)       

Share:

Hacker Rank - Day 21 : Generics

                   Day 21 : Generics

Solution In Java 8:


import java.util.*;

class Printer <T> {
    /**
    *    Method Name: printArray
    *    Print each element of the generic array on a new line. Do not return anything.
    *    @param A generic array
    **/
    
   public static <Element> void printArray(Element[] array) {
        for (Element element : array) {
            System.out.println(element);
        }

    }
}

public class Generics {
    
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Integer[] intArray = new Integer[n];
        for (int i = 0; i < n; i++) {
            intArray[i] = scanner.nextInt();
        }

        n = scanner.nextInt();
        String[] stringArray = new String[n];
        for (int i = 0; i < n; i++) {
            stringArray[i] = scanner.next();
        }
        
        Printer<Integer> intPrinter = new Printer<Integer>();
        Printer<String> stringPrinter = new Printer<String>();
        intPrinter.printArray( intArray  );
        stringPrinter.printArray( stringArray );
        if(Printer.class.getDeclaredMethods().length > 1){
            System.out.println("The Printer class should only have 1 method named printArray.");
        }
    } 

}

Share:

Hacker Rank - Day 20 : Sorting

                                     Day 20 : Sorting

Solution In Java 8:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int temp;
        int n = in.nextInt();
        int a[] = new int[n];
        int numSwaps = 0, firstElement,lastElement;
        
        for(int a_i=0; a_i < n; a_i++){
            a[a_i] = in.nextInt();
        }
       for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - 1; j++) {
                if (a[j] > a[j + 1]) {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                    numSwaps++;
                }
            }

            if (numSwaps == 0) {
                break;
            }
       }     
        System.out.println("Array is sorted in "+numSwaps+" swaps.");
        System.out.println("First Element: "+a[0]);
        System.out.println("Last Element: "+a[n-1]);
    }
}

Solution In Python 3: 

#!/bin/python3

import sys


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
numSwaps = 0
for i in range(0,n):
    for j in range(0,n-1):
        if(a[j] > a[j+1]):
            temp = a[j]
            a[j] = a[j+1]
            a[j+1] = temp
            numSwaps = numSwaps+1
            
print("Array is sorted in {} swaps.".format(numSwaps))
print("First Element: {}".format(a[0]))
print("Last Element: {}".format(a[n-1]))
Share:

Hacker Rank - Day 18 : Queues and Stacks

                   Day 18 : Queues and Stacks

Solution In Java 8:


import java.io.*;
import java.util.*;

public class Solution {
    Queue <Character> queue = new LinkedList<>();
    Deque <Character> stack = new ArrayDeque<>();
   
    public void pushCharacter(char ch){
        stack.push(ch);
    }
    public void enqueueCharacter(char ch){
        queue.add(ch);
    }
    public char popCharacter(){
        return stack.pop();
    }
    public char dequeueCharacter(){
        return queue.remove();
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        scan.close();

        // Convert input String to an array of characters:
        char[] s = input.toCharArray();

        // Create a Solution object:
        Solution p = new Solution();

        // Enqueue/Push all chars to their respective data structures:
        for (char c : s) {
            p.pushCharacter(c);
            p.enqueueCharacter(c);
        }

        // Pop/Dequeue the chars at the head of both data structures and compare them:
        boolean isPalindrome = true;
        for (int i = 0; i < s.length/2; i++) {
            if (p.popCharacter() != p.dequeueCharacter()) {
                isPalindrome = false;               
                break;
            }
        }

        //Finally, print whether string s is palindrome or not.
        System.out.println( "The word, " + input + ", is "
                           + ( (!isPalindrome) ? "not a palindrome." : "a palindrome." ) );
    }
}




Solution In Python 3: 

import sys
class Solution:
    def __init__(self):
        self.stack = []
        self.queue = []
    def pushCharacter(self , ch):
        self.stack.append(ch)
    def enqueueCharacter(self , ch):
        self.queue.append(ch)
    def popCharacter(self):
        return self.stack.pop()
    def dequeueCharacter(self):
        ch = self.queue[0]
        self.queue = self.queue[1:]
        return ch       
        

s=input()

obj=Solution()   

l=len(s)

for i in range(l):
    obj.pushCharacter(s[i])
    obj.enqueueCharacter(s[i])
    
isPalindrome=True

for i in range(l // 2):
    if obj.popCharacter()!=obj.dequeueCharacter():
        isPalindrome=False
        break
#finally print whether string s is palindrome or not.
if isPalindrome:
    print("The word, "+s+", is a palindrome.")
else:
    print("The word, "+s+", is not a palindrome.")    

Share:

Hacker Rank - Day 19 : Interfaces

                   Day 19 : Interfaces

Solution In Java 8:


import java.io.*;
import java.util.*;

interface AdvancedArithmetic{
   int divisorSum(int n);
}

class Calculator implements AdvancedArithmetic
{
    public int divisorSum(int n)
        {
            int sum=0;
             for(int i=1;i<=n/2;i++)
             {
                 if(n%i==0)
                 {
                     sum=sum+i;
                 }
             }
                return (sum+n);
        }
}

class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();
       
          AdvancedArithmetic myCalculator = new Calculator();
        int sum = myCalculator.divisorSum(n);
        System.out.println("I implemented: " + myCalculator.getClass().getInterfaces()[0].getName() );
        System.out.println(sum);
    }
}


Share:

Hacker Rank - Day 17 : More Exceptions

                   Day 17 : More Exceptions

Solution In Java 8:


import java.util.*;
import java.io.*;

class Calculator{
    int power(int n , int p) throws Exception
    {
            if(n < 0 || p < 0)
            {
                    throw new Exception("n and p should be non-negative");
            }
        return (int)Math.pow(n,p);
    }
}

class Solution{

    public static void main(String []argh)
    {
        Scanner in = new Scanner(System.in);
        int T=in.nextInt();
        while(T-->0)
        {
            int n = in.nextInt();
            int p = in.nextInt();
            Calculator myCalculator = new Calculator();
            try
            {
                int ans=myCalculator.power(n,p);
                System.out.println(ans);
               
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
        }

    }
}
 


Solution In Python 3: 
class Calculator(Exception):
    def power(self,n,p):
        self.n = n
        self.p = p
        if self.n < 0 or self.p < 0:
            raise Exception("n and p should be non-negative")

        return self.n**self.p
myCalculator=Calculator()
T=int(input())
for i in range(T):
    n,p = map(int, input().split())
    try:
        ans=myCalculator.power(n,p)
        print(ans)
    except Exception as e:
        print(e)  
Share:

Hacker Rank - Day 16 : Exceptions

                       Day 16 : Exceptions

Solution In Java 8:


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String S = in.next();
      
        try{
            Integer i = Integer.parseInt(S);
            System.out.println(i);
          
        }catch(NumberFormatException nfe){
            System.out.println("Bad String");
        }
    }
}

Solution In Python 3: 

#!/bin/python3

import sys

try:
    print(int(input()))
except ValueError:
    print("Bad String")
Share:

Hacker Rank - Day 15 : Linked List

                       Day 15 : Linked List

Solution In Java 8:


import java.io.*;
import java.util.*;

class Node {
    int data;
    Node next;
    Node(int d) {
        data = d;
        next = null;
    }
}

class Solution {

public static  Node insert(Node head,int data) {
        if(head == null){
            head = new Node(data);
        }
        else{
            Node curr = head;
            while(curr.next!=null){
                curr = curr.next ;
            }
            curr.next = new Node(data);
        }
        return head;
    }
public static void display(Node head) {
        Node start = head;
        while(start != null) {
            System.out.print(start.data + " ");
            start = start.next;
        }
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        Node head = null;
        int N = sc.nextInt();

        while(N-- > 0) {
            int ele = sc.nextInt();
            head = insert(head,ele);
        }
        display(head);
        sc.close();
    }
}


Solution In Python 3: 



class Node:
    def __init__(self,data):
        self.data = data
        self.next = None
class Solution:
    def display(self,head):
        current = head
        while current:
            print(current.data,end=' ')
            current = current.next

    def insert(self,head,data):
        if(head == None):
            head = Node(data)
        else:
            curr = head
       
            while curr.next:
                curr = curr.next
            curr.next = Node(data)
        return head    
 
  mylist= Solution()
T=int(input())
head=None
for i in range(T):
    data=int(input())
    head=mylist.insert(head,data)   
mylist.display(head);
     
Share:

Hacker Rank - Day 12 : Inheritance

                    Hacker Rank - Day 14 : Scope

Solution In Java 8:

import java.util.*;

class Person {
    protected String firstName;
    protected String lastName;
    protected int idNumber;
   
    // Constructor
    Person(String firstName, String lastName, int identification){
        this.firstName = firstName;
        this.lastName = lastName;
        this.idNumber = identification;
    }
   
    // Print person data
    public void printPerson(){
         System.out.println(
                "Name: " + lastName + ", " + firstName
            +     "\nID: " + idNumber);
    }
   
}

class Student extends Person{
    private int[] testScores;
    Student(String firstName , String lastName , int id , int [] testScores){
        super(firstName , lastName , id);
        this.testScores = testScores;
    }
    public String calculate(){
        int sum = 0;
        for(int i = 0; i < testScores.length ; i++){
            sum = sum + testScores[i];
        }
        int avg = sum / testScores.length;
       
        if(avg >= 90 && avg <= 100)
            return "O";
        else if(avg >= 80 && avg < 90)
            return "A";
        else if(avg >= 70 && avg < 80)
            return "E";
        else if(avg >= 55 && avg < 70)
            return "P";
        else if(avg >= 40 && avg < 55)
            return "D";
        else
            return "T";
       
    }
  
}

class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String firstName = scan.next();
        String lastName = scan.next();
        int id = scan.nextInt();
        int numScores = scan.nextInt();
        int[] testScores = new int[numScores];
        for(int i = 0; i < numScores; i++){
            testScores[i] = scan.nextInt();
        }
        scan.close();
       
        Student s = new Student(firstName, lastName, id, testScores);
        s.printPerson();
        System.out.println("Grade: " + s.calculate() );
    }
}
 
Solution In Python 3: 

class Person:
    def __init__(self, firstName, lastName, idNumber):
        self.firstName = firstName
        self.lastName = lastName
        self.idNumber = idNumber
    def printPerson(self):
        print("Name:", self.lastName + ",", self.firstName)
        print("ID:", self.idNumber)
        

class Student(Person):
    def __init__(self,firstName,lastName,ids,testScores):
        super().__init__(firstName,lastName,ids)
        self.testScores = testScores
       
    def calculate(self):
        total = 0
        for testScore in self.testScores:
            total += testScore
        avg = total / len(self.testScores)
        if 90 <= avg <= 100:
            return 'O'
        if 80 <= avg < 90:
            return 'E'
        if 70 <= avg < 80:
            return 'A'
        if 55 <= avg < 70:
            return 'P'
        if 40 <= avg < 55:
            return 'D'
        return 'T'
        

line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input()) # not needed for Python
scores = list( map(int, input().split()) )
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())
Share: