Skip to content

Commit c50724b

Browse files
committed
Implement BaseNegativeNumbers
1 parent 0a17f05 commit c50724b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
r, n = gets.chomp.split
2+
r = r.to_i
3+
result = if r < 0
4+
n.chars.map.with_index { |c, i| c.to_i * (-r) ** (n.length - i - 1) }.reduce(&:+).to_s r.abs
5+
else
6+
nums = n.chars.map.with_index do |c, i|
7+
j = n.length - i - 1
8+
if j % 2 != 0
9+
r ** (j + 1) - (r - c.to_i) * r ** j
10+
else
11+
c.to_i * r ** j
12+
end
13+
end
14+
nums.reduce(&:+).to_s r
15+
end
16+
17+
puts result

0 commit comments

Comments
 (0)