10
\$\begingroup\$

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. However, unfamiliar approaches and constructs may be involved, so take a look at the Pyth Tips as well as the Pyth Character Reference if you get stuck. Solutions may be tested here.

Goal: There are 8 problems, each with a Pyth snippet for you to optimize. Your goal is to create something equivalent but shorter. The reference solutions total 80 bytes. Your goal is to beat that by as much as possible.

The winner will go to the submission that solves all 8 problems with the smallest total number of bytes. Tiebreaker is earlier post.

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.

Each submission should answer every problem and give the corresponding byte count, but feel free to use the reference implementation if you cannot improve it.

Details: If the question calls for a certain value or output, q equality is desired, so 1 and !0 are equivalent. If the question calls for testing whether a condition is true, the output must be truthy if the condition is true and falsy if the condition is false, but is unconstrained beyond that. You may not swap true for false and false for true. If the question calls for something to be printed, nothing else may be printed except a trailing newline.

All answers must be valid for the most recent Pyth commit as of this question's posting.

Problem 1: Given a set in Q, output a list containing the elements of Q in any order.

; 3 bytes
f1Q

Problem 2: Output the list [1, 1, 0, 0, 1, 1, 0].

; 9 bytes
[J1JZZJJZ

Problem 3: Given a positive integer in Q, test whether all of Q's digits are positive (not zero).

; 7 bytes
!f!TjQT

Problem 4: Given a string in z, test whether z contains any quotation marks - " or '.

; 9 bytes
|}\'z}\"z

Problem 5: Map Q=1 to 'Win', Q=0 to 'Tie' and Q=-1 to 'Lose'.

; 20 bytes
@["Tie""Win""Lose")Q

Problem 6: Print 0123456789.

; 6 bytes
sm`dUT

Problem 7: Given a string in z, count the number of inversions.

(Indexes i and j form an inversion if i < j but z[i] > z[j]).

; 17 bytes
ssmm>@zd@zkrdlzUz

Problem 8: Given a list in z, count the number of repeated adjacent elements.

; 9 bytes
lfqFT.:z2
\$\endgroup\$

closed as off-topic by Martin Ender May 30 '17 at 10:27

  • This question does not appear to be about programming puzzles or code golf within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ @user1737909 That answer is correct. f1Q filters Q on a function that is always true, so it always outputs every element of the set. f outputs a list, which meets the objective in that question. \$\endgroup\$ – isaacg Feb 4 '16 at 17:28
  • \$\begingroup\$ I'm closing this question as off-topic because multi-part challenges with no interaction between the parts are disallowed. meta.codegolf.stackexchange.com/a/8464/45941 \$\endgroup\$ – Martin Ender May 30 '17 at 10:27
9
\$\begingroup\$

52 bytes

Problem 1: 2 bytes

    SQ

Problem 2: 5 bytes
    jC\f2

Problem 3: 4 bytes
    -0`Q
Problem 4: 6 bytes
    @z+N\'
Problem 5: 17 bytes
    %3>"LTWoiisene"hQ
or
    @c3"LoseTieWin"hQ
Problem 6: 3 bytes
    pMT
Problem 7: 9 bytes
    s>R_d.cz2
Problem 8: 6 bytes
    sqVztz
Combining @xnor's solution for problem 4 with mine (both 6 bytes) gives a nice 4 bytes solution. So 48 bytes are possible.

\$\endgroup\$
  • \$\begingroup\$ You can't combine your solution with xnor's, because xnor's is invalid (see my comment on his answer). \$\endgroup\$ – orlp Jul 16 '15 at 19:54
  • \$\begingroup\$ @orlp *combining everything but the invalid stuff ;-) Added the solution in my post. \$\endgroup\$ – Jakube Jul 16 '15 at 20:03
  • \$\begingroup\$ Ah, I see now :) Guess I was 6 bytes (and counting) worse than optimal. \$\endgroup\$ – orlp Jul 16 '15 at 20:10
  • \$\begingroup\$ aaah! thanks! hadn't realized :). I'll remove that. \$\endgroup\$ – Ven Feb 12 '16 at 14:45
7
\$\begingroup\$

54 bytes

