Show newer

@Absinthe I have hardly any experience with Python but I think you ought to write your own parser that reports an error on input that can't represent an actual tree (this is what I did in my solution, anyway). The other issue is making sure you escape any special characters in Node.val so your parser doesn't treat them as control characters.

@namark @Absinthe

CONS = construct
CAR = Contents of Address Register
CDR = Contents of Decrement Register

Assembly language instructions for one of the first lisp interpreters. I don’t know which machine.

@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'


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.

#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

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 

@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) >

Good morning and here comes the latest challenge.

git.qoto.org/Absinthe/onetonin

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().

A Python solution 

@Absinthe Here is my no-division solution:

```
#!/usr/bin/env python
from collections import deque
from functools import reduce
from random import randint

def get_product_and_rotate(d):
"""Gets product of all but the first items in the queue"""
prod = reduce(lambda a, b: a * b, list(d)[1:])
d.rotate(-1) # Rotates left so that the next
return prod

if __name__ == "__main__":
n_items = randint(2, 10)
input_list = [randint(0, 9) for _ in range(n_items)]
print(input_list)
product_queue = deque(input_list)
print([get_product_and_rotate(product_queue) for _ in input_list])
```

Uses `reduce` for the multiplication, and uses a `deque` (double-ended queue) to rotate the list. I've read that using a deque is actually more efficient than just taking slices out of the list.

@zingbretsen care to take a shot at a fix for my one-line solution in this one?

Show thread

I found a corner case on the find sum in list that I overlooked. If the list is [1,2,5] and the number is 4 I will return true because I am looking through the whole list for K-element and will of course find element. I was so happy to get a single line that worked, I am wondering if I can fix it and still keep it one line?

git.qoto.org/Absinthe/sum-of-n

I know count may work better than any, or some other logic to handle the special case of K-element == element

@Absinthe That sounds very fair. I'll submit am example using a `deque` later today!

Okay, here is another freebie :) I will put in a real one before the end of the weekend.

Basically, you are given a list from which you need to create a new list where each element is the product of all the "OTHER" elements.

I found a few interesting corner cases.

I challenge you to give it a try!

Read the challenge here:

git.qoto.org/Absinthe/productn

My attempt is checked into that repo as well.

Here's another freebie!! I signed up for a programming problem of the day. I got my first one today, rated "easy".

Here's the description of the problem.

git.qoto.org/Absinthe/sum-of-n

My code to solve it, though not clean and delicious, is in that same repo, but certainly give it a crack before peeking. :)

Show older
Qoto Mastodon

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