Skip to content

Instantly share code, notes, and snippets.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class SecretSanta {
public static void main(String[] args) {
File inputFile = new File("names.txt");
List<String> names = readNamesFromFile(inputFile);
Map<String, String> secretSanta = generateSecretSanta(names);
printSecretSanta(secretSanta);
@ravichandrae
ravichandrae / comparestrint.py
Created January 25, 2018 08:38
Comparing two different types in python
batch_size = cnf.get_batch_size()
batch = []
#Some code to fill the batch
if len(batch) == batch_size:
#Flush out the batch by saving to DB
else:
#Some other code
#The Fix here is not to forget to convert the config parameter to int
batch_size = int(cnf.get_batch_size())
@ravichandrae
ravichandrae / CheckEditDistance1.java
Last active November 27, 2017 13:51
Given two strings, how do we check if their edit distance is 1?
import java.util.Scanner;
public class Solution {
public static void main(String []args) {
Scanner reader = new Scanner(System.in);
String str1 = reader.nextLine();
String str2 = reader.nextLine();
if(hasEditDistance1(str1, str2))
System.out.println("Yes");
else
@ravichandrae
ravichandrae / reproad.cpp
Created January 27, 2017 09:46
SPOJ problem REPROAD - Repair Road solution
#include <iostream>
#include <vector>
#include <cstdlib>
#include <set>
using namespace std;
int main() {
int t;
cin >> t;
@ravichandrae
ravichandrae / anuarm_codechef.cpp
Created January 24, 2017 06:45
ANUARM: A codechef problem
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n,m;
@ravichandrae
ravichandrae / binary_tree_path.cpp
Last active January 23, 2017 12:46
Path length between two nodes in a full binary tree
#include <iostream>
using namespace std;
int path_length(int p, int q) {
int len = 0;
//Until p and q reach their lowest common ancestor.
while (p != q ) {
if( p > q ) {
p = p/2;
len++;
@ravichandrae
ravichandrae / minimum_bill.cpp
Created July 19, 2016 11:33
This is my solution for the Codechef problem https://www.codechef.com/problems/ANUBTG
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int t;
cin >> t;
while( t-- ) {
int n;
@ravichandrae
ravichandrae / adjacent_numbers.cpp
Created March 19, 2016 11:20
searching for a given number in adjacent array
#include <iostream>
#include <vector>
using namespace std;
bool search_adj(const vector<int> &arr, int s) {
bool ret = false;
size_t i;
for( i = 0; i < arr.size(); ) {
if( arr[i] == s ) {
@ravichandrae
ravichandrae / double_string.cpp
Last active March 16, 2016 06:14
How to check if a given string is a double string possible by deleting a character.
#include <iostream>
#include <vector>
#include <climits>
#include <cstdlib>
using namespace std;
bool is_double(string &input) {
size_t len = input.size();
if( len % 2 == 1 )
return false;
@ravichandrae
ravichandrae / print_diagonal.cpp
Created February 26, 2016 14:01
Printing the given string as a cross mark
#include <iostream>
using namespace std;
int main() {
string input="12345";
int len = input.size();
int i,j;
for( i = 0; i < len; i++)
{
for( j = 0; j < len; j++)