My personal take on the Google Foobar challenges.
Easy problem.
Solved by generating and storing each prime on a string, returning the
substring (n
to n+5
) once we have stored n+5
digits on the string,
since that's the number we're looking for.
algorithm prime_seq_substring is
input: Index N of first digit on the substring
output: String of 5 digits of prime numbers
for each prime in range[2..30000] do
concat prime to prime_seq
if length of prime_seq >= N+5 do
break
return prime_seq[N..N+5]
Another easy problem, and I had quite a lot of fun solving this in the least number of lines. Because... why not?.
Solved by splitting the string by the char <
and counting the number of times
the char >
appears on each element of the list.
Then, create a list with the indexes of the list but in descending order, since
the that's the number of times each employee on each section of the list will
face an employee walking on the other direction.
The last leg of the solution is, multiplying each element on the list with it's corresponding element on the list of indexes (this gives the number of encounters of each employee) and multiplying this number by two, since each encounter generates two salutes.
And we return the sum of all this numbers (the total number of salutes of all employees).
algorithms salutes_solution is
input: String S representing the employees
output: Number of salutes performed by the employees
for each section in (S split by '<') do
concat (number of '>' in section) to sec_list
index_list := (length of sec_list)..0
for I in range[0..(length of sec_list)] do
concat ( (elem I of sec_list) * (elem I of index_list) * 2) to salutes_list
return sum of elements in salutes_list