Last active
April 4, 2020 21:57
-
-
Save bjonnh/5a60e3be8ec3c1083d6eaef7ce20c522 to your computer and use it in GitHub Desktop.
Python fuzzy arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import math | |
class FuzzyArray: | |
def __init__(self, *content): | |
self.content = content | |
def __getitem__(self, index): | |
low_index = math.floor(index) | |
high_index = math.ceil(index) | |
if (high_index>=len(self.content)): | |
high_index = len(self.content) | |
p = index - low_index | |
print(p) | |
if (random.random()<p): | |
return self.content[high_index] | |
else: | |
return self.content[low_index] | |
test = FuzzyArray(1,20,30) | |
print("test = FuzzyArray(1,20,30)") | |
print(f"test[1] = {test[1]}") | |
print(f"test[1.25] = {test[1.25]}") | |
print(f"test[1.5] = {test[1.5]}") | |
print(f"test[1.75] = {test[1.75]}") | |
print(f"test[1.5] = {test[1.5]}") | |
print(f"test[-0.5] = {test[-0.5]}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment