CoCalc Shared Filesalgorithm_for_read_to.sagewsOpen in CoCalc with one click!
Authors: Tim Clemans SMC, John Jeng, Harald Schilly, ℏal Snyder, William A. Stein
Views : 16
# Given say {"a": 3, "b": 10, "c": 7, "d": 19} and [1,5,15] return {0: ["a"], 1: ["b", "c"], 2: ["d"]} def find_read_to(read_dates, message_dates): read_indexes_to_user_ids = {} for user_id in read_dates: for i, date in enumerate(message_dates): last_index = i + 1 == len(message_dates) next_index = -1 if last_index else i + 1 if last_index or (read_dates[user_id] >= date and read_dates[user_id] < message_dates[next_index]): if i in read_indexes_to_user_ids: read_indexes_to_user_ids[i].append(user_id) else: read_indexes_to_user_ids[i] = [user_id] break return read_indexes_to_user_ids find_read_to({"a": 3, "b": 10, "c": 7, "d": 19}, [1,5,15])
{0: ['a'], 1: ['c', 'b'], 2: ['d']}
import bisect def find_read_to(read_dates, message_dates): read_indexes_to_user_ids = {} user_ids, read_dates = zip(*read_dates.items()) def find_between(read_date): answer = bisect.bisect_left(message_dates, read_date) answer -= 1 if answer == -1: return None return answer indexes_for_read_up_to = map(find_between, read_dates) for i, index_for_read_up_to in enumerate(indexes_for_read_up_to): user_id = user_ids[i] if index_for_read_up_to in read_indexes_to_user_ids: read_indexes_to_user_ids[index_for_read_up_to].append(user_id) else: read_indexes_to_user_ids[index_for_read_up_to] = [user_id] return read_indexes_to_user_ids find_read_to({"a": 3, "b": 10, "c": 7, "d": 19}, [1,5,15])
{0: ['a'], 1: ['c', 'b'], 2: ['d']}
%coffee underscore = require('underscore') Array::bisect = (val) -> idx = undefined if @length == 0 return 0 idx = 0 while idx < @length if val < @[idx] return idx idx++ idx find_read_tos = (read_dates, message_dates) -> read_indexes_to_user_ids = {} [user_ids, read_dates] = [Object.keys(read_dates), underscore.values(read_dates)] find_between = (read_date) -> answer = message_dates.bisect(read_date) answer -= 1 if answer == -1 return null return answer indexes_for_read_up_to = (find_between(date) for date in read_dates) for index_for_read_up_to, i in indexes_for_read_up_to user_id = user_ids[i] if index_for_read_up_to is null continue if String(index_for_read_up_to) in Object.keys(read_indexes_to_user_ids) read_indexes_to_user_ids[index_for_read_up_to].push(user_id) else read_indexes_to_user_ids[index_for_read_up_to] = [user_id] return read_indexes_to_user_ids console.log(find_read_tos({"a": 3, "b": 10, "c": 7, "d": 19}, [1,5,15]))
{ '0': [ 'a' ], '1': [ 'b', 'c' ], '2': [ 'd' ] }
%coffee array_bisect = (arr, val) -> idx = undefined if arr.length == 0 return 0 idx = 0 while idx < arr.length if val < arr[idx] return idx idx++ idx console.log(array_bisect([0,3,5,15,30,90], 11))
3
coffee??
File: /projects/d8eac431-3b55-40d3-89ee-fcada259f1ad Unable to read source code (source code not available)
%coffee find_smaller_number = (arr, val) -> idx = undefined if arr.length == 0 return -1 idx = 0 while idx < arr.length if val < arr[idx] return idx - 1 idx++ idx - 1 console.log(find_smaller_number([], 9))
-1
%coffee _ = require('underscore') console.log(_.sortBy([5,3,9,1]))
[ 1, 3, 5, 9 ]