Hudson River Trading | HRT | Code Screen | Code Signal
Anonymous User
2050
Jan 19, 2023
2 hours, 10 minutes
"""
When a user submits code, each submission has its own
scoreboard organized by programming language.
Solutions in these scoreboards are sorted by their length.
When the length of a solution is calculated, comments and spaces are
ignored. Implement a simplified version of this length
calculator.
Assume that there are only two types of comments:
* line-comment: starting with '//' and ending at the end of the line:
* block-comment: starting with '/*' and ending with the first occurrence of
'*/'. It may span several lines.
Each non-space character outside comments adds 1 to the total length.
Note that all characters inside each comment are ignored, so they don't introduce nested comments.
See the examples for more details.
Also note that outside any other comment both '//' and '/*' start a new comment
(i.e. in this task you don't have to consider the cases where '//', '/*', or '*/' appear inside a string literal).
"""
def solution(source: List[str]):
block_start = '/*'
block_end = '*/'
line_comment = '//'
num_chars = 0
inside_block = False
for line in source:
if len(line) == 0:
continue
idx = 0
line_length = len(line)
chars_in_line = 0
while idx < line_length:
next_two_chars = line[idx:idx+2]
if inside_block == False and next_two_chars == line_comment:
break
if next_two_chars == block_start and not inside_block:
idx += 1
inside_block = True
elif next_two_chars == block_end and inside_block:
idx += 1
inside_block = False
elif not inside_block and line[idx] != " ":
chars_in_line += 1
if not inside_block:
num_chars += chars_in_line
return num_chars
"""
You have a string s that represents a chemical reaction.
The chemical reaction is a string in the following format:
molecule (+ molecule)* = molecule (+ molecule)*
where "*" means any non-negative number of repeats.
For example,
"A = B"
"A = B + C"
"A + 2B + C = D + E + F"
A molecule is concatenation of a coefficient (optional) and a string that represents the concatenation of elements and their coefficents (optional).
A coefficent is a positive integer that doesn't exceed 1000, and it represents the number of repeats. For example,
12Cu means 12 repeats of Cu element.
"6H20"
means 6 repeats of "H" element and 3 repeats of
"O" elements: 3 is a coefficient of the "H2o"
molecule and 2 is a coefficient of the "H" element.
Determine whether the equation is balanced.
"""
import collections
def solution(source):
sides = source.split("=")
left = sides[0]
right = sides[1]
return atom_counts(left) == atom_counts(right)
def atom_counts(string):
molecules = string.split("+")
counter = collections.Counter()
for molecule in molecules:
molecule = molecule.strip()
molecule_count = 1
left_idx = 0
idx = 0
size = len(molecule)
while idx < size and molecule[idx].isnumeric():
idx += 1
if idx != left_idx:
molecule_count = int(molecule[left_idx : idx])
while idx < size:
left_idx = idx
while idx < size and molecule[idx].isalpha():
idx += 1
atom_name = molecule[left_idx : idx]
leftStart = idx
while idx < size and molecule[idx].isnumeric():
idx += 1
if idx == leftStart:
atom_count = 1
else:
atom_count = int(molecule[left_idx : idx])
counter[atom_name] += atom_count * molecule_count
return counter
"""
Given a string, determine how many ways we could add one letter to the string so that
it is the permutation of some palindrome.
"""
def solution(source):
counter = collections.Counter(source)
num_ways = 1 if is_a_palindrome(counter) else 0
for char in "abcdefghijklmnopqrstuvwxyz":
counter[char] += 1
num_ways += is_palindrome(counter)
counter[char] -= 1
return num_ways
def is_a_palindrome(counter):
return sum(1 for value in counter.values() if value % 2 == 1) <= 1Comments (2)


