Pergunta de entrevista da empresa Dropbox

given a number as a string write a algorithm to map to its oral description. I.e. "1" -> "11" //this can be thought of as there is one one. "11" -> "21" // there are two ones "21" -> "1211" // there is one two and one one "1211" -> "111221" ect. //there is one one, one two and two ones

Respostas da entrevista

Sigiloso

9 de abr. de 2012

Rio, your code breaks on your own provided example. The following code solves this: f = string[0] for i in range(0, len(string)): if string[i] == f: count = 1 if i == len(string)-1: sys.stdout.write("%d%s" %(count, f)) break while (string[i+1] == string[i]): count += 1 i += 1 if (i+1) >= len(string): break sys.stdout.write("%d%s" %(count, f)) if (i+1) >= len(string): break f = string[i+1] print "" I might have some extra unnecessary code and could definitely clean this up a little, but it's a working version.

1

Sigiloso

18 de out. de 2012

In PHP: function lookAndSay($number) { $len = strlen($number); $count = 1; $output = ""; for($i = 1; $i < $len; $i++) { if ($number{$i} === $number{$i-1}) { $count++; } else { $output .= $count.$number{$i-1}; $count = 1; } } $output .= $count.$number{$i-1}; return $output; }

Sigiloso

23 de jan. de 2014

Probably the simplest Python solution: from itertools import groupby def oral(num): return ''.join([str(len(list(j))) + str(i) for i, j in groupby(num)])

Sigiloso

27 de fev. de 2015

std::string oralDescription(std::string input) { if (input.size() == 0) { return ''; } std::string output; char c = input[0]; int pos = 0; int count = 1; while (pos < input.size()) { ++pos; while (pos < input.size() && input[pos] == c) { ++pos; ++count; } output += std::to_string(count) + c; if (pos < input.size()) { c = input[pos]; count = 1; } } return output; }

Sigiloso

5 de nov. de 2011

public static String expand(String input){ String output = ""; char prev = input.charAt(0); int count = 0; for(int i = 0; i = input.length()) output += count + "" + cur; } return output; }

Sigiloso

14 de nov. de 2011

The above implementation is incorrect. Firstly, for 11 it will return 2 for 1 because it counts the original. The following implementation in Python does the job nicely. def oralDescription(string): firstchar = string[0] count = 0 all = "" for i in range(0, len(string), 2): print "There are", string[i], "of", string[i+1] oralDescription("112111")

1