Decoding a JSON without keys in Swift 4

If the structure stays the same, you can use this Decodable approach. First create a decodable Model like this: struct MyModel: Decodable { let firstString: String let stringArray: [String] init(from decoder: Decoder) throws { var container = try decoder.unkeyedContainer() firstString = try container.decode(String.self) stringArray = try container.decode([String].self) } } Or if you really want to … Read more

Given an array, can I find in O(n) the longest range, whose endpoints are the greatest values in the range?

A simple stack based solution. Iterate over the array left to right, with the stack holding elements (technically, indexes, but use values for comparisons) that are either: The largest from the left (i.e. no larger or equal element between the start of the array and the element) The largest since the previous element on the … Read more

A data structure supporting O(1) random access and worst-case O(1) append?

There is a beautiful structure called an extendible array that has worst-case O(1) insertion and O(n) memory overhead (that is, it’s asymptotically comparable to a dynamic array, but has O(1) worst-case insertion). The trick is to take the approach that the vector uses – doubling and copying – but to make the copying lazy. For … Read more

Accessing a JSON object in Bash – associative array / list / another model

If you want key and value, and based on How do i convert a json object to key=value format in JQ, you can do: $ jq -r “to_entries|map(\”\(.key)=\(.value|tostring)\”)|.[]” file SALUTATION=Hello world SOMETHING=bla bla bla Mr. Freeman In a more general way, you can store the values into an array myarray[key] = value like this, just … Read more

Smallest number that cannot be formed from sum of numbers from array

There’s a beautiful algorithm for solving this problem in time O(n + Sort), where Sort is the amount of time required to sort the input array. The idea behind the algorithm is to sort the array and then ask the following question: what is the smallest positive integer you cannot make using the first k … Read more

tech