Jump to content

Wikipedia:Reference desk/Archives/Computing/2019 April 26

From Wikipedia, the free encyclopedia
Computing desk
< April 25 << Mar | April | May >> April 27 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


April 26

[edit]

What is wrong with this Python function?

[edit]

Here's the relevant Python function:

def is_power(a,b):
   if a%b > 0:
       return False
   else:
       if a/b == b:
           return True
       else:
           if a/b/b == b:
               return True
           else:
               while a > 1 or a == 1:
                   a = a/b
                   is_power(a,b)

When I type "print(is_power(2,16))" or "print(is_power(3,81))" or print(is_power(4,256))", the program prints out "None" (instead of "True"). In other words, there's an issue with powers of four. I previously had this problem with powers of three in this Python function, but then I changed the function a bit, fixed the issue with the powers of three, but ended up having a problem with powers of four instead. Anyway, how do I fix my Python function here?

Basically, I want to return True if a is a power of b but return False otherwise. Futurist110 (talk) 06:16, 26 April 2019 (UTC)[reply]

Do you mean "a and b are positive integers. I want a function that returns True if a is a positive integer power of b."? Your tests all yield fractional powers of b i.e. 2=160.25, 3=810.25, 4=2560.25. DroneB (talk) 09:32, 26 April 2019 (UTC)[reply]
@Futurist110: What's the definition of a power you try to implement here? Is 1 a power (with a zero exponent) of 3? Is zero a power of zero? --CiaPan (talk) 10:49, 26 April 2019 (UTC)[reply]
@Futurist110: I guess you get None because there is no return under the last else, so when the loop terminates there is no value specified to be returned. --CiaPan (talk) 10:59, 26 April 2019 (UTC)[reply]
As noted, the recursive call to is_power must be returned. You want "return is_power(a,b)" - assuming you actually want to use the logic that you implemented. 68.115.219.139 (talk) 11:48, 26 April 2019 (UTC)[reply]
The logic is pretty confusing and doesn't look right. The test "a>1 or a==1" at the end can be written "a >= 1". If you use "return is_power(...)" as people mention, then the "while" loop can't actually loop. Your tests like "is_power(2,16)" make it look like the code has the args backwards (it tests whether 2 is a power of 16 rather than the other way around). For example, is_power(9,3) if you run the code will return True immediately. You don't really need all those nested tests or the separate a/b and a/b/b tests. The nested tests aren't needed because if you execute a return statement inside an if block, the code after the if doesn't get run, so you can omit the "else" and return to the earlier nesting level.
Some stylistic points: in Python you should use // instead of / for integer division, since the behaviour of / in Python 3 changed to return a floating point result (so 1/2 is 0.5). And in Python you should generally prefer to use loops instead of recursion, since Python doesn't understand how to convert tail recursion to a loop. It's helpful to include a comment at the top of the function saying what the function is supposed to do. I'd write that function like this:
def is_power(a,b):   # return true if a is a power of b, else false
    if b == 0: 
        return False
    while b%a == 0:
        b //= a
    return (b == 1)
173.228.123.207 (talk) 04:41, 27 April 2019 (UTC)[reply]
That's better than the OP but not quite correct. This version incorrectly returns True for is_power(5,1) and loops forever on is_power(1,5). (Also you reverse a and b compared to the OP.) I'd write it as
def is_power(a,b):   # return true if a is a power of b, else false
    if b == 0:
        return (a == 0)
    if b == 1:
        return (a == 1)
    if a == 0:
        return False
    while a % b == 0:
        if a == b:
            return True
        a //= b
    return False
CodeTalker (talk) 07:17, 27 April 2019 (UTC)[reply]

Thank you very much for your assistance, guys! Futurist110 (talk) 01:46, 8 May 2019 (UTC)[reply]

Resolved

MediaWiki: This might be an SEO problem yet to be fixed over the years

[edit]

Please visit this question in Webmasters Stackexchange. — Preceding unsigned comment added by 103.199.26.76 (talk) 17:35, 26 April 2019 (UTC)[reply]

Wrong link posted; please visit again.

iPhone app

[edit]

Is there iPhone app that when installed list me the app installed on iPhone that are compatible with iPad? --151.49.89.141 (talk) 18:17, 26 April 2019 (UTC)[reply]

The iOS sandbox is incredibly restrictive on what apps can do, and is especially restrictive in allowing interactions between apps. Likewise Apple is very restrictive on apps doing things they consider forbidden. Listing installed apps, even if you have a reason and your app mentions it's doing it and doesn't send that info anywhere is not likely something they'll accept. [1] [2] [3] [4] [5] From those sources, I think there were loopholes or bugs which allowed it prior to iOS 11 but these are all shut down now. But Apple has shut them down now. It's possible or even likely some loopholes or flaws still exist, notably for any device which can be properly jailbroken you can find some way. Still Apple's stance means there's almost zero chance you will find a device on Apple's App Store. Assuming a method can be found, it's possible an app would exist. If this method didn't require a jailbreak you could sideload the app onto your device. However Apple is quite restrictive of sideloading as well. Some use the method Facebook got in trouble for, namely distributing the app via some Enterprise Certificate intended for companies to be able to install apps on devices used by people working for them, but such things have a risk of being shutdown. Another method is to use your own certificate. It seems there are tools to help you do that, with the disadvantage if you don't pay Apple to be a developer your certificate only lasts 7 days. I guess this wouldn't matter for the type of app you're referring to. And of course if there's no known way to do it on iOS 11+ without jail breaking, that raises a whole other level of stuff you need to do. Alternatively an app could rely on screenshots whether of the icons or installed lists from the Apple App Store. But ultimately the complexities mean while I can't be sure such an app doesn't exist, there's a fair chance it doesn't. Frankly, I suspect you're more likely to find a program for Mac OS X or Windows which will get a list from somewhere. Nil Einne (talk) 14:55, 28 April 2019 (UTC)[reply]

How do Wikipedia blocks work?

[edit]

This is more of the technical side of it, does the normal blocking function block a specific username and password from logging in, or does it block the user's IP? What if the user logs out and edits via IP spoofing? (Or create new account(s) from different IPs)? And what if the user is vandalising (or whatever they're doing) cross-wiki? It feels like determined users will always find some way to evade a block or ban. Rob3512 chat? what I did 19:17, 26 April 2019 (UTC)[reply]

You might find Wikipedia:Autoblock one of the more fundamental pages to understand some of this. If someone needs blocking on all wikis they get a global lock (for an account) or a global block (for IP addresses) (see m:Global blocks). The short answer is that some people can always find new ways to edit, but we can usually limit their activity through one means or another. Blocks are often overrated, but they are just one of a number of tools available to admins. -- zzuuzz (talk) 20:01, 26 April 2019 (UTC)[reply]