Skip to main content

UNT CSCE 2100 Question 1

0 likes • Nov 18, 2022 • 1 view
Python
Loading...

More Python Posts

delay time lambda

0 likes • Nov 19, 2022 • 0 views
Python
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

Distinct Primes Finder > 1000

0 likes • Nov 18, 2022 • 3 views
Python
primes=[]
products=[]
def prime(num):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
return False
else:
primes.append(num)
return True
for n in range(30,1000):
if len(primes) >= 20:
break;
else:
prime(n)
for previous, current in zip(primes[::2], primes[1::2]):
products.append(previous * current)
print (products)

Delete all even numbers

0 likes • Nov 19, 2022 • 0 views
Python
# Deleting all even numbers from a list
a = [1,2,3,4,5]
del a[1::2]
print(a)

Differentiate Between type() and instance()

0 likes • May 31, 2023 • 0 views
Python
class Rectangle:
pass
class Square(Rectangle):
pass
rectangle = Rectangle()
square = Square()
print(isinstance(rectangle, Rectangle)) # True
print(isinstance(square, Rectangle)) # True
print(isinstance(square, Square)) # True
print(isinstance(rectangle, Square)) # False

sum of powers

0 likes • Nov 19, 2022 • 0 views
Python
def sum_of_powers(end, power = 2, start = 1):
return sum([(i) ** power for i in range(start, end + 1)])
sum_of_powers(10) # 385
sum_of_powers(10, 3) # 3025
sum_of_powers(10, 3, 5) # 2925

Currency Converter

0 likes • Nov 19, 2022 • 0 views
Python
""" 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)