P

@praveen11

merge two sorted arrays without extra space

C++
3 years ago
#include <iostream> #include <vector> using namespace std; int main() { int n=4,m=4; vector<int> a = {1,3,4,5}; vector<int> b = {2,4,6,8}; for(int i=0;i<a.size();i++){ cout<<a[i]<<" "; }

integer indexes to string

C++
3 years ago
#include <iostream> #include <string> using namespace std; int main() { string s,ans; cin>>s; for(int i=0;i<s.length();i++){ if(s[i]>='0' and s[i]<='9'){ ans += to_string(i); }

max index product

Python
3 years ago
def maxindex(a): n=len(a) if(n == 0): return None else: maxi = 0 for i in range(0,n): l = 0 r = 0 for j in range(i-1, 0, -1):

movehypens

Python
3 years ago
def MoveHyphen(str): cnt=0 res='' for i in str: if(i == '-'): cnt+=1 else: res+=i res = '-'*cnt + res return res

get current time and date in js

NodeJS
3 years ago
var d = new Date(); // console.log("utc time : ",d); // we get UTC time var ISToffset = 330; //for IST the offset is +5:30 => 60*5+30 = 330 offset = ISToffset*60000; var ISTTime = new Date(d.getTime()+offset) // console.log("ist time : ",ISTTime); // UTC time is converted to IST var dd = ISTTime.getDate(); var mm = ISTTime.getMonth(); var yy = ISTTime.getFullYear(); var hh = ISTTime.getHours();

js questions

NodeJS
3 years ago
console.log(typeof typeof typeof true) /* the output will be string Here the log statement looks like typeof(typeof(typeof(true))). lets see the step-by-step evaluation of the statement: typeof(true) returns 'boolean' which is string. then the statement becomes typeof(typeof('boolean')). typeof('boolean') returns 'string' because 'boolean' itself is a string. then the statement becomes typeof('string'). finally, typeof('string') returns 'string'.

js questions

NodeJS
3 years ago
const circle = { radius: 20, diameter() { return this.radius * 2; }, perimeter: () => 2 * Math.PI * this.radius, }; console.log(circle.diameter()); console.log(circle.perimeter());

Pangrams or not

Python
3 years ago
s=input() ss=set(s.lower()) print(len(ss)) print(ss) count=0 for i in ss: if(i>='a' and i<='z'): count+=1 print(count)

Huffman coding

Python
4 years ago
class node: def __init__(self, freq, symbol, left=None, right=None): self.freq = freq self.symbol = symbol self.left = left self.right = right self.huff = '' def printNodes(node, val=''): newVal = val + str(node.huff)

dijkstra's algorithm

Python
4 years ago
class Graph(): def __init__(self, vertices): self.V = vertices self.graph = [[0 for column in range(vertices)] for row in range(vertices)] def printSolution(self, dist): print("Vertex \t Distance from Source")

0/1 knapsack in python

Python
4 years ago
def knapSack(W, wt, val, n): if n == 0 or W == 0: return 0 if (wt[n-1] > W): return knapSack(W, wt, val, n-1) else: return max(val[n-1] + knapSack(W-wt[n-1], wt, val, n-1),knapSack(W, wt, val, n-1)) val = [int(i) for i in input().split()] wt = [int(i) for i in input().split()]

knapsack problem in python

Python
4 years ago
class Solution: def solve(self, weights, values, capacity): res = 0 for pair in sorted(zip(weights, values), key=lambda x: - x[1]/x[0]): if not bool(capacity): break if pair[0] > capacity: res += int(pair[1] / (pair[0] / capacity)) capacity = 0 elif pair[0] <= capacity:

randomised quicksort in python

Python
4 years ago
import random def quicksort(arr, start , stop): if(start < stop): pivotindex = partitionrand(arr, start, stop) quicksort(arr , start , pivotindex-1) quicksort(arr, pivotindex + 1, stop) def partitionrand(arr , start, stop): randpivot = random.randrange(start, stop) arr[start], arr[randpivot] = arr[randpivot], arr[start]

quick sort in python

Python
4 years ago
def partition(arr, low, high): i = (low-1) pivot = arr[high] for j in range(low, high): if arr[j] <= pivot: i = i+1 arr[i], arr[j] = arr[j], arr[i] arr[i+1], arr[high] = arr[high], arr[i+1] return (i+1)

binary search in python

Python
4 years ago
def binary_search(a,low,high,key): if(low <= high): mid = (low + high)//2 if(a[mid] == key): return mid elif(a[mid] > key): return binary_search(a,low,mid-1,key) else: return binary_search(a,mid+1,high,key) else:

merge sort in python

Python
4 years ago
def mergeSort(arr): if len(arr) > 1: mid = len(arr)//2 L = arr[:mid] R = arr[mid:] mergeSort(L) mergeSort(R) i = j = k = 0 while i < len(L) and j < len(R): if L[i] < R[j]:

destructing the object

NodeJS
4 years ago
var person = { name : "Praveen", rollno : "521", mail : "praveen8642kumar@gmail.com" } const {name,rollno,mail} = person; console.log(name); console.log(mail);

rest operator, spread operator

NodeJS
4 years ago
function sample(name,...rest){ //...variable --> indicates rest operator console.log(name); console.log(rest); } sample('Praveen',521,'praveen8642kumar@gmail.com','8688574614'); var a = [1,2,3]; var b = [6,7,8]; var c = [...a,...b]; //[...var1,...var2,............] indicates spread operator console.log(c)

arrow function in js

NodeJS
4 years ago
const func = (a) => console.log("a =",a) //arrow function func(10);

indexOf(), lastIndexOf() in js

NodeJS
4 years ago
var a=[1,2,3,4,5,6] console.log(a.indexOf(3)) var b=[1,2,3,4,5,6,2,3,4,3,3] console.log(b.lastIndexOf(3))