0% found this document useful (0 votes)
92 views

Test Result: Escaped (Prisoners, Arrived, Departed)

The document describes two functions: 1. escaped(prisoners, arrived, departed) that takes lists of prisoner counts, arrivals, and departures each day and returns the number of escaped prisoners based on differences in counts. 2. bus_capacity(hop_in, hop_out, capacity) that takes lists of passenger boardings and exits for each stop and bus capacity, returns number of passengers who missed the bus due to full capacity.

Uploaded by

Nathaniel Lucas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Test Result: Escaped (Prisoners, Arrived, Departed)

The document describes two functions: 1. escaped(prisoners, arrived, departed) that takes lists of prisoner counts, arrivals, and departures each day and returns the number of escaped prisoners based on differences in counts. 2. bus_capacity(hop_in, hop_out, capacity) that takes lists of passenger boardings and exits for each stop and bus capacity, returns number of passengers who missed the bus due to full capacity.

Uploaded by

Nathaniel Lucas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a function 

escaped(prisoners, arrived, departed)  that takes three inputs prisoners


(list of integers), arrived (list of integers) and departed (list of integers). prisoners represent
the number of prisoners counted each day. arrived represents the newly arrived prisoners
each day, and departed is the newly departed prisoners each day. Sometimes, prisoners
jailbreak, and we can find out how many by finding the difference in numbers.
For example, if prisoners = [10, 25, 39, 49], arrived = [10, 16, 15, 12] and departed = [0, 0, 1,
1], then we have:

1. day 1: 10 arrived and 0 left, count is 10 and 10 found -> no prisoners escaped.
2. day 2: 16 arrived and 0 left, count is 26 but 25 found -> 1 prisoner escaped.
3. day 3: 15 arrived and 1 left, count is 39 and 39 found -> no prisoners escaped.
4. day 4: 12 arrived and 1 left, count is 50 but 49 found 0> 1 prisoner escaped.

Finally, the function returns the number of escaped prisoners (for the above example, 2).
Note, the size of all lists are the same, and the number of prisoners will not be negative.
NOTE: You are NOT allowed to use built-in sum function!
For example:

Test Result

print(escaped([10, 25, 39, 49], [10, 16, 15, 12], [0, 0, 1, 1])) 2

print(escaped([10, 25, 39, 49, 53, 63], [10, 16, 15, 12, 7, 10], [0, 0, 1,
1, 1, 0]))
Write a function bus_capacity(hop_in, hop_out, capacity) that takes two lists that contains
the number of passengers hopping on and off at each stop and the bus capacity. The
function returns the number of people who missed the bus due to the capacity being full.
The bus is empty at the beginning. Please note, passengers get off FIRST before others hop
on.
For example if hop_in = [4, 7, 2], hop_out = [0, 0, 1] and capacity 5, then:

1. at first stop: 0 hops off, 4 hops on. - no one missed bus.


2. at second stop: 0 hops off, only 1 can hop on. - 6 missed bus.
3. at third stop: 1 hops off, only 1 can hop on. - 1 missed bus.

Total, 7 people missed the bus. Hence, the function returns 7.


Note: the number of people hop off will never cause the bus to have a negative number of
passengers.

For example:

Test Result

print(bus_capacity([4, 7, 2], [0, 0, 1], 7


5))
This question is related to assignment 2, but you do not necessarily have completed
assignment 2 to complete this question.
Write a function proportion_converted(eng_sentence)  that converts the English
sentence into a  Hajanian sentence, and calculate the number of English words that has
been converted. For example:
Example1 - same sentence:
eng_sentence = "this apple tastes delicious but not as good as strawberry"
This converts into a Hajanian sentence:
"this apple tastes delicious but not as guwud as strawberryeeh"
This example will return 2 as a result (words "guwud" and "strawberryeeh" has been
changed).
The translate function has already been implemented for you (along with all other necessary
functions for translate function to work), which you are expected to use to solve this
question. The usage of the translate function is shown below:
eng_sentence = "this apple tastes delicious but not as good as strawberry"
dictionary = eng_sentence
haj_sentence = translate(eng_sentence, dictionary, "", "english")
#haj_sentence contains "this apple tastes delicious but not as guwud as strawberryeeh"

Note: We will treat all inputted English sentences as valid and does not require beautifying. 
For example:

Test Result

eng_text = "this apple tastes delicious but not as good as 2


strawberry"
print(proportion_converted(eng_text))

eng_text = "sunday lunch was yum but bacon was not cooked properly" 4
print(proportion_converted(eng_text))

eng_text = "never ever trust him" 0


print(proportion_converted(eng_text))

Answer:(penalty regime: 0, 10, ... %)


A king is in the mood to invade. He consults his wise counsellors, who will provide their
opinions in yes or no. If the king's mood is good, he will still go on invasion as long as
more than 1/3 counsellors agree. But on a typical day, more than half of the counsellors
must agree. But there is a catch: if the land to invade grows grapes, then he will invade
regardless.
write a function go_invading(decision, mood, has_grapes)  to see whether the king will
go invasion or not (return "it is a war!" if invading, else "maybe next time"). Please note
the following.
decision is the list of 1s and 0s representing yes and no, respectively by the counsellors.

mood is the king's mood in boolean. True represents he is in a good mood, else it is a


typical day.
has_grapes is boolean to state whether the invading land has grapes or not.

For example:

Test Result

print(go_invading([1, 1, 1, 0, 0, 0, 0], True, False)) it is a war!

print(go_invading([1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1], True, it is a war!


False))

print(go_invading([1, 0, 0, 0, 1, 0], True, False)) maybe next


time

You might also like