Question

Python: Using your favorite text editor, write a program that outputs the string representation of numbers...

Python: Using your favorite text editor, write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Submit by uploading a .py file

Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

Use this code as your starting point:

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        """FizzBuzz
        
        Returns a list of numeric strings starting with '1'.
        Multiples of 3 become 'Fizz'.
        Multiples of 5 'Buzz'.
        Multiples of 3 and 5, 'FizzBuzz'
        """
    # your code here. Feel free to add helper methods
0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Solution:
    def fizzBuzz(self, n):
        """FizzBuzz

        Returns a list of numeric strings starting with '1'.
        Multiples of 3 become 'Fizz'.
        Multiples of 5 'Buzz'.
        Multiples of 3 and 5, 'FizzBuzz'
        """
        lst = []
        for i in range(1,n+1):
            if(i%3==0 and i%5==0):
                lst.append("FizzBuzz")
            elif(i%3==0):
                lst.append("Fizz")
            elif (i % 3 == 0):
                lst.append("Buzz")
            else:
                lst.append(i)
        return lst
Add a comment
Know the answer?
Add Answer to:
Python: Using your favorite text editor, write a program that outputs the string representation of numbers...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3 coding with AWS/Ide. Objective: The purpose of this lab is for you to become familiar with Python’s built-in te...

    Python 3 coding with AWS/Ide. Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str-- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents...

  • Your friend is writing a Python program to randomly provide the user with an inspiring quote....

    Your friend is writing a Python program to randomly provide the user with an inspiring quote. Unfortunately, your friend is a terrible programmer, and you promised to help them with their program. Basically, there is a list of inspirational sayings in the code, and the program enters a loop and gives the user a random quote. The program is then supposed to ask the user if they want another quote. If they answer anything other than ‘y’ the program ends....

  • Your mission in this programming assignment is to create a Python program that will take an...

    Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents.   Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...

  • Programming With the List of Elevations, Latitudes, & Longitudes Pseudocode Using CodeHS Sandbox, write a program...

    Programming With the List of Elevations, Latitudes, & Longitudes Pseudocode Using CodeHS Sandbox, write a program using the following guidelines: Program should have comments at the top with: program name, student name, date, and description of what the program does. Write a program that uses three separate functions: elevation_range, ave_position, and max_change. elevation_range will accept the elevation list as a parameter and find the range of elevation by finding the max elevation and min elevation and returning the difference (range...

  • Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this...

    Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this is in Zybooks Existing Code # Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price,...

  • In python, write the following program, high school question, please keep simple. When I submitted the...

    In python, write the following program, high school question, please keep simple. When I submitted the solution, I get an error as 'wrong answer'. Please note below the question and then the solution with compiler errors. 7.2 Code Practice: Question 2 Instructions Write a program to generate passwords. The program should ask the user for a phrase and number, and then create the password based on our special algorithm. The algorithm to create the password is as follows: If the...

  • Python code question - you are given the grand prize(in USD) and the winning numbers. Your...

    Python code question - you are given the grand prize(in USD) and the winning numbers. Your program will read multiple tickets (6 selected numbers in each ticket) from the input file. For each ticket, the program calculates its prize won, and outputs the prize (in USD) to the output file. How to Play the Game? In each ticket, the player selects five numbers from a set of 69 white balls (numbered 1 to 69) and one number from 26 red...

  • Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc"....

    Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc". An alphabetically-ordered sequence of substrings of s would be {"a", "ab", "abc", "b", "bc", "c"}. If the sequence is reduced to only those substrings that start with a vowel and end with a consonant, the result is {"ab", "abc"}. The alphabetically first element in this reduced list is "ab", and the alphabetically last element is "abc". As a reminder: • Vowels: a, e, i,...

  • Write the Python Function program to find the RRi of Given Protein Sequence. input: Two inputs...

    Write the Python Function program to find the RRi of Given Protein Sequence. input: Two inputs will be one is Sequence and the other is Amino Acid. Output: RRi Example: Here input is a different sequence and Amino acid is "A". Code Example : def Function(Sequence,AminoAcide): # Your Code This means for a given residue type, minimum RRI will be 1 and maximum will be total number of that type of residues in sequence. This value measures multiple runs of...

  • Infinite Spiral of Numbers (due 17 Feb 2020) HELLO, WE ARE USING PYTHON 3 TO COMPLETE...

    Infinite Spiral of Numbers (due 17 Feb 2020) HELLO, WE ARE USING PYTHON 3 TO COMPLETE THIS PROJECT!! PLEASE FOLLOW CODE SKELETON AS GIVEN AT THE END. THIS IS DUE 17TH FEB 2020, ANY AND ALL HELP WOULD BE GREATLY APPRECIATED, THANK YOU! Consider the natural numbers laid out in a square spiral, with 1 occupying the center of the spiral. The central 11 x 11 subset of that spiral is shown in the table below. 111 112 113 114...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT