A community driven mojo lib
import random

def prepare(run):
    list = [random.randint(0, 1000) for i in range(run)]
    return list

def bsort(list, run):
    for i in range(0, len(list)):
        print((i/run)*100)
        for j in range(0, len(list) - i - 1):
            if list[j] > list[j + 1]:
                list[j], list[j + 1] = list[j + 1], list[j]
    return list

def main():
    run = 10000
    list = prepare(run)
    bsort(list, run)

if __name__ == '__main__':
    main()
Algorithms/Bubblesort/buble_python.py
Lynet101/Mojo_community-lib
from random import randint as ri

def prepare():
    arr = [ri(0, 100) for _ in range(1000)]
    return arr


def quicksort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left) + middle + quicksort(right)


def main():
    arr = prepare()
    arr = quicksort(arr)
    print(arr)

main()
Algorithms/Quicksort/Python/sort.py
Lynet101/Mojo_community-lib
class DynamicArray:
    def __init__(self): #implemented
        self.size = 0
        self.capacity = 1
        self.array = self.make_array(self.capacity)

    def __len__(self): #implemented
        return self.size

    def append(self, item): #implemented
        if self.size == self.capacity:
            self._resize(2 * self.capacity)
        self.array[self.size] = item
        self.size += 1

    def _resize(self, new_capacity): #implemented
        new_array = self.make_array(new_capacity)
        for i in range(self.size):
            new_array[i] = self.array[i]
        self.array = new_array
        self.capacity = new_capacity

    def make_array(self, new_capacity): #implemented
        return [None] * new_capacity

    def __getitem__(self, index): #implemented
        return self.array[index]

    def clear(self): #implemented
        self.size = 0
        self.capacity = 1
        self.array = self.make_array(self.capacity)
 
    def count(self, item): #later
        count = 0
        for i in range(self.size):
            if self.array[i] == item:
                count += 1
        return count

    def extend(self, other): #later
        for i in range(other.size):
            self.append(other.array[i])

    def index(self, item): #later
        for i in range(self.size):
            if self.array[i] == item:
                return i
        return -1

    def remove(self, item):  #not functioning
        i = self.index(item)
        if i == -1:
            raise ValueError("item not in array")
            return
        self.pop(i)

    def pop(self, index=None): #not functioning
        if self.size == 0:
            raise IndexError("pop from empty array")
        if index is None:
            index = self.size - 1
        item = self.array[index]
        # Shift elements to fill the gap
        for i in range(index, self.size - 1):
            self.array[i] = self.array[i + 1]
        # Clear the last slot
        self.array[self.size - 1] = None
        self.size -= 1
        # Shrink the array if it's less than a quarter full
        if 0 < self.size < self.capacity // 4:
            self._resize(self.capacity // 2)
        return item
    
    def insert(self, index, item): #Implemented
        if index < 0 or index > self.size:
            raise IndexError("index out of range")
        if self.size == self.capacity:
            self._resize(2 * self.capacity)
        # Shift elements to make space for the new item
        for i in range(self.size, index, -1):
            self.array[i] = self.array[i - 1]
        self.array[index] = item
        self.size += 1

    def reverse(self): #implemented
        left = 0
        right = self.size - 1
        while left < right:
            self.array[left], self.array[right] = self.array[right], self.array[left]
            left += 1
            right -= 1

    def copy(self): #later
        new_array = DynamicArray()
        for item in self:
            new_array.append(item)
        return new_array

    def sort(self): #later
        self._quicksort(0, self.size - 1)

    def _quicksort(self, low, high):
        if low < high:
            pivot_index = self._partition(low, high)
            self._quicksort(low, pivot_index - 1)
            self._quicksort(pivot_index + 1, high)

    def _partition(self, low, high): 
        pivot = self.array[high]
        i = low - 1
        for j in range(low, high):
            if self.array[j] <= pivot:
                i += 1
                self.array[i], self.array[j] = self.array[j], self.array[i]
        self.array[i + 1], self.array[high] = self.array[high], self.array[i + 1]
        return i + 1

    def swap(self, index1, index2): #implemented
        self.array[index1], self.array[index2] = self.array[index2], self.array[index1]

    def __getitem__(self, index): #implemented
        if index < 0 or index >= self.size:
            raise IndexError("index out of range")
        return self.array[index]

    def __setitem__(self, index, item): #implemented
        if index < 0 or index >= self.size:
            raise IndexError("index out of range")
        self.array[index] = item

    def __iter__(self): #implemented
        self.indexe = 0
        return self

    def __next__(self): #implemented
        if self.indexe >= self.size:
            raise StopIteration
        item = self.array[self.indexe]
        self.indexe += 1
        return item
    
    def __enumerate__(self):
        return zip(range(self.size), self.array[:self.size])
DataStructures/DArrayClassic/Python_sketch/array.py
Lynet101/Mojo_community-lib
class Test():
    def __init__(self):
        self.arr = DynamicArray()
        self.append_test()
        print([i for i in self.arr])
        self.main()

    def main(self):
        self.reverse_test()
        self.sort_test()

    def sort_test(self):
        self.arr.sort()
        print([i for i in self.arr])

    def copy_test(self):
        self.arr2 = self.arr.copy()

    def reverse_test(self):
        self.arr.reverse()
        print([i for i in self.arr])

    def insert_test(self):
        self.arr.insert(1, 7)
        print([i for i in self.arr])

    def remove_test(self):
        self.arr.remove(2)
        print([i for i in self.arr])

    def pop_test(self):
        print(self.arr.pop())
        print([i for i in self.arr])
        print(self.arr.pop(2))
        print([i for i in self.arr])
        print(self.arr.pop())
        print([i for i in self.arr])

    def append_test(self):
        self.arr.append(1)
        self.arr.append(2)
        self.arr.append(3)
        self.arr.append(2)

    def index_test(self):
        print(self.arr.index(2))

    def count_test(self):
        print(self.arr.count(2))

    def extend_test(self):
        self.arr2 = DynamicArray()
        self.arr2.append(1)
        self.arr2.append(2)
        self.arr2.append(3)
        self.arr2.append(2)
        self.arr.extend(arr2)
        print([i for i in self.arr])

    def len_test(self):
        print(len(self.arr))

    def clear_test(self):
        self.arr.clear()
        print([i for i in self.arr])

Test()
DataStructures/DArrayClassic/Python_sketch/array_test.py
Lynet101/Mojo_community-lib
def calculate_pi(terms):
    pi = 0
    for i in range(terms):
        term = ((-1) ** i) / (2 * i + 1)
        pi += term
    pi *= 4
    return pi

def main():
    print(calculate_pi(100000000))

if __name__ == "__main__":
    main()

Tests n' stuff/Pi/pi.py
Lynet101/Mojo_community-lib
from python import Python
from python import PythonObject
from math import math
from time import now

struct np_loader:
    var lib:PythonObject
    var loaded: Bool
    fn __init__(inout self):
        try:
            self.lib = Python.import_module("numpy")
            self.loaded = True
        except e:
            self.loaded = False
    fn __getitem__(inout self, key:StringLiteral)raises->PythonObject:
        return self.lib.__getattr__(key)

fn main() raises:
    var np = np_loader()
    if np.loaded:
        var python_result = np["linspace"](0, 255,256) #get linear space from python
        var simd_mojo_array = SIMD[DType.float64,256]() #simd is really fast

        var size:Int=python_result.size.to_float64().to_int() #convert arr size to mojo int
        for x in range(size):
            simd_mojo_array[x]=python_result[x].to_float64() #from python float object to mojo float

        var pi = np["pi"].to_float64()

        var start:Int64
        var after:Int64
        #compute one cycles of 256hz


        var backup = simd_mojo_array

        var min_cnt:Int64=99999999
        var tmp:Int64
        for i in range(1000000):
            simd_mojo_array = backup
            start= now()
            simd_mojo_array = math.cos(simd_mojo_array*(pi*2.0/256.0))
            after= now()
            tmp=after-start
            #print(tmp)
            if tmp<min_cnt:
                min_cnt=tmp


        print("------------------")
        print(min_cnt) 
        print("------------------")

mini-tutorials/benchmark.py
Lynet101/Mojo_community-lib
# the file is named .py to have colors. it should be .mojo
# it will hopefully help you use mojo, hand in hand with python
# this code is also part of the [community effort](https://github.com/Lynet101/Mojo_community-lib)
```
from python import Python
from python import PythonObject
from math import math
from time import now
from sys.intrinsics import llvm_intrinsic
struct np_loader:
    var lib:PythonObject
    var loaded: Bool
    fn __init__(inout self):
        try:
            self.lib = Python.import_module("numpy")
            self.loaded = True
        except e:
            self.loaded = False
    fn __getitem__(inout self, key:StringLiteral)raises->PythonObject:
        return self.lib.__getattr__(key)

fn main() raises:
    var np = np_loader() *** get numpy from python *** 
    if np.loaded:
        var python_result = np["linspace"](0, 255,256) # *** get linear space from python ***
        var simd_mojo_array = SIMD[DType.float64,256]() # simd is really fast

        # ***  python returns python objects therefore they always require conversion *** 
        var pi = np["pi"].to_float64()

        var size:Int=python_result.size.to_float64().to_int() # ***  convert arr size to mojo int *** 
        for x in range(size):
            simd_mojo_array[x]=python_result[x].to_float64() # ***  from python float object to mojo float *** 

        # ***  perform the simd cos operation *** 
        simd_mojo_array = math.cos(simd_mojo_array*(pi*2.0/256.0))

        print(simd_mojo_array)
```

mini-tutorials/numpy_mojo_simd_in_colors.py
Lynet101/Mojo_community-lib