Range modifications: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
|||
Line 54: | Line 54: | ||
remove 9 |
remove 9 |
||
remove 7 |
remove 7 |
||
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library] |
|||
=={{header|AutoHotkey}}== |
|||
<lang AutoHotkey>RangeModifications(arr, Modify, v){ |
|||
global steps ; optional line to track examples steps |
|||
if (Modify = "add") |
|||
arr.push([v, v]) |
|||
if (Modify = "remove") |
|||
for i, obj in arr |
|||
if (v >= (start := obj.1)) && (v <= (stop := obj.2)) |
|||
{ |
|||
arr.RemoveAt(i) |
|||
if (start = v) && (v = stop) |
|||
continue |
|||
arr.push(start < v ? [start, v-1] : [v+1, v+1]) |
|||
arr.push(v < stop ? [v+1, stop] : [v-1, v-1]) |
|||
} |
|||
result := RangeConsolidation(arr) |
|||
steps .= Modify "`t" v "`t:`t" obj2string(result) "`n" ; optional line to track examples steps |
|||
return result |
|||
} |
|||
RangeConsolidation(arr){ ;-) borrowed my own function from http://www.rosettacode.org/wiki/Range_consolidation#AutoHotkey |
|||
arr1 := [], arr2 := [], result := [] |
|||
for i, obj in arr |
|||
arr1[i,1] := min(arr[i]*), arr1[i,2] := max(arr[i]*) ; sort each range individually |
|||
for i, obj in arr1 |
|||
if (obj.2 > arr2[obj.1]) |
|||
arr2[obj.1] := obj.2 ; creates helper array sorted by range |
|||
i := 1 |
|||
for start, stop in arr2 |
|||
if (i = 1) || (start > result[i-1, 2] + 1) ; first or non overlapping range |
|||
result[i, 1] := start, result[i, 2] := stop, i++ |
|||
else ; overlapping range |
|||
result[i-1, 2] := stop > result[i-1, 2] ? stop : result[i-1, 2] |
|||
return result |
|||
} |
|||
obj2string(arr){ |
|||
for i, obj in arr |
|||
str .= obj.1 "-" obj.2 "," |
|||
return Trim(str, ",") |
|||
} |
|||
string2obj(str){ |
|||
arr := [] |
|||
for i, v in StrSplit(str, ",") |
|||
x := StrSplit(v, "-"), arr.push([x.1, x.2]) |
|||
return arr |
|||
}</lang> |
|||
Examples:<lang AutoHotkey>arr := string2obj("") |
|||
steps .= "start with`t:`t" obj2string(arr) "`n" |
|||
arr := RangeModifications(arr, "Add", 77) |
|||
arr := RangeModifications(arr, "Add", 79) |
|||
arr := RangeModifications(arr, "Add", 78) |
|||
arr := RangeModifications(arr, "Remove", 77) |
|||
arr := RangeModifications(arr, "Remove", 78) |
|||
arr := RangeModifications(arr, "Remove", 79) |
|||
MsgBox % steps |
|||
steps := "" |
|||
arr := string2obj("1-3,5-5") |
|||
steps .= "start with`t:`t" obj2string(arr) "`n" |
|||
arr := RangeModifications(arr, "Add", 1) |
|||
arr := RangeModifications(arr, "Remove", 4) |
|||
arr := RangeModifications(arr, "Add", 7) |
|||
arr := RangeModifications(arr, "Add", 8) |
|||
arr := RangeModifications(arr, "add", 6) |
|||
arr := RangeModifications(arr, "Remove", 7) |
|||
MsgBox % steps |
|||
steps := "" |
|||
arr := string2obj("1-5,10-25,27-30") |
|||
steps .= "start with`t:`t" obj2string(arr) "`n" |
|||
arr := RangeModifications(arr, "Add", 26) |
|||
arr := RangeModifications(arr, "Add", 9) |
|||
arr := RangeModifications(arr, "Add", 7) |
|||
arr := RangeModifications(arr, "Remove", 26) |
|||
arr := RangeModifications(arr, "Remove", 9) |
|||
arr := RangeModifications(arr, "Remove", 7) |
|||
MsgBox % steps |
|||
return</lang> |
|||
{{out}} |
|||
<pre>start with : |
|||
Add 77 : 77-77 |
|||
Add 79 : 77-77,79-79 |
|||
Add 78 : 77-79 |
|||
Remove 77 : 78-79 |
|||
Remove 78 : 79-79 |
|||
Remove 79 : |
|||
start with : 1-3,5-5 |
|||
Add 1 : 1-3,5-5 |
|||
Remove 4 : 1-3,5-5 |
|||
Add 7 : 1-3,5-5,7-7 |
|||
Add 8 : 1-3,5-5,7-8 |
|||
add 6 : 1-3,5-8 |
|||
Remove 7 : 1-3,5-6,8-8 |
|||
start with : 1-5,10-25,27-30 |
|||
Add 26 : 1-5,10-30 |
|||
Add 9 : 1-5,9-30 |
|||
Add 7 : 1-5,7-7,9-30 |
|||
Remove 26 : 1-5,7-7,9-25,27-30 |
|||
Remove 9 : 1-5,7-7,10-25,27-30 |
|||
Remove 7 : 1-5,10-25,27-30</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |