Skip to content

Instantly share code, notes, and snippets.

View Shaddyjr's full-sized avatar
📉
You must construct additional models

Mahdi Shadkam-Farrokhi Shaddyjr

📉
You must construct additional models
View GitHub Profile
# source: https://www.hackerrank.com/challenges/find-maximum-index-product/problem?isFullScreen=false
# video: https://youtu.be/z8iSOFUbQUM
# Stack convenience wrapper for list
class Stack():
def __init__(self):
self._stack = [] # store internal list
def is_empty(self): # true when stack is empty
return len(self._stack) == 0
# video: https://youtu.be/I7RFycpqbDk
from math import ceil, log2
class SegmentationTree():
def __init__(self, input_list):
self._input_list = input_list[:] # copy of input_list
self._init_tree()
self._is_propogated = True
def _init_tree(self):
# source: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/distinct-integers-in-range-66eca44b/description/
import re
### SETUP ###
max_int = 5000
MAX_VALUE = (1 << max_int) - 1
re_splitter = re.compile(" ")
def agg_func(val_1, val_2):
output = val_1 | val_2
# source: https://www.hackerrank.com/challenges/bear-and-steady-gene
# video: https://youtu.be/eIW7vBWzibE
def extras_available(all_counts, max_nucleotide_count):
'''Returns True if any nucleotide count is greater than max_nucleotide_count'''
# loop through all counts
for counts in all_counts.values():
# if any count is greater than max_nucleotide_count, return True
if counts > max_nucleotide_count:
return True
# if got past loop, no extras found => return False
# source: https://www.hackerrank.com/challenges/lilys-homework
# video: https://youtu.be/HmyDuVZoE8Y
def lilysHomework_helper(arr, reverse = False):
# clone arr, since modifying
arr = arr[:] # O(n)?
# arr = [2, 5, 3, 1]
# store sorted array
sorted_arr = sorted(arr, reverse = reverse) # O(nlogn)
# sorted_arr = [1, 2, 3, 5]
# OR [5, 3, 2, 1] if reverse = True
# source: https://www.hackerrank.com/challenges/gridland-metro
# video: https://youtu.be/H1Qbd652Oig
def gridlandMetro(n, m, k, tracks): # ALTERED NAME TO PLURAL
# tracks = [
# [row, col_start, col_end],
# ...
# ]
# sort by col start
tracks.sort(key = lambda track: min(track[1], track[2])) # O(nlogn)
# source: https://www.hackerrank.com/challenges/kindergarten-adventures
# video: https://youtu.be/9vcrruySa9s
def sanitize_index(index, n):
# index already valid
if 0 <= index and index < n:
return index
# negative index
if index < 0:
return n + index
// source: https://www.youtube.com/redirect?q=https%3A%2F%2Fwww.hackerrank.com%2Fchallenges%2Fwaiter%2Fproblem%3Fh_r%3Dinternal-search&event=video_description&redir_token=QUFFLUhqbWRvZWVfbjJJUVQ0Q2J0WnpTLTVVUnBPYnFYd3xBQ3Jtc0trUXJ5MGF5UVJ5UlgxakpSQnZsXzB0QXY2bk5INzJ4MmhkRk5YLWUzV1RjLXQzX2xqUFl3LUtUVnhlT29QV0VoY3FhOVVqMDZrZ21JQjRXVEdvbUdHR3BZajllbmNuVGJBMTFtcXY0VWxaMGxyUmt1NA%3D%3D&v=UW-1KB9PFrA
// video: https://www.youtube.com/watch?v=UW-1KB9PFrA&lc=UgyNhlGoJNiMul7Byad4AaABAg
function getPrimes(n){
const primesStr = "2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,7
// https://www.hackerrank.com/challenges/angry-professo
function angryProfessor(k, a) {
// establishing constants
const YES = "YES";
const NO = "NO";
let inClassCount = 0; // keeping running total
for(const arrivalTime of a){ // looping through each arrivalTime; O(n)
inClassCount += arrivalTime <= 0; // get boolean (true/false) if student is late or on time, adding (1/0) to total
if(inClassCount >= k) return NO; // before loop is done, if the total count is past threshold, stop and return NO
# source: https://leetcode.com/problems/combinations/
class Solution(object):
def combine(self, n, k):
output = []
stack = []
i = 1
length = 0
while True:
# if stack if k long, add to solution (as copy)