Skip to content

Instantly share code, notes, and snippets.

@agnel
agnel / codesignal-arcade-intro-edge-of-the-ocean-shapeArea.md
Last active Jun 16, 2022
Codesignal - Aracde Intro - Edge of the Ocean - shapeArea
View codesignal-arcade-intro-edge-of-the-ocean-shapeArea.md

Codesignal

Aracde > Intro > Edge of the Ocean

Challenge: shapeArea

Imgur

@agnel
agnel / day-3-two-pointers-167-two_sum_ii_input_array_is_sorted.md
Created Jun 10, 2022
Algorithm I Day 3 Two Pointers 167. Two Sum II - Input Array is Sorted
View day-3-two-pointers-167-two_sum_ii_input_array_is_sorted.md

Logic: The approach to this question differs to that of the classic Two Sum problem in that we have some direction with how we want to search for our target.

Since the array is sorted, we can make some general observations:

Smaller sums would come from the left half of the array Larger sums would come from the right half of the array Therefore, using two pointers starting at the end points of the array, we can choose to increase or decrease our current sum however we like. Pay attention to the example below:

Algorithm I Day 3 Two Pointers 167 Two Sum II Input array is sorted - Visual Explanation

@agnel
agnel / SimpleStorage.sol
Created Jun 10, 2022
The SimpleStorage contract in Solidity from Lesson 1: Welcome to Remix! Simple Storage on FreeCodeCamp
View SimpleStorage.sol
// SPDX-License-Identifier: MIT
pragma solidity >= 0.6.0 <0.9.0;
contract SimpleStorage {
// this will get initialized as 0
uint256 favoriteNumber;
struct People {
uint256 favoriteNumber;
string name;
View algorithm-i-day-2-two-pointers-189-rotate-array.md

The official Leetcode page talks about four following approaches:

  1. Approach 1: Brute Force
  2. Approach 2: Using Extra Array
  3. Approach 3: Using Cyclic Replacements
  4. Approach 4: Using Reverse

The second approach can be done like this: (but only works in irb and not on leetcode somehow)

View algorithm-i-day-2-two-pointers-977-squares_of_a_sorted_array-explanation.md

The Optimized implementation of Bubble Sort from GeeksforGeeks failed the below test case:

Code:

# @param {Integer[]} nums
# @return {Integer[]}
def sorted_squares(nums)
  nums = nums.map { |n| n*n } # square the numbers
  
  # now lets sort with bubble sort
@agnel
agnel / prefix_set.rb
Last active Jun 4, 2022
Codility Challange: PrefixSet [Ruby solution]
View prefix_set.rb
=begin
Codility Challange: PrefixSet
https://app.codility.com/programmers/task/prefix_set/
Solution: https://codility.com/media/train/solution-prefix-set.pdf
=end
def prefixSetGolden(arr)
n = arr.size
occur = {}
@agnel
agnel / qiskit-chapter-2-exercise-3.4-question-2.ipynb
Last active May 17, 2022
Solution to Qiskit Textbook Chapter 2 Exercise 3.4 Question 2
View qiskit-chapter-2-exercise-3.4-question-2.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@agnel
agnel / qiskit-chapter-2-exercise-3.4-question-1.ipynb
Last active May 17, 2022
Solution to Qiskit Textbook Chapter 2 Exercise 3.4 Question 1
View qiskit-chapter-2-exercise-3.4-question-1.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@agnel
agnel / min_max.js
Created May 7, 2022
Find min and max values without Math functions in js
View min_max.js
/**
* These methods only work for an array of numbers
*/
function findMinimum(array) {
let length = array.length;
let min = Infinity;
while(length--) {
if(array[length] < min) {
@agnel
agnel / Counter.rb
Last active Mar 9, 2022
A base 36 incrementing Counter Model for Mongoid
View Counter.rb
module CounterIncrementor
def next(counter_name)
counter = self.find_by(name: counter_name)
counter.increment
end
end
class Counter
include Mongoid::Document
include Mongoid::Timestamps