simona.valenti.0
BAN USERgroovy bis!
class RequestOnArray {
def array = [3, 4, 5, 0, 1, 2]
def Map<List<Integer>, List<Integer>> sliceMap
RequestOnArray() {
preprocess(array)
}
static void main(String... args) {
def requestOnArray = new RequestOnArray()
println requestOnArray.request(2, 5, 2)
}
Integer request(int sliceStart, int sliceEnd, int index) {
sliceMap.get(array.subList(sliceStart, sliceEnd)).get(index)
}
private void preprocess(List<Integer> array) {
sliceMap = new HashMap<>()
for (int i = 0; i < array.size(); i++) {
for (int j = i+1; j < array.size()+1; j++) {
def slice = array.subList(i, j)
def sortedSlice = new ArrayList(slice).sort()
sliceMap.put(slice, sortedSlice)
}
}
}
}
ops...sorry, no need to redefile sliceMap inside toSliceMap()!!
- simona.valenti.0 December 10, 2015groovy
the request is a simple get by key
class RequestOnArray {
def array = [3, 4, 5, 0, 1, 2]
def Map<List<Integer>, List<Integer>> sliceMap
RequestOnArray() {
sliceMap = toSliceMap(array)
}
static void main(String... args) {
def requestOnArray = new RequestOnArray()
println requestOnArray.request(2, 5, 2)
}
Integer request(int sliceStart, int sliceEnd, int index) {
sliceMap.get(array.subList(sliceStart, sliceEnd)).get(index)
}
private Map<List<Integer>, List<Integer>> toSliceMap(List<Integer> array) {
Map<List<Integer>, List<Integer>> sliceMap = new HashMap<>()
for (int i = 0; i < array.size(); i++) {
for (int j = i+1; j < array.size()+1; j++) {
def slice = array.subList(i, j)
def sortedSlice = new ArrayList(slice).sort()
sliceMap.put(slice, sortedSlice)
}
}
sliceMap
}
}
groovy
without creating a new array
class ArraySorting {
static void main(String... args) {
String[] elements = ["C", "D", "E", "F", "G"]
int[] indexes = [3, 0, 4, 1, 2]
if (elements.length == indexes.length) {
println(sort(elements, indexes))
}
}
static String[] sort(String[] elements, int[] indexes) {
for (int i = 0; i < indexes.length; i++) {
String tmp = elements[i]
elements[i] = elements[indexes[i]]
elements[indexes[i]] = tmp
int iTemp = indexes[i]
int iTempIndex = indexes.findIndexOf { it == i }
indexes[i] = i
indexes[iTempIndex] = iTemp
}
elements
}
}
sorry for the fancy dictionary!
class WordTransform {
static def dictionary = ["black", "browk", "brown", "blowk", "block", "white"]
static void main(String... args) {
println iterationsToTransform("brown", "black")
println iterationsToTransform("white", "black")
}
static Integer iterationsToTransform(String from, String to) {
if (from && to && from.length() == to.length()
&& dictionary.contains(to)) {
int iterations = 0
int index = 0
while (!from.equals(to) && index < from.size()) {
def word = tryToReplace(from, to, index++)
if (word != from) {
iterations++
index = 0
}
from = word
}
if (from.equals(to))
iterations
}
else null
}
static String tryToReplace(String from, String to, int index) {
def word = from.replace(from.charAt(index), to.charAt(index))
dictionary.contains(word) ? word : from
}
}
int[] zeroToRight(int[] numbers) {
numbers.toList().sort { o1, o2 ->
o1 == 0 ? 1 : (o2 == 0 ? -1 : 0)
}
}
groovy
static boolean existConsecutiveElementsAddUpToTarget(List<Integer> integers, int target) {
int i = 0
while (i < integers.size() - 1) {
int first = integers.get(i)
int second = integers.get(i+1)
if (first != 0 && second != 0 && first + second == target) {
println "true {${[first, second]}}"
return true
}
i++
}
println "false"
return false
}
sorry I've posted 2 times!
- simona.valenti.0 December 03, 2015groovy
class RangeOverlap {
static void main(String... args) {
def ranges = [(1..4),(30..40),(20..91),(8..10),(6..7),(3..9),(9..12),(11..14),(3..5)]
def toOverlap = (3..13)
println findRangeSequence(ranges, toOverlap, [])
}
static List<IntRange> findRangeSequence(List<IntRange> ranges, IntRange toOverlap, List<IntRange> results) {
IntRange range = null
List<IntRange> currentRanges = ranges.findAll({
it.first() == toOverlap.first()
})
currentRanges.each {
if (!range) { range = it }
else if (it.size() > range.size()) { range = it }
}
if (range) {
results << range
if (!range.containsAll(toOverlap)) {
ranges.removeAll(currentRanges)
toOverlap = (range.last()..toOverlap.last())
findRangeSequence(ranges, toOverlap, results)
} else
results
}
else {
if (toOverlap.first() > 1) {
toOverlap = (toOverlap.first() - 1..toOverlap.last())
findRangeSequence(ranges, toOverlap, results)
}
else null
}
}
}
class RangeOverlap {
static void main(String... args) {
def ranges = [(1..4),(30..40),(20..91),(8..10),(6..7),(3..9),(9..12),(11..14),(3..5)]
def toOverlap = (3..13)
println findRangeSequence(ranges, toOverlap, [])
}
static List<IntRange> findRangeSequence(List<IntRange> ranges, IntRange toOverlap, List<IntRange> results) {
IntRange range = null
List<IntRange> currentRanges = ranges.findAll({
it.first() == toOverlap.first()
})
currentRanges.each {
if (!range) { range = it }
else if (it.size() > range.size()) { range = it }
}
if (range) {
results << range
if (!range.containsAll(toOverlap)) {
ranges.removeAll(currentRanges)
toOverlap = (range.last()..toOverlap.last())
findRangeSequence(ranges, toOverlap, results)
} else
results
}
else {
if (toOverlap.first() > 1) {
toOverlap = (toOverlap.first() - 1..toOverlap.last())
findRangeSequence(ranges, toOverlap, results)
}
else null
}
}
}
RepHi, I am Alisa from Southfield, USA, I am working as a Toolmaker in an Audio-Visions company. I am also ...
Repnatetouche, Applications Developer at Alcatel Lucent
My name is NateTouche . I currently live in Seattle . One desire that has always been a constant since As a ...
Repberrysvickers, Associate at Absolute Softech Ltd
Spent 2001-2004 developing strategies for country Luxury Car Rental Miami. Had some great experience building robots for the underprivileged. Have ...
RepRaimeCarrillo, Area Sales Manager at 247quickbookshelp
Hi, I am Raime from Tampa USA . I work as an Local account executive employee. I work in many fields ...
- simona.valenti.0 December 21, 2015