Tôi đang viết một chương trình trong Java sẽ sử dụng đa thức. Tôi cần có khả năng thể hiện đa thức dựa trên thứ mà người dùng nhập vào.
Ví dụ, người dùng có thể nhập chuỗi sau. "
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
5"Các đa thức cho điều này ở đây sẽ là
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
6Nhưng đây là điều bắt được. Tôi không thể chia từng số trong chuỗi thành ints riêng biệt bằng cách sử dụng parse, điều này sẽ quá dễ dàng! Bởi vì chuỗi có thể có bất kỳ số số nào trong đó, ví dụ này có 5 số khác có thể có 6 hoặc ít hơn thế.
Bất kỳ ý tưởng nào về cách tôi có thể thể hiện một đa thức trong Java?
\ $ \ beingroup \ $
Hàm
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
2 có giá trị hệ số và công suất và sử dụng các đặc điểm của hai người đó để chọn chuỗi định dạng phù hợp để tạo chuỗi đại diện cho một thuật ngữ riêng lẻ.def enumerate2[xs, start=0, step=1]:
for x in xs:
yield [start, x]
start += step
def poly[xs]:
"""Return string representation of a polynomial.
>>> poly[[2,1,0]]
"2x^2 + x"
"""
res = []
for e, x in enumerate2[xs, len[xs]-1, -1]:
variable = 'x'
if x == 1:
coefficient = ''
elif x == -1:
coefficient = '-'
else:
coefficient = str[x]
if e == 1:
power = ''
elif e == 0:
power = ''
variable = ''
else:
power = '^' + str[e]
if x < 0:
coefficient = '[' + coefficient
power = power + ']'
if x != 0:
res.append[coefficient + variable + power]
return ' + '.join[res]
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
7 là phiên bản tùy chỉnh của >>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
8 hỗ trợ bước biến. Kết quả trông như thế này:>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
Làm cách nào để làm cho mã này thanh lịch hơn và có thể chung chung hơn? Ồ, và kết quả là tối ưu phụ, vì các thuật ngữ tiêu cực được đặt trong ngoặc, thay vì thay đổi dấu cộng trước đó thành dấu trừ.
200_success
Phù bằng vàng 143K2222 gold badges185 silver badges468 bronze badges
Đã hỏi ngày 18 tháng 6 năm 2014 lúc 16:21Jun 18, 2014 at 16:21
\ $ \ endgroup \ $
\ $ \ beingroup \ $
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
7 của bạn là một liên lạc tốt đẹp nhưng tôi không hoàn toàn tin rằng điều này là cần thiết: nếu bạn chơi với chiều dài bằng tay, bạn cũng có thể tính toán sức mạnh từ chỉ mục theo cách thủ công.Ngoài ra, nếu bạn xử lý tiêu cực với điểm trừ thay vì cộng, bạn sẽ có thể thoát khỏi giá đỡ. Mặt khác, bạn không thể sử dụng
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
0 nữa, điều này hơi đau vì nó là một chức năng mát mẻ và hiệu quả.Dù sao, đây là lần thử của tôi:
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
và đầu ra tương ứng:
2x^8 + 3x^6 - 4x^5 - 3x^4 + 2x^3 + x + 10
Tìm thấy lỗi
Khi tôi đang nhìn vào triển khai ban đầu của mình, tôi đã tìm thấy một lỗi xảy ra trong bạn: Hãy thử với
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
1.Đã trả lời ngày 18 tháng 6 năm 2014 lúc 17:12Jun 18, 2014 at 17:12
SylvaindsylvaindSylvainD
Huy hiệu vàng 28.9K11 gold badge44 silver badges92 bronze badges
\ $ \ endgroup \ $
2
\ $ \ beingroup \ $
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
7 của bạn là một liên lạc tốt đẹp nhưng tôi không hoàn toàn tin rằng điều này là cần thiết: nếu bạn chơi với chiều dài bằng tay, bạn cũng có thể tính toán sức mạnh từ chỉ mục theo cách thủ công.fmt = [
[ "", "", "" ],
[ "{c:+g}", "{sign:s}x", "{sign:s}x^{n:g}" ],
[ "{c:+g}", "{c:+g}x", "{c:+g}x^{n:g}" ]
]
def term[c, n]:
return fmt[cmp[abs[c],1]+1][cmp[n,1]+1].format[sign="- +"[cmp[c,0]+1], c=c, n=n]
def poly[xs]:
return "".join[term[xs[i],len[xs]-i-1] for i in xrange[len[xs]]]
def suppsign[s]:
return s.lstrip['+']
print suppsign[poly[[1,1,1]]]
Ngoài ra, nếu bạn xử lý tiêu cực với điểm trừ thay vì cộng, bạn sẽ có thể thoát khỏi giá đỡ. Mặt khác, bạn không thể sử dụng
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
0 nữa, điều này hơi đau vì nó là một chức năng mát mẻ và hiệu quả.Dù sao, đây là lần thử của tôi:
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
và đầu ra tương ứng:Jun 19, 2014 at 0:08
Tìm thấy lỗiEdward
Khi tôi đang nhìn vào triển khai ban đầu của mình, tôi đã tìm thấy một lỗi xảy ra trong bạn: Hãy thử với
1.4 gold badges109 silver badges274 bronze badgesdef poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
\ $ \ endgroup \ $
3
\ $ \ beingroup \ $
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
7
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
7 của bạn là một liên lạc tốt đẹp nhưng tôi không hoàn toàn tin rằng điều này là cần thiết: nếu bạn chơi với chiều dài bằng tay, bạn cũng có thể tính toán sức mạnh từ chỉ mục theo cách thủ công.for e, x in enumerate2[xs, len[xs]-1, -1]:
Ngoài ra, nếu bạn xử lý tiêu cực với điểm trừ thay vì cộng, bạn sẽ có thể thoát khỏi giá đỡ. Mặt khác, bạn không thể sử dụng
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
0 nữa, điều này hơi đau vì nó là một chức năng mát mẻ và hiệu quả.for e, x in zip[itertools.count[len[xs]-1, -1], xs]:
Dù sao, đây là lần thử của tôi:
for e, x in zip[reversed[range[len[xs]], xs]:
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
9
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
if x == 0:
continue
và đầu ra tương ứng:
Tìm thấy lỗi
def coefficient[x]:
"""returns the string representation of `x`."""
if x == 1:
return ""
if x == -1:
return "-"
return str[x]
Khi tôi đang nhìn vào triển khai ban đầu của mình, tôi đã tìm thấy một lỗi xảy ra trong bạn: Hãy thử với def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
1.
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
Đã trả lời ngày 18 tháng 6 năm 2014 lúc 17:12
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
0f-string
Sylvaindsylvaind
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
1yield
Huy hiệu vàng 28.9K1
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
23>>> poly[[2,0,3,-4,-3,2,0,1,10]] '2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
>>> poly[[2,0,3,-4,-3,2,0,1,10]]
'2x^8 + 3x^6 + [-4x^5] + [-3x^4] + 2x^3 + x + 10'
4Tôi nghĩ rằng có một cách đơn giản hơn để làm điều này:Apr 26, 2019 at 10:44
Hàm
def poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
2 có giá trị hệ số và công suất và sử dụng các đặc điểm của hai người đó để chọn chuỗi định dạng phù hợp để tạo chuỗi đại diện cho một thuật ngữ riêng lẻ.Maarten FabréHàm
3 sử dụng khả năng hiểu danh sách để kết hợp hiệu quả chuỗi cho mỗi thuật ngữ.1 gold badge12 silver
badges27 bronze badgesdef poly[p, var_string='x']:
res = ''
first_pow = len[p] - 1
for i, coef in enumerate[p]:
power = first_pow - i
if coef:
if coef < 0:
sign, coef = [' - ' if res else '- '], -coef
elif coef > 0: # must be true
sign = [' + ' if res else '']
str_coef = '' if coef == 1 and power != 0 else str[coef]
if power == 0:
str_power = ''
elif power == 1:
str_power = var_string
else:
str_power = var_string + '^' + str[power]
res += sign + str_coef + str_power
return res
\ $ \ endgroup \ $
2