Pergunta de entrevista da empresa Tesla

Given a phone number, write an algorithm to print all possible alpha-codes that correspond to it.

Respostas da entrevista

Sigiloso

20 de jan. de 2018

import itertools def get_combos(phone_num): key_chars = { '1':'', '2':'abc', '3':'def', '4':'ghi', '5':'jkl', '6':'mno', '7':'prqs', '8':'tuv', '9':'wxyz', '0':'' } return list(itertools.product(*filter(lambda x:len(x) > 0, [key_chars[i] for i in str(phone_num)])))

Sigiloso

16 de fev. de 2018

Dictionary> keyPad = new Dictionary> { {'2', new List{ 'A', 'B', 'C' }}, {'3', new List{ 'D', 'E', 'F' }}, {'4', new List{ 'G', 'H', 'I' }}, {'5', new List{ 'J', 'K', 'L' }}, {'6', new List{ 'M', 'N', 'O' }}, {'7', new List{ 'P', 'Q', 'R', 'S' }}, {'8', new List{ 'T', 'U', 'V' }}, {'9', new List{ 'W', 'X', 'Y', 'Z' }} }; static void Main() { PrintWords("02356894"); } void PrintWords(string number) { var wordBuffer = new char[number.Length]; DoPrint(wordBuffer, number, 0, 0); } void DoPrint(char[] wordBuffer, string number, int currentDigitIndex, int writeIndex) { if (currentDigitIndex == number.Length) { Console.WriteLine(new string(wordBuffer)); return; } if (number[currentDigitIndex] == '0' || number[currentDigitIndex] == '1') { wordBuffer[writeIndex] = number[currentDigitIndex]; writeIndex ++; DoPrint(wordBuffer, number, currentDigitIndex + 1, writeIndex); return; } foreach (var c in keyPad[number[currentDigitIndex]]) { wordBuffer[currentDigitIndex] = c; writeIndex ++; DoPrint(wordBuffer, number, currentDigitIndex + 1, writeIndex); writeIndex --; } }

Sigiloso

27 de ago. de 2020

This is a m/e question from leetcode

Sigiloso

13 de jun. de 2016

Or in Python you could do this: def phone_combos(num): # num would be, for instance, '327' key_pad = {'2' : ['a','b','c'], '3' : ['d','e','f'], '4' : ['g','h','i'], '5' : ['j','k', 'l'], '6' : ['m', 'n', 'o'], '7' : ['p', 'q', 'r','s'], '8' : ['t', 'u', 'v'], '9' : ['w', 'x', 'y', 'z']} result = [[]] arg = [key_pad[num[i]] for i in range(len(num))] for pool in arg: result = [x+[y] for x in result for y in pool] return result