• Nov 19, 2022 •CodeCatch
0 likes • 0 views
from time import sleep def delay(fn, ms, *args): sleep(ms / 1000) return fn(*args) delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second
• Mar 26, 2023 •AustinLeath
import os # Get the current directory current_dir = os.getcwd() # Loop through each file in the current directory for filename in os.listdir(current_dir): # Check if the file name starts with a number followed by a period and a space if filename[0].isdigit() and filename[1] == '.' and filename[2] == ' ': # Remove the number, period, and space from the file name new_filename = filename[3:] # Rename the file os.rename(os.path.join(current_dir, filename), os.path.join(current_dir, new_filename))
0 likes • 10 views
""" 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)
• Nov 18, 2022 •AustinLeath
# @return a list of strings, [s1, s2] def letterCombinations(self, digits): if '' == digits: return [] kvmaps = { '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz' } return reduce(lambda acc, digit: [x + y for x in acc for y in kvmaps[digit]], digits, [''])
• Jun 1, 2023 •CodeCatch
0 likes • 2 views
from colorama import init, Fore # Initialize colorama init() 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 colorama print(Fore.RESET + "This text is back to the default color.")
0 likes • 1 view
# Python program for implementation of Radix Sort # A function to do counting sort of arr[] according to # the digit represented by exp. def countingSort(arr, exp1): n = len(arr) # The output array elements that will have sorted arr output = [0] * (n) # initialize count array as 0 count = [0] * (10) # Store count of occurrences in count[] for i in range(0, n): index = (arr[i]/exp1) count[int((index)%10)] += 1 # Change count[i] so that count[i] now contains actual # position of this digit in output array for i in range(1,10): count[i] += count[i-1] # Build the output array i = n-1 while i>=0: index = (arr[i]/exp1) output[ count[ int((index)%10) ] - 1] = arr[i] count[int((index)%10)] -= 1 i -= 1 # Copying the output array to arr[], # so that arr now contains sorted numbers i = 0 for i in range(0,len(arr)): arr[i] = output[i] # Method to do Radix Sort def radixSort(arr): # Find the maximum number to know number of digits max1 = max(arr) # Do counting sort for every digit. Note that instead # of passing digit number, exp is passed. exp is 10^i # where i is current digit number exp = 1 while max1/exp > 0: countingSort(arr,exp) exp *= 10 # Driver code to test above arr = [ 170, 45, 75, 90, 802, 24, 2, 66] radixSort(arr) for i in range(len(arr)): print(arr[i]),