input_str = 'zoo apple carrot bike dragon boat anyone' spaces = input_str.count(' ') index = 1 input_str = input_str + ' ' while index <= spaces: slice_str = input_str for i in range(1,index): slice_str = slice_str[slice_str.find(' ')+1:] prev_str = input_str[:len(input_str) - len(slice_str)] #print(index) #print('current : ' + slice_str) #print('previous: ' + prev_str) word1 = slice_str[0:slice_str.find(' ')] #print('word1 = ' + word1) word2 = slice_str[len(word1)+1:slice_str.find(' ' , slice_str.find(' ')+1) ] #print('word2 = ' + word2) last_str = slice_str[len(word1)+len(word2)+1:] #print('remain:' + last_str) if word1 > word2: #print ('*swap*') input_str = prev_str.rstrip(' ') + ' ' + word2 + ' ' + word1 + ' ' + last_str.lstrip(' ') #print("after swap:" + input_str) input_str = input_str.lstrip(' ') index = 0 #final check if (index==spaces): break index+=1 print(input_str)
import random import numpy as np def dice(): #Roll the dice! return random.randint(1,6) def rain(prob): #Down came the rain and washed the spider out? r = random.random() #Random number between 0 and 1 return (r<prob) #If number less than 'prob', its raining! (return 'true') def game(prain,length): #Play one game until you win, note moves taken n = 0 #Start at the bottom i = 0 #Rest counter while (n<length): #Keep playing until score is at least "length" i=i+1 #Increase 'go' counter d = dice() #Roll the dice! n = n + d #Add your score if rain(prain): #Is it raining n=0 #LOSE YOUR POINTS! return i def manygames(NMAX,length,prain): #Repeat game to accumulate statistics g = list() #Name of list to put results in for x in range(NMAX): #Repeat NMAX games! goes = game(prain,length) #Play one game g.append(goes) #Add the number of goes to the summary r = np.asarray(g) #COnvert python list to numerical array return r #Return the results def cumulative(y,p): #After how many goes is cumulative prob > p? c = 0 i = 0 while (c<=p): #Loop over histogram i = i + 1 #Bin number c = c + y[i] #cumulative probability return i def analyse(rs): #Analyse the results! print 'Average goes/game = {0}'.format(np.mean(rs)) bb = np.bincount(rs) bins = np.arange(0.5,rs.max()+0.5,1) #Histogram bins h = np.histogram(rs, bins, density=True) #Bin data x = h[1][:] #Bins in 2nd column y = h[0][:] #data in 1st column cp5 = cumulative(y,0.5) #Goes until cumul.P > 0.5? print 'Half of games take {0} goes or fewer'.format(cp5) cp95 = cumulative(y,0.95) #until cumululative P >0.95? print '95% of games are complete after {0} goes'.format(cp95) return y data1 = manygames(100000,10,0.5) #Incy Game print 'In 10 step Incy, with a 50% chance of rain...' y1 = analyse(data1) data2 = manygames(10000,35,0.0) #Ludo print '\n In 35 step Ludo (with no rain)...' y2 = analyse(data2)
#-*- coding: cp1252 -*- # # ------------------------------------------# # Agenda eletronica em Python. # # @autor: Jhonathan Paulo Banczek # # data:05/09/2010 # # ------------------------------------------# #------------------------------------------------------------------------------# #--------------------GLOBAL--------------------# lista = [] #define uma variavel para a lista #le os dados do arquivo e joga pra lista arq2 = open("agenda.csv","a+") lista = arq2.readlines() arq2.close() #-----------------------------------------------# #------------------------------------------------------------------------------# #função: menu, constroi o menu def menu(): print "\n", "=" * 50 print """\nAGENDA ELETRONICA\n\n escolha a opção: \n (1) Inserir contato:\n (2) Deletar:\n (3) Mostrar agenda:\n (4) finalizar programa:\n""" #fim da função menu #------------------------------------------------------------------------------# #função: lista_agenda, adiciona, remove e mostra a lista def lista_agenda(nome, data, opc): if( opc == 1): contato = nome + ";" + data + "\n" #concatenao o 'nome' ';' e 'data' lista.append(contato) lista.sort() #ordena a lista por prioridade elif (opc == 2): print "=" * 30 if ( lista == []): #se lista for vazia print "Lista Vazia\n" else: lista.pop(0)#remove o elemento de maior prioridade, ou seja, indice 0 da lista print "Removido o elemento de maior prioridade" print "=" * 30 elif (opc == 3): print "=" * 30 if (lista == []): print "Lista vazia!" else: print "Nome: | Data:" for i in lista: print i print "=" * 30 elif (opc == 4): arq = open("agenda.csv","w") #escreve por cima do arquivo antigo (atualiza lista) tam = len(lista) #recebe o tamanho da lista #for que insere os valores no arquivo separado por ';' sendo nome = indice par e data = impar for i in range(tam): arq.write(lista[i]) arq.close() #fim da função lista_agenda #------------------------------------------------------------------------------# #funcao principal opc = 0 #variavel pro menu while (opc != 4 ): menu() #chama o menu while True: try: opc = int(raw_input(': ')) #recebe a opção break except: print "digite só numeros válidos" if ( opc == 1 ): print "Informe o nome do contato: \n" nome = raw_input(': ') print "informe a data de nascimento: \n" data = raw_input(': ') lista_agenda(nome, data, 1) #chama a função para inserir na lista elif ( opc == 2): lista_agenda(0,0,2) #chama a função para deletar o nome na lista elif (opc == 3): #chama a função para mostrar na tela print "Lista de contatos: " lista_agenda(0,0,3) lista_agenda(0,0,4) #grava a lista na agenda print "FIM DO PROGRAMA! " #------------------------------------------------------------------------------# #fim
# Hello World program in Python FirstName= "Joshua " LastName= "Kim" FullName= FirstName+LastName print(FullName) Number1= "10" Number2= "20" Calculation= Number1+Number2 print (Calculation)
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more