Skip to content

Instantly share code, notes, and snippets.

View cschlyter's full-sized avatar

Carlos Schlyter cschlyter

  • Florianópolis - SC
View GitHub Profile
class Solution:
def findCircleNum(self, isConnected: List[List[int]]) -> int:
count = 0
length = len(isConnected)
for i in range(length):
if isConnected[i][i] == 1:
count += 1
self.dfs(i, length, isConnected)
return count
def river_sizes(matrix):
sizes = []
visited = [[False for value in row] for row in matrix]
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if visited[i][j]:
continue
traverse_node(i, j, matrix, visited, sizes)
return sizes
@cschlyter
cschlyter / semicolon2csv.py
Created December 10, 2013 16:33
[python] Convert a semicolon separated file to csv
import sys
import csv
semicolonin = csv.reader(sys.stdin, delimiter=';')
commaout = csv.writer(sys.stdout, delimiter=',')
for row in semicolonin:
commaout.writerow(row)
var mongo = require('mongodb');
var Inserter = function (collection) {
this.collection = collection;
this.data = [];
this.maxThreads = 6;
this.currentThreads = 0;
this.batchSize = 5000;
this.queue = 0;
this.inserted = 0;
@cschlyter
cschlyter / toTitleCase.js
Created November 21, 2013 16:29
Convert string to title case. input: fuel output: Fuel
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
@cschlyter
cschlyter / mongo_regex.js
Created August 5, 2013 23:59
[MongoDB] query db using regex.
db.zips.find({city : { $regex : /^[0-9]/ }})
@cschlyter
cschlyter / mongoimport.sh
Created July 22, 2013 03:22
[MongoDB] mongoimport example
mongoimport --db pcat --collection products < products.json
@cschlyter
cschlyter / mongo_group_by_day.js
Created June 12, 2013 16:46
MongoDB: group by day (aggregate)
var mention_id = 620996;
db.mentionStats.aggregate([
{ $match: {'mention_id': mention_id}},
{ $group: {'_id': {
'year': { '$year': "$verification_date" },
'month': { '$month': "$verification_date" },
'day': { '$dayOfMonth': "$verification_date" }
},
'retweets': { $last: "$retweets" }}},
@cschlyter
cschlyter / findDatesInRange.js
Created June 5, 2013 13:42
MongoDB: Find dates in a range.
var start = new Date(2013, 5, 4)
var end = new Date(2013, 5, 6)
db.mention_stat.find({"verification": {"$gte": start, "$lt": end}})
@cschlyter
cschlyter / mongo_group_by_day.js
Created May 2, 2013 14:45
MongoDB: group data by day (group)
db.link_access.group(
{
keyf: function(doc) {
var date = new Date(doc.date);
var dateKey = (date.getMonth()+1)+"/"+date.getDate()+"/"+date.getFullYear()+'';
return {'day': dateKey};
},
cond: {short_id: "N"},
initial: {count:0},
reduce: function(obj, prev) {prev.count++;}