isaacg1 / pyth

Pyth, an extremely concise language. Try it here:
https://pyth.herokuapp.com/
MIT License
263 stars 57 forks source link

Missing help on lambdas/map functions in documentation #164

Closed Pwntheon closed 8 years ago

Pwntheon commented 8 years ago

I've been trying to learn Pyth, and so far i've completed a couple of golfing challenges. However i've arrived at a place where i can't seem to progress because i (think i) need to use map and/or lambda and i can't figure out how.

So i have this list of numbers in an array, representing stock prices over time. I need to find the maximum profit you can make by buying at one point and selling at a later point, only once.

In pseudocode i want to do this: for each number in the list: (this is where i think map might help) add the difference between this number and each of the remaining numbers to a list

sort the list, output tail.

I tried using a loop, but couldn't figure out how to get the index so i could compare to "the rest of the list". I figured i might use map, but i can't seem to figure out how to use it, and whether i had to define a function first (and how).

refi64 commented 8 years ago

Have you tried using the debug mode? It prints the Python code that Pyth generates; I personally find it really helpful.

The docs say for m:

Python 2 map. Int as second arg -> range(). implicit lambda, d -> k -> b ->

That means that map's works just like in Python, but the lambda is implicit; it takes an argument d. The ->s are for if you have nested maps; the outermost will take d, the next will take k, etc.

For instance, this:

mdQ

is compiled to:

imp_print(Pmap(lambda d:Z,urange(T)))

Maybe you can see how it works now.

Note that, in your example, this isn't that helpful on its own. I feel like Pyth has a builtin that returns a list of tails, but I can't find it now...

Pwntheon commented 8 years ago

Thank you kirby, i will try to use the debug mode. I assume that doesn't work in the online implementation?

refi64 commented 8 years ago

It sure does! Just check the Debug on? check box.

Pwntheon commented 8 years ago

Ah, didn't see that initially. Thanks a lot. Now onwards to learn Python :)

isaacg1 commented 8 years ago

Also, check out https://pyth.readthedocs.org for more of a tutorial.