Loading...
More Python Posts
import copybegining = [False,False,False,False,False,None,True,True,True,True,True]#False = black True = whiteits = [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 layoutCopydef 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] += 1print(layout,list(x[0] for x in steps))returnstep = Nonefor i in range(len(layout)):if(layout[i] == None):if(i >= 1): #If the empty space could have something to the leftif(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 leftif(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 rightif(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 rightif(layout[i + 2] == True):step = [2,i]recurse(swap(layout,step), its, (steps+[step]))its[0] += 1#return Nonerecurse(begining,its,[])print(its[0])
""" Currency Converter----------------------------------------"""import urllib.requestimport jsondef 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_outputexcept (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_4217currency_from = "USD"currency_to = "TRY"rate = currency_converter(currency_from, currency_to, currency_input)print(rate)
from statistics import median, mean, modedef print_stats(array):print(array)print("median =", median(array))print("mean =", mean(array))print("mode =", mode(array))print()print_stats([1, 2, 3, 3, 4])print_stats([1, 2, 3, 3])
import randomclass Node:def __init__(self, c):self.left = Noneself.right = Noneself.color = cdef SetColor(self,c) :self.color = cdef PrintNode(self) :print(self.color)def insert(s, root, i, n):if i < n:temp = Node(s[i])root = temproot.left = insert(s, root.left,2 * i + 1, n)root.right = insert(s, root.right,2 * i + 2, n)return rootdef MakeTree(s) :list = insert(s,None,0,len(s))return listdef MakeSet() :s = []count = random.randint(7,12)for _ in range(count) :color = random.randint(0,1) == 0 and "Red" or "White"s.append(color)return sdef ChangeColor(root) :if (root != None) :if (root.color == "White") :root.SetColor("Red")ChangeColor(root.left)ChangeColor(root.right)def PrintList(root) :if root.left != None :PrintList(root.left)else :root.PrintNode()if root.right != None :PrintList(root.right)else :root.PrintNode()t1 = MakeTree(MakeSet())print("Original Colors For Tree 1:\n")PrintList(t1)ChangeColor(t1)print("New Colors For Tree 1:\n")PrintList(t1)t2 = MakeTree(MakeSet())print("Original Colors For Tree 2:\n")PrintList(t2)ChangeColor(t2)print("New Colors For Tree 2:\n")PrintList(t2)t3 = MakeTree(MakeSet())print("Original Colors For Tree 3:\n")PrintList(t3)ChangeColor(t3)print("New Colors For Tree 3:\n")PrintList(t3)
from colorama import init, Fore# Initialize coloramainit()print(Fore.RED + "This text is in red color.")print(Fore.GREEN + "This text is in green color.")print(Fore.BLUE + "This text is in blue color.")# Reset coloramaprint(Fore.RESET + "This text is back to the default color.")
# Python program for implementation of Bubble Sortdef bubbleSort(arr):n = len(arr)# Traverse through all array elementsfor i in range(n-1):# range(n) also work but outer loop will repeat one time more than needed.# Last i elements are already in placefor j in range(0, n-i-1):# traverse the array from 0 to n-i-1# Swap if the element found is greater# than the next elementif arr[j] > arr[j+1] :arr[j], arr[j+1] = arr[j+1], arr[j]# Driver code to test abovearr = [64, 34, 25, 12, 22, 11, 90]bubbleSort(arr)print ("Sorted array is:")for i in range(len(arr)):print ("%d" %arr[i]),