This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str.gsub("season", "movie") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Hi 12, lets play 101".gsub(/\d/, "2") | |
# "Hi 22, lets play 202" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"H1".gsub(/(\w)(\d)/, '\2\1') | |
# "1H" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"master".gsub(/\w+/) { |course| course == "master" ? "phd" : "master" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animal = { | |
'D' => 'dog', | |
'C' => 'cat' | |
} | |
puts 'DC'.gsub(/\w/,animal) | |
# O/P: 'dogcat' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 👇:" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
OlderNewer