Skip to main content

Reverse a linked list

Nov 19, 2022CodeCatch
Loading...

More Python Posts

lambda example

Nov 19, 2022CodeCatch

0 likes • 0 views

list_1 = [1,2,3,4,5,6,7,8,9]
cubed = map(lambda x: pow(x,3), list_1)
print(list(cubed))
#Results
#[1, 8, 27, 64, 125, 216, 343, 512, 729]

AnyTree Randomizer

Apr 15, 2021NoahEaton

0 likes • 0 views

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]

Compute all the Permutation of a String

May 31, 2023CodeCatch

0 likes • 0 views

import itertools
def compute_permutations(string):
# Generate all permutations of the string
permutations = itertools.permutations(string)
# Convert each permutation tuple to a string
permutations = [''.join(permutation) for permutation in permutations]
return permutations
# Prompt the user for a string
string = input("Enter a string: ")
# Compute permutations
permutations = compute_permutations(string)
# Display the permutations
print("Permutations:")
for permutation in permutations:
print(permutation)

Convert Decimal to Binary and Hexadecimal

May 31, 2023CodeCatch

0 likes • 0 views

# Prompt user for a decimal number
decimal = int(input("Enter a decimal number: "))
# Convert decimal to binary
binary = bin(decimal)
# Convert decimal to hexadecimal
hexadecimal = hex(decimal)
# Display the results
print("Binary:", binary)
print("Hexadecimal:", hexadecimal)

Bubble sort

Nov 19, 2022CodeCatch

0 likes • 0 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]),

Find Coin

Oct 4, 2023AustinLeath

0 likes • 10 views

weigh = lambda a,b: sum(b)-sum(a)
FindCoin = lambda A: 0 if (n := len(A)) == 1 else (m := n//3) * (w := 1 + weigh(A[:m], A[2*m:])) + FindCoin(A[m*w:m*(w+1)])
print(FindCoin([1,1,1,1,1,1,1,2,1]))