Reverse Double Linked List

Given a double linked list’s head node, reverse the list and return the new head node. Java Solution /* * For your reference: * * DoublyLinkedListNode { * int data; * DoublyLinkedListNode next; * DoublyLinkedListNode prev; * } * */ static DoublyLinkedListNode reverse(DoublyLinkedListNode head) { DoublyLinkedListNode p = head; DoublyLinkedListNode newHead = head;   while(p!=null){ … Read more

R read data from csv files

Reading data from csv files is very useful as csv file is a standard output of many programs. In R, you can read csv file in one line: df <- read.csv("/path/to/file.txt", row.names=1,head=FALSE, sep=",")df <- read.csv("/path/to/file.txt", row.names=1,head=FALSE, sep=",") The code above specifies the first column is just a name by using row.names=1. By using head=FALSE, it … Read more

Python read data from MySQL database

The following code can read data from MySQL database. import MySQLdb   # Open database connection db = MySQLdb.connect("localhost","username","password","DBName")   # prepare a cursor object using cursor method cursor = db.cursor()   sql = "select * from table1" cursor.execute(sql) results = cursor.fetchall() for row in results: print row[0]   # disconnect from server db.close()import MySQLdb … Read more

R hierarchical cluster data read from CSV file

The following code records how I load csv files to R and run hierarchical clustering algorithm on the data. I have to say that using R to plot the data is extremely EASY to do! > df <- read.csv("C:/Users/Creek/Desktop/data.txt", row.names=1,head=FALSE, sep=",") > d <- dist(as.matrix(df)) > hc <- hclust(d) > plot(hc) > q()> df <- … Read more

LeetCode – Subarray Sum Equals K (Java)

Given an array of integers and an integer k, find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note that empty array is not considered as a subarray. Java Solution The sum problems can often be solved by using a hash map … Read more