• Aug 12, 2024 •AustinLeath
0 likes • 5 views
magnitude = lambda bits: 1_000_000_000_000_000_000 % (2 ** bits) sign = lambda bits: -1 ** (1_000_000_000_000_000_000 // (2 ** bits)) print("64 bit sum:", magnitude(64) * sign(64)) print("32 bit sum:", magnitude(32) * sign(32)) print("16 bit sum:", magnitude(16) * sign(16))
• Nov 19, 2022 •CodeCatch
0 likes • 0 views
""" 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()
from collections import defaultdict def combine_values(*dicts): res = defaultdict(list) for d in dicts: for key in d: res[key].append(d[key]) return dict(res) d1 = {'a': 1, 'b': 'foo', 'c': 400} d2 = {'a': 3, 'b': 200, 'd': 400} combine_values(d1, d2) # {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}
0 likes • 3 views
# Python program for implementation of Bubble Sort def bubbleSort(arr): n = len(arr) # Traverse through all array elements for i in range(n-1): # range(n) also work but outer loop will repeat one time more than needed. # Last i elements are already in place for j in range(0, n-i-1): # traverse the array from 0 to n-i-1 # Swap if the element found is greater # than the next element if arr[j] > arr[j+1] : arr[j], arr[j+1] = arr[j+1], arr[j] # Driver code to test above arr = [64, 34, 25, 12, 22, 11, 90] bubbleSort(arr) print ("Sorted array is:") for i in range(len(arr)): print ("%d" %arr[i]),
• Jun 16, 2024 •lagiath
0 likes • 1 view
print('hello, world')
• Apr 15, 2021 •NoahEaton
import anytree as at import random as rm # Generate a tree with node_count many nodes. Each has a number key that shows when it was made and a randomly selected color, red or white. def random_tree(node_count): # Generates the list of nodes nodes = [] for i in range(node_count): test = rm.randint(1,2) if test == 1: nodes.append(at.Node(str(i),color="white")) else: nodes.append(at.Node(str(i),color="red")) #Creates the various main branches for i in range(node_count): for j in range(i, len(nodes)): test = rm.randint(1,len(nodes)) if test == 1 and nodes[j].parent == None and (not nodes[i] == nodes[j]): nodes[j].parent = nodes[i] #Collects all the main branches into a single tree with the first node being the root for i in range(1, node_count): if nodes[i].parent == None and (not nodes[i] == nodes[0]): nodes[i].parent = nodes[0] return nodes[0]