Você está na página 1de 6

Codigos en python

----------------------------------------------------------------------------------------------------------------------------------# Write Python code that assigns to the /escribir un cdigo que asigne
# variable url a string that is the value /a la variable url una cadena que es el valor
# of the first URL that appears in a link /de la primera URL que aparece en el link
# tag in the string page./asociada a la cadena de la pagina
# page = contents of a web page/contenido de una pagina web
page =('<div id="top_bin"><div id="top_content" class="width960">'
'<div class="udacity float-left"><a href="http://udacity.com">')
start_link = page.find('<a href=')
end_link = page.find (">",start_link)
url = page[start_link+9: end_link-1]
----------------------------------------------------------------------------------------------------------------------------------# Given the variables s and t defined as:/dadas las variables s y t
s = 'udacity'
t = 'bodacious'
# write Python code that prints out udacious/escriba un cdigo que imprima udacious
# without using any quote characters in/sin usar ninguna letra
# your code./en el codigo
print s[:3] + t[4:]
----------------------------------------------------------------------------------------------------------------------------------# Hint: The str function can convert any number into a string.
# eg str(89) converts the number 89 to the string '89'
# Along with the str function, this problem can be solved
# using just the information introduced in unit 1.
# x = 3.14159
# >>> 3 (not 3.0)
# x = 27.63
# >>> 28 (not 28.0)
# x = 3.5
# >>> 4 (not 4.0)
x = 3.14159
#ENTER CODE BELOW HERE
y = x + 0.5
z = str(y)
pos = z.find(".")
print z[:pos]
-----------------------------------------------------------------------------------------------------------------------------------

fragA, fragB, fragC = 'supercalifragilisticexpialudacious', \


'SUPERMAN', 'ytiroirepus'
part1, part2, part3 = fragB[1],fragA[-7:-4],fragC[2::-1]
print part1 + part2[0:2] + part3[-1] / debe ser Uday
print part1 + part2 + part3 / debe ser Udacity
print part1 + part2[-1] + part3/ debe ser Ucity
----------------------------------------------------------------------------------------------------------------------------------# Replace the first occurrence of marker in the line below.
# There will be at least one occurence of the marker in the
# line of text. Put the answer in the variable 'replaced'.
# Hint: You can find out the length of a string by command
# len(string). We might test your code with different markers!
# Example 1
marker = "AFK"
replacement = "away from keyboard"
line = "I will now go to sleep and be AFK until lunch time tomorrow."
# YOUR CODE BELOW. DO NOT DELETE THIS LINE
replaced = line[:line.find("AFK")]+replacement+line[line.find("AFK")+3:]
print replaced
# Example 1 output should be:
#>>> I will now go to sleep and be away from keyboard until lunch time tomorrow.
----------------------------------------------------------------------------------------------------------------------------------# Example 1
marker = "AFK"
replacement = "away from keyboard"
line = "I will now go to sleep and be AFK until lunch time tomorrow."
# YOUR CODE BELOW. DO NOT DELETE THIS LINE
end= len(marker)
hit=line.find(marker)
replaced = line[:hit]+replacement+line[hit+end:]
print replaced
----------------------------------------------------------------------------------------------------------------------------------Transformar un valor en monedas de 5,2 y 1 pesos.
def stamps(a):
# Your code here
a1=0
a2=0
a3=0
r=a
while r!=0:
r=r-5
if r<0:
r=r+5

r=r-2
if r<0:
r=r+2
r=r-1
a3=a3+1
else:
a2=a2+1
else:
a1=a1+1
return (a1,a2,a3)

