Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Copyright (c) 2020 applescript_superior
- # All Rights Reserved
- # This product is protected by copyright and distributed under licenses restricting copying, distribution and decompilation.
- # Unauthorized distribution of this software and/or any participles pertaining to it will result in a fine (not actually lol).
- # Links:
- # https://www.reddit.com/r/applescript/comments/jpu9nn/how_to_determine_if_all_items_in_a_list_are_the/
- # https://macscripter.net/viewtopic.php?id=48011
- # https://apple.stackexchange.com/questions/405923/how-to-determine-if-all-items-in-a-list-are-the-same
- # Created with AppleScript 2.7
- # globals every variable used to prevent errors throughout the script
- global common_dif
- global common_dif_check
- global dif_check
- global extrap_num
- global input_sequence
- global local_dif
- global occurrence
- global selected_item
- global sequence
- global x
- set title to "Arithmetic Sequential Extrapolator"
- global title
- my input()
- on input()
- # input sequence of numbers
- set input_sequence to text returned of (display dialog "Enter Linear Sequence of Numbers" default answer "1,3,5,7,9,50" with icon 1 buttons {"Cancel", "Extrapolate"} default button 2 with title title) as string
- # sets sequence to result of string_to_list
- set sequence to my string_to_list(input_sequence, ",")
- if ((number of items of sequence) < 2) or ((number of characters of input_sequence) < 3) then
- my input()
- end if
- try
- set extrap_num to (last item of sequence) as integer
- on error
- my input()
- end try
- set sequence to reverse of (rest of (reverse of sequence))
- my extrap_sequence()
- end input
- on extrap_sequence()
- # determines local difference between every item of sequence
- set x to 0
- set dif_check to {}
- set common_dif_check to true
- repeat ((number of items of sequence) - 1) times
- try
- set x to (x + 1)
- set selected_item to (item x of sequence)
- set local_dif to ((item (x + 1) of sequence) - selected_item)
- # determines if the selected item equals the rest of dif_check
- # dissect this code later
- if (dif_check ≠ {}) and (dif_check does not contain local_dif) then
- set common_dif_check to false
- set common_dif to "n/a"
- exit repeat
- end if
- set common_dif to (local_dif) as number
- set dif_check to dif_check & {local_dif}
- on error
- my input()
- end try
- end repeat
- # determines answer and returns result depending on the input parameters
- if common_dif_check = true then
- set answer to my arithmetic_progression(sequence, extrap_num, common_dif)
- return ("item " & extrap_num & " of sequence is " & answer & "; common difference is " & common_dif) as string
- (* set action to button returned of (display alert extrap_num & "ᵃ = " & answer message "Common Difference: " & common_dif buttons {"Close", "OK"} default button 2)
- if action = "Close" then
- my quit_script()
- else if action = "OK" then
- my input()
- end if *)
- else if common_dif_check = false then
- return ("item " & extrap_num & " of sequence is undefined; common difference is " & common_dif) as string
- (* set action to button returned of (display alert extrap_num & "ᵃ = undefined" message "Common Difference: " & common_dif buttons {"Close", "OK"} default button 2) as string
- if action = "Close" then
- my quit_script()
- else if action = "OK" then
- my input()
- end if *)
- end if
- end extrap_sequence
- on arithmetic_progression(sequence, extrap_num, common_dif)
- # formula for arithmetic progression of linear sequences
- set answer to ((first item of sequence) + ((extrap_num - 1) * common_dif)) as number
- return answer
- end arithmetic_progression
- on string_to_list(input, input_delimiter)
- # separates string into individual items using string delimiters
- set old_delimiters to AppleScript's text item delimiters
- set AppleScript's text item delimiters to input_delimiter
- set sequence to every text item of input
- set AppleScript's text item delimiters to old_delimiters
- return sequence
- end string_to_list
- on quit_script()
- # just a handler to safely quit the script
- end quit_script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement