首页 > 试题广场 >

A standard 52-card deck, which

[问答题]
A standard 52-card deck, which contains 2, 3, …, J, Q, K, A. Pick four cards from the deck as a hand. Provide a function to figure out who is the winner of given two hands.

There are different types:

T1. Any four cards of the same rank. In this type, the bigger card decides who wins. 
    For example: 3,3,3,3< 6,6,6,6

T2. Any string with all four cards, and in such case (consecutive), ‘A’ can count as either a high or a low card. Otherwise, A is a highest card. In this type, the highest card decides who wins.
    For example: A,2,3,4< J,Q,K,A

T3. Any three cards of the same rank, In this type, the higher three same cards wins.
    For example: 3,3,3,2> 2,2,2,A

T4. Any two cards of the same rank together with another two cards of the same rank. In this type, if two hands share same higher Pair, the bigger another Pair decides who wins. Otherwise, bigger Pair wins.
    For example: 3,3,4,4> 4,2,4,2

T5. Any two cards of the same rank. In this type, bigger Pair wins. If two hands share same Pair, the bigger Third decides who wins. Otherwise, the bigger Fourth wins.
    For example: 3,3,7,4< 3,3,7,5

T6. Any hand not in the above-mentioned hands. In this type, the highest card decides who wins.
    For example: A,K,Q,9< 10,Q,A,K

We don’t take the suit into consideration. If two hands in different type, the rule is: T1>T2>T3>T4>T5>T6

You will get two lines as input:
6,3,4,5
3,4,5,6
You should output the result:
0
If the first two line is bigger than the second line, the result should be 1. If the first line is smaller than the second line, the result should be -1. If they are equal, you should return 0.
If a participator cheats in this game, the number of the same card will be more than four in the two hands. In this case, you should return -2.

Symmetric number is a kind of non-negative number which looks the same as its inversion. For example, ”12321”is a symmetric number. Given any number n represented by a string, find the next symmetric number which is bigger than n.

Here are two examples,

Sample 1:
Input:
123
Output:
131

Sample 2:
Input:
12321
Output:
12421

YOUR ANSWER:
Common Ancestor(Programming)
Question:
A endless Complete Trinary Tree. Node ID is its back-and-forth order in level travel. Find the closest common ancestor.

(“Complete Trinary” means each node has three children, neither more or less)


Example:

The node 13 and 15 have a closest common parent12
The node 16 and 10 have a closest common parent 1
If one Node is the ancestor of the another one, that Node is the closest common ancestor.

Input:
You will get two Nodes’ ID like:
13  9

Output:
You should return the closest common parent node ID like:
0
第二题 来个 python 版本:
# coding:utf-8
import sys

def main():
    # 因为即便输入就是对称的,也要返回一个比他大的,而不是返回输入本身,所以入口处先加1
    src = str(int(sys.argv[1])+1)
    _len = len(src)
    # 判断奇偶,划分左边和右边
    if _len%2 is 0:
        left = src[:int(_len/2)]
        right = src[int(_len/2):]
        middle = ""
    else:
        left = src[:int(_len/2)]
        right = src[int(_len/2)+1:]
        middle = src[int(_len/2)]
    
    if left[::-1] == right:
        return src
    # 如果左边倒过来比右边大,说明右边直接变大到左边的逆序,左边不变就可以了
    elif int(left[::-1]) > int(right):
        return left + middle + left[::-1]
    # 如果左边倒过来比右边小,说明右边变大后需要进位。进位就是左边加1
    elif int(left[::-1]) < int(right):
        new_left = str(int(left+middle)+1)
        if middle:
            return new_left + new_left[:-1][::-1]
        else:
            return new_left + new_left[::-1]

if __name__ == '__main__':
    print(main())



第一题 来个 python 版本:
写的比较挫,没做优化,数据结构用的比较笨,而且每次比较两张牌的大小都得 sq.index 一下效率也不行,不想优化了,不过肯定功能是 OK 的
import sys

sq = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

