Author Topic: ( Challenge ) How many 'carrys'?  (Read 6528 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) How many 'carrys'?
« Reply #15 on: March 18, 2009, 02:59:09 PM »
python 3.x:

Code: [Select]
def carries (a, b):
    return (a%10 + b%10)//10 + carries (a//10, b//10) if a else 0

is cleaner, but may execute slower than:

Code: [Select]
def carries (a, b):
    return 0 if not a else (a%10 + b%10)//10 + carries (a//10, b//10)

or

Code: [Select]
def test (a, b):
    if a: return (a%10 + b%10)//10 + carries (a//10, b//10)           
    return 0
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: ( Challenge ) How many 'carrys'?
« Reply #16 on: March 18, 2009, 03:26:09 PM »
ElpanovEvgeniy,
(test1 99 11) -> 1
should be 2

My strategy was unsuccessful...

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: ( Challenge ) How many 'carrys'?
« Reply #17 on: March 18, 2009, 03:33:52 PM »
Code: [Select]
(defun test1 (a b) (test-lst (list a b)))(test1 99 11) => 2

uncoolperson

  • Guest
Re: ( Challenge ) How many 'carrys'?
« Reply #18 on: March 18, 2009, 03:37:44 PM »
not pretty, but I had fun

Code: [Select]
(DEFUN carry (num1 num2)
  (SETQ carried 0
tocarry 0
index 0
str1 (ITOA num1)
str2 (ITOA num2)
  )
  (SETQ list1 (REVERSE
(MAPCAR '(LAMBDA (someitem) (ATOI (CHR someitem)))
(VL-STRING->LIST str1)
)
      )
  )
  (SETQ list2 (REVERSE
(MAPCAR '(LAMBDA (someitem) (ATOI (CHR someitem)))
(VL-STRING->LIST str2)
)
      )
  )
  (WHILE (< index (MAX (LENGTH list1) (LENGTH list2)))
    (IF (NOT
  (ZEROP
    (SETQ tocarry (REM (+ (NTH index list1) (NTH index list2) tocarry)
       10
  )
    )
  )
)
      (SETQ carried (1+ carried))
    )
    (SETQ index (1+ index))
  )
)


and again (same idea, kinda but funner)

Code: [Select]
(DEFUN carry (num1 num2)
  (SETQ carried 0
tocarry 0
index 0
str1 (ITOA num1)
str2 (ITOA num2)
  )
  (SETQ list1 (REVERSE
(MAPCAR '(LAMBDA (someitem) (ATOI (CHR someitem)))
(VL-STRING->LIST str1)
)
      )
  )
  (SETQ list2 (REVERSE
(MAPCAR '(LAMBDA (someitem) (ATOI (CHR someitem)))
(VL-STRING->LIST str2)
)
      )
  )
  (dowork list1 list2 0)
)


