+ 1

How to write a python program on prime squares without if and else?

Write a Python function primesquare(l) that takes a nonempty list of integers and returns True if the elements of l alternate between perfect squares and prime numbers, and returns Falseotherwise. Note that the alternating sequence of squares and primes may begin with a square or with a prime. Here are some examples to show how your function should work. >>> primesquare([4]) True >>> primesquare([4,5,16,101,64]) True >>> primesquare([5,16,101,36,27]) False

16th Feb 2019, 6:22 AM
Roshini Manoharan
7 odpowiedzi
+ 2
Just alternating? No preference whether one should start first?
16th Feb 2019, 6:24 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
Roshini Manoharan Please show an attempt of your own first. We can't do your homework for you https://www.sololearn.com/discuss/1316935/?ref=app
16th Feb 2019, 7:23 AM
Anna
Anna - avatar
+ 2
https://code.sololearn.com/cF0VL22dIv3R/?ref=app
16th Feb 2019, 7:43 AM
Anna
Anna - avatar
+ 1
The alternating sequence of squares and primes may begin with a square or with a prime.
16th Feb 2019, 6:27 AM
Roshini Manoharan
16th Feb 2019, 6:39 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
from math import sqrt def square(n): if(sqrt(n) % 1 == 0): return True else: return False def prime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return False return True def primesquare(l): s=l[0] if(prime(s)): for i in range(0,len(l),2): if(prime(l[i])==False): return False for i in range(1,len(l),2): if(square(l[i])==False): return False return True elif(square(s)): for i in range(0,len(l),2): if(square(l[i])==False): return False for i in range(1,len(l),2): if(prime(l[i])==False): return False return True else: return False Anna, this is my attempt. But I wanna know other methods of solving the problem for the same output
16th Feb 2019, 7:41 AM
Roshini Manoharan
0
You can change if...else section with while one. For instance if x > 5: print("greater") else: print("ok") is equal to while x > 5: print("greater") break else: print("ok") 😊
16th Feb 2019, 7:53 AM
portpass