9
\$\begingroup\$

Some trading cards have real value and can be sold for money. Bending the cards is frowned upon because it takes away their value and makes them look less new. Say you have a deck of trading cards (Pokemon, Magic, etc.) and you want to shuffle them. Instead of doing the bridge that bends all of the cards, another simple way to shuffle cards is to put them into piles. Here's what I mean.

Background

With a 60 card deck in need of shuffling, you could separate the 60 cards into three piles of 20 cards. There are multiple ways to do this, the most plain being to put a card into pile A, then one into pile B, then one into pile C. Another way is to put a card into pile C, then B, then A. There are also ways to spread the cards across the piles unevenly. Here's one: put a card in pile A, put another card in A, then put a card in pile B, then put a card in pile C.

Challenge

Create a full program that will output even if a certain way to shuffle into piles spreads the cards in the piles evenly, and outputs uneven and the number of cards in each pile otherwise.

Input

Input will be taken through STDIN or the closest alternative (no functions).

[sequence] [deck size]
  • sequence is a string of characters. It tells the pattern the cards are laid down into the piles. Each different character corresponds to a single pile. This string will always be under the deck size and will contain only capital letters A-Z.
  • deck size is an integer that specifies how many cards are in the deck. If deck size is 60, the number of cards in the deck is 60.

Output

even

If the number of cards in each pile at the end of the shuffle are the same, your program should output this.

uneven [pile1] [pile2] [...]

If the number cards in each pile at the end of the shuffles are the not the same, your program should output uneven and the number of cards in each pile like this: uneven 20 30 if pile A contains 20 canrds and pile B contains 30. The order of the pile numbers does not matter.

Other information

  • This is a code golf challenge, so the shortest code in bytes on September 25 wins. If there is a tie in byte counts, the code that was submitted first wins.
  • Your program must be a program, not a function.
  • If possible, please include a link to an online interpreter or a link to a place where I can download an interpreter for your language in your answer.
  • Anything I do not specify within this challenge is fair game, meaning if I don't say it, it's up to you. If anything is vague, tell me and I will edit the answer accordingly. (Hopefully this goes more smoothly than my last challenge.)

Examples

     Input | Output | Alternate outputs (if uneven)
           |
    ABC 30 | even
    ABC 31 | uneven 11 10 10 | uneven 10 11 10 | uneven 10 10 11
    BCA 60 | even
    BBA 24 | uneven 8 16 | uneven 16 8
ABACBC 120 | even
  BBABA 50 | uneven 20 30 | uneven 30 20
  AABBB 12 | even
\$\endgroup\$
8
  • 1
    \$\begingroup\$ I don't understand what sequence looks like or how it works. Could you please add some test cases? \$\endgroup\$
    – xnor
    Sep 19, 2015 at 0:08
  • \$\begingroup\$ @xnor Yes, adding now. \$\endgroup\$ Sep 19, 2015 at 0:09
  • 2
    \$\begingroup\$ ...I am never playing cards with you. :P \$\endgroup\$ Sep 19, 2015 at 0:24
  • \$\begingroup\$ @ThomasKwa Input should be taken through standard input or the closest alternative. \$\endgroup\$ Sep 19, 2015 at 0:26
  • \$\begingroup\$ Is ABDD 12 a valid input? What should be the output? Also, do I understand right that AABBB 12 is even? \$\endgroup\$
    – xnor
    Sep 19, 2015 at 0:27

3 Answers 3

5
\$\begingroup\$

Pyth, 33 31 bytes

jd+>"uneven"yK!tl{J/L@LzQ{z*J!K

Example input:

ABACBC
120
\$\endgroup\$
0
4
\$\begingroup\$

Python 3.x, 106 128 138 bytes

s,n=input().split()
n=int(n)
t=(s*n)[:n]
*z,=map(t.count,set(t))
b=z[:-1]!=z[1:];print("un"*b+"even",*z*b)

This duplicates the input sequence (more times than needed, which is good enough) and then takes only the first n characters. These are counted and either uneven or even is chosen, and if the former, the counts also are printed with print(*z), which automatically unpacks z for me.

Saved 32 bytes thanks to xnor and Sp3000!

Also, Python 3.5 has a new feature that allows this 102 byte solution:

s,n=input().split()
n=int(n)
t=(s*n)[:n]
*z,=map(t.count,{*t})
b=len({*z})>1;print("un"*b+"even",*z*b)

len({*z})>1 unpacks the list z, makes a set from it, then checks to see if it has more than one element. It is {*z} specifically that's new.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Happy to see a new Python golfer! Some quick tips. The list comp [t.count(x) for x in set(t)] can be done as a map: map(t.count,set(t)). The question allows the piles in any order, so I think you can avoid doing sorted, even if it means you have to do something a bit longer than z==z[::-1] like z[1:]==z[:-1]. \$\endgroup\$
    – xnor
    Sep 19, 2015 at 3:08
  • \$\begingroup\$ Also, Python 3 lets you print* to print the elements of a list space-separated. This is usually shorter than ' '.join. \$\endgroup\$
    – xnor
    Sep 19, 2015 at 3:13
  • \$\begingroup\$ @xnor: Much appreciated! \$\endgroup\$ Sep 19, 2015 at 3:29
2
\$\begingroup\$

CJam, 35 bytes

rri_@*<$e`0f=_)-"uneven":Ua@+S*U2>?

Try it online in the CJam interpreter.

How it works

rri      e# Read a string (s) and an integer (n) from STDIN.
_@       e# Push a copy of n and rotate s on top of it.
*        e# Repeat s n times.
<        e# Keep only the first n characters.
$e`      e# Sort and perform run-length encoding.
0f=      e# Keep only the multiplicities.
_)-      e# Push a copy, pop the last element and remove its remaining occurrences.
         e# The result with be an empty array (falsy) iff all elements are equal.
"uneven" e# Push that string.
:Ua      e# Save it in U and wrap it in an array.
@+       e# Concatenate ["uneven"] with the array of multiplicities.
S*       e# Join, separating by spaces.
U2>      e# Push "even".
?        e# Select the result depending on whether _)- pushed an empty array.
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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