Skip to content

Instantly share code, notes, and snippets.

View d4vsanchez's full-sized avatar

David Sánchez d4vsanchez

View GitHub Profile
@d4vsanchez
d4vsanchez / leetcode_9_palindrome_number.rs
Created May 11, 2024 14:38
Solution for the "9. Palindrome Number" problem from LeetCode
impl Solution {
pub fn is_palindrome(x: i32) -> bool {
if x < 0 {
return false;
}
let mut x_copy = x;
let mut reverse: i32 = 0;
while x_copy > 0 {
reverse = (reverse * 10) + (x_copy % 10);
@d4vsanchez
d4vsanchez / calculator.rs
Created June 18, 2022 17:43
Simple calculator in Rust
use std::str::FromStr;
use regex::Regex;
const ADDITION: &str = "(\\d+)\\s*?(\\+)\\s*?(\\d+)";
const SUBTRACTION: &str = "(\\d+)\\s*?(\\-)\\s*?(\\d+)";
const MULTIPLICATION: &str = "(\\d+)\\s*?(\\*)\\s*?(\\d+)";
const DIVISION: &str = "(\\d+)\\s*?(/)\\s*?(\\d+)";
fn main() {
@d4vsanchez
d4vsanchez / simple_calculator.rb
Created August 14, 2021 20:33
Simple calculator done for The Complete Ruby on Rails Developer Course
module CalculatorStrategy
def calculate(number_a, number_b)
raise "Not implemented"
end
end
class AdditionStrategy
include CalculatorStrategy
def calculate(number_a, number_b)
@d4vsanchez
d4vsanchez / strictEquals.js
Created August 1, 2021 14:53
strictEquals implementation without using === or !== operator
function strictEquals(a, b) {
// If one is NaN, we'll always receive false anyway
if (Object.is(a, NaN)) {
return false;
}
// -0 and 0 special case
if ((Object.is(a, 0) && Object.is(b, -0)) || (Object.is(a, -0) && Object.is(b, 0))) {
return true;
}
@d4vsanchez
d4vsanchez / PizzaBuilder.ts
Created April 17, 2021 17:16
An example showing how to create a function that must call its pre-requisites in order to be called, using TypeScript.
// Let's create a class to build a simple Pizza
// Our PizzaBuilder can set:
// 1. Pizza size (required)
// 2. Pizza flavour (required)
// 3. Pizza crust (optional)
// We must make sure that the "cook" function is type-checked and can be called
// only if we have set a size or a flavour.
type PizzaSizes = 'small' | 'medium' | 'large' | 'extra large';
type PizzaFlavour = 'pepperoni' | 'bbq chicken' | 'margherita' | 'vegetarian';
@d4vsanchez
d4vsanchez / test_report.py
Created August 21, 2017 22:03
Fake classes to mock boto3
class FakeResource():
def Bucket(self, bucket):
return self
def put_object(self, Key=None, Body=None, ACL='', Expires='', ContentType=''):
# We do nothing here, but return the same data type without data
return {}
class FakeSession(Session):
def resource(self, name):
@d4vsanchez
d4vsanchez / test_report.py
Last active January 16, 2019 22:11
Final version of the tests for the report model example
import mock
from django.core import mail
from django.test import TestCase
from boto3.session import Session
from reports.models.report import Report
class FakeResource():
def Bucket(self, bucket):
return self
@d4vsanchez
d4vsanchez / test_report.py
Created August 21, 2017 21:21
First version of the tests for the report model example
from django.core import mail
from django.test import TestCase
from reports.models.report import Report
class ReportTest(TestCase):
def tearDown(self):
Report.objects.all().delete()
mail.outbox = []
def test_create_report_and_send_by_email(self):
@d4vsanchez
d4vsanchez / report.py
Created August 21, 2017 20:47
Report model example
import StringIO
from django.db import models
from boto3.session import Session
class Report(models.Model):
# ...
# Information about the model
def create_report_and_send_by_email(self):