Skip to content

Instantly share code, notes, and snippets.

View sadedv's full-sized avatar

Dmitry Sadovnikov sadedv

View GitHub Profile
@sadedv
sadedv / knapsack.java
Created April 1, 2016 17:10
Knapsack greedy algorithm
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
public class knapsack {
public static void main(String[] args){
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
int realtarget=0;
@sadedv
sadedv / Collections.sort.java
Created March 8, 2016 12:49
Collections.sort
List list = new ArrayList(wordCountMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> a, Map.Entry<Integer, Integer> b) {
return a.getValue() - b.getValue();
}
});
Collections.reverse(list);
@sadedv
sadedv / timer.java
Last active March 8, 2016 17:22
Timer таймер
long start = System.currentTimeMillis();
//write your code here
long timeSpent = System.currentTimeMillis() - start;
System.out.println(timeSpent + " ms");
@sadedv
sadedv / comparator.java
Created March 7, 2016 14:58
Comparator
List<Person> persons = personsExtracter.getPersons();
Collections.sort(persons, new AgeComparator());
public class AgeComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
Integer age1 = o1.getAge();
Integer age2 = o2.getAge();
return age1.compareTo(age2);
}
@sadedv
sadedv / fileReaderAndWriter.java
Created March 7, 2016 14:56
Дописываем в файл и читаем из файла запись
private static void write() throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(file, true), "UTF-8"));
Scanner scanner = new Scanner(System.in);
String line;
while (true) {
line = scanner.nextLine();
if ("--stop".equals(line)) break;
printWriter.println(line);
}
@sadedv
sadedv / fastJava.java
Created March 4, 2016 13:25
Java Template (Fast I/O)
import java.util.*;
import java.io.*;
class
{
/************************ SOLUTION STARTS HERE ************************/
private static void solve(FastScanner s1, PrintWriter out){
@sadedv
sadedv / CreateEngWordBase.java
Created February 18, 2016 08:23
EnglishWordsAndSentences
package preparingDB;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
/**
@sadedv
sadedv / IntegerToBinary.java
Last active February 14, 2024 19:09
Перевести число в различные системы счисления - Java (в шестнадцатиричную, бинарную (двоичную)) Integer to Binary, to Hex and to Octal
public class Main {
public static void main(String[] args) {
Integer number = 255;
// Бинарный формат числа
String convert = Integer.toBinaryString(number);
System.out.println(convert);
// Восьмиричная форма
convert = Integer.toOctalString(number);
@sadedv
sadedv / Collections.reverseOrder().java
Last active March 8, 2016 12:45
Обратная сортировка коллекции
public class Sort
{
public static Map<Double, String> map = new TreeMap<Double, String>(Collections.reverseOrder());
public static void main(String args[])
{
// create linked list object
LinkedList<Integer> list = new LinkedList<Integer>();
// populate the list
@sadedv
sadedv / WordCountJob.java
Last active March 8, 2016 12:48
MapReduce Hadoop Java
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;