You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
get app secret from environment variable defaulting to None
# get app secret from environment default to Noneapp_secret=os.getenv('APP_SECRET', None)
sort list of name descending
names= ['Cecil', 'Adam', 'Brian']
# sort list of names descendingdefsort_names(names):
returnsorted(names, reverse=True)
print(sort_names(names))
calculate n factorial
# calculate n factorialdeffactorial(n):
ifn==0:
return1else:
returnn*factorial(n-1)
fibonaci
# print fibonacci sequencedeffib(n):
a, b=0, 1whileb<n:
print(b, end=' ')
a, b=b, a+bprint(fib(1000))
# calculate fibonacci sequence simple waydeffib(n):
a, b=0, 1foriinrange(n):
a, b=b, a+byieldaforninfib(10):
print(n)
# calculate fibonacci sequence recursivelydeffib(n):
ifn==0:
return0elifn==1:
return1else:
returnfib(n-1) +fib(n-2)
print(fib(10))
get bitcoind price from coinbase
importrequests# Get bitcoin pricer=requests.get('https://api.coindesk.com/v1/bpi/currentprice/BTC.json')
print(r.json())
# get bitcoind price from coinbasedefget_price(currency):
url='https://api.coinbase.com/v2/exchange-rates?currency='+currencyr=requests.get(url)
ifr.status_code==200:
returnr.json()['data']['rates']['USD']
else:
returnNoneprint(get_price('BTC'))
get covid stats from jhcovid.org and write it to file data.json
# get covid stats from jhcovid.org and write it to file data.jsonr=requests.get('https://jhcovid.org/api/stats/')
withopen('data.json', 'w') asf:
f.write(r.text)
# get tesla stock price from yahoo financedefget_stock_price(stock_name):
url='http://finance.yahoo.com/q?s='+stock_nameresponse=requests.get(url)
returnresponse.text# get covid 19 cases in Denmark and write to filedefget_covid_cases():
# get cases from covid websitecovid_cases=requests.get(
'https://api.covid19api.com/countries/dk/states/copenhagen/cases'
)
covid_cases=covid_cases.json()
covid_cases=covid_cases['count']
covid_cases=str(covid_cases)
returncovid_cases# get covid19 stats from john hopkins api and save to filedefget_covid19(country="Denmark"):
covid19_url="https://api.johnhopkins.edu/covid19/countries/{}/".format(country)
covid19_response=requests.get(covid19_url)
covid19_data=covid19_response.json()
covid19_data=covid19_data["data"]
covid19_data=covid19_data["countries"]
covid19_data=covid19_data[0]
covid19_data=covid19_data["cases"]
covid19_data=covid19_data["confirmed"]
covid19_data=covid19_data["deaths"]
covid19_data=covid19_data["recovered"]
covid19_data=covid19_data["active"]
covid19_data=covid19_data["total_cases"]
covid19_data=covid19_data["total_deaths"]
covid19_data=covid19_data["total_active"]
covid19_data=covid19_data["total_recovered"]
covid19_data=covid19_data["total_cases_last_week"]
covid19_data=covid19_data["total_deaths_last_week"]
covid19_data=covid19_data["total_active_last_week"]
covid19_data=covid19_data["total_recovered_last_week"]
covid19_data=covid19_data["total_cases_this_week"]
covid19_data=covid19_data["total_deaths_this_week"]
withopen("covid19.json", "w") ascovid19_file:
json.dump(covid19_data, covid19_file)
JavaScript
// reverse stringfunctionreverseString(str){returnstr.split("").reverse().join("");}// make ajax requestfunctionmakeAjaxRequest(url,method,data,callback){$.ajax({url: url,type: method,data: data,success: callback}).success(function(data){console.log(data);}).error(function(data){console.log(data);})}// reload pagefunctionreloadPage(){window.location.reload();}// alert user that his mama is getting to fatfunctionmamaAlert(){alert("Your mama is getting to fat. She needs to lose weight by the end of the month!");}// alert user that his mama is awesome every 5 secondsfunctionmamaAwesome(){setTimeout(function(){alert('Your mom is awesome!');},5000);}