@namark what are the inplications of "cons" "car" and "cdr"? What are these terms suggesting?
#toyprogrammingchallenge
#python 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'
#toyprogrammingchallenge
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
https://git.sr.ht/~namark/mercury_stuff/tree/master/toys/other_product.m
hate all the edge cases
no division version in the working, hopefully would be much smoother.
@namark Now I really wish I had gotten the compiler installed. :) We'll see what happens during the week.
@freemo good look!
re: A Python solution
@billstclair @zingbretsen excellent! I can't wait.
re: A Python solution
re: A Python solution
@billstclair @zingbretsen got it
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
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)
>
@billstclair thanks!
#toyprogrammingchallenge
And here is my solution in python
https://git.qoto.org/Absinthe/onetonine/tree/master
Good morning and here comes the latest challenge.
https://git.qoto.org/Absinthe/onetonine/blob/master/README.md
The numbers 1 2 3 4 5 6 7 8 9 can have either the operator "+" or "-" placed between them.
As well, if no operator is placed, then the adjoining numbers go together such as 1 2 would
become "12" or 1 2 3 would become "123" and so forth.
The goal is to find the combinations that would make the line calculate up to 100 exactly.
Here is an example: 12 + 3 - 4 + 5 + 67 + 8 + 9 = 100
Would like maybe something other than eval().
re: A Python solution
@billstclair @zingbretsen I have access to gcl but it looks like I need more to make this function
@billstclair @freemo #toyprogrammingchallenge
I thought I sent this one to @freemo privately. :) Live and learn. This is going to be the one for this week.
I am going to try to run this, but just walking the code is showing
me more Lisp than I currently know. Although, I would have expected the Lisp to look shorter than my python .....
It does work with gcl from my command line :)
The green faerie