Skip to main content

curry function

0 likes • Nov 19, 2022 • 1 view
Python
Loading...

More Python Posts

Differentiate Between del, remove, and pop on a List

0 likes • May 31, 2023 • 0 views
Python
my_list = [1, 2, 3, 4, 5]
removed_element = my_list.pop(2) # Remove and return element at index 2
print(removed_element) # 3
print(my_list) # [1, 2, 4, 5]
last_element = my_list.pop() # Remove and return the last element
print(last_element) # 5
print(my_list) # [1, 2, 4]

ZapFinder

0 likes • Jan 23, 2021 • 0 views
Python
import subprocess #for the praat calls
import os #for ffmpeg and the pause call at the end
#Even if we wanted all videos being rendered asynchronously, we couldn't see progress or errors
import glob #for the ambiguous files
import tempfile
audioFileDirectory = 'Audio Files'
timeList = {}
fileList = glob.glob(audioFileDirectory + '\\*.wav')
pipeList = {}
for fileName in fileList:
arglist = ['Praat.exe', '--run', 'crosscorrelateMatch.praat', 'zeussound.wav', fileName, "0" , "300"]
print(' '.join(arglist))
pipe = subprocess.Popen(arglist, stdout=subprocess.PIPE)
pipeList[fileName[len(audioFileDirectory)+1:-4]] = pipe #+1 because of back slash, -4 because .wav
#for fileName, pipe in pipeList.items():
# text = pipe.communicate()[0].decode('utf-8')
# timeList[fileName] = float(text[::2])
for fileName, pipe in pipeList.items():
if float(pipe.communicate()[0].decode('utf-8')[::2]) > .0003: #.000166 is not a match, and .00073 is a perfect match. .00053 is a tested match
arglist = ['Praat.exe', '--run', 'crosscorrelate.praat', 'zeussound.wav', audioFileDirectory + '\\' + fileName + '.wav', "0" , "300"]
print(' '.join(arglist))
text = subprocess.Popen(arglist, stdout=subprocess.PIPE).communicate()[0].decode('utf-8')
timeList[fileName] = float(text[::2])
clipLength = 10
for fileName, time in timeList.items():
arglist = ['ffmpeg', '-i', '"'+fileName+'.mp4"', '-ss', str(time-clipLength), '-t', str(clipLength*2), '-acodec', 'copy' , '-vcodec', 'copy', '"ZEUS'+ fileName + '.mp4"']
print(' '.join(arglist))
os.system(' '.join(arglist))
tempFile = tempfile.NamedTemporaryFile(delete=False)
for fileName in glob.glob('ZEUS*.mp4'):
tempFile.write(("file '" + os.path.realpath(fileName) + "'\n").encode());
tempFile.seek(0)
print(tempFile.read())
tempFile.close()
arglist = ['ffmpeg', '-safe', '0', '-f', 'concat', '-i', '"'+tempFile.name+'"', '-c', 'copy', 'ZeusMontage.mp4']
print(' '.join(arglist))
os.system(' '.join(arglist))
os.unlink(tempFile.name) #Delete the temp file
#print(timeList)
os.system('PAUSE')

Size of tuple

0 likes • Nov 19, 2022 • 0 views
Python
import sys
# sample Tuples
Tuple1 = ("A", 1, "B", 2, "C", 3)
Tuple2 = ("Geek1", "Raju", "Geek2", "Nikhil", "Geek3", "Deepanshu")
Tuple3 = ((1, "Lion"), ( 2, "Tiger"), (3, "Fox"), (4, "Wolf"))
# print the sizes of sample Tuples
print("Size of Tuple1: " + str(sys.getsizeof(Tuple1)) + "bytes")
print("Size of Tuple2: " + str(sys.getsizeof(Tuple2)) + "bytes")
print("Size of Tuple3: " + str(sys.getsizeof(Tuple3)) + "bytes")

Check Armstrong Number

0 likes • May 31, 2023 • 0 views
Python
# Function to check Armstrong number
def is_armstrong_number(number):
# Convert number to string to iterate over its digits
num_str = str(number)
# Calculate the sum of the cubes of each digit
digit_sum = sum(int(digit) ** len(num_str) for digit in num_str)
# Compare the sum with the original number
if digit_sum == number:
return True
else:
return False
# Prompt user for a number
number = int(input("Enter a number: "))
# Check if the number is an Armstrong number
if is_armstrong_number(number):
print(number, "is an Armstrong number.")
else:
print(number, "is not an Armstrong number.")

Calculator

0 likes • Nov 19, 2022 • 0 views
Python
""" Calculator
----------------------------------------
"""
def addition ():
print("Addition")
n = float(input("Enter the number: "))
t = 0 //Total number enter
ans = 0
while n != 0:
ans = ans + n
t+=1
n = float(input("Enter another number (0 to calculate): "))
return [ans,t]
def subtraction ():
print("Subtraction");
n = float(input("Enter the number: "))
t = 0 //Total number enter
sum = 0
while n != 0:
ans = ans - n
t+=1
n = float(input("Enter another number (0 to calculate): "))
return [ans,t]
def multiplication ():
print("Multiplication")
n = float(input("Enter the number: "))
t = 0 //Total number enter
ans = 1
while n != 0:
ans = ans * n
t+=1
n = float(input("Enter another number (0 to calculate): "))
return [ans,t]
def average():
an = []
an = addition()
t = an[1]
a = an[0]
ans = a / t
return [ans,t]
// main...
while True:
list = []
print(" My first python program!")
print(" Simple Calculator in python by Malik Umer Farooq")
print(" Enter 'a' for addition")
print(" Enter 's' for substraction")
print(" Enter 'm' for multiplication")
print(" Enter 'v' for average")
print(" Enter 'q' for quit")
c = input(" ")
if c != 'q':
if c == 'a':
list = addition()
print("Ans = ", list[0], " total inputs ",list[1])
elif c == 's':
list = subtraction()
print("Ans = ", list[0], " total inputs ",list[1])
elif c == 'm':
list = multiplication()
print("Ans = ", list[0], " total inputs ",list[1])
elif c == 'v':
list = average()
print("Ans = ", list[0], " total inputs ",list[1])
else:
print ("Sorry, invilid character")
else:
break

clamp number

0 likes • Nov 19, 2022 • 0 views
Python
def clamp_number(num, a, b):
return max(min(num, max(a, b)), min(a, b))
clamp_number(2, 3, 5) # 3
clamp_number(1, -1, -5) # -1