class Hand(object):
    def __init__(self, str):
        self.cards = self._sort([card.strip().upper() for card in str.split(',')])
        self._type = self.get_type()

    def _sort(self, cards):
        sorted = []
        sorted.append(cards[0])
        for c in cards[1:]:
            for i in range(len(sorted)):
                if sq.index(c) <= sq.index(sorted[i]):
                    sorted.insert(i, c)
                    break
                else:
                    i += 1
            if i == len(sorted):
                sorted.append(c)
        return sorted    
    
    def _is_type1(self):
        if self.cards[0] == self.cards[1] == self.cards[2] == self.cards[3]:
            return True
        else:
            return False
    
    def _is_type2(self):
        if self.cards == ['2', '3', '4', 'A']:
            return True
        for i in range(3):
            if (sq.index(self.cards[i+1]) - sq.index(self.cards[i])) is not 1:
                return False
        return True
    
    def _is_type3(self):
        if self.cards[0] == self.cards[1] == self.cards[2] or self.cards[1] == self.cards[2] == self.cards[3]:
            return True
        else:
            return False
        
    def _is_type4(self):
        if self.cards[0] == self.cards[1] and self.cards[2] == self.cards[3]:
            return True
        else:
            return False
    
    def _is_type5(self):
        for i in range(3):
            if self.cards[i+1] == self.cards[i]:
                return True
        return False
        
    def get_type(self):
        if self._is_type1():
            return 1
        elif self._is_type2():
            return 2
        elif self._is_type3():
            return 3
        elif self._is_type4():
            return 4
        elif self._is_type5():
            return 5
        else:
            return 6        
            

def compare_same_type(h1, h2):
    tp = h1._type
    if tp is 1:
        if sq.index(h1.cards[0]) > sq.index(h2.cards[0]):
            return 1
        elif sq.index(h1.cards[0]) < sq.index(h2.cards[0]):
            return -1
        else:
            return -2
    if tp is 2:
        if sq.index(h1.cards[0]) > sq.index(h2.cards[0]):
            return 1
        elif  sq.index(h1.cards[0]) < sq.index(h2.cards[0]):
            return -1
        # special case: [2,3,4,5] [2,3,4,A]
        elif h1.cards[0] == h2.cards[0] == '2':
            if h1.cards[3] == h2.cards[3]:
                return 0
            elif h1.cards[3] == '5':
                return 1
            else:
                return -1
        else :
            return 0
    if tp is 3:
        if h1.cards[0] == h1.cards[1]:
            h1_same = 0
        else:
            h1_same = 1
        if h2.cards[0] == h2.cards[1]:
            h2_same = 0
        else:
            h2_same = 1
        if sq.index(h1.cards[h1_same]) > sq.index(h2.cards[h2_same]):
            return 1
        elif sq.index(h1.cards[h1_same]) < sq.index(h2.cards[h2_same]):
            return -1
        else:
            return -2    
    if tp is 4:
        if sq.index(h1.cards[2]) > sq.index(h2.cards[2]):
            return 1
        elif sq.index(h1.cards[2]) < sq.index(h2.cards[2]):
            return -1
        else:
            if sq.index(h1.cards[0]) > sq.index(h2.cards[0]):
                return 1
            elif sq.index(h1.cards[0]) < sq.index(h2.cards[0]):
                return -1
            else:
                return 0
    if tp is 5:
        for i in range(3):
            if h1.cards[i] == h1.cards[i+1]:
                h1_same = i
                h1_odd_smaller, h1_odd_bigger = [j for j in [0,1,2,3] if not (j == i or j == i+1) ]
                break
        for i in range(3):
            if h2.cards[i] == h2.cards[i+1]:
                h2_same = i
                h2_odd_smaller, h2_odd_bigger = [j for j in [0,1,2,3] if not (j == i or j == i+1) ]
                break    
        if sq.index(h1.cards[h1_same]) > sq.index(h2.cards[h2_same]):
            return 1
        elif sq.index(h1.cards[h1_same]) < sq.index(h2.cards[h2_same]):
            return -1
        else:
            if sq.index(h1.cards[h1_odd_bigger]) > sq.index(h2.cards[h2_odd_bigger]):
                return 1
            elif sq.index(h1.cards[h1_odd_bigger]) < sq.index(h2.cards[h2_odd_bigger]):
                return -1
            else:
                if sq.index(h1.cards[h1_odd_smaller]) > sq.index(h2.cards[h2_odd_smaller]):
                    return 1
                elif sq.index(h1.cards[h1_odd_smaller]) < sq.index(h2.cards[h2_odd_smaller]):
                    return -1
                else:
                    return 0
    if tp is 6:
        for i in range(3,-1,-1):
            if sq.index(h1.cards[i]) > sq.index(h2.cards[i]):
                return 1
            elif sq.index(h1.cards[i]) < sq.index(h2.cards[i]):
                return -1
        return 0
            
