Posted to tcl by sevarat at Thu Oct 24 23:32:02 GMT 2024view raw

  1. #!/bin/tclsh
  2.  
  3. array set alnum {
  4. 2 abc
  5. 3 def
  6. 4 ghi
  7. 5 jkl
  8. 6 mno
  9. 7 pqrs
  10. 8 tuv
  11. 9 wxyz
  12. }
  13.  
  14. set numbers [split [lindex $argv 0] {}]
  15. set letters [lmap n $numbers {split [lindex [array get alnum $n] 1] ""}]
  16.  
  17. proc letterMap {x y} {
  18. return [lmap a $x {lmap b $y {string cat $a $b}}]
  19. }
  20.  
  21. proc phoneLetters {letterList} {
  22. if {[llength $letterList] < 2} {return [join $letterList]}
  23. return [phoneLetters [list [join [letterMap {*}[lrange $letterList 0 1]]] {*}[lrange $letterList 2 end]]]
  24. }
  25.  
  26. puts [phoneLetters $letters]
  27.