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
***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...
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...
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,...
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,...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report the total number of words (as an integer) and the average word length (as an un-rounded number). For example, suppose the file tobe.txt contains the following text: To be or not to be, that is the...
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: 1. Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that...
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...
ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as...
ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as an input. The first task is to compute the frequency of each character appearing in the string. In the output, the characters have to be arranged in the same order as they appear in the input string. Then characters have to be rearranged, such that all the characters having a specific frequency, say xx, come together. Let the frequency of a character, lying in...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT