Skip to content

Instantly share code, notes, and snippets.

View codertcet111's full-sized avatar
💭
Consistency brings happiness

codertcet111

💭
Consistency brings happiness
View GitHub Profile
str.gsub("season", "movie")
"Hi 12, lets play 101".gsub(/\d/, "2")
# "Hi 22, lets play 202"
"H1".gsub(/(\w)(\d)/, '\2\1')
# "1H"
"master".gsub(/\w+/) { |course| course == "master" ? "phd" : "master" }
animal = {
'D' => 'dog',
'C' => 'cat'
}
puts 'DC'.gsub(/\w/,animal)
# O/P: 'dogcat'
def sum(a, *b)
a + b.inject(0) { |a, b| a + b}
end
sum_of_all = method(:sum).curry(3)
puts method(:sum)
puts "Normal Sum: #{sum(1,2,3)}"
puts "Curried Sum 👇:"
Algorithm DivideAndConquer ( Problem ) {
if Problem is too small and easily solvable then{
return solution for Problem
}
else {
Divide the Problem into subproblems P1, P2, ......., Pn
return Combine( DivideAndConquer(P1), DivideAndConquer(P2), ........, DivideAndConquer(Pn))
}
}
Function get_min_and_max (Array a[]){
max, min = a[0]
for i in a {
if a[i] > max then
max = a[i]
if a[i] < min then
min = a[i]
}
return min, max
}
function get_min_and_max(int i, int j){
//get the minimum and maximum values for the caller array
//If the array has a single element then
if (i == j) {
max, min = a[i]
}
else if (i == j-1) { //If the array has two elements, The Problem is too small to solve.
if (a[i] < a[j]) {
max = a[j]
min = a[i]
Algorithm sum(a, b)
// Problem: Addition of two numbers
//Input: Two input values
//Output: One number
//MAX_VALUE = Maximum integer value as supported by the programming language
//Range of Input: a and b are from '0 to MAX_VALUE'