{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "651ea1f7b5d946e28d7a22d2b856fd26", "keywords": "散列表,算法中阶,数据结构,散列函数", "title": "按要求实现程序功能", "desc": [ { "content": "\n
(1)定义一个函数prime判断某个整数是否为素数;\n(2)然后从键盘输入一行字符串,将其中的连续数字依次提取出来形成一个列表。例如,字符串“ab12cd34fg67”按要求提取后形成列表[12,34,67];\n(3)将列表中的所有非素数改为0(要求用prime函数判断列表中的元素是否为素 数);\n(4)输出原始字符串及修改前、修改后的列表。\n提示:可以用s.isdigit()判断s是否为数字字符(串)
", "language": "markdown" } ], "answer": [ { "content": "", "language": "python" } ], "prepared": [ [ { "content": "", "language": "python" } ], [ { "content": "", "language": "python" } ], [ { "content": "", "language": "python" } ] ], "template": { "content": "import math,re\ndef prime(num):\n\tflag = False\n\tif num > 1:\n\t\tfor i in range(2, math.floor(math.sqrt(num))):\n\t\t\tif (num % i) == 0:\n\t\t\t\tflag = True\n\t\t\t\tbreak\n\tif flag:\n\t\tprint(num, \"不是素数\")\n\telse:\n\t\tprint(num, \"是素数\")\n\treturn flag\ns = input(\"请输入字符串:\")\nsList = re.findall(r'(\\d+)', s)\nsNum = [int(x) for x in sList]\ny = lambda x: 0 if prime(x) else x\nsNew = [y(x) for x in sNum]\nprint(sNum) \nprint(sNew) ", "language": "python" }, "node_id": "dailycode-0e465e8d70a1454a8aac9dd2da5f9325", "license": "csdn.net", "created_at": 1637894161, "topic_link": "https://bbs.csdn.net/topics/600470198" }