Skip to content

Instantly share code, notes, and snippets.

View victor-iyi's full-sized avatar
🎯
Focusing

Victor I. Afolabi victor-iyi

🎯
Focusing
View GitHub Profile
@victor-iyi
victor-iyi / anagram.py
Last active March 12, 2021 06:22
Check if two words are anagram
"""Classic interview question to check if two words are anagram."""
import collections
import itertools
import pytest
def is_anagram(first: str, second: str) -> bool:
"""Implementation of anagram checker with 3 different methods.
Note:
@victor-iyi
victor-iyi / .vimrc
Last active May 19, 2021 11:09
My Vim configuration file
" File: ~/.vimrc
"
" Author: Victor I. Afolabi
syntax enable
colorscheme desert
" highlight Normal guibg=none
" =============================================================================
@victor-iyi
victor-iyi / fibonacci.rs
Created December 13, 2020 01:37
Implementing the fibonacci series using Rust's Iterator
// Author: Victor I. Afolabi
// Implementing the Fibonacci series using Rust's Iterator
#[derive(Debug, PartialEq, PartialOrd)]
pub struct Fib {
current: usize,
next: usize,
}
impl Fib {
@victor-iyi
victor-iyi / flatten.rs
Created December 13, 2020 01:34
Flatten a nested Iterator in Rust
// Author: Victor I. Afolabi
//
// Exploring Iterator Traits in Rust to implement Flatten functionality of 2D-iterators.
//
pub trait IteratorExt: Iterator {
fn flatten_ext(self) -> Flatten<Self>
where
Self: Sized + std::fmt::Debug,
Self::Item: IntoIterator,
@victor-iyi
victor-iyi / tokenizer.py
Last active November 13, 2020 04:08
Get word tokens using the tokenizers module from HuggingFace
"""
# Tokenizers
Provides an implementation of today's most used tokenizers, with a focus on performance and versatility.
## Main features:
- Train new vocabularies and tokenize, using today's most used tokenizers.
- Extremely fast (both training and tokenization), thanks to the Rust implementation. Takes less than 20 seconds to tokenize a GB of text on a server's CPU.
- Easy to use, but also extremely versatile.
@victor-iyi
victor-iyi / gcd.rs
Created November 8, 2020 21:42
Greatest common divisor in Rust
/// Computes the greatest common divisor of two integers using Euclid's algorithm
/// (https://en.wikipedia.org/wiki/Euclidean_algorithm).
///
/// # Example
///
/// ```rust
/// assert_eq!(gcd(3, 5), 1);
///
/// assert_eq!(gcd(2 * 3 * 5 * 11 * 17, 3 * 7 * 11 * 13 * 19), 3 * 11);
/// ```
@victor-iyi
victor-iyi / defaultdict_demo.py
Last active November 13, 2020 04:10
Demonstrating Python's defaultdict
"""
# Using Python's `collections.defaultdict`.
```python
>>> import collections
>>> dct = collections.defaultdict(int)
>>> int()
0
>>> dct['a']
0
@victor-iyi
victor-iyi / spinner.py
Created October 2, 2020 06:50
A spinner function as a visual aid for processing long computation
import sys
import time
import multiprocessing as mp
DELAY = 0.1
DISPLAY = ['|', '/', '-', '\\']
def spinner_fn(before='', after=''):
write, flush = sys.stdout.write, sys.stdout.flush
pos = -1
@victor-iyi
victor-iyi / pow.py
Created August 29, 2020 13:32
Implement the Python's `pow(...)` function as efficient as possible
import time
def power(base, exp):
"""Implement the `pow(...)` function recursively."""
# Base case.
if exp == 0:
return 1
elif exp == 1:
return base
# Compute: a^b = a^(c+d) = (a^c) * (a^d)
@victor-iyi
victor-iyi / attention-heatmap.py
Created April 5, 2020 06:56
Convert the text or attention list to LaTeX code to generate text heatmap based on attention weights.
"""Convert the text/attention list to latex code,
which will further generates the text heatmap based on attention weights.
"""
import numpy as np
latex_special_token = ["!@#$%^&*()"]
def generate(text_list, attention_list, latex_file, color='red', rescale_value = False):
assert(len(text_list) == len(attention_list))