Skip to content

Instantly share code, notes, and snippets.

View scaffeinate's full-sized avatar

Sudharsanan Muralidharan scaffeinate

View GitHub Profile
public static int isPrime(int x) {
if(x <= 2) return 1;
int num = 2, k = 0, sqrt = (int) Math.sqrt(x);
boolean[] arr = new boolean[x-1];
while(num <= sqrt) {
if(arr[num-2]) {
num++;
continue;
}
public static SinglyLinkedListNode removeNodes(SinglyLinkedListNode listHead, int x) {
SinglyLinkedListNode head = null, tail = null;
while (listHead != null) {
if (listHead.data <= x) {
SinglyLinkedListNode node = new SinglyLinkedListNode();
node.data = listHead.data;
if(head == null) {
head = tail = node;
} else {
tail.next = node;
def findSchedules(workHours, dayHours, pattern):
totalHours = 0
result = []
for c in pattern:
if c != '?':
totalHours = totalHours + int(c)
diff = workHours - totalHours
constructResult(list(pattern), 0, diff, dayHours, result)
return result
@scaffeinate
scaffeinate / TicTacToeGame.java
Last active December 26, 2017 01:47
Two player Tic-Tac-Toe implementation in Java
package matrix;
import java.util.Arrays;
import java.util.Scanner;
/**
* TicTacToeGame
*/
public class TicTacToeGame {
import java.util.HashMap;
import java.util.Map;
/**
* Created by sudharti on 10/30/17.
*/
public class BudgetShopping {
static int budgetShopping(int n, int[] bundleQuantities, int[] bundleCosts) {
return budgetShopping(n, bundleQuantities, bundleCosts, new HashMap<>());
}
@scaffeinate
scaffeinate / api_v1_user_serializer.rb
Last active June 8, 2016 11:16
Rails 5 API user serializer
class Api::V1::UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
def name
"#{object.first_name} #{object.last_name}"
end
end
@scaffeinate
scaffeinate / api_v1_users_controller.rb
Last active June 8, 2016 10:48
Api users controller
module Api::V1
class UsersController < ApplicationController
def show
@user = User.find_by(email: params[:email])
render json: @user
end
end
end
@scaffeinate
scaffeinate / create_users.rb
Created June 8, 2016 10:13
Rails 5 API Migration
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :first_name, null: false, default: ''
t.string :last_name, null: false, default: ''
t.string :email, null: false
t.timestamps
end
add_index :users, :email, unique: true
@scaffeinate
scaffeinate / Gemfile
Last active May 25, 2016 09:32
rails-shop/Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5.1'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
@scaffeinate
scaffeinate / better-nodejs-require-paths.md
Created April 10, 2016 07:41 — forked from branneman/better-nodejs-require-paths.md
Better local require() paths for Node.js

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

var Article = require('../../../models/article');

Those suck for maintenance and they're ugly.

Possible solutions