Quiz: Word Count

def count_words(p):
    result = p.split()
    return len(result)

passage =("The number of orderings of the 52 cards in a deck of cards "
"is so great that if every one of the almost 7 billion people alive "
"today dealt one ordering of the cards per second, it would take "
"2.5 * 10**40 times the age of the universe to order the cards in every "
"possible way.")
print count_words(passage)
#>>>56

Quiz : Latency

이렇게 되는 이유좀..

# Write a procedure, speed_fraction, which takes as its inputs the result of
# a traceroute (in ms) and distance (in km) between two points. It should 
# return the speed the data travels as a decimal fraction of the speed of
# light.

speed_of_light = 300000. # km per second

def speed_fraction(ms, km):
    ms_s_o_l = speed_of_light/1000
    between = km/ms_s_o_l/ms*2
    return between



print speed_fraction(50,5000)
#>>> 0.666666666667

print speed_fraction(50,10000)
#>>> 1.33333333333  # Any thoughts about this answer, or these inputs?

Quiz: Converting Seconds

def result(h, m ,s):
    hstr = str(int(h)) + " hour"
    mstr = ", "+ str(int(m)) + " minute"
    sstr = ", " + str(s) + " second"
    if h > 1 or h == 0:
        hstr = hstr + "s"
    if m > 1 or m == 0:
        mstr = mstr + "s"
    if s > 1 or s == 0:
        sstr = sstr + "s"

    return hstr + mstr + sstr

def convert_seconds(time):
    if time > 59:
        min = time/60
        second = time%60
        if min > 59:
            hour = min/60
            min = min%60
        else:
            hour = 0
    else:
        hour = 0
        min = 0
        second = time


    return result(int(hour),int(min),second)

print convert_seconds(3661)
#>>> 1 hour, 1 minute, 1 second

print convert_seconds(7325)
#>>> 2 hours, 2 minutes, 5 seconds

print convert_seconds(7261.7)
#>>> 2 hours, 1 minute, 1.7 seconds

print convert_seconds(3661)
#>>> 1 hour, 1 minute, 1 second

Quiz: Download Calculator

이거풀면서 진짜 재밌다는걸 느꼈음.

# Write a procedure download_time which takes as inputs a file size, the
# units that file size is given in, bandwidth and the units for
# bandwidth (excluding per second) and returns the time taken to download 
# the file.
# Your answer should be a string in the form
# "<number> hours, <number> minutes, <number> seconds"

# Some information you might find useful is the number of bits
# in kilobits (kb), kilobytes (kB), megabits (Mb), megabytes (MB),
# gigabits (Gb), gigabytes (GB) and terabits (Tb), terabytes (TB).

#print 2 ** 10      # one kilobit, kb
#print 2 ** 10 * 8  # one kilobyte, kB

#print 2 ** 20      # one megabit, Mb
#print 2 ** 20 * 8  # one megabyte, MB

#print 2 ** 30      # one gigabit, Gb
#print 2 ** 30 * 8  # one gigabyte, GB

#print 2 ** 40      # one terabit, Tb
#print 2 ** 40 * 8  # one terabyte, TB

# Often bandwidth is given in megabits (Mb) per second whereas file size 
# is given in megabytes (MB).

def result(h, m ,s):
    ...생략...


def convert_seconds(time):
    ...생략...

def download_time(a,b,c,d):
    unit = ['k','M','G','T']
    b_index = unit.index(b[0])
    d_index = unit.index(d[0])
    bB = b[1]
    dB = d[1]

    if b_index > d_index:
        time = (a*2**10*(b_index-d_index))/c
    elif b_index < d_index:
        time = a/(c*2**10*(d_index-b_index))
    else:
        time = a/c

    if bB == dB:
        return convert_seconds(time)
    elif bB == 'B':
        return convert_seconds(time*8)
    else:
        return convert_seconds(time/8)

print download_time(11,'GB', 5, 'MB')
# 0 hours, 37 minutes, 32.8 seconds

# print download_time(10,'MB', 2, 'kB')
# #>>> 1 hour, 25 minutes, 20 seconds  # 20.0 seconds is also acceptable

# print download_time(10,'MB', 2, 'kb')
# #>>> 11 hours, 22 minutes, 40 seconds  # 40.0 seconds is also acceptable

# print download_time(1024,'kB', 1, 'MB')
# #>>> 0 hours, 0 minutes, 1 second

# print download_time(1024,'kB', 1, 'Mb')
# #>>> 0 hours, 0 minutes, 8 seconds  # 8.0 seconds is also acceptable

# print download_time(13,'GB', 5.6, 'MB')
# #>>> 0 hours, 39 minutes, 37.1428571429 seconds

# print download_time(13,'GB', 5.6, 'Mb')
# #>>> 5 hours, 16 minutes, 57.1428571429 seconds

results matching ""

    No results matching ""