Loading...
More Python Posts
def when(predicate, when_true):return lambda x: when_true(x) if predicate(x) else xdouble_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)print(double_even_numbers(2)) # 4print(double_even_numbers(1)) # 1
# Python code to demonstrate# method to remove i'th character# Naive Method# Initializing Stringtest_str = "CodeCatch"# Printing original stringprint ("The original string is : " + test_str)# Removing char at pos 3# using loopnew_str = ""for i in range(len(test_str)):if i != 2:new_str = new_str + test_str[i]# Printing string after removalprint ("The string after removal of i'th character : " + new_str)
"""Take screenshots at x interval - make a movie of doings on a computer."""import timefrom datetime import datetimeimport ffmpegimport pyautoguiwhile True:epoch_time = int(time.time())today = datetime.now().strftime("%Y_%m_%d")filename = str(epoch_time) + ".png"print("taking screenshot: {0}".format(filename))myScreenshot = pyautogui.screenshot()myScreenshot.save(today + "/" + filename)time.sleep(5)## and then tie it together with: https://github.com/kkroening/ffmpeg-python/blob/master/examples/README.md#assemble-video-from-sequence-of-frames#"""import ffmpeg(ffmpeg.input('./2021_01_22/*.png', pattern_type='glob', framerate=25).filter('deflicker', mode='pm', size=10).filter('scale', size='hd1080', force_original_aspect_ratio='increase').output('movie.mp4', crf=20, preset='slower', movflags='faststart', pix_fmt='yuv420p').run())"""
#SetsU = {0,1,2,3,4,5,6,7,8,9}P = {1,2,3,4}Q = {4,5,6}R = {3,4,6,8,9}def set2bits(xs,us) :bs=[]for x in us :if x in xs :bs.append(1)else:bs.append(0)assert len(us) == len(bs)return bsdef union(set1,set2) :finalSet = set()bitList1 = set2bits(set1, U)bitList2 = set2bits(set2, U)for i in range(len(U)) :if(bitList1[i] or bitList2[i]) :finalSet.add(i)return finalSetdef intersection(set1,set2) :finalSet = set()bitList1 = set2bits(set1, U)bitList2 = set2bits(set2, U)for i in range(len(U)) :if(bitList1[i] and bitList2[i]) :finalSet.add(i)return finalSetdef compliment(set1) :finalSet = set()bitList = set2bits(set1, U)for i in range(len(U)) :if(not bitList[i]) :finalSet.add(i)return finalSetdef implication(a,b):return union(compliment(a), b)################################################################################################################# Problems 1-6 ###################################################################################################################################p \/ (q /\ r) = (p \/ q) /\ (p \/ r)def prob1():return union(P, intersection(Q,R)) == intersection(union(P,Q), union(P,R))#p /\ (q \/ r) = (p /\ q) \/ (p /\ r)def prob2():return intersection(P, union(Q,R)) == union(intersection(P,Q), intersection(P,R))#~(p /\ q) = ~p \/ ~qdef prob3():return compliment(intersection(P,R)) == union(compliment(P), compliment(R))#~(p \/ q) = ~p /\ ~qdef prob4():return compliment(union(P,Q)) == intersection(compliment(P), compliment(Q))#(p=>q) = (~q => ~p)def prob5():return implication(P,Q) == implication(compliment(Q), compliment(P))#(p => q) /\ (q => r) => (p => r)def prob6():return implication(intersection(implication(P,Q), implication(Q,R)), implication(P,R))print("Problem 1: ", prob1())print("Problem 2: ", prob2())print("Problem 3: ", prob3())print("Problem 4: ", prob4())print("Problem 5: ", prob5())print("Problem 6: ", prob6())'''Problem 1: TrueProblem 2: TrueProblem 3: TrueProblem 4: TrueProblem 5: TrueProblem 6: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}'''
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]
# Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.# For example, if n is 10, the output should be “2, 3, 5, 7”. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19”.# Python program to print all primes smaller than or equal to# n using Sieve of Eratosthenesdef SieveOfEratosthenes(n):# Create a boolean array "prime[0..n]" and initialize# all entries it as true. A value in prime[i] will# finally be false if i is Not a prime, else true.prime = [True for i in range(n + 1)]p = 2while (p * p <= n):# If prime[p] is not changed, then it is a primeif (prime[p] == True):# Update all multiples of pfor i in range(p * 2, n + 1, p):prime[i] = Falsep += 1prime[0]= Falseprime[1]= False# Print all prime numbersfor p in range(n + 1):if prime[p]:print (p)# driver programif __name__=='__main__':n = 30print("Following are the prime numbers smaller")print("than or equal to ", n)print("than or equal to ", n)SieveOfEratosthenes(n)