Thursday, July 19, 2018

#1 2003-04-29 12:39:26 pm

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Sum Integers by Hand

This handler will substract what AppleScript doesn't, but it will work only for positive numbers.
To make it work, you must provide a list of strings (numbers), without any limitation in its number of digits. You must do it in order (the bigger, the first).

OS version: OS X

Applescript:

substractIntegersByHand({"9876543534546432109876543210", "5654", "98765432109876543210"})

to substractIntegersByHand(string_list)
   --> reverse items to form columns
   repeat with i from 1 to string_list's length
       set string_list's item i to (reverse of (string_list's item i)'s items as text)
   end repeat
   
   set final_result to {}
   set current_digit to 1
   set the_rest to 0
   repeat
       try
           --> get column of current digit
           set digit_list to {}
           repeat with i in string_list
               try
                   set end of digit_list to (i's item current_digit as integer)
               end try
           end repeat
           
           if digit_list = {} then exit repeat
           
           --> sum rest + all digits, except for the first one
           set partial_result to 0
           repeat with i from 1 to count digit_list
               if i = 1 then -- es el primero
                   set partial_result to the_rest
               else
                   set partial_result to partial_result + (digit_list's item i)
               end if
           end repeat
           
           set partial_last_digit to item -1 of (partial_result as text) as integer
           set partial_firsts_digits to (reverse of (rest of (reverse of (items of (partial_result as text)))) as text)
           set bigger_ten to (partial_firsts_digits & digit_list's item 1) as integer
           if bigger_ten < partial_result then set bigger_ten to bigger_ten + 10
           set beginning of final_result to bigger_ten - partial_result
           set the_rest to bigger_ten div 10
           
           set current_digit to current_digit + 1
       on error msg
           exit repeat
       end try
   end repeat
   try
       repeat while item 1 of final_result is 0
           set final_result to rest of final_result
       end repeat
   on error
       set final_result to 0
   end try
   return final_result as text
end substractIntegersByHand


One mans trash is another mans treasure

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)