Get Gaps between numbers Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Get Gaps between numbers

Post by HiSoKa » 28 Nov 2022, 11:01

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

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Get Gaps between numbers

Post by mikeyww » 28 Nov 2022, 11:30

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
}

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Get Gaps between numbers  Topic is solved

Post by Rohwedder » 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
Last edited by Rohwedder on 29 Nov 2022, 01:38, edited 2 times in total.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Get Gaps between numbers

Post by mikeyww » 28 Nov 2022, 11:46

That's a nice one. High and low could also be parsed from the output string.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Get Gaps between numbers

Post by HiSoKa » 28 Nov 2022, 11:47

nice, Thanks you mikeyww and Rohwedder

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Get Gaps between numbers

Post by HiSoKa » 14 Dec 2022, 09:44

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

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Get Gaps between numbers

Post by Chunjee » 14 Dec 2022, 17:29

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

Post Reply

Return to “Ask for Help (v1)”