Skip to content

Instantly share code, notes, and snippets.

View iamakimmer's full-sized avatar

Matthew Kim iamakimmer

View GitHub Profile
@iamakimmer
iamakimmer / gist:4748382
Last active December 12, 2015 08:59
2 different methods of calculating Square Root. First is the Newton/Raphson method and the second is the Heron of Alexandria's Method. It seems like Heron of Alexandria's method is simpler but just as fast. However I'm getting an infinite loop on large numbers or my computer hates the heron method on really big numbers. What am I doing wrong?
#newton-raphson for sq root
def newton(square):
guess = square/2.0
attempts = 0
# f(x) = x**2 - square
# then f'(x) = 2x
while abs(guess*guess) - square >= .001:
# print guess
attempts+=1
guess = guess - ((guess**2 - square)/(2*guess))
@iamakimmer
iamakimmer / gist:4965456
Created February 16, 2013 03:50
Quick Find Weighed Union
public class QuickFindWeighted
{
private int[] arr; //the nodes
private int[] size; //the levels on the graph
public QuickFindWeighted(int n) {
arr = new int[n];
size = new int[n];
for (int i=0;i<n;i++) {
arr[i] = i;
@iamakimmer
iamakimmer / replica set
Created February 28, 2013 04:08
Create a replica set in mongodb
mongod --replSet rs1 --logpath "1.log" --dbpath /data/rs1 --port 27017 --fork
mongod --replSet rs2 --logpath "2.log" --dbpath /data/rs1 --port 27018 --fork
mongod --replSet rs3 --logpath "3.log" --dbpath /data/rs1 --port 27019 --fork
def index
@products = Product.includes(:user).all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
format.xml { render xml: @products }
end
end
@iamakimmer
iamakimmer / json_xml_responder.xml
Created March 23, 2013 20:21
how to set up your spring config to respond to json/xml
<!-- Enable annotation scanning. -->
<context:component-scan base-package="com.matthewjkim.controllers" />
<!-- Map objects to XML. -->
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller" />
<bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="xstreamMarshaller" />
</bean>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<!-- Bind the return value of the Rest service to the ResponseBody. -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
@iamakimmer
iamakimmer / gist:fc698109cd2977559a3d
Created September 17, 2014 19:27
letsdoRecursion
options = [{property: “name", variants: [A,B,C]}, {property: “size", variants: [1,2,3]} ,{property: “color", variants: [Blue,Green]}]
3x3x2 = 18 combinations
A-1-Blue
A-1-Green
A-2-Blue
A-2-Green
document.mynumberlist[0] = 2;
document.save();
document.mynumberlist[0] = 2;
document.markModified('mynumberlist'); //notify mongoose of the change
document.save();
document.mynumberlist.set(0, 2);
document.save();
// will not save updated date
doc.eventDate.setDay(10);
doc.save();
// will save updated date
doc.eventDate.setDay(10);
doc.markModified('eventDate');
doc.save();