Skip to content

Instantly share code, notes, and snippets.

View mohamedallam1991's full-sized avatar
🎯
Focusing

Mohamed Allam mohamedallam1991

🎯
Focusing
View GitHub Profile
@mohamedallam1991
mohamedallam1991 / BenchmarkingAppendSliceAndArray.go
Created November 7, 2022 16:05
This benchmarking test in Go determines how much it takes to do this operations.
package main
import "testing"
const TesthowManyTime int = 10_000
func BenchmarkAppendSice(b *testing.B) {
for i := 0; i < b.N; i++ {
useAppendSlice()
}
@mohamedallam1991
mohamedallam1991 / AppendToArraySliceOrPredefinedSlice.go
Created November 7, 2022 15:27
The differences using arrays or append to a slices or a predefined length of a slice usePredifinedSlice 21.516612212s useArray 21.730010841s useAppendSlice 2m33.419673096s
package main
import (
"fmt"
"sync"
"time"
)
const howManyTime int = 1_000_000_000
@mohamedallam1991
mohamedallam1991 / twwSumProblem.go
Created October 7, 2022 00:22
The Two Sum problem on unsorted array in golang
package main
import (
"fmt"
)
func main() {
array := []int{1, 2, 5, 6, 4, 5, 7}
fmt.Println(bruteForce(array, 10))
@mohamedallam1991
mohamedallam1991 / InterpolationSearch.go
Created September 10, 2022 08:54
Interpolation search, particularly useful if the elements are uniformly distributed, this allow you to go to the closest possibility.
func InterpolationSearch(array []int, key int) int {
var mid, low int = 0, 0
var high int = len(array) - 1
var valBig, valSmal int
for array[low] < key && array[high] > key {
valSmal = array[low]
valBig = array[high]
mid = low + ((key-valSmal)*(high-low))/(valBig-valSmal)
if array[mid] < key {
@mohamedallam1991
mohamedallam1991 / TwoCrystalBalls.go
Created September 8, 2022 09:27
100 Story Building, Two crystal balls or how to implement Jump search in Golang, The Go programming language
func TwoCrystalBalls(array []int, target int) int {
step := int(math.Round(math.Sqrt(float64(len(array)))))
rbound := len(array)
for i := step; i < len(array); i += step {
if array[i] > target {
rbound = i
break
}
}
@mohamedallam1991
mohamedallam1991 / BinarySearch.go
Last active September 7, 2022 08:49
Binary Search in Golang and Linear Search method
package main
import "fmt"
func main() {
b := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 100, 159, 192, 350, 1230, 1341, 4533}
fmt.Println(BinarySearch(b, 31))
fmt.Println(LinearSearch(b, 31))
@mohamedallam1991
mohamedallam1991 / ExampleTest.php
Created September 22, 2021 18:52
Assertion database has the same title we expect
$this->assertDatabaseHas('products', [
'id' => 1,
'title' => $this->product->title,
]);
$this->assertSame($this->product->title, Product::first()->title);
@mohamedallam1991
mohamedallam1991 / Factory multiple creation
Created September 22, 2021 18:27
Laravel refactoring a test using an array to assign multiple objects to a model factory that we just created
[$product, $product1, $product2] = Product::factory()->count(3)->create();
$product = Product::factory()->create();
$product1 = Product::factory()->create();
$product2 = Product::factory()->create();