[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: String Manipulation Challenge

William James

11/16/2015 4:53:00 PM

D. Herring wrote:

> > the instructions say that the caller should use the digits on the
> > phone keypad to enter numbers and letters. However, each letter must
> > be preceded with an asterisk.
> >
> > So, for example, if my account number is "1234", I would simply enter
> > "1" "2" "3" "4". If my account number was G3W70, I would enter "*" "4"
> > "3" "*" "9" "7" "0".
> >
> > Now, when searching for the account in the system, the system should
> > search for all possible matches and so the account entered into the
> > IVR, would need to be converted to an list of account numbers.
> >
> > So, if the caller entered 1234, the possible account numbers to search
> > is (1234). However, if the caller entered *43*970, the possible
> > account numbers to search is (G3W70, H3W70, I3W70, G3X70, H3X70,
> > I3X70, G3Y70, H3Y70, I3Y70, G3Z70, H3Z70, I3Z70).
> >
> > So, the question is: how to efficient transform the user's input into
> > a list of possible matches by expanding the digits into the
> > corresponding letters (1=1, 2=A|B|C, 3=D|E|F, 4=GHI, 5=JKL, 6=MNO,
> > 7=PQRS, 8=TUV, 9=WXYZ, 0=0) and doing all corresponding combinations
> > of those digits/letters to come up with all strings?
>
> Here's a first stab:
>
> (defun letters (number)
> "North American Classic as per http://dialabc.com/motion/keypads....
> (case number
> (#\1 (signal :no-letter-assigned))
> (#\2 '(#\a #\b #\c))
> (#\3 '(#\d #\e #\f))
> (#\4 '(#\g #\h #\i))
> (#\5 '(#\j #\k #\l))
> (#\6 '(#\m #\n))
> (#\7 '(#\p #\r #\s))
> (#\8 '(#\t #\u #\v))
> (#\9 '(#\w #\x #\y))
> (t (signal :not-a-number))))
>
> (defun translate (keys)
> "translate a string of keystrokes into all possible accounts"
> (let ((accounts (list ""))
> (escape nil))
> (dotimes (n (length keys))
> (let ((k (char keys n)))
> (if escape
> (progn
> (let ((tmp (list)))
> (dolist (a accounts)
> (dolist (l (letters k))
> (push (concatenate 'string
> a (string l))
> tmp)))
> (setf accounts tmp))
> (setf escape nil))
> (cond
> ((digit-char-p k)
> (setf accounts
> (mapcar
> (lambda (a)
> (concatenate 'string
> a
> (string k)))
> accounts)))
> ((eql k #\*)
> (setf escape t))
> (t (signal :unhandled-key k))))))
> accounts))

MatzLisp (Ruby):

@letters = Hash[2,"ABC", 3,"DEF", 4,"GHI", 5,"JKL", 6,"MNO",
7,"PQRS", 8,"TUV", 9,"WXYZ"]

def possibles(input)
head, *tail = input.scan(/\*?\d/).
map{|s| n = s[-1].to_i
"*" == s[0] ? @letters[n].split("") : [n]}
head.product(*tail).map &:join
end

possibles("2345")
==>["2345"]
possibles("*43*970")
==>["G3W70", "G3X70", "G3Y70", "G3Z70", "H3W70", "H3X70", "H3Y70",
"H3Z70", "I3W70", "I3X70", "I3Y70", "I3Z70"]

--
The struggle of our time is to concentrate, not to dissipate: to renew our
association with traditional wisdom: to re-establish a vital connection between
the individual and the race. It is, in a word, a struggle against Liberalism.
--- T. S. Elliot
1 Answer

The Peeler

6/6/2013 10:01:00 PM

0

On Thu, 6 Jun 2013 23:30:56 +0200, The Peeler
<finishingoff@themoronicRevd.invalid> wrote:

>On Thu, 06 Jun 2013 04:35:48 -0700, The Rectum, the resident psychopath of
>sci and scj, FAKING his time zone again and IMPERSONATING his master, The
>Peeler, wrote:
>
>>>????? ??? ?????? ????? ??? ???? ?????? ?????? ??????? ??????? ???? ??????? ??? ?????? ????? ?? ?????? ???? ????
>>> ?????? ???? ???? ???????? ??? ????? ???? ????? ????? ????? ?? ??? ???? ?????
>>>
>>>Spirit Lord Yahweh upon me, Yahweh anointed me.
>>
>> Did, he Rogering? Lucky, YOU! <G>
>
>He obviously SHIT upon ME, The Rectum! Lucky ME! <BG>

Lucky you innit Grik anus! <GB>