Skip to content

Instantly share code, notes, and snippets.

View crashtech's full-sized avatar
🛰️
Creating magic method by method

Carlos Silva crashtech

🛰️
Creating magic method by method
View GitHub Profile

Math Code Challange

Written for BSIT 330: Discrete Mathematics (Westcliff University), on April 25, 2021
Reviwed on September 22, 2024

As a common practice, developers are requested to complete code challenges to assess their knowledge of a specific language or prove a certain level of experience in logical conundrums. Usually, such tests ask the programmer to receive a specific input and provide an output based on a series of criteria. Sometimes, the expected solution has to be elegant, be aware of edge cases, and work relatively fast given heavier inputs. However, there are cases that the best solution could be found using a comprehensive understanding of mathematics or other subjects. That is undoubtedly the case of the challenge that is going to be presented in this paper.

# This is the PEG-based grammar for GraphQL based on:
# https://spec.graphql.org/October2021/#sec-Document-Syntax
#
# It is also inspired by another PEG grammar of GraphQL for JavaScript:
# https://github.com/madjam002/graphqlite/blob/master/grammar.pegjs
#
# This only complies with the GraphQL spec with no further additions, focusing
# only the **Executable Definitions**, to optimize the output and processing
grammar Rails.GraphQL.Parser
Small Crunchy Finger Basket
{"ref":"MenuItem:06129e99-923f-4e0d-8863-f4f295b5f419","id_field":"guid","pos_active":true,"title":"Small Crunchy Finger Basket","price":9.55,"image_url":"https://s3.amazonaws.com/toasttab/restaurants/restaurant-26036000000000000/menu/items/3/item-400000004246731573_1582403485.jpg","description":"3 fingers with 1 sauce and french fries.","tax_rate":15.5,"observation_flags":{"suspend_until_flags":{"sold_out_in_pos":"observed"},"alcohol_problems":{"observed_missing_alcohol_state_from_pos":"observed"}}}
Classic Fries
{"ref":"MenuItem:a9537c02-dc72-45a2-bddd-a8da9af5263d","id_field":"guid","pos_active":true,"title":"Classic Fries","price":4.45,"image_url":"https://s3.amazonaws.com/toasttab/restaurants/restaurant-26036000000000000/menu/items/2/item-400000004249845722_1582410894.jpg","description":"3/8th\" straight cut fries with crispy batter.","tax_rate":15.5,"observation_flags":{"suspend_until_flags":{"sold_out_in_pos":"observed"},"alcohol_problems":{"observed_missing_alcohol_state_f
@crashtech
crashtech / find_aunt.rb
Created October 22, 2020 19:21
Able code interview
CHECKERS = {
'cats' => :>,
'trees' => :>,
'pomeranians' => :<,
'goldfish' => :<,
}
def find_aunt(criteria, input)
input.scan(/Sue (\d+): (.*)\n/).find do |(number, data)|
break number if data.scan(/(\w+): (\d+)/).all? do |(item, value)|
# app/models/topic.rb
class Topic < ApplicationRecord
self.table_name = 'topic'
## Associations
has_many :posts
## Validations
validates :name, presence: true
end
@crashtech
crashtech / RoR, from MVC to GraphQL.md
Last active September 22, 2024 21:59
RoR, from MVC to GraphQL

Ruby on Rails, from MVC to GraphQL

As a Ruby on Rails developer, I'm always looking for the best tools and practices to apply to my day-to-day development. This framework has a fantastic structure where all its inner parts communicate very well. But, as applications start growing, or developers want to start using more modern technologies, several things start getting out of control.

Recent versions of Rails implemented Webpack, facilitating the use of modern JavaScript frameworks, which added another level of robustness to everyone's applications. Not only is this becoming the new standard, but it also enables bigger code-bases without falling into madness.

Facebook, as the creator of ReactJS (one of these modern JS frameworks and the most popular one) introduced to the world another of its technologies, GraphQL. This tool, like any other tool, can be a great asset, as long as it's in the right hands, meaning that you first need to know what role it plays before exploring how to use it in your RoR appli

@crashtech
crashtech / google-data-studio.sql
Created February 14, 2019 19:20
Google Data Studio Stories
SELECT "stories"."id" AS "Id",
"stories"."title" AS "Title",
"stories"."publication_id" AS "Publication Id",
"publications"."name" AS "Publication Name",
"story_authors"."authors" AS "Authors",
"stories"."date" AS "Date",
"stories"."media_type" AS "Media Type",
"stories"."url" AS "URL",
"stories"."length" AS "Length",
"stories"."solutions_3" AS "Solutions 3",
@crashtech
crashtech / cosine_distance.rb
Last active August 31, 2018 09:09
Similarity algorithms
def cosine_distance(this, that)
return 1.0 if this === that
a_vector = Hash.new(0).tap{ |v| this.each{ |e| v[e] += 1 } }
b_vector = Hash.new(0).tap{ |v| that.each{ |e| v[e] += 1 } }
a_mag = 0.0
product = a_vector.inject(0.0) do |total, (key, value)|
a_mag += value ** 2
total += value * b_vector[key]
SELECT "orders"."id" AS t0_r0,
"orders"."customer_id" AS t0_r1,
"orders"."vendor_id" AS t0_r2,
"orders"."billing_address_id" AS t0_r3,
"orders"."shipping_address_id" AS t0_r4,
"orders"."status" AS t0_r5,
"orders"."partner_order_number" AS t0_r6,
"orders"."created_at" AS t0_r7,
"orders"."updated_at" AS t0_r8,
"orders"."description" AS t0_r9,
@crashtech
crashtech / rvm-trace.txt
Last active April 12, 2017 03:13
RVM Trace erro CD
+ 1491966648.266378976 /scripts/cli : __rvm_parse_args() 749 > [[ -z '' ]]
+ 1491966648.271087905 /scripts/cli : __rvm_parse_args() 749 > [[ -n '' ]]
+ 1491966648.275022367 /scripts/cli : __rvm_parse_args() 752 > [[ error == '' ]]
+ 1491966648.278861310 /scripts/cli : __rvm_parse_args() 752 > [[ 0 -eq 1 ]]
+ 1491966648.282690210 /scripts/cli : __rvm_parse_args() 752 > [[ -n '' ]]
+ 1491966648.286148429 /scripts/cli : __rvm_parse_args() 15 > [[ -n use ]]
+ 1491966648.289327313 /scripts/cli : __rvm_parse_args() 17 > rvm_token=use
+ 1491966648.292945688 /scripts/cli : __rvm_parse_args() 19 > (( 1 > 0 ))
+ 1491966648.296664270 /scripts/cli : __rvm_parse_args() 21 > next_token=/home/poc.myadmin.dev/
+ 1491966648.300582448 /scripts/cli : __rvm_parse_args() 22 > shift