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 quicksort arr,startpos,endpos | |
def choose_pivot arr,startpos,endpos | |
#THis is for selecting median of three , you can return a random number as well. | |
if ((arr[startpos] < arr[endpos]) and (arr[startpos] > arr[startpos+ (endpos-startpos)/2])) or ((arr[startpos] > arr[endpos]) and (arr[startpos] < arr[startpos+ (endpos-startpos)/2])) | |
return startpos | |
elsif ((arr[endpos] < arr[startpos]) and (arr[endpos] > arr[startpos+ (endpos-startpos)/2])) or ((arr[endpos] > arr[startpos]) and (arr[endpos] < arr[startpos+ (endpos-startpos)/2])) | |
return endpos |
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 closestpair points | |
px = points | |
def euclid_distance pt1, pt2 | |
Math.sqrt ((pt1[0] - pt2[0])**2 + (pt1[1] - pt2[1])**2) | |
end | |
def closestsplitpair pts, minpt, dist |
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
!!! strict | |
!!! XML | |
%html | |
-# Self closing tags | |
%img{:src => "happy.jpg"}/ | |
%div.myclass | |
.myclass1 |
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
#To get a column from a Model | |
Movie.all # Returns all row from the table Movies | |
Movie.select(:title).map { |m| m.title } # Returns array of title from the Model movie | |
Movie.select(:title).map(&:title) # Returns array of title from the Model 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
#Model | |
@user.should have(1).error_on(:username) # Checks whether there is an error in username | |
@user.errors[:username].should include("can't be blank") # check for the error message | |
#Rendering | |
response.should render_template(:index) | |
#Redirecting | |
response.should redirect_to(movies_path) |
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 reverse_word str | |
str.split(" ").reverse.join(" ") | |
end | |
reverse_word "This is really awesome" |
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 mergesort arr | |
def merge arr1, arr2 | |
arr = [] | |
j = 0 | |
i = 0 | |
while (i != arr1.size) && (j != arr2.size) | |
if arr1[i] < arr2[j] | |
arr << arr1[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
class Numeric | |
def find_last_bit | |
r = 0 | |
n = self | |
32.times do | |
n >>= 1 | |
break if n==0 | |
r = r+1 | |
end | |
return r |
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 count_inversion arr | |
def count_and_merge arr1, arr2 | |
arr = [] | |
j = 0 | |
i = 0 | |
count = 0 | |
while (i != arr1.size) && (j != arr2.size) | |
if arr1[i] < arr2[j] |
NewerOlder