Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #58996

Rosetta: CUSIP

From "Robert L." <No_spamming@noWhere_7073.org>
Newsgroups comp.lang.forth
Subject Rosetta: CUSIP
Date 2017-05-10 17:02 +0000
Organization Aioe.org NNTP Server
Message-ID <oevh3k$le7$1@gioia.aioe.org> (permalink)

Show all headers | View raw


> A  CUSIP  is a nine-character alphanumeric code that
> identifies a North American financial security for the
> purposes of facilitating clearing and settlement of trades.
> The CUSIP was adopted as an American National Standard under
> Accredited Standards X9.6.
> 
> Task
> 
> Ensure the last digit (i.e., the check digit) of the
> CUSIP code (the 1st column) is correct, against the following:
> 
>   037833100       Apple Incorporated 
>   17275R102       Cisco Systems 
>   38259P508       Google Incorporated 
>   594918104       Microsoft Corporation 
>   68389X106       Oracle Corporation   (incorrect)
>   68389X105       Oracle Corporation
> 
> Example pseudo-code below.
> 
> algorithm Cusip-Check-Digit(cusip) is
>    Input: an 8-character CUSIP
>  
>    sum := 0
>    for 1 = i = 8 do
>       c := the ith character of cusip
>       if c is a digit then
>          v := numeric value of the digit c
>       else if c is a letter then
>          p := ordinal position of c in the alphabet (A=1, B=2...)
>          v := p + 9
>       else if c = "*" then
>          v := 36
>       else if c = "@" then
>          v := 37
>       else if' c = "#" then
>          v := 38
>       end if
>       if i is even then
>          v := v * 2
>       end if
>  
>       sum := sum + int ( v div 10 ) + v mod 10
>    repeat
>  
>    return (10 - (sum mod 10)) mod 10
> end function


(use srfi-42)

(define (char-value chr i)
  (define v
    (* (if (odd? i) 2 1)
      (cond
        ((string->number (string chr))) 
        ((char-upper-case? chr)
         (- (char->integer chr) (char->integer #\A) -10))
        ((char=? chr #\*) 36)
        ((char=? chr #\@) 37)
        ((char=? chr #\#) 38)
        (else (error "Invalid char. in CUSIP" chr)))))
  (+ (fx/ v 10) (modulo v 10)))

(define (check-cusip str)
  (define v
    (sum-ec
      (:string chr (index i) (substring str 0 8))
      (char-value chr i)))
  (= (string->number (substring str 8 9))
     (modulo (- 10 (modulo v 10)) 10)))

(do-ec
  (:list s '("037833100" "17275R102" "38259P508" "594918104" "68389X106"
             "68389X105"))
  (print s " " (check-cusip s)))

037833100 #t
17275R102 #t
38259P508 #t
594918104 #t
68389X106 #f
68389X105 #t

In Forth?


-- 
Goyim were born only to serve us....  They will work, they will plow, they will
reap. We will sit like an effendi and eat. --- Rabbi Ovadia Yosef
web.archive.org/web/20101020044210/http://www.jpost.com/JewishWorld/JewishNews/Article.aspx?id=191782
archive.org/download/DavidDuke_videos/TopRabbiExposesJewishRacism-cybsdwjezqi.ogv

Back to comp.lang.forth | Previous | Next | Find similar | Unroll thread


Thread

Rosetta: CUSIP "Robert L." <No_spamming@noWhere_7073.org> - 2017-05-10 17:02 +0000

csiph-web