21
\$\begingroup\$

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:

0
...
9
a
...
z

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)
\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Luis Mendo Dec 28 '15 at 17:21
  • 3
    \$\begingroup\$ @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 \$\endgroup\$ – Jakube Dec 28 '15 at 17:38
  • \$\begingroup\$ In 6, are the numbers positive single-digit integers? \$\endgroup\$ – xnor Dec 28 '15 at 18:15
  • \$\begingroup\$ @xnor You can assume that they are positive, but they might consist of more than one digits. I'll change the test case. \$\endgroup\$ – Jakube Dec 28 '15 at 18:22
  • \$\begingroup\$ @Jakube I see. Thanks for the link! \$\endgroup\$ – Luis Mendo Dec 28 '15 at 18:34
9
\$\begingroup\$

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)

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Damn, I forgot U did l on lists! \$\endgroup\$ – lirtosiast Feb 11 '16 at 3:06
  • \$\begingroup\$ Nice on beating isaacg in his own language. \$\endgroup\$ – Stan Strum Nov 11 '17 at 18:08
1
\$\begingroup\$

 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.

\$\endgroup\$
  • \$\begingroup\$ Nice attempt. 3rd problem are 8 bytes though. \$\endgroup\$ – Jakube Nov 11 '17 at 0:02
  • \$\begingroup\$ 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. \$\endgroup\$ – Jakube Nov 11 '17 at 0:05
  • \$\begingroup\$ @Jakube Oh thanks for noticing! \$\endgroup\$ – Mr. Xcoder Nov 11 '17 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.