(DEFUN dowork (list1 list2 carry)
  (COND ((AND list1 list2)
(+ (COND ((> (SETQ tocarry (/ (+ (CAR list1) (CAR list2) carry) 10))
      0
   )
   1
  )
  (T 0)
    )
    (dowork (CDR list1) (CDR list2) tocarry)
)
)
(list1 (dowork list1 '(0) carry))
(list2 (dowork '(0) list2 carry))
(T 0)
  )
)
« Last Edit: March 18, 2009, 04:07:15 PM by uncoolperson »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: ( Challenge ) How many 'carrys'?
« Reply #19 on: March 18, 2009, 03:48:45 PM »
Code: [Select]
(defun test-lst (l)
 (if (cdr l)
  ((lambda (a) (+ a (test-lst (vl-remove 0 (cons a (mapcar '(lambda (a) (/ a 10)) l))))))
   (/ (apply '+ (mapcar '(lambda (a) (rem a 10)) l)) 10)
  )
  0
 ) ;_  if
) ;_  defun
(defun test2 (/ f l)
 (setq f (open "D:\\random_ints.txt" "r"))
 (while (car (setq l (cons (read-line f) l))))
 (close f)
 (setq f (open "D:\\carry.txt" "a"))
 (foreach a (reverse (cdr l)) (write-line (itoa (test-lst (read (strcat "(" a ")")))) f))
 (close f)
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ( Challenge ) How many 'carrys'?
« Reply #20 on: March 18, 2009, 04:52:19 PM »
My kludge.  :-)
Code: [Select]
(defun carries (a b / c clast)
  (setq clast 0
        c 0)
  (and (= (type a) 'INT) (setq a (float a)))
  (and (= (type b) 'INT) (setq b (float b)))
  (mapcar
    (function
      (lambda(m n)
        (if (> (+ m n clast -96) 9)
          (setq c (1+ c)
                clast 1)
          (setq clast 0)
        )
       )
      )
    (reverse(vl-remove 46(vl-string->list(rtos a 2 10))))
    (reverse(vl-remove 46(vl-string->list(rtos b 2 10))))
    )
  c
)

test code:
Code: [Select]
(defun c:test ()
  (princ (carries 9999 8888))           ;#=> 4
  (princ (carries 1111 8888))           ;#=> 0
  (princ (carries 56 45))               ;   #=> 2
  (princ (carries 11 89))               ;   #=> 2
  (princ (carries 11 79))               ;    1
  (princ (carries 99 11))               ;  2
  (princ (carries 99.795 11.54))        ;  4
  (princ)
)
« Last Edit: March 18, 2009, 05:43:58 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

SomeCallMeDave

  • Guest
Re: ( Challenge ) How many 'carrys'?
« Reply #21 on: March 18, 2009, 05:07:56 PM »
Revised Ruby
Code: [Select]
def count_the_carry2(string1, string2)
  #assumes strings are same length and numeric
  array1 = string1.split(//).collect{|x| x.to_i}.reverse!
  array2 = string2.split(//).collect{|x| x.to_i}.reverse!
  carry = 0
  carry_count = 0
  array1.size.times {|i|
    sum = array1[i]+array2[i]
    carry_count += ((sum+carry) >= 10 ? 1 : 0 )
    carry = (sum >= 10 ? 1 : 0)
  }
  carry_count
end


Tests
Code: [Select]
puts count_the_carry2("9999", "8888")   #=> 4
puts count_the_carry2("1111", "8888")   #=> 0
puts count_the_carry2("56", "45")          #=> 2
puts count_the_carry2("11", "89")          #=> 2
puts count_the_carry2("11", "79")          #=> 1

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: ( Challenge ) How many 'carrys'?
« Reply #22 on: March 18, 2009, 05:18:09 PM »
Hi,

LISP
Code: [Select]
(defun test (a b)
  (if (zerop a)
    0
    ((lambda (d / c)
       (+ c (/ d 10) (test d (/ b 10)))
     )
      (+ (/ a 10) (setq c (/ (+ (rem a 10) (rem b 10)) 10)))
    )
  )
)

C#
Code: [Select]
public static int test(int a, int b)
        {
            int c = (a % 10 + b % 10) / 10, d = a / 10 + c;
            return (a == 0) ? 0 : c + d / 10 + test(d , b / 10);
        }
« Last Edit: March 18, 2009, 06:18:59 PM by gile »
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: ( Challenge ) How many 'carrys'?
« Reply #23 on: March 19, 2009, 01:35:42 AM »
Hi gile! :-)

(test 11 89) => ; error: bad argument type: numberp: nil

 :-(

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: ( Challenge ) How many 'carrys'?
« Reply #24 on: March 19, 2009, 01:40:24 AM »
Hi CAB! :)

(carries 9 91) => 1

:(

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: ( Challenge ) How many 'carrys'?
« Reply #25 on: March 19, 2009, 02:09:01 AM »
Corrected (I hope...)

LISP
Code: [Select]
(defun test (a b / c d)
  (if (zerop a)
    0
    (progn
      (setq c (/ (+ (rem a 10) (rem b 10)) 10)
    d (/ a 10)
      )
      (+ c (test d c) (test (+ d c) (/ b 10)))
    )
  )
)

C#
Code: [Select]
public static int test(int a, int b)
        {
            int c = (a % 10 + b % 10) / 10, d = a / 10;
            return (a == 0) ? 0 : c + test(d, c) + test(d + c , b / 10);
        }
« Last Edit: March 19, 2009, 02:55:57 AM by gile »
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: ( Challenge ) How many 'carrys'?
« Reply #26 on: March 19, 2009, 03:31:21 AM »
Good code gile!

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: ( Challenge ) How many 'carrys'?
« Reply #27 on: March 19, 2009, 04:59:15 AM »
Not recursion...
Code: [Select]
(defun carries (a b / c i)
 (setq c 0 i 0)
 (while (not (zerop (+ a b)))
  (setq c (/ (+ (rem a 10) (rem b 10) c) 10)
        i (+ c i)
        a (/ a 10)
        b (/ b 10)
  )
 )
 i
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: ( Challenge ) How many 'carrys'?
« Reply #28 on: March 19, 2009, 09:03:11 AM »
Hi CAB! :)

(carries 9 91) => 1

:(
Thanks for testing.
Code: [Select]
(defun carries (a b / c clast)
  (setq clast 0
        c 0)
  (and (= (type a) 'INT) (setq a (float a)))
  (and (= (type b) 'INT) (setq b (float b)))
  (setq a (reverse(vl-remove 46(vl-string->list(rtos a 2 10)))))
  (setq b (reverse(vl-remove 46(vl-string->list(rtos b 2 10)))))
  (if (> (length a)(length b)) ; add leading zero
    (setq a (append a (list 48)))
    (setq b (append b (list 48)))
  )
  (while (> (length a)(length b)) (setq b (append b (list 48))))
  (while (> (length b)(length a)) (setq a (append a (list 48))))
  (mapcar
    (function
      (lambda(m n)
        (if (> (+ m n clast -96) 9)
          (setq c (1+ c) clast 1)
          (setq clast 0)
        )
       )
      )
    a b
    )
  c
)

Code: [Select]
(defun c:test ()
  (print (carries 9999 8888))           ;  4
  (print (carries 1111 8888))           ;  0
  (print (carries 56 45))               ;  2
  (print (carries 11 89))               ;  2
  (print (carries 11 79))               ;  1
  (print (carries 99 11))               ;  2
  (print (carries 99.795 11.54))        ;  4
  (print (carries 99 1))                ;  2
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.