Skip to content

Instantly share code, notes, and snippets.

View itanfeng's full-sized avatar

Feng Tan itanfeng

  • Sichuan University
  • Chengdu
View GitHub Profile
@ameerkat
ameerkat / BoyerMooreStringSearch.py
Created October 14, 2010 17:46
Boyer Moore string search implementation in Python
# Boyer Moore String Search implementation in Python
# Ameer Ayoub <ameer.ayoub@gmail.com>
# Generate the Bad Character Skip List
def generateBadCharShift(term):
skipList = {}
for i in range(0, len(term)-1):
skipList[term[i]] = len(term)-i-1
return skipList
@dbrgn
dbrgn / boyer-moore.py
Created August 18, 2011 12:55
Boyer-Moore-Algorithm in Python
class last_occurrence(object):
"""Last occurrence functor."""
def __init__(self, pattern, alphabet):
"""Generate a dictionary with the last occurrence of each alphabet
letter inside the pattern.
Note: This function uses str.rfind, which already is a pattern
matching algorithm. There are more 'basic' ways to generate this
dictionary."""
@brlauuu
brlauuu / train_network.m
Created January 25, 2018 13:48
Training CNN using Matlab R2017b NN and CV toolbox
% Load training data.
imageDir = fullfile('training_data');
labelDir = fullfile('label_data');
% Create an image datastore for the images.
imds = imageDatastore(imageDir, 'IncludeSubfolders',true, ...
'LabelSource','foldernames');
% Create a pixelLabelDatastore for the ground truth pixel labels.
@the-bass
the-bass / confusion_matrix_between_two_pytorch_tensors.py
Last active November 24, 2023 09:56
Calculating the confusion matrix between two PyTorch tensors (a batch of predictions) - Last tested with PyTorch 0.4.1
import torch
def confusion(prediction, truth):
""" Returns the confusion matrix for the values in the `prediction` and `truth`
tensors, i.e. the amount of positions where the values of `prediction`
and `truth` are
- 1 and 1 (True Positive)
- 1 and 0 (False Positive)
- 0 and 0 (True Negative)
@pakosaldanaort
pakosaldanaort / Boyer-Moore-Horspool.py
Created November 5, 2018 18:36
Boyer-Moore-Horspool Python implementation
class BoyerMoore:
def __init__(self, text, pattern):
self.text = text
self.pattern = pattern
self.m = len(pattern)
self.n = len(text)
self.skip = []
for i in range(256): self.skip.append(-1)
for i in range(self.m): self.skip[ord(pattern[i])] = self.m
@halolimat
halolimat / pso.py
Created June 10, 2019 01:45
A module that solves a minimization problem using Particle Swarm Optimization (PSO)
#!/usr/bin/env python
################################################################################
# File name: pso.py #
# Description: A module that solves a minimization problem using PSO #
#------------------------------------------------------------------------------#
# Author: Hussein S. Al-Olimat #
# Email: hussein@knoesis.org #
#------------------------------------------------------------------------------#
# Date-last-modified: Nov 7, 2016 #
def turbo_bmh(string, pattern):
"""
Bad character heuristic, good suffix heuristic, turbo-shift heuristic implemented on Python
"""
def _suffices_preprocessing(suffix):
suffix[m - 1] = m
g = m - 1
for i in range(m - 2, -1, -1):
if i > g and suffix[i + m - f - 1] < i - g: