Loading...
More Python Posts
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])
def calculate_values():value1 = 10value2 = 20return value1, value2result1, result2 = calculate_values()print("Result 1:", result1)print("Result 2:", result2)
def print_x_pattern(size):i,j = 0,size - 1while j >= 0 and i < size:initial_spaces = ' '*min(i,j)middle_spaces = ' '*(abs(i - j) - 1)final_spaces = ' '*(size - 1 - max(i,j))if j == i:print(initial_spaces + '*' + final_spaces)else:print(initial_spaces + '*' + middle_spaces + '*' + final_spaces)i += 1j -= 1print_x_pattern(7)
magnitude = lambda bits: 1_000_000_000_000_000_000 % (2 ** bits)sign = lambda bits: -1 ** (1_000_000_000_000_000_000 // (2 ** bits))print("64 bit sum:", magnitude(64) * sign(64))print("32 bit sum:", magnitude(32) * sign(32))print("16 bit sum:", magnitude(16) * sign(16))
""" 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)
class Solution(object):def floodFill(self, image, sr, sc, newColor):R, C = len(image), len(image[0])color = image[sr][sc]if color == newColor: return imagedef dfs(r, c):if image[r][c] == color:image[r][c] = newColorif r >= 1: dfs(r-1, c)if r+1 < R: dfs(r+1, c)if c >= 1: dfs(r, c-1)if c+1 < C: dfs(r, c+1)dfs(sr, sc)return image