Skip to content

Instantly share code, notes, and snippets.

View Per48edjes's full-sized avatar
👉
This is a good point.

Ravi Dayabhai Per48edjes

👉
This is a good point.
View GitHub Profile
@Per48edjes
Per48edjes / bubble_sort.c
Last active May 27, 2022 03:42
Implementation of bubble sort on array of integers in C
#include <stdio.h>
void swap(int *a, int *b);
void print_array(int *arr, int len_arr);
int main(void)
{
int items[] = { 5, 2, 7, 4, 1, 6, 3, 0 };
@Per48edjes
Per48edjes / binary_search.c
Last active May 28, 2022 02:53
Implementation of binary search on sorted array of integers in C
#include <stdio.h>
#include <stdlib.h>
int binary_search(const int *arr, const int arr_len, const int n);
int main(int argc, char *argv[])
{
if (argc != 2)
{
@Per48edjes
Per48edjes / merge_sort.c
Last active May 28, 2022 04:44
Implementation of merge sort on array of integers in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int * merge_sort(int *parr, int arr_len);
void print_array(int *arr, int len_arr);
int main(void)
{
@Per48edjes
Per48edjes / insertion_sort.c
Created June 18, 2022 00:36
Implementation of insertion sort on array of integers in C
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b);
void insert(int *subseq, int subseq_len);
void print_array(int *arr, int len_arr);
int main(void)
{
int items[] = {
@Per48edjes
Per48edjes / integer_partitions.py
Last active August 21, 2022 13:40
Partitioning integer `n` using parts up to size `m`
#!/usr/bin/env python3
"""
Problem statement:
- https://composingprograms.com/pages/17-recursive-functions.html#example-partitions
- https://youtu.be/ngCos392W4w?t=678
"""
from functools import lru_cache
@Per48edjes
Per48edjes / longest_increasing_subsequence.py
Created July 4, 2022 11:55
LeetCode 300. Longest Increasing Subsequence
"""
Given an integer array nums, return the length of the longest strictly
increasing subsequence.
A subsequence is a sequence that can be derived from an array by deleting some
or no elements without changing the order of the remaining elements. For
example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
"""
from typing import List
@Per48edjes
Per48edjes / climbing_stairs.py
Last active April 29, 2023 03:25
LeetCode 70. Climbing Stairs
"""
You are climbing a staircase. It takes `n` steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
"""
from functools import lru_cache
@lru_cache
@Per48edjes
Per48edjes / euclid_algorithm.py
Last active October 23, 2022 02:39
Recursive implementation of Euclid's algorithm to compute GCD of two integers
from typing import Tuple
def gcd(a: int, b: int) -> int:
"""
Simple recursive implementation of Euclidean algorithm
"""
a, b = abs(a), abs(b)
if b == 0:
return a
@Per48edjes
Per48edjes / merge_ordered_lists.py
Last active August 3, 2022 01:48
Merge arbitrary number of ordered lists via recursion + reduction
from functools import reduce
def merge(lst1, lst2):
"""Merges two sorted lists.
>>> s1 = [1, 3, 5]
>>> s2 = [2, 4, 6]
>>> merge(s1, s2)
[1, 2, 3, 4, 5, 6]
>>> s1
@Per48edjes
Per48edjes / digital_root.py
Last active August 23, 2022 13:59
Recursively calculate the digital root of a non-negative integer
def digital_root(n: int, original=None) -> int:
"""
Return the digital root of a non-negative integer `n`. Note that this is
basically the same as `n % 9` except in the special case when `n` is a
multiple of 9.
@param n: A non-negative integer
@type n: int
@return: The digital root of `n`