Skip to main content

Calculator

0 likes • Nov 19, 2022 • 0 views
Python
Loading...

More Python Posts

Reverse a linked list

0 likes • Nov 19, 2022 • 0 views
Python
# Python program to reverse a linked list
# Time Complexity : O(n)
# Space Complexity : O(n) as 'next'
#variable is getting created in each loop.
# Node class
class Node:
# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
# Function to reverse the linked list
def reverse(self):
prev = None
current = self.head
while(current is not None):
next = current.next
current.next = prev
prev = current
current = next
self.head = prev
# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
# Utility function to print the linked LinkedList
def printList(self):
temp = self.head
while(temp):
print temp.data,
temp = temp.next
# Driver program to test above functions
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
print "Given Linked List"
llist.printList()
llist.reverse()
print "\nReversed Linked List"
llist.printList()

Factorial of N

0 likes • Nov 19, 2022 • 0 views
Python
import math
def factorial(n):
print(math.factorial(n))
return (math.factorial(n))
factorial(5)
factorial(10)
factorial(15)

print indices

0 likes • Nov 18, 2022 • 0 views
Python
# List
lst = [1, 2, 3, 'Alice', 'Alice']
# One-Liner
indices = [i for i in range(len(lst)) if lst[i]=='Alice']
# Result
print(indices)
# [3, 4]

return maximum

0 likes • Nov 19, 2022 • 0 views
Python
def max_n(lst, n = 1):
return sorted(lst, reverse = True)[:n]
max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3, 2]

Sherlock Holmes Curious Lockbox Solver

0 likes • Mar 12, 2021 • 0 views
Python
import copy
begining = [False,False,False,False,False,None,True,True,True,True,True]
#False = black True = white
its = [0]
def swap(layout, step):
layoutCopy = copy.deepcopy(layout)
layoutCopy[(step[0]+step[1])], layoutCopy[step[1]] = layoutCopy[step[1]], layoutCopy[(step[0]+step[1])]
return layoutCopy
def isSolved(layout):
for i in range(len(layout)):
if(layout[i] == False):
return (i >= (len(layout)/2))
def recurse(layout, its, steps = []):
if isSolved(layout):
its[0] += 1
print(layout,list(x[0] for x in steps))
return
step = None
for i in range(len(layout)):
if(layout[i] == None):
if(i >= 1): #If the empty space could have something to the left
if(layout[i - 1] == False):
step = [-1,i]
recurse(swap(layout,step), its, (steps+[step]))
if(i > 1): #If the empty space could have something 2 to the left
if(layout[i - 2] == False):
step = [-2,i]
recurse(swap(layout,step), its, (steps+[step]))
if(i < (len(layout)-1)): #If the empty space could have something to the right
if(layout[i + 1] == True):
step = [1,i]
recurse(swap(layout,step), its, (steps+[step]))
if(i < (len(layout)-2)): #If the empty space could have something to the right
if(layout[i + 2] == True):
step = [2,i]
recurse(swap(layout,step), its, (steps+[step]))
its[0] += 1
#return None
recurse(begining,its,[])
print(its[0])

Rock paper scissors

0 likes • Nov 19, 2022 • 0 views
Python
""" Rock Paper Scissors
----------------------------------------
"""
import random
import os
import re
os.system('cls' if os.name=='nt' else 'clear')
while (1 < 2):
print "\n"
print "Rock, Paper, Scissors - Shoot!"
userChoice = raw_input("Choose your weapon [R]ock], [P]aper, or [S]cissors: ")
if not re.match("[SsRrPp]", userChoice):
print "Please choose a letter:"
print "[R]ock, [S]cissors or [P]aper."
continue
// Echo the user's choice
print "You chose: " + userChoice
choices = ['R', 'P', 'S']
opponenetChoice = random.choice(choices)
print "I chose: " + opponenetChoice
if opponenetChoice == str.upper(userChoice):
print "Tie! "
#if opponenetChoice == str("R") and str.upper(userChoice) == "P"
elif opponenetChoice == 'R' and userChoice.upper() == 'S':
print "Scissors beats rock, I win! "
continue
elif opponenetChoice == 'S' and userChoice.upper() == 'P':
print "Scissors beats paper! I win! "
continue
elif opponenetChoice == 'P' and userChoice.upper() == 'R':
print "Paper beat rock, I win! "
continue
else:
print "You win!"