Belatedly, a Balanced Brackets tester, in Python:
https://raw.githubusercontent.com/patchcali/toy-programs-gpl/master/test_brackets_balanced.py
The number and nature of the compromises I have to make when choosing a programming language for every goddamn project really gets to me
Except for C. To every use-case which C is suitable, it has never been a disappointment to me.
Python Solution
def snail_sort(matrix):
new_list = []
while matrix:
new_list.extend(matrix.pop(0))
if not matrix:
continue
for i in range(len(matrix)):
new_list.append(matrix[i].pop())
if not matrix:
break
if not matrix:
continue
new_list.extend(reversed(matrix.pop()))
if not matrix:
continue
for i in range(len(matrix) - 1, -1, -1):
new_list.append(matrix[i].pop(0))
if not matrix:
break
return new_list
MATRIX = [
[1, 2, 3, 4, 5],
[16, 17, 18, 19, 6],
[15, 24, 25, 20, 7],
[14, 23, 22, 21, 8],
[13, 12, 11, 10, 9],
]
snail_sort(MATRIX)
I borrowed this one from codewars
Snail Sort
Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.
array = [[1,2,3],
[4,5,6],
[7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]
For better understanding, please follow the numbers of the next array consecutively:
array = [[1,2,3],
[8,9,4],
[7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]
This image will illustrate things more clearly:
NOTE: The idea is not sort the elements from the lowest value to the highest; the idea is to traverse the 2-d array in a clockwise snailshell pattern.
Octave solution
function sorted = snail(array)
if numel(array) <= 1
sorted = array;
else
sorted = [array(1, 1:(end-1)),...
array(1:(end-1), end)',...
array(end, end:-1:2),...
array(end:-1:2, 1)',...
snail(array(2:(end-1), 2:(end-1))) ]; end; end;
For those of you who missed the announcement from the @freemo account just wanted to reiterate it here.
In honor of our new Indian friends we added two new emoji to the server.
Also if anyone has any emoji they want added, even if its just for personal use (like a monogram of your name or a picture of your own face, anything) we can add it. Just send a 50kb or less PNG image to @freemo and it can be added.
I borrowed this one from codewars
Snail Sort
Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.
array = [[1,2,3],
[4,5,6],
[7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]
For better understanding, please follow the numbers of the next array consecutively:
array = [[1,2,3],
[8,9,4],
[7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]
This image will illustrate things more clearly:
NOTE: The idea is not sort the elements from the lowest value to the highest; the idea is to traverse the 2-d array in a clockwise snailshell pattern.
@Absinthe I am learning about it and want to shift my career to business analytics. Right now, I am working as sports analyst.
@Absinthe where are the data science people? Any idea?
Here's a freebie!
This problem was asked by Google.
Given a list of integers S and a target number k, write a function that returns a subset of S that adds up to k. If such a subset cannot be made, then return null.
Integers can appear more than once in the list. You may assume all numbers in the list are positive.
For example, given S = [12, 1, 61, 5, 9, 2] and k = 24, return [12, 9, 2, 1] since it sums up to 24.
okay, this is not exactly a #toyprogrammingchallenge but I hope you hate this problem as much as I did...
The problem is not just getting the right solution, but getting it in under the running timeout of 12000 ms. Good luck.
Here's a #toyprogrammingchallenge requiring a bit of algebra.
For any four non-collinear points A, B, C, and D in three-dimensional space, there is a unique hyperbolic paraboloid H containing the lines AB, BC, CD, and AD (that is, every point on any of these lines is also on the surface of H). Write a program that accepts Cartesian coordinate triplets representing A, B, C, and D and prints an equation in x, y, and z that is satisfied if and only if <x, y, z> is a point on H.
Your program's output may differ from the examples but should be optimised for human readability. Combine like terms, omit terms equal to zero and avoid unnecessary factors (e.g. prefer "x = y" to "2x + 0 = 4y - 2y" even though both describe the same surface).
Example 1 input:
<0, 1, -1>; <1, 0, 1>; <0, -1, -1>; <-1, 0, 1>
Example 1 output:
z = x² - y²
Example 2 input:
<1, 1, 1>; <1, 0, -1>; <-1, 1, -1>; <-1, 0, 1>
Example 2 output:
2y = xz + 1
Example 3 input:
<0, 1, 1>; <0, 1, -1>; <0, -1, -1>; <0, -1, 1>
Example 3 output:
x = 0
Octave solution & discussion
Pretty straightforward. Might be possible to improve on the factorial one by counting factors, because computers can generally calculate x^n faster than just multiplying it out, so if you figure out that the answer would be 2^a * 3^b * 5^c... it could outpace the naive 1 * 2 * 3 * ... * n method.
That said, I tried such an approach and while it does scale better than linear, the upfront cost is high enough that the breakeven point is way past anything my computer can represent in floating point. So for practical use, this is what I'd put forward.
function term = fibonacci(index)
root = sqrt(5);
term = (((1 + root)/2).^index - ((1 - root)/2).^index) ./ root; end;
function number = factorial(argument)
number = ones(size(argument));
for factor = 1:max(argument)
number(argument >= factor) *= factor; end; end;
Kind of feels like I am phoning this one in, but I really liked this puzzle and hadn't really seen it before. However, if you have already seen this (and it is likely that you have) feel free to just ignore it.
Given a short message containing no more than 10 unique letters
written in the form of a simple equasion. Show the numeric values
for the letters to make it true. No letter that starts a word can
have the value Zero.
For example:
I + LOVE + YOU == DORA
1 + 2784 + 975 == 3760
Here is an interesting that is true in both cases, and will have only one solution:
ONE + TWO + FIVE + NINE + ELEVEN + TWELVE + FIFTY = NINETY
Still trying to find a style i like for the double L... Thoughts on this one?