• Nov 19, 2022 •CodeCatch
0 likes • 7 views
def when(predicate, when_true): return lambda x: when_true(x) if predicate(x) else x double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2) print(double_even_numbers(2)) # 4 print(double_even_numbers(1)) # 1
0 likes • 3 views
# Input for row and column R = int(input()) C = int(input()) # Using list comprehension for input matrix = [[int(input()) for x in range (C)] for y in range(R)]
• Jun 16, 2024 •lagiath
0 likes • 1 view
print('hello, world')
• Mar 26, 2023 •AustinLeath
import os # Get the current directory current_dir = os.getcwd() # Loop through each file in the current directory for filename in os.listdir(current_dir): # Check if the file name starts with a number followed by a period and a space if filename[0].isdigit() and filename[1] == '.' and filename[2] == ' ': # Remove the number, period, and space from the file name new_filename = filename[3:] # Rename the file os.rename(os.path.join(current_dir, filename), os.path.join(current_dir, new_filename))
• Nov 18, 2022 •AustinLeath
import random class Node: def __init__(self, c): self.left = None self.right = None self.color = c def SetColor(self,c) : self.color = c def PrintNode(self) : print(self.color) def insert(s, root, i, n): if i < n: temp = Node(s[i]) root = temp root.left = insert(s, root.left,2 * i + 1, n) root.right = insert(s, root.right,2 * i + 2, n) return root def MakeTree(s) : list = insert(s,None,0,len(s)) return list def MakeSet() : s = [] count = random.randint(7,12) for _ in range(count) : color = random.randint(0,1) == 0 and "Red" or "White" s.append(color) return s def ChangeColor(root) : if (root != None) : if (root.color == "White") : root.SetColor("Red") ChangeColor(root.left) ChangeColor(root.right) def PrintList(root) : if root.left != None : PrintList(root.left) else : root.PrintNode() if root.right != None : PrintList(root.right) else : root.PrintNode() t1 = MakeTree(MakeSet()) print("Original Colors For Tree 1:\n") PrintList(t1) ChangeColor(t1) print("New Colors For Tree 1:\n") PrintList(t1) t2 = MakeTree(MakeSet()) print("Original Colors For Tree 2:\n") PrintList(t2) ChangeColor(t2) print("New Colors For Tree 2:\n") PrintList(t2) t3 = MakeTree(MakeSet()) print("Original Colors For Tree 3:\n") PrintList(t3) ChangeColor(t3) print("New Colors For Tree 3:\n") PrintList(t3)
""" Number Guessing Game ---------------------------------------- """ import random attempts_list = [] def show_score(): if len(attempts_list) <= 0: print("There is currently no high score, it's yours for the taking!") else: print("The current high score is {} attempts".format(min(attempts_list))) def start_game(): random_number = int(random.randint(1, 10)) print("Hello traveler! Welcome to the game of guesses!") player_name = input("What is your name? ") wanna_play = input("Hi, {}, would you like to play the guessing game? (Enter Yes/No) ".format(player_name)) // Where the show_score function USED to be attempts = 0 show_score() while wanna_play.lower() == "yes": try: guess = input("Pick a number between 1 and 10 ") if int(guess) < 1 or int(guess) > 10: raise ValueError("Please guess a number within the given range") if int(guess) == random_number: print("Nice! You got it!") attempts += 1 attempts_list.append(attempts) print("It took you {} attempts".format(attempts)) play_again = input("Would you like to play again? (Enter Yes/No) ") attempts = 0 show_score() random_number = int(random.randint(1, 10)) if play_again.lower() == "no": print("That's cool, have a good one!") break elif int(guess) > random_number: print("It's lower") attempts += 1 elif int(guess) < random_number: print("It's higher") attempts += 1 except ValueError as err: print("Oh no!, that is not a valid value. Try again...") print("({})".format(err)) else: print("That's cool, have a good one!") if __name__ == '__main__': start_game()