Loading...
More Python Posts
# Python program for Bitonic Sort. Note that this program# works only when size of input is a power of 2.# The parameter dir indicates the sorting direction, ASCENDING# or DESCENDING; if (a[i] > a[j]) agrees with the direction,# then a[i] and a[j] are interchanged.*/def compAndSwap(a, i, j, dire):if (dire==1 and a[i] > a[j]) or (dire==0 and a[i] > a[j]):a[i],a[j] = a[j],a[i]# It recursively sorts a bitonic sequence in ascending order,# if dir = 1, and in descending order otherwise (means dir=0).# The sequence to be sorted starts at index position low,# the parameter cnt is the number of elements to be sorted.def bitonicMerge(a, low, cnt, dire):if cnt > 1:k = cnt/2for i in range(low , low+k):compAndSwap(a, i, i+k, dire)bitonicMerge(a, low, k, dire)bitonicMerge(a, low+k, k, dire)# This funcion first produces a bitonic sequence by recursively# sorting its two halves in opposite sorting orders, and then# calls bitonicMerge to make them in the same orderdef bitonicSort(a, low, cnt,dire):if cnt > 1:k = cnt/2bitonicSort(a, low, k, 1)bitonicSort(a, low+k, k, 0)bitonicMerge(a, low, cnt, dire)# Caller of bitonicSort for sorting the entire array of length N# in ASCENDING orderdef sort(a,N, up):bitonicSort(a,0, N, up)# Driver code to test abovea = [3, 7, 4, 8, 6, 2, 1, 5]n = len(a)up = 1sort(a, n, up)print ("\n\nSorted array is")for i in range(n):print("%d" %a[i]),
filename = "data.txt"with open(filename, "r") as file:file_contents = file.readlines()file_contents = [line.strip() for line in file_contents]print("File contents:")for line in file_contents:print(line)
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]
from itertools import productV='∀'E='∃'def tt(f,n) :xss=product((0,1),repeat=n)print('function:',f.__name__)for xs in xss : print(*xs,':',int(f(*xs)))print('')# p \/ (q /\ r) = (p \/ q) /\ (p \/ r)def prob1(p,q,r) :x=p or (q and r)y= (p or q) and (p or r)return x==ytt(prob1,3)# p/\(q\/r)=(p/\q)\/(p/\r)def prob2(p,q,r) :x=p and ( q or r )y=(p and q) or (p and r)return x==ytt(prob2,3)#~(p/\q)=(~p\/~q)def prob3(p,q) :x=not (p and q)y=(not p) or (not q)return x==ytt(prob3,2)#(~(p\/q))=((~p)/\~q)def prob4(p, q):x = not(p or q)y = not p and not qreturn x == ytt(prob4, 2)#(p/\(p=>q)=>q)def prob5(p,q):x= p and ( not p or q)return not x or qtt(prob5,2)# (p=>q)=((p\/q)=q)def prob6(p,q) :x = (not p or q)y=((p or q) == q)return x==ytt(prob6,2)#((p=>q)=(p\/q))=qdef prob7(p,q):if ((not p or q)==(p or q))==q:return 1tt(prob7,2)#(p=>q)=((p/\q)=p)def prob8(p,q):if (not p or q)==((p and q)==p):return 1tt(prob8,2)#((p=>q)=(p/\q))=pdef prob9(p,q):if ((not p or q)==(p and q))==p:return '1'tt(prob9,2)#(p=>q)/\(q=>r)=>(p=>r)def prob10(p,q,r) :x = not ((not p or q) and (not q or r)) or (not p or r)return xtt(prob10, 3)# (p = q) /\ (q => r) => (p => r)#answer 1def prob11(p,q,r) :x = not((p is q) and (not q or r)) or (not p or r)return xtt(prob11, 3)#(p=q)/\(q=>r)=>(p=>r)#answer 2def prob11(p,q,r):x=(p==q) and (not q or r)y=not p or rreturn not x or ytt(prob11,3)#((p=>q)/\(q=r))=>(p=>r)def prob12(p,q,r):x=(not p or q) and ( q==r )y=not p or rreturn not x or ytt(prob12,3)#(p=>q)=>((p/\r)=>(q/\r))def prob13(p,q,r):x=not p or qy=(not(p and r) or ( q and r))return not x or ytt(prob13,3)#Question#2----------------------------------------#(p=>q)=>r=p=>(q=>r)def prob14(p,q,r):x=(not(not p or q) or r)y=(not p or (not q or r))return x==ytt(prob14,3)def prob15(p, q):x = not(p and q)y = not p and not qreturn x == ytt(prob15, 2)def prob16(p, q):x = not(p or q)y = not p or not qreturn x == ytt(prob16, 2)def prob17(p):x = py = not preturn x == ytt(prob17, 1)
import itertoolsimport stringimport timedef guess_password(real):chars = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuationattempts = 0for password_length in range(1, 9):for guess in itertools.product(chars, repeat=password_length):startTime = time.time()attempts += 1guess = ''.join(guess)if guess == real:return 'password is {}. found in {} guesses.'.format(guess, attempts)loopTime = (time.time() - startTime);print(guess, attempts, loopTime)print("\nIt will take A REALLY LONG TIME to crack a long password. Try this out with a 3 or 4 letter password and see how this program works.\n")val = input("Enter a password you want to crack that is 9 characters or below: ")print(guess_password(val.lower()))
""" Number Guessing Game----------------------------------------"""import randomattempts_list = []def show_score():if len(attempts_list) <= 0:print("There is currently no high score, it's yours for the taking!")else:print("The current high score is {} attempts".format(min(attempts_list)))def start_game():random_number = int(random.randint(1, 10))print("Hello traveler! Welcome to the game of guesses!")player_name = input("What is your name? ")wanna_play = input("Hi, {}, would you like to play the guessing game? (Enter Yes/No) ".format(player_name))// Where the show_score function USED to beattempts = 0show_score()while wanna_play.lower() == "yes":try:guess = input("Pick a number between 1 and 10 ")if int(guess) < 1 or int(guess) > 10:raise ValueError("Please guess a number within the given range")if int(guess) == random_number:print("Nice! You got it!")attempts += 1attempts_list.append(attempts)print("It took you {} attempts".format(attempts))play_again = input("Would you like to play again? (Enter Yes/No) ")attempts = 0show_score()random_number = int(random.randint(1, 10))if play_again.lower() == "no":print("That's cool, have a good one!")breakelif int(guess) > random_number:print("It's lower")attempts += 1elif int(guess) < random_number:print("It's higher")attempts += 1except ValueError as err:print("Oh no!, that is not a valid value. Try again...")print("({})".format(err))else:print("That's cool, have a good one!")if __name__ == '__main__':start_game()