Re: How to calculate/process this one?
Re: How to calculate/process this one?
- Subject: Re: How to calculate/process this one?
- From: Arthur J Knapp <email@hidden>
- Date: Sat, 27 Apr 2002 12:42:09 -0400
>
Date: Sat, 27 Apr 2002 14:10:01 +0200
>
Subject: How to calculate/process this one?
>
From: Martin Michel <email@hidden>
>
I have a problem thinking of a routine which does the following:
>
Let's say I have a random number like 11050. Now I want to add each pair
>
of consecutive digits to give the next line (so 11050 becomes 2155). I
>
want to repeat this step until I have a two-digit number.
I don't understand, how does 11050 become 2155 ???
Adding pairs of consecutive digits would go something like this:
1 1 0 5 0
1 + 1 = 2, 0 + 5 = 5, 0
Concatente: "2" & "5" & "0" = "250"
2 + 5 = 7, final result: 70
set x to 11050
-- to string
--
set s to "" & x --> does not take scientific notation into account
-- to list
--
set a to s's items --> {"1", "1", "0", "5", "0"}
tell a -- save some typing: "item x" instead of "item x of a"
-- We save a virtual length, that represents the last item to
-- be processed for each inner loop:
--
set len to length -- len will be the last "unprocessed" item
-- We stop when there are only 2 items left that can be
-- processed:
--
repeat until len = 2
-- We keep an index to a "save position", where the sum of
-- the current 2 digits gets placed. This can be visualized:
--
-- add 1 + 2 save into j
-- { 1, 2, 3, 4, 5 } --> { 3, 2, 3, 4, 5 }
--
-- add 3 + 4 save into j
-- { 3, 2, 3, 4, 5 } --> { 3, 7, 3, 4, 5 }
--
set j to 1 --> track the last "processed" item
repeat with i from 2 to len by 2 -- by 2 because of even/odd
-- Every 2 pairs are added and then placed into the save
-- position j. We use "0 + " because initially each item
-- is a string.
--
set item j to 0 + (item (i - 1)) + (item i)
-- Increament save position:
--
set j to j + 1
end repeat
-- If the current len was odd, then there was an odd man out,
-- so we just move him to the next position j
--
if (len mod 2 = 1) then
set item j to item len
-- Our current length then becomes j, ie:
--
set len to j
else
set len to j - 1 --> adjust for final increment in loop
end if
end repeat
-- String-concatenate, then coerce to number:
--
set v to 0 + ("" & item 1 & item 2)
end tell
v --> 70, a 2 digit number
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
<
mailto:email@hidden>
try
<
http://www.facespan.com/>
on error number -128
end try
}
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.