• Nov 19, 2022 •CodeCatch
0 likes • 0 views
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]}
• Nov 18, 2022 •AustinLeath
0 likes • 6 views
# Python Program to calculate the square root num = float(input('Enter a number: ')) num_sqrt = num ** 0.5 print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
• Mar 12, 2021 •mo_ak
0 likes • 3 views
prime_lists=[] # a list to store the prime numbers def prime(n): # define prime numbers if n <= 1: return False # divide n by 2... up to n-1 for i in range(2, n): if n % i == 0: # the remainder should'nt be a 0 return False else: prime_lists.append(n) return True for n in range(30,1000): # calling function and passing starting point =30 coz we need primes >30 prime(n) check=0 # a var to limit the output to 10 only for n in prime_lists: for x in prime_lists: val= n *x if (val > 1000 ): check=check +1 if (check <10) : print("the num is:", val , "=",n , "* ", x ) break
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)
• Mar 10, 2021 •Skrome
0 likes • 2 views
import string def caesar(text, shift, alphabets): def shift_alphabet(alphabet): return alphabet[shift:] + alphabet[:shift] shifted_alphabets = tuple(map(shift_alphabet, alphabets)) final_alphabet = "".join(alphabets) final_shifted_alphabet = "".join(shifted_alphabets) table = str.maketrans(final_alphabet, final_shifted_alphabet) return text.translate(table) plain_text = "Hey Skrome, welcome to CodeCatch" print(caesar(plain_text, 8, [string.ascii_lowercase, string.ascii_uppercase, string.punctuation]))
• Mar 26, 2023 •AustinLeath
0 likes • 1 view
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))