QuestionQuestion

"""
Exercise 3
"""
import random

"""
0
"""

"""
define a function that computes the number of rolls of TWO dice required to roll a 2. Thus on each call of this function a different answer is likely to be returned

You will need to use random.randint(1,6) which returns a random integer between 1 and 6 inclusive
"""

def roll_until_2():
    """
    pre: True (requires nothing)
    post: \result = no. of rolls of 2 dice
    until a 2 turns up
    """
    pass


"""
Now perform the preceding experiment 1000 times and return the average number of rolls to get a 2. What answer do you expect.
remember: averages are floats
"""

   
"""
Define a function of one int input n >0 that does the preceding action n times (instead of 1000)
WITHOUT calling roll_until_2. i.e. the whole thing should be self contained and should have a loop within a loop.
"""


"""
1
"""

def insert_in_place(item,lst):
    """
    item:int
    lst:ORDERED list of ints
    pre: 0<= i < j<=len(lst) --> lst[i] <= lst[j]
    lst could be empty!!!
    ensures:\result = ORDERED list of length len(lst) + 1
    resulting from inserting item into lst in its proper place.
    i.e.:
    (x in lst -> x in \result), and item in \result
    and len(\result) = len(lst) + 1
    note that insert_in_place(3,[]) ==> [3]
    insert_in_place(300,[1,2,3] ==> [1,2,3,300]

    use while loop and give a loop invariant
    """
    pass


#TESTS
print insert_in_place(300,[1,2,30]),[1,2,30,300]
print insert_in_place(20,[1,2,30]),[1,2,20,30]


"""
2
"""


def longest_upsequence(lst):
    """
    pre:lst = list of ints
    post:\result = length of longest subsequence of
    lst which is *strictly* ordered, i.e. increasing (each member < next)

    longest_upsequence([1,2,0,-1,0,2,5,3,2,4,5]) ==> 4
    (namely the length of the sequence [-1,0,2,5])
    """
    pass


"""
3
"""

"""

In the next problem, an increasing run of numbers means a sequence of *consecutive* numbers, such as 1, 2, 3 or 2, 3, 4, 5.
The length of an increasing run is the number of numbers in it (so 2, 3, 4, 5 has length 4), and the start of an increasing run is the first number in it.


Write a function that receives two parameters, num_runs and run_length, and returns a list containing num_runs increasing runs of consecutive numbers, each of length run_length, and each starting one number higher than the previous one, with the first one starting at 0.

For example, increasing_runs(2,2) should return a list with two increasing runs, each of length 2,
where the first starts with 0 (so 0, 1), and the second starts with 1 (so 1, 2):
[ 0 1 1 2 ]

increasing_runs(3,2) should return a list with three increasing runs, each of length 2, the first starting with 0 (so 0, 1) then 1 (so 1, 2) then 2 (so 2, 3):
[ 0 1 1 2 2 3 ]

increasing_runs(2,3) should return a list with two increasing runs, each of length 3, starting with 0 (so (0, 1, 2)) then 1 (so 1, 2, 3).
[ 0 1 2 1 2 3 ]
"""


def increasing_runs(num_runs, run_length):
    """
    num_runs,run_length:int>0
    post: as described above
    """
    pass

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

def roll_until_2():
    """
    pre: True (requires nothing)
    post: \result = no. of rolls of 2 dice
    until a 2 turns up
    """
    count = 0
    while True:
       count += 1
       die1 = random.randint(1,6)
       die2 = random.randint(1,6)
       if die1 == 2 or die2 == 2:
            return count...

By purchasing this solution you'll be able to access the following files:
Solution.py.

$38.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats