Pyth Practice 2



Time for another Pyth practice. I present here 8 problem statements with a Pyth solution each. These solutions are written by a Pyth beginner. He is quite happy about these solutions, since they are a lot shorter than his Python answers. Your task however is to show him better. Create equivalent but shorter programs.

This is a challenge about the the tricks and optimizations that can be used when golfing in Pyth. Pyth golfers may recognize many of the tricks involved, that lead to shorter solutions. However some problems will require some unusual approaches that are rarely used. Some of the tricks I've actually never seen in the wild. But no solution requires any bugs or strange behavior, that was not intentional by the designer(s) of Pyth. All answers must be valid for the most recent Pyth commit (2b1562b) as of this question's posting. You can use the Pyth interpreter for testing. It is up-to-date right now and I don't expect any big changes in Pyth, that will invalidate optimal solutions or make shorter solutions possible. The online interpreter also features the new Character Reference. Since it is pretty new, you can (should) also use the old docs, in case something is incorrect or missing.

Goal: The reference solutions total 81 bytes. Your goal is to beat that by as much as possible. The submission that solves all 8 problems with the smallest total number of bytes wins. Tiebreaker is the submission date.

Of course only submissions are valid, which contain solutions for all 8 problems. You can use the reference implementation, if you can't improve the score of one (or more) particular problem.

Your solutions must print the exact same output as the reference solutions. Except for an optional trailing newline.

Since this is a Pyth practice, only programs written in the language Pyth are allowed.

Answering: Please spoiler your entire answer, except for your total score. It is intended that you do not look at other people's answers before submitting your own. You can create spoilers by putting >! in front of every line, like:

>! Problem 1: V9m?>dNd0S9 (11 bytes)
>! Problem 2: VTN)VGN (7 bytes)
>! ...

I hope I haven't chosen too difficult or too trivial problems. Hoping for a lots of participators and for everyone to gain a few new insights into Pyth. Happy golfing!

Problem 1:

Create the following 9x9 matrix and print it:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 0, 3, 4, 5, 6, 7, 8, 9]
[0, 0, 0, 4, 5, 6, 7, 8, 9]
[0, 0, 0, 0, 5, 6, 7, 8, 9]
[0, 0, 0, 0, 0, 6, 7, 8, 9]
[0, 0, 0, 0, 0, 0, 7, 8, 9]
[0, 0, 0, 0, 0, 0, 0, 8, 9]
[0, 0, 0, 0, 0, 0, 0, 0, 9]

Reference solution (Link):

V9m?>dNd0S9 (11 bytes)

Problem 2:

Print all digits and all letters on separate lines:


Reference solution (Link):

VTN)VGN (7 bytes)

Problem 3:

Find the lexicographically smallest palindrome, that is lexicographically bigger or equal than an input string containing lowercase letters and is of the same than the input string.

a -> a
abc -> aca
adcb -> adda

Reference solution (Link):

hf&gTzqT_T^Glz (14 bytes)

Problem 4:

Check, if a number is in the range [0, input number). This should also work for floats.

4, 6 -> True
5.5, 6 -> True
6, 6 -> False
6, 6.1 -> True

Reference solution (Link):

&gQ0<QE (7 bytes)

The reference format is to be tested value<newline>end value. You can choose a different input format however. Important is only, that you accomplish the problem statement and produce the correct results.

Problem 5:

Parse an input string of the format "\d+[a-zA-Z]+". Notice that the number really has to be a number, not a string containing digits.

'123Test' -> [123, 'Test']

Reference solution (Link):

A.ggk\Az,sGH (12 bytes)

Problem 6:

Compute the sum of numbers, that are separated by one or multiple commas. You can assume that there is at least one number in the string.

11,2,,,3,5,,8 -> 29

Reference solution (Link):

svM:z",+"3 (10 bytes)

Problem 7:

Read positive integers from the input until you get the number 0. Print the sum of all numbers.

Reference solution (Link):

WJE=+ZJ)Z (9 bytes)

Problem 8:

Sum up all elements of a square matrix, except the ones of the main diagonal (left upper corner to right bottom corner).

Reference solution (Link):

-ssQs.e@bkQ (11 bytes)


Posted 2015-12-28T16:08:13.523

Reputation: 21 462

I really like the idea of language-specific questions (and I have upvoted this one). But I thought language-specific questions were frowned upon in PPCG? – Luis Mendo – 2015-12-28T17:21:43.817


@LuisMendo I think language-specific quesitions are only frowned upon, if the reason behind the question is a general dislike against golfing languages like CJam, Pyth, ..., or because the OP only understands certain languages. This here is a little different. This question is mainly designed to show some of the more hidden features of a popular (on PPCG) language. In a way it's like a collection of tricks. It doesn't make sence to allow it to every language. Here's a Meta-post about this topic: link

– Jakube – 2015-12-28T17:38:05.597

In 6, are the numbers positive single-digit integers? – xnor – 2015-12-28T18:15:15.230

@xnor You can assume that they are positive, but they might consist of more than one digits. I'll change the test case. – Jakube – 2015-12-28T18:22:52.347

@Jakube I see. Thanks for the link! – Luis Mendo – 2015-12-28T18:34:56.177

In 4, which one is the end value and which is the one being tested? – Maltysen – 2015-12-28T21:46:30.783

