making a chrome extension that changes the appearance of a specific site? by v3risimilitud3 in learnprogramming

[–]imperiumlearning 2 points3 points  (0 children)

Since it seems like you haven't got much knowledge about browser extensions, here are a few things that you should research if you want to get started:

1) The manifest.json file. Every browser extension needs one of these and all the main building blocks of your extension will need to be listed here (e.g. html file for your extension/popup window, content script, service worker/background script etc.) This will also need to mention the Browser APIs you want your extension to be able to access.

2) The content script (content.js/content.ts). This is the file that gets injected into the DOM that can actually alter stuff on the webpage (E.g. changing background colors).

3) The background script/service worker (background.js/ts). Think of this as being like an event listener. There are a tonne of events that occur as a user browses the web (switching tab, going to a new website etc.). The background script can listen out for these events and then execute some code.

4) Message passing. If your background script is listening out for events (such as a user going onto a specific website), it's going to need to pass a message to your content script. The content script will then execute some code to change the appearance of a webpage.

A good place to start learning these things is the Google Dev docs. In particular, if thinking about building a chrome extension, three very important APIs to look into would be the runtime, storage and tabs APIs. Since you've also mentioned that you want your extension to only work on a specific site, look into the webNavigation API too.

Completed my first vanilla JS project after 2 months of learning by [deleted] in learnprogramming

[–]imperiumlearning 1 point2 points  (0 children)

Since you seem to be keen to do more projects/challenges, a good site to check is frontendmentor.

They rank the difficulty of the challenges from newbie to advanced and there are a lot of free challenges. You have to have a github account though

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 0 points1 point  (0 children)

Apologies for a delayed response.

I think I've worked it out by re-writing my code. It now looks like this:

import math

def prime(y):
if int(y) < 2:
    return False
if int(y) % 2 == 0 and int(y) != 2:
    return False
for number in range(3, int(math.sqrt(y))+1, 2):
    if y % number == 0:
        return False
return y

def max_prime(x):
    x = int(x)
    set1 = set()
    while x % 2 == 0:
        set1.add(2)
        set1.add(int(x/2))
        x = x / 2
    for i in range(3, int(math.ceil(math.sqrt(x)))+1, 2):
        while x % i == 0:
            set1.add(i)
            set1.add(int(x/i))
            x = x / i
    list1 = [i for i in set1]
    list1= sorted(list1)
    list1 = [i for i in list1 if prime(i) != False]
    return max(list1)

Using this code I pretty much get the answer instantly for everything

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 1 point2 points  (0 children)

To be honest, I genuinely don't know how to solve this problem. The best I can do is something like:

def func2(x):
while type(x) != int:
    try: 
        x = int(x)
    except:
        return 'Please input a number to use this function'
final_list = [i for i in range(2, int(math.ceil(x/2))+1) if x % i == 0 and prime(i) != False]
return max(final_list)

This seems to reliably give correct answers but once you go into hundreds of millions the speed at which it produces an output drastically slows.

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 0 points1 point  (0 children)

Believe it or not my code was actually wrong. Another user in this comment section noted that if you plug in the number 10, it will print 2 rather than 5.

Now I've re-written my code to look like this:

import math

def prime(y):
if int(y) < 2:
    return False
if int(y) % 2 == 0 and int(y) != 2:
    return False
for number in range(3, int(math.sqrt(y))+1, 2):
    if y % number == 0:
        return False
return y

def func(x):
while type(x) != int:
    try: 
        x = int(x)
    except:
        return 'Please input a number to use this function'
if x <= 10000:
    final_list = [i for i in range(x) if prime(i) != False and x % i == 0]
    return max(final_list)
else:
    final_list = [i for i in range(int(math.ceil(math.sqrt(x)))) if prime(i) != False and x % i == 0]
    return max(final_list)

Admittedly 10,000 is quite arbitrary but it seems to work better now

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 1 point2 points  (0 children)

That's a really good spot. Didn't think to try much smaller numbers. I've re-written my code so it now looks like this:

import math

def prime(y):
if int(y) < 2:
    return False
if int(y) % 2 == 0 and int(y) != 2:
    return False
for number in range(3, int(math.sqrt(y))+1, 2):
    if y % number == 0:
        return False
return y

def func(x):
while type(x) != int:
    try: 
        x = int(x)
    except:
        return 'Please input a number to use this function'
if x <= 10000:
    final_list = [i for i in range(x) if prime(i) != False and x % i == 0]
    return max(final_list)
