Skip to content

Instantly share code, notes, and snippets.

View saisgit's full-sized avatar

SaiKumar saisgit

View GitHub Profile
/* Content of CSV File
Id;ProductName;SupplierId;UnitPrice;Package;IsDiscontinued
1;Chai;1;18;10 boxes x 20 bags;False
2;Chang;1;19;24 - 12 oz bottles;False
3;Aniseed Syrup;1;10;12 - 550 ml bottles;False
4;Chef Anton's Cajun Seasoning;2;22;48 - 6 oz jars;False
5;Chef Anton's Gumbo Mix;2;21.35;36 boxes;True
*/
/*
Assume below is our sample data and we want to create DataFrame for it.
id,name,age
1,Ram,12
2,Sam,11
3,Tom,10
*/
@saisgit
saisgit / snowflake_from_emr.py
Created May 23, 2021 13:26
Program to Create Spark dataframe on Snowflake Table from AWS EMR
# Program to Create spark dataframe on Snowflake Table from AWS EMR.
# Launch pyspark with SnowFlake jdbc connectors
# pyspark --packages net.snowflake:snowflake-jdbc:3.11.1,net.snowflake:spark-snowflake_2.11:2.5.7-spark_2.4
import boto3
import json
# Create Spark Session
spark = SparkSession.builder.appName("Connect-Snowflake").enableHiveSupport().getOrCreate()
# Function takes secretkey value that defined in AWS secrets manager and returns snowflake credentials as json
def binarySearch_tailRec(arr: Array[Int], target: Int): Int = {
@tailrec
def bs_helper(arr: Array[Int], target: Int, start: Int, end: Int): Int = {
if (start > end) return -1
val mid = start + (end - start) / 2
arr(mid) match {
case i if (i == target) => mid
case i if (i > target) => bs_helper(arr, target, start, mid - 1)
case _ => bs_helper(arr, target, mid + 1, end)
}
def binarySearch_Recursive(list: Array[Int], target: Int)
(start: Int = 0, end: Int = list.length - 1): Int = {
if (start > end) -1
val mid = start + (end - start + 1) / 2
if (list(mid) == target)
mid
else if (list(mid) > target)
binarySearch_Recursive(list, target)(start, mid - 1)
else
binarySearch_Recursive(list, target)(mid + 1, end)
def binarySearch_iterative(arr: Array[Int], target: Int): Int = {
var (start, end) = (0, arr.length - 1)
var mid = start + (end - start) / 2
while (start <= end) {
mid = start + (end - start) / 2
if (arr(mid) == target)
return mid
if (arr(mid) > target) end = mid - 1
else
def twoSum(nums, target):
index_map = {}
for i in range(len(nums)):
num = nums[i]
pair = target - num
if pair in index_map:
return [index_map[pair], i]
index_map[num] = i
return None
#Essential Libraries
import os
import re
import sys
import ConfigParser
#Create File Method. Take 2 parameters:1)Filename 2)Data
def createFile(fName,data):
file = open(fName,'w+')
file.write(data)
'''
Given an array, find the average of all contiguous subarrays of size ‘n’ in it.
Array: [1, 3, 2, 6, -1, 4, 1, 8, 2], n=5
Output: [2.2, 2.8, 2.4, 3.6, 2.8]
'''
def findAveragesOfSubArrays(arr, n):
result = []
sum = 0.0
windowStart = 0 # initialise sum and starting point of window
for windowEnd in range(len(arr)):
"""
Given an array of sorted numbers and a target sum, find a pair in the array whose sum is equal to the given target.
Write a function to return the indices of the two numbers (i.e. the pair) such that they add up to the given target.
Example 1:
Input: [1, 2, 3, 4, 6], target=6
Output: [1, 3]
Explanation: The numbers at index 1 and 3 add up to 6: 2+4=6
Algorithm Used : Two Pointer
"""
def pairWithTargetSum(arr, target_sum):