Ad

The best and only way

Code
Diff
  • def oddEven(n):
        even = False
        for i in range(n):
            even = not even
        return even
    • def oddEven(n):
    • return not str(n)[-1] in "02468"
    • even = False
    • for i in range(n):
    • even = not even
    • return even

This is my solution from a Kata I did called "What's a Perfect Power anyway?".
The code finds a pair of numbers a and b that are natural such that a^b=n if given n which is a given natural number. The code will retrun [a,b] if it finds a pair, or None if not.

def isPP(n):  # is it a perfect prime
    for factor in range(2, int(n/2) + 1):
        if n % factor == 0:  # find factors
            power = 1
            while True:  # try higher and higher powers
                power += 1
                if not n % (factor ** power) == 0:  # can it be the solution
                    break
                elif n / (factor ** power) == 1:  # is it the solution
                    return [factor, power]
    return None
Sets

Best not to use eval and best to keep "const" out of func.

Code
Diff
  • wins = {
        "Rock": "Paper",
        "Paper": "Scissors",
        "Scissors": "Rock"
    }
    def dumbRockPaperScissors(player1, player2):
        if player1 == wins[player2]:
            return "Player 1 wins"
        elif player2 == wins[player1]:
            return "Player 2 wins"
        else:
            return "Draw"
     
    
    • wins = {
    • "Rock": "Paper",
    • "Paper": "Scissors",
    • "Scissors": "Rock"
    • }
    • def dumbRockPaperScissors(player1, player2):
    • Rock = {"Paper"}
    • Paper = {"Scissors"}
    • Scissors = {"Rock"}
    • if player1 in eval(player2):
    • if player1 == wins[player2]:
    • return "Player 1 wins"
    • elif player2 in eval(player1):
    • elif player2 == wins[player1]:
    • return "Player 2 wins"
    • else:
    • return "Draw"