楼主:
erimow (Erimo)
2024-08-08 17:04:24※ 引述《erimow (阿欧伊)》之铭言:
: Write a function called "isPrime" that takes an integer as input, and returns
: a boolean value that indicates if the input number is prime.
: isPrime(1)
: # returns false
: isPrime(5)
: # returns true
: isPrime(91)
: # returns false
: isPrime(1000000)
: # returns false
: 我的想法
: 质数 = 只能被1和自己整除
: 1不是是质数,所以=1要False
: 2以后的数字,我只要写如果在从2开始到自己前一位的数字,除下去有任何一个余数是0
: 那就False
: 剩下就是True
: def isPrime(int):
: if int == 1:
: print(False)
: return False
: for i in range(2, int):
: if (int % i) == 0:
: print(False)
: return False
: print(True)
: return True
: 这个应该没问题
: # Write a function called "palindrome" that checks if the input string is a
: palindrome.
: 这题我一开始想法是
: 回文就从第一个往后看和从最后一个往前看是一样的
: 所以我本来是想说用index第一个跟最后一个一样,第二个跟倒数第二个一样
: n = len(str)
: 只要i 在range n里面,j则是从index-1开始往回数
: 两个 ==
: 那就是对的
: 反之就str[0] != str[-1]那就是false
: 但这样不知道为啥写不出来
: 前面这边应该没问题
: def palindrome(str):
: n = len(str)
: if str[0] != str[-1]:
: print(False)
: return False
: 但有问题的是后面这里
: else:
: for i in range(0, n):
: for j in range(-1, n, -1):
: if str[i] == str[j]:
: print(True)
: return True
: 如果不是上面的情况应该是要回应True的
: 但却没办法
: 我的想法应该是错在for j这句
: -1开始应该没错,最后-1倒数应该也没错
: 但中间要放啥啊
: 感觉不是n n代表我数第一个就停了
def palindrome(string):
n = len(string)
for i in range(0, n):
if string[i] != string[n - i - 1]:
print(False)
return False
print(True)
return True
总之我就拿aaba在试
就是i要让它里面先跑一轮
每一个都碰一下确定都对
有不等于就是错的
如果都没有错
for循环跑完
那就代表对对碰都是对的
所以外面return True