print stamps(8)
#>>> (1, 1, 1) # one 5p stamp, one 2p stamp and one 1p stamp
print stamps(5)
#>>> (1, 0, 0) # one 5p stamp, no 2p stamps and no 1p stamps
print stamps(29)
#>>> (5, 2, 0) # five 5p stamps, two 2p stamps and no 1p stamps
print stamps(0)
#>>> (0, 0, 0) # no 5p stamps, no 2p stamps and no 1p stamps
print stamps(50)
----------------------------------------------------------------------------------------------------------------------------------Buscar cada letra dentro de otro texto
def fix_machine(debris, product):
a=0
while a<=len(product):
b=product[a]
c=debris.find(b)
if c<0:
return "Give me something that's not useless next time."
break
else:
a=a+1
if a==len(product):
return product
### WRITE YOUR CODE HERE ###
### TEST CASES ###
print "Test case 1: ", fix_machine('UdaciousUdacitee', 'Udacity') == "Give me something that's not
useless next time."
print "Test case 2: ", fix_machine('buy me dat Unicorn', 'Udacity') == 'Udacity'
print "Test case 3: ", fix_machine('AEIOU and sometimes y... c', 'Udacity') == 'Udacity'
print "Test case 4: ", fix_machine('wsx0-=mttrhix', 't-shirt') == 't-shirt'
---------------------------------------------------------------------------------------------------------------------------------Regresa como valor el dia siguiente, suponiendo que son meses de 30 dias
def nextDay(year, month, day):

"""
Returns the year, month, day of the next day.
Simple version: assume every month has 30 days.
"""
# YOUR CODE HERE
if day <=29:
day = day + 1
else:
day = 1
if month < 12:
month = month + 1
else:
month = 1
year=year+1
return(year,month,day)
----------------------------------------------------------------------------------------------------------------------------------def nextDay(year, month, day):
"""Simple version: assume every month has 30 days"""
if day < 30:
return year, month, day + 1
else:
if month == 12:
return year + 1, 1, 1
else:
return year, month + 1, 1
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
"""Returns the number of days between year1/month1/day1
and year2/month2/day2. Assumes inputs are valid dates
in Gergorian calendar, and the first date is not after
the second."""
# YOUR CODE HERE!
reachDate = year2,month2,day2
startDate = year1,month1,day1
result = 0
while reachDate != startDate:
result += 1
startDate = nextDay(startDate[0],startDate[1],startDate[2])
return result

----------------------------------------------------------------------------------------------------------------------------------Calcula los das entre dos fechas


def isLeapYear(year):
if year % 400 == 0:
return True
if year % 100 == 0:
return False
if year % 4 == 0:

return True
return False
def daysInMonth(year, month):
if month == 2:
if isLeapYear(year):
return 29
return 28
if month == 4 or month == 6 or month ==9 or month == 11:
return 30
return 31
def nextDay(year, month, day):
"""Simple version: assume every month has 30 days"""
if day < daysInMonth(year, month):
return year, month, day + 1
else:
if month == 12:
return year + 1, 1, 1
else:
return year, month + 1, 1
def dateIsAfter(year1, month1, day1, year2, month2, day2):
"""Returns True if year1-month1-day1 is after year2-month2-day2. Otherwise, returns False."""
if year1 > year2:
return True
if year1 == year2:
if month1 > month2:
return True
if month1 == month2:
return day1 > day2
return False
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
"""Returns the number of days between year1/month1/day1
and year2/month2/day2. Assumes inputs are valid dates
in Gregorian calendar."""
# program defensively! Add an assertion if the input is not valid!
assert dateIsAfter(year1, month1, day1, year2, month2, day2)==False
days = 0
while dateIsAfter(year2, month2, day2, year1, month1, day1):
days += 1
(year1, month1, day1) = nextDay(year1, month1, day1)
return days
def test():
test_cases = [((2012,1,1,2012,2,28), 58),
((2012,1,1,2012,3,1), 60),
((2011,6,30,2012,6,30), 366),
((2011,1,1,2012,8,8), 585 ),
((1900,1,1,1999,12,31), 36523)]
for (args, answer) in test_cases:

result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()
------------------------------------------------------------------------------------------------------------------------------------------------

Você também pode gostar