Golf Practice: Pyth

10

2

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

isaacg

Posted 2015-07-16T11:00:28.850

Reputation: 39 268

Question was closed 2017-05-30T10:27:01.080

@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. – isaacg – 2016-02-04T17:28:07.447

I'm closing this question as off-topic because multi-part challenges with no interaction between the parts are disallowed. http://meta.codegolf.stackexchange.com/a/8464/45941

– Martin Ender – 2017-05-30T10:27:01.080

Answers

3

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

isaacg

Posted 2015-07-16T11:00:28.850

Reputation: 39 268

are we restricting to Pyth versions from before the question ? – Optimizer – 2015-07-21T12:10:33.410

1"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 – isaacg – 2015-07-21T12:14:10.597

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. – Optimizer – 2015-07-21T12:16:39.760

9

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.

Jakube

Posted 2015-07-16T11:00:28.850

Reputation: 21 462

You can't combine your solution with xnor's, because xnor's is invalid (see my comment on his answer). – orlp – 2015-07-16T19:54:06.557

@orlp *combining everything but the invalid stuff ;-) Added the solution in my post. – Jakube – 2015-07-16T20:03:40.970

Ah, I see now :) Guess I was 6 bytes (and counting) worse than optimal. – orlp – 2015-07-16T20:10:43.113

aaah! thanks! hadn't realized :). I'll remove that. – Ven – 2016-02-12T14:45:01.270

7

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

orlp

Posted 2015-07-16T11:00:28.850

Reputation: 37 067

Nicely golfed :-) – Vedaad Shakib – 2015-07-16T11:47:46.633

is you answer for task 4 valid since on falsey it just prints a blank line? – Maltysen – 2015-07-16T12:20:43.847

@Maltysen Yes, because the truthy value of "" is False because not "" is True. – orlp – 2015-07-16T12:21:29.433

3

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

Maltysen

Posted 2015-07-16T11:00:28.850

Reputation: 25 023

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 =/ – orlp – 2015-07-16T12:13:08.853

@orlp V automatically truncates unequal inputs – Maltysen – 2015-07-16T12:19:40.253

2

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.

xnor

Posted 2015-07-16T11:00:28.850

Reputation: 115 687

Your solution for #4 is invalid. For example the string te\\st is a false positive. – orlp – 2015-07-16T19:53:34.947

@orlp Replaced it with different, shorter solution. – xnor – 2015-07-16T20:09:22.843