Linear Search using Python

”’
Created on Jun 18, 2018
Linear Search in Python
@author: asharda
”’

def linear(arr,num):
for i in arr:
if(i==num):
return True
return False

arr=[10,7,12,5]
print(linear(arr, 8))

Advertisements
Posted in Data Structures, Python, Uncategorized | Leave a comment

Recursion in Python

”’
Created on Jun 15, 2018

@author: asharda
”’

def factorial(num):
if (num==1):
return 1;
else:
return factorial(num-1)*num;

print(factorial(5))

Posted in Uncategorized | Leave a comment

Convert List to an Array and Array to List Java

List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());

where nums is an int []

 

list.stream().mapToInt(i->i).toArray();

which returns int[]

Posted in Uncategorized | Leave a comment

Divide Two Integers

https://leetcode.com/problems/divide-two-integers/description/

package src.test.java.examples;package src.test.java.examples;
public class Dividend {
public int divide(int dividend, int divisor) { int count = 0; boolean flag = false; if (dividend < 0 && divisor > 0) { flag = true; }
else if (dividend > 0 && divisor < 0) { flag = true; }
else if (dividend < 0 && divisor < 0) { flag = false; } if (dividend == -2147483648) { dividend = -2147483647; } dividend = Math.abs(dividend); divisor = Math.abs(divisor); while (dividend >= divisor) { dividend = dividend – divisor; count++; }
if (flag) { count = count * -1; }
return count; }
public static void main(String[] args) {
Dividend d = new Dividend(); System.out.println(d.divide(-2147483648, -1)); }
}

Posted in Java, LeetCode Solutions, Uncategorized | Tagged | Leave a comment

Using Predicates in Java 8

https://howtodoinjava.com/java-8/how-to-use-predicate-in-java-8/

Posted in Uncategorized | Leave a comment

Using Lambda Expressions

 

package src.test.java.examples;
/** * @author asharda * */
interface MyNumbers{ boolean test(int a,int b);}public class Lambda {
/** * @param args */ public static void main(String[] args) {
MyNumbers num; num=(a,b) -> { if(a%b ==0) { return true; } else { return false; } }; System.out.println(num.test(10, 2)); }
}

Posted in Uncategorized | Leave a comment

Space Complexity

Posted in Uncategorized | Leave a comment