Skip to content

Instantly share code, notes, and snippets.

View damdafayton's full-sized avatar
💭
I may be slow to respond.

damdafayton

💭
I may be slow to respond.
View GitHub Profile
@damdafayton
damdafayton / SumOddOrEven.js
Last active September 2, 2022 06:42
Akbank Web3 Practicum
function sumOddOrEven(num){
return num.toString().split('').reduce((prev,cur)=> parseInt(prev, 10) + parseInt(cur, 10)) % 2 === 0 ? 'Even' : 'Odd'
}
@damdafayton
damdafayton / solution.js
Last active August 9, 2022 18:49
Testovac
const fs = require("fs");
const FILES = ["example", "example_big", "test"];
const FILE_NAME = FILES[2];
const source = require(`./${FILE_NAME}.in.json`);
let allResults = [];
@damdafayton
damdafayton / jest-doesnt-terminate.js
Last active July 7, 2022 12:28
Jest doesnt terminate after successfull test if mongodb store is used in express sessions
const MongoStore = require('connect-mongo');
const express = require('express');
const session = require('express-session');
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoose = require('mongoose');
const request = require('supertest');
const app = express();
// This will create an new instance of "MongoMemoryServer" and automatically start it
@damdafayton
damdafayton / fullMergeSort.rb
Created July 1, 2022 11:01
Full Merge Sort Algorithm
def merge_sort(array1, array2)
# write your code here
sortedArray = []
while array1.length>0 && array2.length>0
if (array1[0].split(' ')[0] > array2[0].split(' ')[0])
sortedArray.push(array2[0])
array2.shift()
else
sortedArray.push(array1[0])
array1.shift()
@damdafayton
damdafayton / mergeSort.rb
Created July 1, 2022 11:01
Merge Sort Algorithm
def merge_sort(array1, array2)
# write your code here
sortedArray = []
while array1.length>0 && array2.length>0
p array1, array2
if (array1[0] > array2[0])
sortedArray.push(array2[0])
array2.shift()
else
sortedArray.push(array1[0])
@damdafayton
damdafayton / advanced_quicksort.rb
Last active August 9, 2022 09:27
Advanced Quick Sort - Ruby
# Instead of copying the array into multiple sub-arrays, use indices to keep track of the different sub-arrays.
# You can pass the indices to a modified partition method.
# The partition method should partition the sub-array and then return the index location where the pivot gets placed, so you can then call partition on each side of the pivot.
# Since you cannot just create new sub-arrays for the elements, Partition will need to use another trick to keep track of which elements are greater and which are smaller than the pivot.
def partition(array, start_pointer, end_pointer)
# print [start_pointer, end_pointer], "\n"
# return array if array.length==1
pivot_pointer = start_pointer
@damdafayton
damdafayton / simple_quicksort.rb
Last active June 18, 2022 06:39
Simple quick sort - Ruby
# In Insertion sort, you simply went through each element in order and inserted it into a sorted sub-array. In this challenge, you cannot focus on one element at a time, but instead must deal with whole sub-arrays. Each time you call partition, you are sorting two parts of the array with respect to each other. Notice if you called partition on two elements, that sub-array would be fully sorted.
# Can you repeatedly call partition on each sub-array so that the entire array ends up sorted?
# In this challenge, print your array every time you finish your partitioning method, i.e. when you combine the partitioned array together. The first element in a sub-array should be used as a pivot. Partition the left side before partitioning the right side. Do not add the pivot to either side. Instead, put it in the middle when combining the two lists together.
def simple_quicksort(array)
# write your code here
# choose one > make two lists > loop the rest > if list bigger than 1 recurse > return all
pivot = array [
@damdafayton
damdafayton / bst-in-order-search.rb
Last active May 17, 2022 10:14
BST Successor Search
# In a Binary Search Tree (BST),
# an Inorder Successor of a node is defined as the node with the smallest key greater than the key of the input node.
# Given a node inputNode in a BST,
# you’re asked to write a function findInOrderSuccessor that returns the Inorder Successor of inputNode.
# If inputNode has no Inorder Successor, return null.
class Node
attr_reader :value
attr_accessor :left, :right
/*
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
https://www.hackerrank.com/challenges/mini-max-sum/problem?isFullScreen=true
*/
function miniMaxSum(arr) {
// Write your code here
let min=arr[0], max=arr[0];
arr.forEach((num,i)=>{
min = Math.min(arr[i],min)