The Coding Puzzle 🧩
Last week’s puzzle:
Given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn’t contain a binary gap.
Given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5.
Given N = 32 the function should return 0, because N has binary representation ‘100000’ and thus no binary gaps.
Solution for last week’s problem:
N = 1041 br = str(bin(N))[2:] def solution(N, br): br_group = False br_highest = 0 bin_zero_counter = 0 for char in br: if char == '1': br_group = False bin_zero_counter = 0 elif char == '0': br_group = True bin_zero_counter += 1 if br_highest < bin_zero_counter: br_highest = bin_zero_counter print('Binary output is: ', br, 'highest gap is: ', br_highest) if N==32: print('100000 has no binary gaps') else: solution(N, br)
This week’s puzzle:
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
class Solution: def removeDuplicates(self, nums: List[int]) -> int:
Input: nums = [1,1,2] Output: 2, nums = [1,2,_ ] Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4,,,,,_] Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
Developer Shares & Headlines 📢
Want to recommend someone for our developer spotlight, developer event, or just say hello? Send us an email at firstname.lastname@example.org we want to hear from you!
The DLU Team