Is it that hard to call sort()?
Not exactly hard. But since the coin sets in the examples were
ordered, I asked myself if this is intended.
Thomas.
Is it that hard to call sort()?
Not exactly hard. But since the coin sets in the examples were
ordered, I asked myself if this is intended.
Thomas.
James G. [2008-01-26 04:59]:
On Jan 25, 2008, at 5:44 PM, Jens W. wrote:
James G. [2008-01-25 23:57]:
I guess I should have said: is it that hard to call sort {
|a,b| b <=> a }?
am i missing something? whatās wrong with sort.reverse? apart
from the fact that itās a whole lot fasterBecause my first computer science teacher drilled into my head
that you sort it correctly in the first place, instead of using
two operations to get what you wanted.
well, i guess i have to be glad to be unprejudiced in this regard,
since i donāt have any formal CS education
I guess he hadnāt run into Ruby yet.
thatās so true! rubyās expressiveness is just amazing. with the
added benefit that the most simple way to do something is almost
always the most efficient one (both in terms of coding speed as well
as execution speed) ā at least from a laymanās perspectiveā¦
cheers
jens
make_change 14, [10,7,3]
my original implementation missed this one
Why would this fail?
Because my code tried [10, 3] and saw it couldnāt make 14
I think my job here is to be the person the rest of you point to and
go āha! at least Iām not that badāā¦
I guess I should have said: is it that hard to call sort { |a,b|
b <=> a }?
am i missing something? whatās wrong with sort.reverse? apart from
the fact that itās a whole lot fasterI guess he hadnāt run into Ruby yet.
I think reversing an array/list is rather cheap (especially when the
interpreter can use a built-in method coded in C) in comparison to
interpreting a code snippet and performing a comparison for each
element.
Stupid me
sort() does not exclude the idiom you have mentioned above.
Funny error of mine.
R.
ā
http://ruby-smalltalk.blogspot.com/
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein
On [Sat, 26.01.2008 15:43], Pit C. wrote:
2008/1/26, Andrew T. [email protected]:
make_change 14, [10, 5, 3] would fail though (ā¦)
=> [5, 3, 3, 3]
Regards,
Pit
I wonder if it would be a big problem, if my script wouldnt handle this
case.
It works perfect on the standard cases and (14, [10, 7, 3]) though.
James G. wrote:
Is it ok to share test cases before the spoiler?
Sure.
James Edward G. II
seeems like in some weird case:
so there is no rule as to whether 100 or 99 should be used in the first
place.
Regards,
Pit
Nice Pit
Next time Iāll test before opening my mouth.
Hereās another test case:
make_change 1000001, [1000000, 1] # => [1000000, 1] AND complete in a
reasonable time (avoid brute force searches)
/dh
Sharon P. wrote:
make_change 14, [10,7,3]
my original implementation missed this one
Why would this fail?
Because my code tried [10, 3] and saw it couldnāt make 14
I think my job here is to be the person the rest of you point to and
go āha! at least Iām not that badāā¦
Are you Pisces, Sharon? Pisces like to make people feel happy.
Hereās another test case:
Yet another:
make_change(4563, [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41,
37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3])
=> [5, 7, 89, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
On 26 Jan 2008, at 15:20, tho_mica_l wrote:
Hereās another test case:
Yet another:
make_change(4563, [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41,
37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3])
=> [5, 7, 89, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
It wasnāt an explicit requirement, but I think the higher value coins
should appear first in the output list.
On Jan 25, 5:50 pm, Ruby Q. [email protected] wrote:
coins to work with.
We have even richer set of common coins here in UA: [50, 25, 10, 5, 2,
1] (though 1 and 2 are rarely used, and there is also a coin with the
value of 100, but itās not that common).
BTW, is it reasonable to assume amount <= 100 or do we need to prepare
for this one:
def test_huge
assert_equal([ā¦], make_change(1_000_001)
end
?
It wasnāt an explicit requirement, but I think the higher value coins
should appear first in the output list.
I took extra care to sort them this way in order to compensate for my
above demand that the input list should be sorted in descending
order.
Yet another:
make_change(4563, [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41,
37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3])
=> [5, 7, 89, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
That one has two answers (at least):
[3, 17, 89, 89, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
-JJ
On Jan 26, 2008 9:10 AM, Denis H. [email protected] wrote:
Hereās another test case:
make_change 1000001, [1000000, 1] # => [1000000, 1] AND complete in a
reasonable time (avoid brute force searches)
Didnāt you have something like this in mind instead?
assert_equal([1_000_000, 1], make_change(1_000_001, [1_000_000, 2, 1]))
Marcelo
That one has two answers (at least):
Youāre right. Here is another one:
97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97
97 97 97 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89
89 89 83
Sorry.
On Jan 26, 2008, at 1:40 PM, Alex S. wrote:
BTW, is it reasonable to assume amount <= 100 or do we need to prepare
for this one:def test_huge
assert_equal([ā¦], make_change(1_000_001)
end?
I leave that to your best judgement. We should probably remember that
not all places in the world have a 100 cent dollar though.
James Edward G. II
On 26 Jan 2008, at 20:33, Marcelo wrote:
1]))
Marcelo
No. I just meant that exhaustive searches should be avoided. Where a
large number of answer permutations are possible, they need to be
pruned early.
/dh
JesĆŗs Gabriel y GalĆ”n wrote:
There, mines, using rspec (a first try for the fun).
itās basically a compilation of the examples given today, nothing new.
It took me a while to have the last 2 ones running in a decent time
because I wasnāt pruning enough.
$ spec quiz154_spec.rb
ā¦
Finished in 0.210093 seconds
10 examples, 0 failures
Cheers,
Yoan
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs