Skip to main content

Convert Decimal to Binary and Hexadecimal

0 likes • May 31, 2023 • 0 views
Python
Loading...

More Python Posts

Calculate the Area of a Triangle

0 likes • May 31, 2023 • 0 views
Python
# Prompt user for base and height
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Calculate the area
area = (base * height) / 2
# Display the result
print("The area of the triangle is:", area)

Hello World

0 likes • Sep 9, 2023 • 11 views
Python
print("test")

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])

Currency Converter

0 likes • Nov 19, 2022 • 0 views
Python
""" Currency Converter
----------------------------------------
"""
import urllib.request
import json
def currency_converter(currency_from, currency_to, currency_input):
yql_base_url = "https://query.yahooapis.com/v1/public/yql"
yql_query = 'select%20*%20from%20yahoo.finance.xchange%20where%20pair' \
'%20in%20("'+currency_from+currency_to+'")'
yql_query_url = yql_base_url + "?q=" + yql_query + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
try:
yql_response = urllib.request.urlopen(yql_query_url)
try:
json_string = str(yql_response.read())
json_string = json_string[2:
json_string = json_string[:-1]
print(json_string)
yql_json = json.loads(json_string)
last_rate = yql_json['query']['results']['rate']['Rate']
currency_output = currency_input * float(last_rate)
return currency_output
except (ValueError, KeyError, TypeError):
print(yql_query_url)
return "JSON format error"
except IOError as e:
print(str(e))
currency_input = 1
#currency codes : http://en.wikipedia.org/wiki/ISO_4217
currency_from = "USD"
currency_to = "TRY"
rate = currency_converter(currency_from, currency_to, currency_input)
print(rate)

primes numbers finder

0 likes • Mar 12, 2021 • 0 views
Python
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

Caesar Encryption

0 likes • Mar 10, 2021 • 0 views
Python
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]))