Skip to content

Instantly share code, notes, and snippets.

@alexradzin
alexradzin / NumbersTest.java
Created April 16, 2023 16:09
Write function that accepts positive number and prints all numbers with number of digits less than given number. For example input=2 should print 1..9, input=3 should print 1..99 etc.
View NumbersTest.java
package com.numberstest;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
public class NumbersTest {
@Test
void n() {
printNumbersBuildOfDigitsLessThan(4);
@alexradzin
alexradzin / CyclicRotation.java
Created March 21, 2023 13:08
Codility:CyclicRotation
View CyclicRotation.java
class CyclicRotation {
public int[] solution(int[] a, int k) {
int res[] = new int[a.length];
for (int i = 0; i < a.length; i++) {
int j = (i + k) % a.length;
res[j] = a[i];
}
return res;
}
}
@alexradzin
alexradzin / binarygap.java
Created March 21, 2023 12:36
Codility:BinaryGap
View binarygap.java
class BinaryGap {
public int solution(int num) {
boolean start = false;
int maxZeroCount = 0;
int zeroCount = 0;
for (; num > 0 ; num = num / 2) {
int b = num % 2;
if (b == 1 && !start) {
start = true;
@alexradzin
alexradzin / Bird.java
Created February 22, 2023 09:45
Evaluator: alternative to the Visitor pattern
View Bird.java
package visitoralternative;
public interface Bird {
}
@alexradzin
alexradzin / git-more.sh
Created August 4, 2022 07:46
Script that iterates over git revisions from oldest to the newest in current branch
View git-more.sh
#!/bin/sh
pressAnyKey="Press any key to continue"
pressAnyKeySpaces=`echo $pressAnyKey | sed 's/./ /g'`
(git log --pretty="format:%H %s"; echo) | tac | while read line; do
hash=`echo $line | cut -d' ' -f1`;
message=`echo $line | cut -d' ' -f2-`;
git checkout -q $hash;
echo $message;
printf "$pressAnyKey\r"
@alexradzin
alexradzin / Twitter.java
Created June 15, 2022 12:47
Trivial twitter implementation
View Twitter.java
package com.sunbit;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@alexradzin
alexradzin / TaskTest.java
Last active June 12, 2022 13:51
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
View TaskTest.java
package com.alexr.exercise;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
View ExchangeRate.java
package com.exercise;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Map;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ExchangeRate {
@alexradzin
alexradzin / SortedJoiningIterator.java
Created June 9, 2022 15:52
iterator (Iterator<T>) that produces globally sorted sequence from the given N iterators
View SortedJoiningIterator.java
package com.exercise;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
public class SortedJoiningIterator<T> implements Iterator<T> {
private final PriorityQueue<Head<T>> iterators;
@alexradzin
alexradzin / Insert.java
Created November 21, 2021 07:10
Insert rows to a database using regular and prepared statement utilizing one-by-one and batch mode
View Insert.java
package org.jdbc.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import static java.lang.String.format;
public class Insert {