@Maltysen I took the second one as the end value and the first one as the one being tested. You can change the order though. I'll clarify this in the post. – Jakube – 2015-12-28T21:54:47.173

In 4, why is 5.5, 6 a False case? – isaacg – 2015-12-29T07:05:28.503

@isaacg Oops. It should be true. I'll correct it. – Jakube – 2015-12-29T07:09:16.133

For problem 5, do we need to handle leading zeros? If so, could you add a test case? – isaacg – 2015-12-29T09:00:59.823

@isaacg No. You can assume that there are no leading zeros. The reference solution also can't handle those. – Jakube – 2015-12-29T09:03:44.523

For problem 7, are the numbers non-negative? – isaacg – 2016-01-01T10:33:36.503

@isaacg Yes, I probably should have specified this. – Jakube – 2016-01-01T10:36:14.167


I'm voting to close this question as off-topic because it is a multi-part challenge with no interaction between the parts

– pppery – 2019-11-07T04:33:19.883

@pppery Perhaps it should be retagged as a [tag:tips] question? – Jo King – 2019-11-07T22:10:39.087

@JoKing I don't see how this fits into the scope of [tag:tips] – pppery – 2019-11-07T22:25:07.087

@ppery For questions asking for tips on a specific piece of code, to make it a better answer to a programming challenge that is on-topic on this site. In this case it would be multiple pieces of code, though answers would be expected to be more detailed. – Jo King – 2019-11-07T23:20:07.407

@JoKing Is it really worth the time arguing about a 4 year old post (that has an accepted answer, and overall good feedback)? At the time it was clearly accepted (see the meta post I linked in the comments above). – Jakube – 2019-11-08T09:59:12.420

@Jakube Rules have changed to no longer permit that sort of question. Therefore, all questions of this type must be closed. The fact that this question is old is utterly irrelevant. – pppery – 2019-11-08T12:41:55.850



54 bytes

Here are the intended solutions. Except for task 8 all solutions were found.

1. j.tmLdS9Z (9 bytes) using map for left-map, transpose and fill with zeros
2. \nMTjG (5 bytes) use newlines for map
3. h.f_IZ1z (8 bytes) generate the possible strings with .f
4. %IQE (4 bytes) found a usecase, where the invariant operator needs 2 parameters
5. ,J.vz-zJ (8 bytes) .v evaluates only the first statement of a string and ignores the rest
6. ssMcz\, (7 bytes) s"" = 0
7. u+GE0 (5 bytes) reduce until it reaches a know number
8. ss.DVQUQ (8 bytes) delete the diagonal using vectorization


Posted 2015-12-28T16:08:13.523

Reputation: 21 462

Damn, I forgot U did l on lists! – lirtosiast – 2016-02-11T03:06:08.473

1Nice on beating isaacg in his own language. – Stan Strum – 2017-11-11T18:08:45.113


59 58 56 bytes

Problem 1:

j.tmLdS9Z (9 bytes)

Problem 2:

MTjG (5 bytes) (The first character is a newline)

Problem 3:

h.f_IZ1z (8 bytes)

Problem 4:

%IQE (4 bytes)

Problem 5:

,J.vz-zJ (8 bytes)

Problem 6:

srXz\,d7 (8 bytes)

Problem 7:

u+GE0 (5 bytes)

Problem 8:

ss.DR~hZQ (9 bytes)


Posted 2015-12-28T16:08:13.523

Reputation: 39 268


66 bytes

1. 10 bytes: V9+mZN}hN9
2. 6 bytes: jUT)jG
3. 8 bytes: h.f_IZ1z
4. (reference implementation) 7 bytes: &gQ0<QE
5. 11 bytes: ,sK-rzZG-zK
6. 7 bytes: ssMcz\,
7. 8 bytes: s<.Qx.QZ
8. 9 bytes: ss.eXbkZQ


Posted 2015-12-28T16:08:13.523

Reputation: 25 023


68 67 66 65 bytes

Task 1

10 bytes: V9m*d>dNS9

Task 2

5 bytes: \nMTjG, where \n is a newline

Task 3

9 bytes: h.fqZ_Z1z

Task 4

5 bytes: qQ%QE

Task 5

Reference solution, 12 bytes: A.ggk\Az,sGH

Task 6

7 bytes: ssMcz\,

Task 7

8 bytes: s<.Qx.Q0

Task 8

9 bytes: ss.DVQUlQ


Posted 2015-12-28T16:08:13.523

Reputation: 20 331


 60 59  57 bytes

1. 9 bytes: j.tmRdS9Z

2. 6 bytes: jbUTjG

3. 8 bytes: h.f_IZ1z

4. 4 bytes: }sEU

5. 8 bytes: ,K.vz-zK

6. 7 bytes: ssMcz\,

7. 8 bytes: s<FxB.Q0

8. 11 10 7 bytes: ss.DVQU Previous version: ss.e+<bk>bh

Note that I developed this solutions completely independently of other answers, although I am quite late to the party.

Mr. Xcoder

Posted 2015-12-28T16:08:13.523

Reputation: 39 774

Nice attempt. 3rd problem are 8 bytes though. – Jakube – 2017-11-11T00:02:52.970

And I'm pretty sure, that the solution for problem 8 did not compile at the time back then. But I haven't tested it. – Jakube – 2017-11-11T00:05:37.883

@Jakube Oh thanks for noticing! – Mr. Xcoder – 2017-11-11T09:18:56.223