Page 1 of 1

Get Gaps between numbers

Posted: 28 Nov 2022, 11:01
by HiSoKa
i have this variable,

Code: Select all

var =
(
1
5
9
1
2
3
4
6
)
How can I get the numbers that do not exist between the largest number and the smallest number in var, And Determine which is the largest and which is the smallest (between the numbers which are not exist)
in my example should i get 7 and 8
smallest is 7
largest is 8

Re: Get Gaps between numbers

Posted: 28 Nov 2022, 11:30
by mikeyww

Code: Select all

list =
(
1
5
9
1
2
3
4
6
)
arr := StrSplit(list, "`n"), arr2 := [], low := low(arr)
Loop, % high(arr) - low - 1
 n := low + A_Index, (!Instr("`n" list "`n", "`n" n "`n")) && arr2.Push(n)
min := low(arr2), max := high(arr2)
MsgBox, 64, Result, Largest = %max%`n`nSmallest = %min%

high(arr) {
 For each, num in arr
  n := n > "" ? Max(n, num) : num
 Return n
}

low(arr) {
 For each, num in arr
  n := n > "" ? Min(n, num) : num
 Return n
}

Re: Get Gaps between numbers  Topic is solved

Posted: 28 Nov 2022, 11:43
by Rohwedder
Hallo,
try:

Code: Select all

var =
(
1
5
9
1
2
3
4
6
)
Sort, var, N U
RegExMatch(var, "^(\d+).*?(\d+)$", M)
Loop,% M2-M1-1
	IF !Instr(Var, "`n" N := A_Index+M1 "`n")
		Out .= N
MsgBox,% Out

Re: Get Gaps between numbers

Posted: 28 Nov 2022, 11:46
by mikeyww
That's a nice one. High and low could also be parsed from the output string.

Re: Get Gaps between numbers

Posted: 28 Nov 2022, 11:47
by HiSoKa
nice, Thanks you mikeyww and Rohwedder

Re: Get Gaps between numbers

Posted: 14 Dec 2022, 09:44
by HiSoKa
Rohwedder wrote:
28 Nov 2022, 11:43
Hallo,
try:

Code: Select all

var =
(
1
5
9
1
2
3
4
6
)
Sort, var, N U
RegExMatch(var, "^(\d+).*?(\d+)$", M)
Loop,% M2-M1-1
	IF !Instr(Var, "`n" N := A_Index+M1 "`n")
		Out .= N
MsgBox,% Out
H̶i̶ ̶R̶o̶h̶w̶e̶d̶d̶e̶r̶,̶ ̶S̶o̶r̶r̶y̶ ̶f̶o̶r̶ ̶t̶h̶e̶ ̶i̶n̶c̶o̶n̶v̶e̶n̶i̶e̶n̶c̶e̶,̶
B̶u̶t̶ ̶I̶ ̶a̶m̶ ̶f̶a̶c̶i̶n̶g̶ ̶a̶ ̶p̶r̶o̶b̶l̶e̶m̶ ̶w̶i̶t̶h̶ ̶t̶h̶i̶s̶ ̶c̶o̶d̶e̶,̶
I̶t̶ ̶d̶o̶e̶s̶ ̶n̶o̶t̶ ̶s̶h̶o̶w̶ ̶m̶e̶ ̶a̶ ̶[̶c̶]̶1̶[̶/̶c̶]̶ ̶w̶h̶e̶n̶ ̶i̶t̶ ̶i̶s̶ ̶n̶o̶t̶ ̶i̶n̶ ̶t̶h̶e̶ ̶l̶i̶s̶t̶,̶ ̶A̶s̶ ̶i̶t̶ ̶i̶s̶ ̶s̶u̶p̶p̶o̶s̶e̶d̶ ̶t̶o̶ ̶b̶e̶

Edit:
Sorry Rohwedder, this is my fault. I must now add the number 0 at the beginning of the list to get the gaps that fit the code I'm working on..
The code works like a charm, But it was my fault, Because I did not understand the basic function of the code, which is to get between the gaps between the lowest number and the highest number in the list, and not, as I thought, between the 0 number to the largest number in the list
Thank you again

Re: Get Gaps between numbers

Posted: 14 Dec 2022, 17:29
by Chunjee
for fun, with arrays:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

var =
(
1
5
9
1
2
0
3
4
6
)

arr := strSplit(var, "`n", "`r")
gaps := A.difference(A.range(A.min(arr), A.max(arr)), arr)
; => [7, 8]

https://biga-ahk.github.io/biga.ahk/#/?id=difference