Loading...
More Python Posts
from collections import Counterdef find_parity_outliers(nums):return [x for x in numsif x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0]]find_parity_outliers([1, 2, 3, 4, 6]) # [1, 3]
""" Rock Paper Scissors----------------------------------------"""import randomimport osimport reos.system('cls' if os.name=='nt' else 'clear')while (1 < 2):print "\n"print "Rock, Paper, Scissors - Shoot!"userChoice = raw_input("Choose your weapon [R]ock], [P]aper, or [S]cissors: ")if not re.match("[SsRrPp]", userChoice):print "Please choose a letter:"print "[R]ock, [S]cissors or [P]aper."continue// Echo the user's choiceprint "You chose: " + userChoicechoices = ['R', 'P', 'S']opponenetChoice = random.choice(choices)print "I chose: " + opponenetChoiceif opponenetChoice == str.upper(userChoice):print "Tie! "#if opponenetChoice == str("R") and str.upper(userChoice) == "P"elif opponenetChoice == 'R' and userChoice.upper() == 'S':print "Scissors beats rock, I win! "continueelif opponenetChoice == 'S' and userChoice.upper() == 'P':print "Scissors beats paper! I win! "continueelif opponenetChoice == 'P' and userChoice.upper() == 'R':print "Paper beat rock, I win! "continueelse:print "You win!"
def max_n(lst, n = 1):return sorted(lst, reverse = True)[:n]max_n([1, 2, 3]) # [3]max_n([1, 2, 3], 2) # [3, 2]
#question1.pydef rose(n) :if n==0 :yield []else :for k in range(0,n) :for l in rose(k) :for r in rose(n-1-k) :yield [l]+[r]+[r]def start(n) :for x in rose(n) :print(x) #basically I am printing x for each rose(n) fileprint("starting program: \n")start(2) # here is where I call the start function
# Deleting all even numbers from a lista = [1,2,3,4,5]del a[1::2]print(a)
""" 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)