# Problem 1 (easy): first unique character in a string input_string = 'treetraversal' output = 'v' # Answer def first_unique(input_string): count_map = {} for letter in input_string: if letter not in count_map: count_map[letter] = 1 else: count_map[letter] += 1 for letter in input_string: if count_map[letter] == 1: return letter print(first_unique(input_string)) # Problem 2 (easy): Group Anagrams entries = ['star', 'astr', 'car', 'rac', 'st'] output = [['star', 'astr'], ['car', 'rac'], ['st']] # Answer def group_anagrams(entries): anagram_map = {} for entry in entries: sorted_entry = ''.join(sorted(entry)) if sorted_entry not in anagram_map: anagram_map[sorted_entry] = [entry] else: anagram_map[sorted_entry].append(entry) return anagram_map.values() print(group_anagrams(entries)) # Problem 3 (medium): Design a OOP class for imaginary number arithmetic # Notes that (write this out in math term on the whiteboard) # a + b = (a.r + b.r) + (a.i + b.i)i # a - b = (a.r - b.r) + (a.i - b.i)i class Complex(object): def __init__(self, r, i): self.r = r self.i = i def __str__(self): '''Returns complex number as a string''' return '%s + %si' % (self.r, self.i) def __add__(self, other): '''Adds complex numbers''' return Complex(self.r + other.r, self.i + other.i) def __sub__(self, other): '''Adds complex numbers''' return Complex(self.r - other.r, self.i - other.i) def __mul__(self, other): '''Multiplies numbers: Provide this (ac - bd) + (ad + bc)i''' return Complex( self.r * other.r - self.i * other.i, self.i * other.r - self.r * other.i, ) def __abs__(self): '''absolute value of self''' return (self.r**2 + self.i**2)**0.5 a = Complex(1, 2) b = Complex(2, 3) print(a) print(b) print(a + b) print(a - b) print(a * b) print(abs(a)) # Problem 4 (SQL): # Given a table (name, component, salary) with sample records: # chris, bonus, 1000 # chris, bonus, 500 (there can be multiple entries for bonuses) # chris, base, 10000 # adam, bonus, 5000 # adam, bonus, 50 # adam, base, 30000 # ... # Write SQL to return result as (name, bonus_salary, base_salary) # # Answer # SELECT # name, # SUM(CASE WHEN component = "bonus" THEN salary ELSE 0 END) AS "bonus_salary" # SUM(CASE WHEN component = "base" THEN salary ELSE 0 END) AS "base_salary" # FROM table # GROUP BY name; # # How would you do this in a scripting language e.g. Python. You may assume records is array<Map<string, mixed>> # # Solution: data = [ {'name': 'chris', 'component': 'bonus', 'salary': 1000}, {'name': 'chris', 'component': 'bonus', 'salary': 500}, {'name': 'chris', 'component': 'base', 'salary': 10000}, {'name': 'adam', 'component': 'bonus', 'salary': 5000}, {'name': 'adam', 'component': 'bonus', 'salary': 50}, {'name': 'adam', 'component': 'base', 'salary': 30000}, ] salary_map = {} for d in data: key = (d['name'], d['component']) if key not in salary_map: salary_map[key] = d['salary'] else: salary_map[key] += d['salary'] for (name, component), salary in salary_map.items(): print(name, component, salary)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question