def compare_hand(hand1, hand2):
    if hand1._type < hand2._type:
        return 1
    elif hand1._type > hand2._type:
        return -1
    elif hand1._type == hand2._type:
        return compare_same_type(hand1, hand2)

def check_cheat(hand1, hand2):
    total_cards = hand1.cards + hand2.cards
    for card in total_cards:
        if total_cards.count(card) > 4:
            return True
    return False
    
def main():
    h1 = Hand(sys.argv[1])
    h2 = Hand(sys.argv[2])
    if check_cheat(h1, h2):
        return -2
    return compare_hand(h1, h2)
    
if __name__ == '__main__':
    print(main())

编辑于 2015-04-30 18:53:18 回复(0)
坑啊,这不是一道题,是好几道:第一题最麻烦,我来答第一题吧!:
public class PokerFace {
    public int IsBigger(String[] cardA,String[] cardB){
        if(cardA.length!=4||cardB.length!=4){return -2;}
        int[] handA=convert(cardA);
        int[] handB=convert(cardB);
        double typeA=whichType(handA);
        double typeB=whichType(handB);
        Arrays.sort(handA);
        Arrays.sort(handB);
        if(typeA-typeB>=0.1){return typeA>typeB?-1:1;}//属于不同的type可直接比较
        else if(typeA==3){return handA[2]>handB[2]?1:-1;}//在type3的时候,只需要比较第二大的数即可了,由于“三带一”不可能重复
        else if(typeA==5&&(pairNumber(handA)!=pairNumber(handB))){//属于单对子,且对子数不等
            return pairNumber(handA)>pairNumber(handB)?1:-1;
            //返回对子数更大的那个;
        }
        else if(typeA-typeB<=0.1){
            int i=3;
            while(i>=0){
                if(handA[i]!=handB[i]){return handA[i]>handB[i]?1:-1;}
                i--;
            }
        }
        return 0;
    }
    
    public int pairNumber(int[] A){
        if(A[0]==A[1]){return A[0];}
        if(A[1]==A[2]){return A[1];}
        else{return A[2];}
    }
    public int[] convert(String[] card){
        int[] hand=new int[4];
        for(int i=0;i<4;i++){
                if(card[i]== "A"){hand[i]=14;}
                else if(card[i]== "J"){hand[i]=11;}
                else if(card[i]== "Q"){hand[i]=12;}
                else if(card[i]== "K"){hand[i]=13;}
                else hand[i]=Integer.parseInt(card[i]);    
        }
        return hand;
    }
    public double whichType(int[] hand){
        if(hand[0]==hand[1]&&hand[1]==hand[2]&&hand[2]==hand[3]){return 1;}//炸弹!!!
        Arrays.sort(hand);
        if((hand[0]==hand[1]&&hand[1]==hand[2])||(hand[3]==hand[1]&&hand[1]==hand[2])){return 3;}
        if(hand[0]==hand[1]&&hand[2]==hand[3]){return 4;}
        if(hand[0]==hand[1]||hand[1]==hand[2]||hand[2]==hand[3]){return 5;}
        if(hand[0]==2&&hand[1]==3&&hand[2]==4&&hand[3]==14){return 1.5;}//   “A234返回1.5,方便后面的计算
        if(hand[0]==11&&hand[1]==12&&hand[2]==13&&hand[3]==14){return 2.5;}//"JQKA返回2.5
        if(hand[1]==hand[0]+1&&hand[2]==hand[1]+1&&hand[3]==hand[2]+1){return 2;}
        else return 6;
    }
    
    
    public static void main(String[] args){
//最后是测试方法:还可显示两手牌分别属于哪一个type
        PokerFace pf=new PokerFace();
        String[] cardA={"A","K","9","3"};
        String[] cardB={"10","Q","A","K"};
        //System.out.println(pf.whichType(hand));
        //System.out.println(pf.whichType(pf.convert(cardA)));
        //System.out.println(pf.whichType(pf.convert(cardB)));
        System.out.println(pf.IsBigger(cardA, cardB));
    }
    
/*
整个的思想就是,先判断两手牌属于哪一类,然后根据每个类的比较方法,做出相应的比较,
尤其要注意到的是“A”这个,要谨防出错;比如“A 1 2 3”<“10 J Q K”,很容易出错;
*/
} 

编辑于 2015-04-29 22:20:51 回复(0)