Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Created February 9, 2016 13:21
Show Gist options
  • Save naoto-ogawa/049ebda209eff49a376b to your computer and use it in GitHub Desktop.
Save naoto-ogawa/049ebda209eff49a376b to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
public class AssignIntegerBetweenMinAndMax {
public static void main(String[] args) {
Block block = new Block();
block.setMin(10);
block.setMax(15);
block.addKey("A");
logBlock("01", block);
block.addKey("B");
logBlock("02", block);
block.addKey("C");
logBlock("03", block);
block.addKey("D");
logBlock("04", block);
block.addKey("E");
logBlock("05", block);
}
private static class Block {
private List<String> keys = new ArrayList<String>();
private long min;
private long max;
public Block() {
}
public Block(String key, long min, long max) {
keys.add(key);
this.min = min;
this.max = max;
}
public String getLastKey() {
return keys.get(keys.size() - 1);
}
public void addInfo(String key, long min, long max) {
keys.add(key);
this.min = min;
this.max = max;
}
public void addKey(String s) {
keys.add(s);
}
public List<String> getDat() {
return keys;
}
public long getMin() {
return min;
}
public long getMax() {
return max;
}
public void setMin(long min) {
this.min = min;
}
public void setMax(long max) {
this.max = max;
}
public boolean isAsinable() {
return getIncrement() > 0;
}
public long getIncrement() {
double inc =
(getMax() - getMin())
/
(double)(keys.size() + 1);
return inc < 1 ? 0 : Math.round(inc);
}
public List<String> asigne() {
List<String> ret = new ArrayList<>();
long order = getMin();
final long inc = getIncrement();
for (String key : keys) {
order += inc;
ret.add(String.format("key=%s, val=%d", key, order));
}
return ret;
}
@Override
public String toString() {
return String.format("keys=%s, size=%d, min=%d, max=%d", keys.toString(), keys.size(), (int) min, (int) max);
}
}
private static void logBlock(String test, Block block) {
log("*+ " + test + " **");
log(block.toString());
log(block.isAsinable());
if (block.isAsinable()) {
log(block.asigne());
}
}
public static void log(Object obj) {
System.out.println(obj.toString());
}
}
@naoto-ogawa
Copy link
Author

*+ 01 **
keys=[A], size=1, min=10, max=15
true
[key=A, val=13]
*+ 02 **
keys=[A, B], size=2, min=10, max=15
true
[key=A, val=12, key=B, val=14]
*+ 03 **
keys=[A, B, C], size=3, min=10, max=15
true
[key=A, val=11, key=B, val=12, key=C, val=13]
*+ 04 **
keys=[A, B, C, D], size=4, min=10, max=15
true
[key=A, val=11, key=B, val=12, key=C, val=13, key=D, val=14]
*+ 05 **
keys=[A, B, C, D, E], size=5, min=10, max=15
false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment