-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-- some tasks pulled from the different repositories to have them in …
…one place
- Loading branch information
Showing
92 changed files
with
2,758 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package az.be; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Parking { | ||
public static long carParkingRoof(List<Long> cars, int k) { | ||
List<Long> sorted = cars.stream().sorted().collect(Collectors.toList()); | ||
return IntStream.rangeClosed(0, sorted.size() - k) | ||
.mapToLong(idx -> sorted.get(idx + k - 1) - sorted.get(idx) + 1) | ||
.min() | ||
.orElseThrow(RuntimeException::new); | ||
} | ||
|
||
public static void main(String[] args) { | ||
List<Long> cars = Arrays.asList(6L, 2L, 12L, 7L); | ||
long r = carParkingRoof(cars, 3); | ||
System.out.println(r); // 6 | ||
} | ||
} | ||
// http://167.71.44.71/codenjoy-contest/board/player/vr70mxu5y8ussmvqp6s1?code=7213819288312594213 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package az.be; | ||
|
||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class PrintDiagonal { | ||
static class Track { | ||
int r = 0; | ||
int c = 0; | ||
int dir = 0; | ||
} | ||
|
||
private static String traverse(int R, int C, int[][] a) { | ||
Track t = new Track(); | ||
return IntStream.range(0, R*C).map(n -> { | ||
int val = a[t.r][t.c]; | ||
switch (t.dir) { | ||
case 0: t.c++; t.dir=1; break; | ||
case 1: | ||
if (t.r == R-1) { t.c++; t.dir=2; } | ||
else if (t.c == 0 ) { t.r++; t.dir=2; } | ||
else { t.r++; t.c--; } | ||
break; | ||
case 2: | ||
if (t.c == C-1) { t.r++; t.dir=1; } | ||
else if (t.r == 0) { t.c++; t.dir=1; } | ||
else { t.r--; t.c++; } | ||
} | ||
return val; | ||
}) | ||
.mapToObj(String::valueOf) | ||
.collect(Collectors.joining(" ")); | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[][]a = { | ||
{ 1, 2, 3, 4, }, | ||
{ 5, 6, 7, 8, }, | ||
{ 9, 10, 11, 12, }, | ||
{ 13, 14, 15, 16, }, | ||
{ 17, 18, 19, 20, }, | ||
{ 21, 22, 23, 24, }, | ||
{ 25, 26, 27, 28, }, | ||
}; | ||
System.out.println("1 2 5 9 6 3 4 7 10 13 17 14 11 8 12 15 18 21 25 22 19 16 20 23 26 27 24 28"); | ||
System.out.println(traverse(a.length, a[0].length, a)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package az.be; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class PrintMatrix { | ||
|
||
public static String dataOrdered(int R, int C, int[][] m) { | ||
return IntStream.range(0, R*C).map(idx -> { | ||
int row = idx / C; | ||
int shift = idx - row * C; | ||
int col = (row&1)==0 ? shift : C-1-shift; | ||
return m[row][col]; | ||
}) | ||
.mapToObj(String::valueOf) | ||
.collect(Collectors.joining(" ")); | ||
} | ||
|
||
public static List<Integer> dataOrdered2R(int R, int C, int[][] m, int idx, List<Integer> acc) { | ||
if (idx == R*C) return acc; | ||
int row = idx / C; | ||
int shift = idx - row * C; | ||
int col = (row&1)==0 ? shift : C-1-shift; | ||
acc.add(m[row][col]); | ||
return dataOrdered2R(R, C, m, idx+1, acc); | ||
} | ||
|
||
public static String dataOrdered2(int R, int C, int[][] m) { | ||
return dataOrdered2R(R, C, m, 0, new LinkedList<>()).stream() | ||
.map(String::valueOf) | ||
.collect(Collectors.joining(" ")); | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[][]a = | ||
{ | ||
{ 1, 2, 3 }, | ||
{ 5, 6, 7 }, | ||
{ 9, 10, 11 }, | ||
{ 13, 14, 15 }, | ||
{ 17, 18, 19 }, | ||
{ 21, 22, 23 }, | ||
}; | ||
System.out.println(dataOrdered(a.length, a[0].length, a)); | ||
System.out.println(dataOrdered2(a.length, a[0].length, a)); | ||
// 1 2 3 7 6 5 9 10 11 15 14 13 17 18 19 23 22 21 | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package az.be; | ||
|
||
public class ShoesGroupsOptimized { | ||
public int calc(String origin) { | ||
int groups = 0; | ||
int count = 0; | ||
|
||
for (int i = 0; i < origin.length(); i++) { | ||
char c = origin.charAt(i); | ||
switch (c) { | ||
case 'L': count++; break; | ||
case 'R': count--; break; | ||
} | ||
if (count == 0) groups++; | ||
} | ||
return groups; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package az.be | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks | ||
|
||
object StringRotation { | ||
|
||
def rotate(s: String, n0: Int): String = | ||
n0 % s.length match { | ||
// right | ||
case x if x > 0 => | ||
val r = s.substring(s.length - x) | ||
val l = s.substring(0, s.length - x) | ||
r + l | ||
// left | ||
case x if x < 0 => | ||
val l = s.substring(0, -x) | ||
val r = s.substring(-x) | ||
r + l | ||
case _ => s | ||
} | ||
|
||
} | ||
|
||
class StringRotation extends AnyFunSuite with ScalaCheckPropertyChecks { | ||
import StringRotation._ | ||
|
||
test("1") { | ||
val testCases = Table( | ||
"", | ||
0, | ||
8, | ||
-8, | ||
1, | ||
2, | ||
10, | ||
-1, | ||
-2, | ||
-10 | ||
) | ||
val s = "abcdefgh" | ||
forAll(testCases) { shift => | ||
val s2 = rotate(s, shift) | ||
pprint.log(s2 -> shift) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package az.be; | ||
|
||
public class StringShift { | ||
public static String shift(String orig, int toLeft, int toRight) { | ||
int len = orig.length(); | ||
int total = (toRight - toLeft) % len; | ||
if (total == 0) return orig; | ||
|
||
int middle = (total > 0) ? len-total: -total; | ||
return orig.substring(middle) | ||
.concat(orig.substring(0, middle)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|===========| | ||
| Task 1. | | ||
|===========| | ||
Probably all of you aware of the general rule | ||
of creating English sentences. | ||
It is: subject + verb + object | ||
|
||
------------ | ||
| Given: | | ||
------------ | ||
List<String> subjects = list("Noel", "The cat", "The dog") | ||
List<String> verbs = list("wrote", "chased", "slept on") | ||
List<String> objects = list("the book","the ball","the bed") | ||
|
||
List<String> sentences = all_possible(subjects, verbs, objects) | ||
------------ | ||
| Goal: | | ||
------------ | ||
To build all possible combinations of this words: | ||
|
||
Noel wrote the book | ||
Noel wrote the ball | ||
Noel wrote the bed | ||
Noel chased the book | ||
Noel chased the ball | ||
Noel chased the bed | ||
Noel slept on the book | ||
Noel slept on the ball | ||
Noel slept on the bed | ||
The cat wrote the book | ||
The cat wrote the ball | ||
The cat wrote the bed | ||
The cat chased the book | ||
The cat chased the ball | ||
The cat chased the bed | ||
The cat slept on the book | ||
The cat slept on the ball | ||
The cat slept on the bed | ||
The dog wrote the book | ||
The dog wrote the ball | ||
The dog wrote the bed | ||
The dog chased the book | ||
The dog chased the ball | ||
The dog chased the bed | ||
The dog slept on the book | ||
The dog slept on the ball | ||
The dog slept on the bed | ||
|
||
|
25 changes: 25 additions & 0 deletions
25
algorithms/src/main/scala/az/be/birds/BirdsMigrationApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package lesson35sql01.warmup; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class BirdsMigrationApp { | ||
|
||
static int migratoryBirds(List<Integer> arr) { | ||
Map<Integer, Long> types = arr.stream() | ||
.collect(Collectors.groupingBy(a -> a, Collectors.counting())); | ||
|
||
long max_size = types.values().stream() | ||
.max(Comparator.comparingLong(a -> a)) | ||
.orElseThrow(RuntimeException::new); | ||
|
||
return types.entrySet().stream() | ||
.filter(e -> e.getValue() == max_size) | ||
.map(Map.Entry::getKey) | ||
.min(Comparator.comparingInt(a->a)) | ||
.orElseThrow(RuntimeException::new); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://www.hackerrank.com/challenges/migratory-birds/problem |
45 changes: 45 additions & 0 deletions
45
algorithms/src/main/scala/az/be/dd/DiagonalDifferenceApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package az.be.dd; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
public class DiagonalDifferenceApp { | ||
|
||
public static int diagonalDifference(List<List<Integer>> data) { | ||
class Pair { | ||
final int a; | ||
final int b; | ||
|
||
Pair(int a, int b) { | ||
this.a = a; | ||
this.b = b; | ||
} | ||
} | ||
return IntStream.range(0, data.size()).mapToObj(i -> new Pair( | ||
data.get(i).get(i), | ||
data.get(i).get(data.size()-1-i) | ||
)) | ||
.reduce((p1, p2) -> new Pair(p1.a+p2.a, p1.b+p2.b)) | ||
.map(p -> Math.abs(p.a - p.b)) | ||
.orElseThrow(RuntimeException::new); | ||
} | ||
|
||
public static int diagonalDifference2(List<List<Integer>> data) { | ||
int r = IntStream.range(0, data.size()).map(i -> | ||
data.get(i).get(i) - | ||
data.get(i).get(data.size()-1-i) | ||
).sum(); | ||
return Math.abs(r); | ||
} | ||
|
||
public static void main(String[] args) { | ||
List<List<Integer>> data = Arrays.asList( | ||
Arrays.asList(1, 2, 3), | ||
Arrays.asList(3, 4, 5), | ||
Arrays.asList(5, 6, 8) | ||
); | ||
int r = diagonalDifference(data); | ||
System.out.println(r); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://www.hackerrank.com/challenges/diagonal-difference/problem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package az.be.kangaroo; | ||
|
||
// https://www.hackerrank.com/challenges/kangaroo/problem | ||
public class KangarooApp { | ||
|
||
static String represent(boolean r) { | ||
return r ? "YES" : " NO"; | ||
} | ||
|
||
private static boolean solve(int x1, int v1, int x2, int v2) { | ||
double v = (double) (x2-x1) / (v1-v2); | ||
return (int) v == v; | ||
} | ||
|
||
private static boolean canBeSolved(int x1, int v1, int x2, int v2) { | ||
return (x2>x1 && v1>v2) || (x2<x1 && v1<v2) || (x1==x2 && v1==v2); | ||
} | ||
|
||
static String kangaroo(int x1, int v1, int x2, int v2) { | ||
boolean r = canBeSolved(x1, v1, x2, v2) && solve(x1, v1, x2, v2); | ||
return represent(r); | ||
} | ||
|
||
} |
Oops, something went wrong.