Advertisement
applescript_superior

Arithmetic Sequential Extrapolator v1.1.0

Nov 16th, 2020
3,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Copyright (c) 2020 applescript_superior
  2. # All Rights Reserved
  3. # This product is protected by copyright and distributed under licenses restricting copying, distribution and decompilation.
  4. # Unauthorized distribution of this software and/or any participles pertaining to it will result in a fine (not actually lol).
  5. # Links:
  6. #       https://www.reddit.com/r/applescript/comments/jpu9nn/how_to_determine_if_all_items_in_a_list_are_the/
  7. #       https://macscripter.net/viewtopic.php?id=48011
  8. #       https://apple.stackexchange.com/questions/405923/how-to-determine-if-all-items-in-a-list-are-the-same
  9. # Created with AppleScript 2.7
  10.  
  11. # globals every variable used to prevent errors throughout the script
  12. global common_dif
  13. global common_dif_check
  14. global dif_check
  15. global extrap_num
  16. global input_sequence
  17. global local_dif
  18. global occurrence
  19. global selected_item
  20. global sequence
  21. global x
  22.  
  23. set title to "Arithmetic Sequential Extrapolator"
  24. global title
  25.  
  26. my input()
  27.  
  28. on input()
  29.     # input sequence of numbers
  30.     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
  31.    
  32.     # sets sequence to result of string_to_list
  33.     set sequence to my string_to_list(input_sequence, ",")
  34.     if ((number of items of sequence) < 2) or ((number of characters of input_sequence) < 3) then
  35.         my input()
  36.     end if
  37.    
  38.     try
  39.         set extrap_num to (last item of sequence) as integer
  40.     on error
  41.         my input()
  42.     end try
  43.     set sequence to reverse of (rest of (reverse of sequence))
  44.     my extrap_sequence()
  45. end input
  46.  
  47. on extrap_sequence()
  48.     # determines local difference between every item of sequence
  49.     set x to 0
  50.     set dif_check to {}
  51.     set common_dif_check to true
  52.     repeat ((number of items of sequence) - 1) times
  53.         try
  54.             set x to (x + 1)
  55.             set selected_item to (item x of sequence)
  56.             set local_dif to ((item (x + 1) of sequence) - selected_item)
  57.            
  58.             # determines if the selected item equals the rest of dif_check
  59.             # dissect this code later
  60.             if (dif_check ≠ {}) and (dif_check does not contain local_dif) then
  61.                 set common_dif_check to false
  62.                 set common_dif to "n/a"
  63.                 exit repeat
  64.             end if
  65.             set common_dif to (local_dif) as number
  66.             set dif_check to dif_check & {local_dif}
  67.         on error
  68.             my input()
  69.         end try
  70.     end repeat
  71.    
  72.     # determines answer and returns result depending on the input parameters
  73.     if common_dif_check = true then
  74.         set answer to my arithmetic_progression(sequence, extrap_num, common_dif)
  75.         return ("item " & extrap_num & " of sequence is " & answer & "; common difference is " & common_dif) as string
  76.         (* set action to button returned of (display alert extrap_num & "ᵃ = " & answer message "Common Difference: " & common_dif buttons {"Close", "OK"} default button 2)
  77.         if action = "Close" then
  78.             my quit_script()
  79.         else if action = "OK" then
  80.             my input()
  81.         end if *)
  82.     else if common_dif_check = false then
  83.         return ("item " & extrap_num & " of sequence is undefined; common difference is " & common_dif) as string
  84.         (* set action to button returned of (display alert extrap_num & "ᵃ = undefined" message "Common Difference: " & common_dif buttons {"Close", "OK"} default button 2) as string
  85.         if action = "Close" then
  86.             my quit_script()
  87.         else if action = "OK" then
  88.             my input()
  89.         end if *)
  90.     end if
  91. end extrap_sequence
  92.  
  93. on arithmetic_progression(sequence, extrap_num, common_dif)
  94.     # formula for arithmetic progression of linear sequences
  95.     set answer to ((first item of sequence) + ((extrap_num - 1) * common_dif)) as number
  96.     return answer
  97. end arithmetic_progression
  98.  
  99. on string_to_list(input, input_delimiter)
  100.     # separates string into individual items using string delimiters
  101.     set old_delimiters to AppleScript's text item delimiters
  102.     set AppleScript's text item delimiters to input_delimiter
  103.     set sequence to every text item of input
  104.     set AppleScript's text item delimiters to old_delimiters
  105.     return sequence
  106. end string_to_list
  107.  
  108. on quit_script()
  109.     # just a handler to safely quit the script
  110. end quit_script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement