Question

1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function...

1. Write 3n + 1 Sequence String Generator function (1 point) In lab5.py complete the function sequence that takes one parameter: n, which is the initial value of a sequence of integers. The 3n + 1 sequence starts with an integer, n in this case, wherein each successive integer of the sequence is calculated based on these rules: 1. If the current value of n is odd, then the next number in the sequence is three times the current number plus one. 2. If the current value of n is even, then the next number in the sequence is half of the current number. Note: Make sure to use integer division. 3. Integers in the sequence are generated based the two rules above until the current number becomes 1. Your function should start with an empty string and append characters to the string as described next. The function keeps calculating the numbers in the 3n + 1 sequence, and while there are still numbers left in the sequence (meaning that the number has not reached 1 yet), it keeps appending letters to the string based on these rules: 1. If the number is divisible by 2, then append 'A' to the string. 2. If the number is divisible by 3, then append 'B' to the string. 3. If the number is divisible by 5, then append 'C' to the string. 4. If the number is divisible by 7, then append 'D' to the string. Note: The original number n also should contribute a letter (or letters) to the string. Also, append letters to your accumulating string for all conditions met above. That means, for example, if the current number is 6, the letters 'A' and 'B' will both be appended to the string, in that order. Apply the rules in the order given. For example, for the number 6, your code must append the letters in the order 'AB', not 'BA'. Note: If you find any helper functions useful, feel free to use them. In fact, I suggest that you try to define and use helper functions. This idea applies not only to this problem, but to any problem. In the end, your function should return the string generated (accumulated) by the procedure described above. If n initially is less than 1, the function returns an empty string. Example calls: sequence(4) # returns 'AA' sequence(12) # returns 'ABABBACCAAAA' sequence(24) # returns 'ABABABBACCAAAA'

Homework Answers

Answer #1

Hi, Please find the code below. I have named the function "sequence_generator" which takes a number "n" as input.

def sequence_generator(n):

    #if input smaller than 1 return empty string
    if n<1:
        return ''

    output_string=''
    #keep generating sequence while current number > 1
    while n>1:

        if n%2==0:
            output_string += 'A'
        if n%3==0:
            output_string += 'B'
        if n%5==0:
            output_string += 'C'
        if n%7==0:
            output_string += 'D'
        
        # if n is even then next number is half of current number
        if n%2 == 0:
            n = n//2
        # if n is odd then next number is 3 times current number plus one.
        else:
            n = 3*n+1
    
    return output_string
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an OCaml function named is_square that takes as input an integer, say n, (which may...
Write an OCaml function named is_square that takes as input an integer, say n, (which may be positive or negative) and returns true if that number is square. That is, it returns true if there exists an an integer, say m, if m * m = n. Next, write a function named count_squares that calls count from above to determine the number of squares in a list of integers. For example, count_squares [2;3;5] = 0 count_squares [1;2;3;4;5;6;7;8;9] = 3
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...
Please follow the insturctions and solve it by C++ ASAP. The hailstone sequence is a series...
Please follow the insturctions and solve it by C++ ASAP. The hailstone sequence is a series of numbers that can be generated with an algorithm that takes an input number n. The sequence is defined as: If n is 1, the sequence stops. If n is even, the next number is n / 2. If n is odd, the next number is 3n + 1. For example, if we start with n = 10, the sequence is: 10, 5, 16,...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n,...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n, a positive integer, it returns a list of n tuples corresponding to the numbers 1 through n (both inclusive): the tuple for the number k consists of k as the first component, and exactly one of the following strings as the second: • the string 'two!' if k is divisible by 2 • the string 'three!' if k is divisible by 3 • the...
Write a Python program to ask user input of a string, then ask user input of...
Write a Python program to ask user input of a string, then ask user input of what letters they want to remove from the string. User can either put in "odd", "even" or a number "n" that is greater than 2. When user input "odd", it will remove the characters which have odd numbers in the sequence of the string. When user input "even", it will remove the characters which have even numbers in the sequence of the string. When...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return...
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return the number of positive and negative in the array through the use of parameters. In addition, it also returns the number of 0s in the array as the return value. For example, array of {10, 20, -30, -40, 50, 0, 60} will return 4 for positives, 2 for negatives and 1 for 0's as the return value. 2. Write a function named "rotateRight" that...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
Please Write the whole program in assembly i am able to calculate the fibonacci series but...
Please Write the whole program in assembly i am able to calculate the fibonacci series but not sure how to print it in reverse order. Please give a Complete code !! Programming Exercise 1 (10 points): [call it Ass2-Q1.asm] Write an ASM program that reads an integer number N and then displays the first N values of the Fibonacci number sequence, described by: Fib(0) = 0, Fib(1) = 1, Fib(N) = Fib(N-2) + Fib(N-1) Thus, if the input is N...
Create a function called, convert. This function receives a string parameter called word which only contains...
Create a function called, convert. This function receives a string parameter called word which only contains digits (the string represents a positive number) and returns a list of numbers. This is how the function works: - This function calculates the number of times each digit has repeated in the input string and then generates a number based on that using the following formula and adds it to a list. For instance, if the digit x has been repeated n times,...