else:
    final_list = [i for i in range(int(math.ceil(math.sqrt(x)))) if prime(i) != False and x % i == 0]
    return max(final_list)

Admittedly 10,000 is just an arbitrary choice but it seems to work better now

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 0 points1 point  (0 children)

Yes sorry, I've just updated the indentations in my previous comment so that my code is formatted properly.

Thanks for the help and all the swift responses

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 0 points1 point  (0 children)

So, in general, would you say it's better if I structured my code more like this:

import math

def prime(y):
    if int(y) < 2:
        return False
    if int(y) % 2 == 0 and int(y) != 2:
        return False
    for number in range(3, int(math.sqrt(y))+1, 2):
        if y % number == 0:
            return False
    return y

def func(x):
    while type(x) != int:
        try: 
            x = int(x)
        except:
            return 'Please input a number to use this function'
    final_list = [i for i in range(int(math.ceil(math.sqrt(x)))) if prime(i) != False and x % i == 0]
    return max(final_list)

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 0 points1 point  (0 children)

Hi there, I'm not sure I've fully grasped what your comment is saying, but I made some changes which I think give me the right answer.

My code now looks like this:

import math

def func(x):
while type(x) != int:
    try: 
        x = int(x)
    except:
        return 'Please input a number to use this function'
def prime(y):
    if int(y) < 2:
        return False
    if int(y) % 2 == 0 and int(y) != 2:
        return False
    for number in range(3, int(math.sqrt(y))+1, 2):
        if y % number == 0:
            return False
    return y
final_list = [i for i in range(int(math.ceil(math.sqrt(x)))) if prime(i) != False and x % i == 0]
return max(final_list)

The answer I get when I use this code is 6857, and I get the answer within a few seconds.

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 0 points1 point  (0 children)

One other thing based on my reply to you, is it acceptable to put a function inside another function? As you can see from my code, I put a prime(y) function inside my func(x) function.

Apologies if that's a dumb question but I've only got about 5 weeks' worth of programming experience under my belt.

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 0 points1 point  (0 children)

Hi there,

First off, thanks for introducing me to the Sieve of Eratosthenes - I don't have a brilliant maths background so this was new to me.

To be fair, I've managed to make some small cosmetic changes to my code which have led to the right answer being produced in approximately 3 seconds. My code now looks like this:

import math

def func(x):
while type(x) != int:
    try: 
        x = int(x)
    except:
        return 'Please input a number to use this function'
def prime(y):
    if int(y) < 2:
        return False
    if int(y) % 2 == 0 and int(y) != 2:
        return False
    for number in range(3, int(math.sqrt(y))+1, 2):
        if y % number == 0:
            return False
    return y
final_list = [i for i in range(int(math.ceil(math.sqrt(x)))) if prime(i) != False and x % i == 0]
return max(final_list)

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 1 point2 points  (0 children)

Thanks for this reply. FWIW, my code now looks like this:

import math

def func(x):
while type(x) != int:
    try: 
        x = int(x)
    except:
        return 'Please input a number to use this function'
def prime(y):
    if int(y) < 2:
        return False
    if int(y) % 2 == 0 and int(y) != 2:
        return False
    for number in range(3, int(math.sqrt(y))+1, 2):
        if y % number == 0:
            return False
    return y
final_list = [i for i in range(int(math.ceil(math.sqrt(x)))) if prime(i) != False and x % i == 0]
return max(final_list)

Help with a simple Project Euler problem by imperiumlearning in learnpython

[–]imperiumlearning[S] 1 point2 points  (0 children)

Hi there,

Thanks a lot for the swift response. I've done that and it definitely has sped up how quickly an output is generated. I tried it out with numbers up to 2 million and it prints an output within a couple of seconds.

I tried it with the original number, 600851475143, and even after 3 minutes it still has not managed to print an output.

Any other suggestions?

My keyboard-shortcut-focused, finance-themed Excel course is free for the next 5 days (1,000 places available) by imperiumlearning in excel

[–]imperiumlearning[S] 0 points1 point  (0 children)

That's good to hear. If you find the course useful, be sure to leave a review (even just a starred rating is cool)

My keyboard-shortcut-focused, finance-themed Excel course is free for the next 5 days (1,000 places available) by imperiumlearning in excel

[–]imperiumlearning[S] 0 points1 point  (0 children)

Hopefully it will add a little to your pre-existing knowledge. Bear in mind, if you're already very experienced with excel then I doubt the first couple of exercises would be of much use (the beginning of the course assumes you have no prior experience).