Last active
June 20, 2019 19:56
-
-
Save mburbea/1f673fb2f0e1a62c09f84d3e8185ef1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static Hashset<string> ValidWords; | |
public static IEnumerable<string> TranslateEntry(string input) | |
{ | |
var dialpad = new[] { | |
new char[0], | |
new char[0], | |
new[]{'a','b','c'}, | |
new[]{'d','e','f'}, | |
new[]{'g','h','i'}, | |
new[]{'j','k','l'}, | |
new[]{'p','q','r','s'}, | |
new[]{'t','u','v'}, | |
new[]{'w','x','y','z'} | |
}; | |
var candidates = input.Aggregate(new[] { "" }.AsEnumerable(), (results, elem) => results.SelectMany(c => dialpad[int.Parse(elem.ToString())].Select(e => c + e))); | |
return ValidWords.Intersect(candidates); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
import functools | |
T9 = ['','','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz'] | |
def T9Words(word:int)->List[str]: | |
return functools.reduce(lambda results,elem: [r+d for r in results for d in T9[int(elem)]],str(word),['']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or alter function T9Words(@input int) | |
returns table | |
with schemabinding as return | |
with t9 as ( | |
select i,d | |
from (values | |
(2,'a'),(2,'b'),(2,'c') | |
,(3,'d'),(3,'e'),(3,'f') | |
,(4,'g'),(4,'h'),(4,'i') | |
,(5,'j'),(5,'k'),(5,'l') | |
,(6,'m'),(6,'n'),(6,'o') | |
,(7,'p'),(7,'q'),(7,'r'),(7,'s') | |
,(8,'t'),(8,'u'),(8,'v') | |
,(9,'w'),(9,'x'),(9,'y'),(9,'z')) f(i,d)) | |
,rec as ( | |
select rem=@input,res=convert(varchar(8000),'') | |
union all | |
select rem=rem/10,res=d+res | |
from rec | |
join t9 z | |
on z.i = rem%10 | |
where rem != 0) | |
select res | |
from rec | |
where rem=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment