Author Topic: Missing in a sequence  (Read 1679 times)

0 Members and 1 Guest are viewing this topic.

velasquez

  • Newt
  • Posts: 195
Missing in a sequence
« on: July 04, 2014, 08:16:54 AM »
Can anyone tell me how to find the first value that is missing in a sequence from the list below?
Code: [Select]
'("1" "2" "3" "?" "7" "8")

Thanks,
velasquez

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Missing in a sequence
« Reply #1 on: July 04, 2014, 08:32:45 AM »
Code - Auto/Visual Lisp: [Select]
  1. (setq l'("1" "2" "3" "?" "7" "8"))
  2. (car(acad_strlsort l))

velasquez

  • Newt
  • Posts: 195
Re: Missing in a sequence
« Reply #2 on: July 04, 2014, 09:21:41 AM »
Code - Auto/Visual Lisp: [Select]
  1. (setq l'("1" "2" "3" "?" "7" "8"))
  2. (car(acad_strlsort l))

Sorry ElpanovEvgeniy,

I used the symbol "?" wrongly.
What I need to figure out is the amount to complete the sequence.
For example "4"

Thanks

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Missing in a sequence
« Reply #3 on: July 04, 2014, 11:26:50 AM »
First I did not understand the task...

Code - Auto/Visual Lisp: [Select]
  1. (defun test (l / f)
  2.   ;; (test '("1" "2" "3" "?" "7" "8"))
  3.   (defun f (a b)
  4.     ;; test sequence function for '("1" "2" "3" "?" "7" "8")
  5.     (if (/= (1+ (atoi a)) (atoi b))
  6.       (itoa (1+ (atoi a)))
  7.     )
  8.   )
  9.   (cond ((not (cdr l)) nil)
  10.         ((f (car l) (cadr l)))
  11.         ((test (cdr l)))
  12.   )
  13. )

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Missing in a sequence
« Reply #4 on: July 04, 2014, 02:24:27 PM »
First you need to identify if the sequence is regular or irregular.  The former can be distilled into a function for generating successive numbers.  The latter cannot so will require some sort of lookup table.  In your given example, the sequence is irregular since you only specify one missing entry.  For it to be regular there would have to be multiple missing entries and an identified function.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Missing in a sequence
« Reply #5 on: July 05, 2014, 09:45:32 AM »
Another couple:
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (vl-some '(lambda ( a b ) (if (/= (1+ (atoi a)) (atoi b)) (itoa (1+ (atoi a))))) l (cdr l))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (setq l (mapcar 'atoi l))
  3.     (vl-some '(lambda ( a b ) (if (/= a b) (itoa a))) (mapcar '1+ l) (cdr l))
  4. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (f '("1" "2" "3" "?" "7" "8"))
  2. "4"

And another way to write Evgeniy's:
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (if (cadr l)
  3.         (if (= (1+ (atoi (car l))) (atoi (cadr l)))
  4.             (f (cdr l))
  5.             (itoa (1+ (atoi (car l))))
  6.         )
  7.     )
  8. )

Or a generalised function:
Code - Auto/Visual Lisp: [Select]
  1. (defun missing ( l f )
  2.     (vl-some '(lambda ( a b / c ) (if (/= b (setq c (f a))) c)) l (cdr l))
  3. )
  4. (missing '("1" "2" "3" "?" "7" "8") (lambda ( x ) (itoa (1+ (atoi x)))))
« Last Edit: July 05, 2014, 10:01:14 AM by Lee Mac »

velasquez

  • Newt
  • Posts: 195
Re: Missing in a sequence
« Reply #6 on: July 07, 2014, 01:39:31 PM »
Many thanks for all the answers.