Task 1, 2 bytes: SQ
Task 2, 6 bytes: j102 2
Task 3, 5 bytes: *FjQT
Task 4, 6 bytes: @z"'\"
Task 5, 17 bytes: @c3"LoseTieWin"hQ
Task 6, 4 bytes: jkUT
Task 7, 7 bytes: s>M.cz2
Task 8, 7 bytes: sqM.:z2

\$\endgroup\$
  • \$\begingroup\$ Nicely golfed :-) \$\endgroup\$ – Vedaad Shakib Jul 16 '15 at 11:47
  • \$\begingroup\$ is you answer for task 4 valid since on falsey it just prints a blank line? \$\endgroup\$ – Maltysen Jul 16 '15 at 12:20
  • \$\begingroup\$ @Maltysen Yes, because the truthy value of "" is False because not "" is True. \$\endgroup\$ – orlp Jul 16 '15 at 12:21
3
\$\begingroup\$

58 bytes

Task 1, 2 bytes: SQ
Task 2, 5 bytes: jC\f2
Task 3, 6 bytes: !}ZQ
Task 4, 8 bytes: |}\'z}Nz
Task 5, 18 bytes: @c"Tie\nWin\nLose"bQ
Task 6, 4 bytes: jkUT
Task 7, 9 bytes: lf>FT.cz2
Task 8, 6 bytes: sqVtzz

\$\endgroup\$
  • \$\begingroup\$ I didn't know about V (although I still don't fully understand it, since you're doing vector operations on inequal length operands), so I'm excused for that one, but I really should've thought of C\f =/ \$\endgroup\$ – orlp Jul 16 '15 at 12:13
  • \$\begingroup\$ @orlp V automatically truncates unequal inputs \$\endgroup\$ – Maltysen Jul 16 '15 at 12:19
3
\$\begingroup\$

This post is for the accumulation of the best solutions across all answers. Please edit in the solution and the answerer who first found that solution if there any improvements.

48 bytes

1. SQ - 2 bytes, first posted by @orlp
2. jC\f2 - 5 bytes, first posted by @Maltysen
3. -0`Q - 4 bytes, first posted by @Jakube
4. @z`N - 4 bytes, mixture of @xnor and @Jakube, but not in any answers yet.
(I didn't see it when writing the question, either)
5. @c3"LoseTieWin"hQ - 17 bytes, first posted by @orlp
6. pMT - 3 bytes, first posted by @Jakube
7. s>M.cz2 - 7 bytes, first posted by @orlp
8. sqVtzz - 6 bytes, first posted by @Maltysen

\$\endgroup\$
  • \$\begingroup\$ are we restricting to Pyth versions from before the question ? \$\endgroup\$ – Optimizer Jul 21 '15 at 12:10
  • 1
    \$\begingroup\$ "All answers must be valid for the most recent Pyth commit as of this question's posting." Put I'd be interested to hear what you were thinking of. @Optimizer \$\endgroup\$ – isaacg Jul 21 '15 at 12:14
  • \$\begingroup\$ Pyth is ever evolving, right ? (at a much faster pace than other golfing languages). So its very likely that this number (48) will easily go sub 40 in near future. And since this is a practice challenge, you would ideally want to practice using the latest Pyth version for newer challenges. \$\endgroup\$ – Optimizer Jul 21 '15 at 12:16
2
\$\begingroup\$

57 bytes

1. (2) SQ Sort.
2. (5) jC\f2 Convert f to ASCII val then base 2.
3. (5) /`Q`0 Count '0' in the number string.
4. (5) @z`\' Takes the set intersection of the string with "'".
5. (18) @c"Tie Win Lose"dQ Makes the list with split. I didn't get slicing to work.
6. (4) jkUT Join range(10). Apparently converts to a string automatically.
7. (9) sm>d_d.:zCounts substrings that are greater than their reverse.
8. (9) l@C,zz.:z Length of intersection of sublists of the list and pairs of elements in the list. Same length as reference solution.

\$\endgroup\$
  • \$\begingroup\$ Your solution for #4 is invalid. For example the string te\\st is a false positive. \$\endgroup\$ – orlp Jul 16 '15 at 19:53
  • \$\begingroup\$ @orlp Replaced it with different, shorter solution. \$\endgroup\$ – xnor Jul 16 '15 at 20:09

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