Skip to content

Instantly share code, notes, and snippets.

View AdityaBhutani's full-sized avatar
☀️
Sunny

Aditya Bhutani AdityaBhutani

☀️
Sunny
View GitHub Profile
FROM ruby:2.7.1-alpine
ENV BUNDLER_VERSION=2.1.4
RUN apk add --update --no-cache \
binutils-gold \
build-base \
curl \
file \
g++ \
# str = "3x + 2 + x/6 = 5", expected: 0.94736
# str = "3x = 9", expected: 3.0
# str = '3x - 2 + x/6 = 5', expected: 2.21
# program to solve linear equation with arg as str format of linear eqn.
def linear_equation(str)
elements = str.split(' ')
nums = []
xs = []
signs = ['+', "-"]
next_sign = 1
# Expected arg to be 2-d array and each sub array to have 2 numbers representing range-start and range-end
# [[1, 4], [6, 6], [6, 8], [1, 10]], expected: [[1, 10]]
# program to solve finding the overlapping ranges and combining them.
def range_flatten(arr)
res = []; done = []
arr.each_with_index do |range, i|
a = range.first; b = range.second
arr[(i+1)..-1].each_with_index do |other_range, j|
next if done.include?(i+j+1)
@AdityaBhutani
AdityaBhutani / factorial.rb
Last active January 5, 2021 22:00
Factorial
def factorial(n)
return 1 if n==1
n*factorial(n-1)
end
@AdityaBhutani
AdityaBhutani / custom_flatten.rb
Created February 25, 2020 17:15
custom flatten
def custom_flat (arr, res = [])
arr.each_with_index do |ele, i|
if arr[i].class.name == "Array"
#p "ARR - #{arr[i]}"
custom_flat arr[i], res
else
#p "ELE - #{arr[i]}"
res << arr[i]
#p res
end