[英]Supposedly Simple Python Homework
I have a CompSci problem (optional), don't worry, you are not doing my homework :) I am having a rather impossible time understanding this question, as this is all I have to go on. 我有一个CompSci问题(可选),请不要担心,您没有在做作业:)我现在很难理解这个问题,因为这是我要做的全部事情。 It is in regards to a rather elementary understanding of python.
它是关于python的基本知识。
I have to create a function that satisfies this requirement. 我必须创建一个满足此要求的函数。 I have no idea why there is a 6 in the problem or what it signifies.
我不知道为什么问题中有6或它代表什么。
"Returns a pair of integers 'root' and 'pwr' st 0 < pwr < 6 and root**pwr = user input. If no pairs of integers satisfy these conditions, the function will notify the user." “返回一对整数'root'和'pwr'st 0 <pwr <6并且root ** pwr =用户输入。如果没有整数对满足这些条件,该函数将通知用户。”
Like I said, it's optional, so rather than just get the answer I am hoping for an explanation. 就像我说的那样,它是可选的,因此不仅仅是获得我希望得到解释的答案。 Thanks you guys!
谢谢你们!
You're looking for a value, given a specific root, and a power between 0 and 6 exclusive, that is equivalent to a user's input. 您正在寻找一个给定值的值,该值具有特定的根,并且在0到6之间的幂(独占)等于用户的输入。 An example of that would be an input of 4 == 2**2, hence you'd probably output
(2, 2)
. 例如,输入4 == 2 ** 2,因此您可能会输出
(2, 2)
。
There doesn't appear to be any bounds on root
. root
似乎没有任何界限。
This is how I read it: 这是我的阅读方式:
Create a function that accepts a number as an argument (I'm assuming an integer, but that's not clear) 创建一个接受数字作为参数的函数(我假设一个整数,但是不清楚)
find the nth root of that number, where n is one of 1,2,3,4 or 5. Return n and the root. 找到该数字的第n个根,其中n是1,2,3,4或5之一。返回n和根。
if you can't find a suitable root, display an error. 如果找不到合适的根,则显示错误。
To get you started: 为了帮助您入门:
0 < pwr < 6
… is equivalent to: ……相当于:
(0 < pwr) and (pwr < 6)
This 0 < pwr < 6
notation is pretty common in mathematics, and in a few programming languages (including Python). 这个
0 < pwr < 6
表示法在数学和一些编程语言(包括Python)中非常普遍。 See Chained notation at Wikipedia for details. 有关详细信息,请参见Wikipedia的链接符号 。
So, for each such power, try to find a root
such that root**pwr
is the user's input value. 因此,对于每种这样的能力,请尝试找到一个
root
,使root**pwr
是用户的输入值。
There's a quick way to do this using math.log
, but if you don't know what logarithms are, don't worry about that. 有一种使用
math.log
进行此操作的快速方法,但是如果您不知道对数是什么,请不必担心。 Instead, you can just try every possible root
. 相反,您可以尝试所有可能的
root
。 If any root
is too big, then all larger root
s will also be too big. 如果任何
root
都太大,那么所有更大的root
也将太大。
Except for one problem: What if user_input
is negative? 除了一个问题:如果
user_input
为负怎么办? So, you'll need to find a way to count all positive and negative numbers, until you find one where the absolute value is too big. 因此,您需要找到一种方法来计算所有正数和负数 ,直到找到绝对值太大的数为止。
So, a brief sketch in pseudocode: 因此,用伪代码简要概述一下:
def find_root_pwr(user_input):
for every pwr such that 0 < pwr < 6:
for every possible root:
test_value = root**pwr
if it's a match:
return root, pwr
elif abs(test_value) is too big:
break # don't need to check larger roots
else:
# We tried all 5 powers, and no match
raise ValueError('No answer for {}'.format(user_input))
And to test it: 并对其进行测试:
user_input = int(input()) # raw_input if you're on Python 2.x
root, pwr = find_root_pwr(user_input)
print('{} ** {} = {}'.format(root, pwr, user_input))
See range
and itertools.count
for hints on how to write the loops. 有关如何编写循环的提示,请参见
range
和itertools.count
。 You should be able to fill in the details from here, or at least get far enough to have a more specific followup question. 您应该可以在此处填写详细信息,或者至少可以足够详细地提出更具体的后续问题。
Other answers provided the explanation for the problem description. 其他答案为问题描述提供了解释。 Here's a possible solution for positive input:
这是正输入的可能解决方案:
def find_root_power(n, minpower, maxpower):
for pwr in range(minpower, maxpower + 1): # minpower <= pwr <= maxpower
# find root such that root**pwr == n
f = n ** (1. / pwr) # float
root = int(f + 0.5) # int (per problem statement)
if root**pwr == n:
yield root, pwr # multiple solutions are possible
n = int(raw_input("Input a number:"))
for root, pwr in find_root_power(n, minpower=1, maxpower=5):
print("root: %d pwr: %d" % (root, pwr))
For example, if the user input is 81
; 例如, 如果用户输入为
81
; the output is : 输出为 :
root: 81 pwr: 1
root: 9 pwr: 2
root: 3 pwr: 4
Note: root=n
, pwr=1
is always a solution if n
is an integer. 注意:如果
n
为整数,则root=n
, pwr=1
始终是一个解决方案。 If user input is not an integer then the code raises ValueError
(given the problem description; root**pwr
may only be an integer so no solution exists for non-integer input). 如果用户输入不是整数,则代码将引发
ValueError
(给出问题描述; root**pwr
可能仅是整数,因此对于非整数输入不存在任何解决方案)。
Note: if root ** pwr == n
then also (-root) ** pwr == n
for even pwr
. 注意:如果
root ** pwr == n
那么偶数pwr
也(-root) ** pwr == n
。 You could add: 您可以添加:
if root**pwr == n:
yield root, pwr # multiple solutions are possible
if pwr % 2 == 0: # even
yield -root, pwr
to include negative root as a solution. 包括负根作为解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.