Show newer

@alex I trust you don't eat almonds or other almond products either

@Absinthe ancient abbreviations? and I was hoping they were some cool french words...

Here's a python solution 

Need a better solution for deserialization than eval()

git.qoto.org/Absinthe/serializ

Show thread


Here's another freebie, I assume it is python specific because they start with a base of python code. But if it makes sense, try it in whatever language you like:

This problem was asked by Google.

Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.

For example, given the following Node class

class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
The following test should pass:

node = Node('root', Node('left', Node('left.left')), Node('right'))
assert deserialize(serialize(node)).left.left.val == 'left.left'

@namark what are the inplications of "cons" "car" and "cdr"? What are these terms suggesting?


Here's another freebie, I assume it is python specific because they start with a base of python code. But if it makes sense, try it in whatever language you like:

This problem was asked by Google.

Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.

For example, given the following Node class

class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
The following test should pass:

node = Node('root', Node('left', Node('left.left')), Node('right'))
assert deserialize(serialize(node)).left.left.val == 'left.left'


Here's a Freebie!

This problem was asked by Stripe.

Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.

For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.

You can modify the input array in-place.

re: Solution 

@billstclair I am not sure I am seeing what power they are trying to illustrate...

re: Solution 

@billstclair yep, that is what it seems. But what is the use/benefit of this technology?

#python #toyprogrammingchallenge Here is a problem that even though the answer seemed easy, I don't understand what it is supposed to be teaching, or what's going on. Anyone care to explain this problem to me? 

# This problem was asked by Jane Street.
#
# cons(a, b) constructs a pair,
# and car(pair) and cdr(pair) returns the first and last element of that
# pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4))
# returns 4.
#
# Given this implementation of cons:
#
# def cons(a, b):
# def pair(f):
# return f(a, b)
# return pair
#
# Implement car and cdr

def cons(a, b):
def pair(f):
return f(a, b)
return pair

def car(pair):
def f(a, b):
return a
return pair(f)

def cdr(pair):
def f(a, b):
return b
return pair(f)

print cdr(cons(3,4))
print car(cons(3,4))

@Absinthe
other product using division
git.sr.ht/~namark/mercury_stuf
hate all the edge cases

no division version in the working, hopefully would be much smoother.

#toyprogrammingchallenge #mercurylang

@namark Now I really wish I had gotten the compiler installed. :) We'll see what happens during the week.

re: A Python solution 

@billstclair @zingbretsen excellent! I can't wait.

re: A Python solution 

@Absinthe @zingbretsen

I'm going to do the next one in Elm, working example published at https://ellie-app.com

#toyprogrammingchallenge

re: A Python solution 

@billstclair @zingbretsen right, so I had to do more than just run it :) It just gave me some funtions to call. I got it now.

re: A Python solution 

@Absinthe @zingbretsen

Works for me:

wws@Xossbow:~/lisp/toy-programming-challenge$ git remote -v origin https://github.com/billstclair/toy-programming-challenge.git (fetch) origin https://github.com/billstclair/toy-programming-challenge.git (push) wws@Xossbow:~/lisp/toy-programming-challenge$ ls beer.lisp excluded-product.lisp LICENSE README.md zero-to-nine.lisp wws@Xossbow:~/lisp/toy-programming-challenge$ gcl GCL (GNU Common Lisp) 2.6.12 CLtL1 Oct 29 2015 23:21:28 Source License: LGPL(gcl,gmp), GPL(unexec,bfd,xgcl) Binary License: GPL due to GPL'ed components: (XGCL READLINE UNEXEC) Modifications of this banner must retain notice of a compatible license Dedicated to the memory of W. Schelter Use (help) to get some basic information on how to use GCL. Temporary directory for compiler files: /tmp/ >(load "excluded-product.lisp") Loading excluded-product.lisp Finished loading excluded-product.lisp T >(apropos "excluded" *package*) EXCLUDED-PRODUCT Function EXCLUDED-PRODUCT-WITH-ROTATION Function EXCLUDED-PRODUCT-WITHOUT-DIVISION Function >(excluded-product '(1 2 3 4)) (24 12 8 ...) >*print-length* 3 >(setf *print-length* nil) NIL >*** (24 12 8 6) >(excluded-product-with-rotation '(1 2 3 4)) (24 12 8 6) >

Show older
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.