编写一个Python函数squareprime(l),该函数接受一个非空的整数列表,如果l的元素在完美的平方和素数之间交替,则返回True,否则返回False。请注意,正方形和素数的交替序列可以以正方形或素数开头。
这里有一些例子来说明您的功能应该如何工作。
>>> primesquare([4])
True
>>> primesquare([4,5,16,101,64])
True
>>> primesquare([5,16,101,36,27])
False
答案 0 :(得分:1)
如果输入序列不是两者之一,则它不是有效序列,它将返回False。
import math
def prime_checker(num):
flag = True
if num == 2:
return True
elif num < 2:
return False
else:
for i in range(2, int(num/2)):
if num % i == 0:
flag = False
break
return flag
def is_square(integer):
if integer == 0:
return false
root = math.sqrt(integer)
if int(root + 0.5) ** 2 == integer:
return True
return False`
def primesquare(list_nums):
if len(list_nums) == 0:
return False
if len(list_nums) == 1:
if (is_square(list_nums[0]) or prime_checker(list_nums[0])):
return True
else:
return False
else:
flag = True
if is_square(list_nums[0]):
check_for = 'prime'
elif prime_checker(list_nums[0]):
check_for = 'square'
else:
return False
for i in range(1,len(list_nums)):
if (check_for == 'prime' and prime_checker(list_nums[i])):
check_for = 'square'
elif (check_for == 'square' and is_square(list_nums[i])):
check_for = 'prime'
else:
flag = False
break
if flag:
return True
else:
return False
更新:
由于已经检查了第0个索引处的元素,因此我们不再担心该数字。因此,如果第0个元素是质数,则序列将为[素数,平方,素数,平方,...]。
如果它是一个理想的正方形,则序列将为[正方形,素数,正方形,素数...]。
如果两者都不是,则它不是有效序列,因此返回false
。
现在,如果第一个数字是两个数字中的一个,并且列表的长度大于1,则我们将迭代其余元素,并检查它们是否与我们期望的相似,但是会更改check_for
变量。
如果check_for
的值是prime
并且我们遇到的值也是质数,那么我们知道该序列的下一个数字应该是一个平方数,该序列才是有效的顺序。遇到平方数时也会发生类似的情况。
答案 1 :(得分:0)
from math import sqrt
def square(n):
if(sqrt(n) % 1 == 0):
return True
else:
return False
def isprime(n):
if (n == 1):
return(False)
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def squareprime(l):
s=l[0]
if(isprime(s)):
for i in range(0,len(l),2):
if(isprime(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(isprime(l[i])==False):
return False
return True
else:
return False
答案 2 :(得分:0)
public void ConfigureServices(IServiceCollection services)
{
...
services.AddLocalization(a => a.ResourcesPath = "");
services.Configure<RequestLocalizationOptions>(a =>
{
CultureInfo[] supportedCultures = {
new CultureInfo("sv-SE"),
new CultureInfo("se")
};
a.DefaultRequestCulture = new RequestCulture("se");
a.SupportedCultures = supportedCultures;
a.SupportedUICultures = supportedCultures;
});
...
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseRequestLocalization();
...
app.UseMvcWithDefaultRoute();
}
答案 3 :(得分:0)
import Browser
import Html exposing (Html, Attribute, button, text, h1, div, input, text)
import Html.Attributes exposing (style)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
-- MAIN
main =
Browser.sandbox { init = init, update = update, view = view }
-- MODEL
type alias Player =
{ player : String
, strength : Int
}
type alias Model =
{ content : String
, teams : List Player
}
init : Model
init =
{ content = ""
, teams = []
}
-- UPDATE
type Msg
= Change String
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = newContent }
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h1 [style "font-family" "impact"] [ text "Team Creator" ]
, div[] [ input [ placeholder " Team 1", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color" "#488aff"] [] ]
, input [ placeholder " Strength", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color" "#32db64"] []
, div [] [ button [ style "background-color" "#66cc81", style "color" "white", style "margin-top" "20px", style "width" "300px", style "border-radius" "25px", style "height" "60px", style "font-size" "30px", style "margin-right" "70px"] [ text "+ADD" ] ]
]