You are currently viewing a revision titled "problem #1in Python", saved on September 20, 2023 at 8:50 am by George Vanishvili | |
---|---|
Title | problem #1in Python |
Content | Problem #1 in Pyhhon:
write the function nearestBusStop(street) that takes a nonnegative number
and returns the nearest bus stop to the given street, where buses stop every
8th street including street 0, and tries go to the lower street , so the nearest
bus stop to 12th street is 8th street, and the nearest bus stop to the 13th
street is 16th street
Solution:
file containing finction, named nearestBusStopGV.py:
import math
def nearestBusStopGV(street):
#street= int( input("Enter integer:") )
print(street)
n = street // 8
k= street % 8
print("Street # devide on 8 is", n)
print("Street # % on 8 is", k)
if (k > 0 and k<= 4) :
#print(n)
stop = n
elif (k >= 5 and k < 8):
#print(n+1)
stop = n + 1
else:
("nothing")
print("if Street# is", street, "then Bus Driver must stop the bus on the stop # = ", stop)
file containig main programm, for testing the function, named with any name testNearestBusStopGV.py:
def testNearestBusStop():
print("Testing nearestBusStop()...", end = "")
assert(nearestBusStop(0) == 0)
assert(nearestBusStop(4) == 0)
assert(nearestBusStop(5) == 8)
assert(nearestBusStop(12) == 8)
assert(nearestBusStop(13) == 16)
assert(nearestBusStop(20) == 16)
assert(nearestBusStop(21) == 24)
print("Passed. (Add more tests to be sure!)")
testNearestBusStop()
Verbal explanation of the solution:
If a passenger lives on the 77th street, then he/she must ask CarDriver to stop on the Bus Stop #10
if lives in the 12 th Street, ask to stop on the BusStop #1
if lives in the 15th Street, ask to stop on the BusStop #2
|
Excerpt | |
Footnotes |