When the set ( 3 1 0 0 , 2 1 4 0 , 3 9 0 ) is sorted, we get ( 2 1 4 0 , 3 9 0 , 3 1 0 0 ) .
Sort this set of 100 exponentials and find the sum of the exponents of the elements with even indices.
Details and assumptions .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Good usage of the log function to reduce the cost of the calculations!
Code golf is really a nice idea. It would be nice to see that idea implemented here.
Here's my code:
1 2 3 4 5 6 7 8 9 10 |
|
I basically did the same thing as others: compare logs.
We can exploit the fact that lo g a b = b lo g a . This works because if lo g x > lo g y then x > y .
This makes the computation much faster.
Hi Arul, do you do all of your coding in Python's command line interface? You might want to look into a text editor that you can execute code from, like Sublime Text . Nice use of lo g s to speed things up.
I use IDLE for coding because it's really hard to code in terminal. By the way, Sublime Text is great! Thanks for the suggestion.
Actually, the python's REPL environment lets you see what you're doing. FYI, the link you provided does not work
Wow I saw your StackExchange question and someone answered it for you...
The solution I had in mind is similar to what everyone came up with: to compare two powers a b and c d , compare b l o g ( a ) and c l o g ( d ) instead. However, I want to demonstrate a neat little OOP way of doing the sorting.
We can represent any power as a class (
Power
) , which only stores the base and exponent. We can then implement a
__cmp__
method to allow us to compare two
Power
objects with
>,<,=
. The beauty of this is we can then employ python's in-built
.sort()
method to do the sorting.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
This prints out
1 2 |
|
Note:
. For some weird reason, python decided to make using comparators slightly slower than using
sorted
.
1 2 3 4 |
|
Feel the power of the awk:
Put raw paste data into file called o1. Then:
awk -F "," '{for(i=1;i<=NF;++i){print $i}}' o1|awk -F "^" '{print $1,$2}'|awk '{print $2,$2*log($1)}'|sort -g -k 2,2|awk 'NR%2==1'|awk '{s+=$1}END{print s}'
27385518
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Solution in C++ Same algorithm as the other 2 answers using logarithm to find the number of digits.
EDIT : I first used notepad and replaced all '^' by space and also ',' by space.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
Yes, poor sorting method, I know, I was lazy.
Power of Mathematica. We start by converting the string into a newline-separated list of numbers by replacing "," and "^" by newline. Then we pair them together two by two. We sort the list of pairs by first taking the logarithm and then comparing the results. Finally we sum the exponents which are the second part of each pair.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
I solve this using an Excel spreadsheet.
Excel master. How do you sort using excel?
Log in to reply
How to sort in Excel 2007 and Test Results
Just select "Expand the selection" in the Sort warning so that the corresponding values in the adjacent columns gets automatically swapped as desired.
In this problem, you'd sort the log10 values column ascendingly and select "Expand the selection" in the sort warning so that the base and exponent columns (which would be adjacent to the log10 values column) gets adjusted accordingly and then you just collect required values, sum them and get your answer.
Thanks, Prasun for showing how to sort in Excel.
Here
's a C++ solution that relies a bit on formatted input of C and ascending sort of arrays using nested
for
loops along with the standard
swap()
function.
Is it bad that I don't use file I/O functions and instead take input from
stdin
? ;) Hail Ideone! :P
P.s - For anyone curious about why I check if the
scanf()
return value is equal to
2
or not in the
while
loop, it's because
scanf()
returns the number of inputs read and stored into variables, which here is
2
(one base and one exponent).
Problem Loading...
Note Loading...
Set Loading...
This is the same approach as Chew. I am comparing the logarithms of the numbers because the actual numbers are expensive to compute.
I wish this was a code golf and I could win for writing the shortest code.
Python Code:
Outputs the answer