Using Dictionary Python

”’
Created on Aug 11, 2017

@author: asharda
”’

legs={“human”:2,”cats”:4}
for leg in legs:
num=legs[leg]
print(‘A %s has %d legs’ %(leg,num))

Posted in Python | Leave a comment

Using Stream API Java

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.testng.annotations.Test;

public class FilterTest {

@Test
public void testNotNull()
{
Stream<String> language=Stream.of(“java”,”python”,null);
List<String> list=language.filter(x->x!=null).collect(Collectors.toList());
list.forEach(System.out::println);
}

@Test
public void testNonZero()
{
Stream<Integer> numbers=Stream.of(1,2,3,0);
List<Integer> list=numbers.filter(x->x!=0).collect(Collectors.toList());
list.forEach(System.out::println);
}
}

Posted in Java, Uncategorized | Leave a comment

Using Enums

public enum Calculate {

PLUS,
MINUS,
DIVIDE,
MULTIPLY;

public long operation(long x,long y) throws Exception
{
switch(this)
{
case PLUS:
return x+y;
case MINUS:
return x-y;
case MULTIPLY:
return x*y;
case DIVIDE:
return x/y;
default:
throw new Exception(“No Matching Enum”);

}
}
}

 

import org.testng.annotations.Test;

public class TestCalclulator {
@Test
public void testCalculator() throws Exception
{
System.out.println(“Add 2 numbers” +Calculate.PLUS.operation(10, 20));
System.out.println(“Substract 2 numbers” +Calculate.MINUS.operation(30, 20));
System.out.println(“Multiply 2 numbers” +Calculate.MULTIPLY.operation(10, 20));
System.out.println(“Divide 2 numbers” +Calculate.DIVIDE.operation(100, 20));
}
}

 

Posted in Java, Uncategorized | Leave a comment

For Each Java8

import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Test;
public class TestLambda {
@Test
public void testForEach()
{
List<String> list1=new ArrayList<String>();
list1.add(“Sai”);
list1.add(“Sainath”);
list1.add(“Sairam”);
list1.forEach(item->System.out.println(item));

}
}

Posted in Java, Uncategorized | Leave a comment

Probability Python

from random import choice
destinations=[“Agra”,”Mumbai”,”New York”,”San Francisco”]
print(choice(destinations))

Posted in Python, Uncategorized | Leave a comment

Roll a Dice 10 times using Numpy

import numpy as np
outcome=np.random.randint(1,6,size=10)
print(outcome)

Posted in Uncategorized | Leave a comment

Basic Operations using Numpy

import numpy as np
a=np.array([1,2,3])
print(a.shape)
print(a.dtype)
b=np.zeros((2,2))
print(b)
c=np.ones((1,1))
print(c)
d=np.eye(2)
print(d)
e=np.random.random((3,3,3))
print(e)

Posted in Python, Uncategorized | Leave a comment