Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python interview question already done just need help reviewing

ibk101

New Coder
Python:
millist = []
tenlist = []
for i in range (0, 1001):
    millist.append(i)
alist = []
for i in range(2,1001, 3):
    alist.append(i)
for i in range(9,1001, 10):
    tenlist.append(i)
primelist = []
def isprime(num):
    if num> 1: 
        for n in range(2,num): 
            if (num % n) == 0: 
                return False
        return True
    else:
        return False
for i in millist:
    if (isprime(i)):
        primelist.append(i)
    
def data_to_html_table(list1):
    html = '<body>'
    for item in list1:
        if item in tenlist:
            html += '<strong>' + str(item) + '</strong>' +' '
        elif item in alist:
            html += '<i>' + str(item) + '</i>' +' '
        elif item in primelist:
            html += '<u>' + str(item) + '</u>'+ ' '
        else:
            html += str(item)+ ' '
    
    html += '</body>'
    return html


print(data_to_html_table(millist))

Write a valid HTML + Python Page that will count numbers from 1 to 1,000,000?
i. Display every 10th number in the series in Bold
ii. Display every 3rd number in the series in Italics
iii. Bonus: Underline every Prime number in this series.
 
Hi @ibk101,

Your code does a lot of things that aren't needed. I re-wrote your code to be more concise. The generated HTML will be written to a file called 'nums.html' in the current directory.

Python:
with open("nums.html", "a") as html:
    html.write("<!DOCTYPE html>\n<html>\n   <body>\n")
 
    for num in range(1, 1000001):
        line = str(num)
      
        # If the number is divisble by 10 into a whole number.
        if num % 10 == 0:
            # Add the bold tag.
            line = "<b>" + line +  "</b>"
      
        # If the number is divisble by 3 into a whole number.
        if num % 3 == 0:
            # Add the italic tag.
            line = "<i>" + line + "</i>"

        # If the number is prime.
        if num > 1 and (num == 2 or num % 2 != 0) and all(num % divisor != 0 for divisor in range(3, int(num ** 0.5) + 1, 2)):
            # Add the underline tag.
            line = "<u>" + line + "</u>"
                              
        # Write HTML to the file.
        html.write("    " + line + "<br>\n")
 
    html.write("    </body>\n</html>")
 
Last edited:
Hi @ibk101,

Your code does a lot of things that aren't needed. I re-wrote your code to be more concise. The generated HTML will be written to a file called 'nums.html' in the current directory.

Python:
with open("nums.html", "a") as html:
    html.write("<!DOCTYPE html>\n<html>\n   <body>\n")
 
    for num in range(1, 1000001):
        line = str(num)
   
        # If the number is divisble by 10 into a whole number.
        if num % 10 == 0:
            # Add the bold tag.
            line = "<b>" + line +  "</b>"
   
        # If the number is divisble by 3 into a whole number.
        if num % 3 == 0:
            # Add the italic tag.
            line = "<i>" + line + "</i>"

        # If the number is prime.
        if num > 1 and (num == 2 or num % 2 != 0) and all(num % divisor != 0 for divisor in range(3, int(num ** 0.5) + 1, 2)):
            # Add the underline tag.
            line = "<u>" + line + "</u>"
                           
        # Write HTML to the file.
        html.write("    " + line + "<br>\n")
 
    html.write("    </body>\n</html>")
Good code though it seems to me it's still doing more than it has to. Given that you have already checked if the number is a multiple of 3, then if that is so there's no need to do the prime test for that number. Some performance to be gained here. Initially I had the same argument for multiples of 10, but of course the loop only sees odd numbers. And personally I'd handle 1 and 2 separately so the last if statement can be further simplified. Though that is more cosmetic, and probably insignificant in terms of performance.
 
Last edited by a moderator:
Good code though it seems to me it's still doing more than it has to. Given that you have already checked if the number is a multiple of 3, then if that is so there's no need to do the prime test for that number. Some performance to be gained here. Initially I had the same argument for multiples of 10, but of course the look only looks at odd numbers. And
nd personally I'd handle 1 and 2 separately so the prime test can be further simplified. Though that is more cosmetic, and probably insignificant in terms of performance.

Then again, there's no need to examine multiples of 5 either, and 7, and 11, and so on... But this is getting into the area of prime sieving which I think is way beyond the scope of the assignment - as is performance. So diddly's code is probably the most simple and adequate solution.
 
Also @ibk101, your code actually does not solve the problem. Because in the function 'data_to_html_table', you use an if, elif, elif, else structure, if one statements is true, none of the other statements are run. This is fine if the number is 10. However, if the number is 30, it will only have the bold tag, and won't have the italic tag, even though the number is a multiple of both 10 and 3. You should use multiple if statements instead of one chained if, elif... statement instead.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom